Skip to content

Commit

Permalink
genutil/slicez: rehome chunking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jun 30, 2023
1 parent b8428b1 commit 92b696c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions internal/graph/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
iv1 "github.com/authzed/spicedb/pkg/proto/impl/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/util"
)

var dispatchChunkCountHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Expand Down Expand Up @@ -378,7 +377,7 @@ func (cc *ConcurrentChecker) checkDirect(ctx context.Context, crc currentRequest
toDispatch := make([]directDispatch, 0, subjectsToDispatch.Len())
subjectsToDispatch.ForEachType(func(rr *core.RelationReference, resourceIds []string) {
chunkCount := 0.0
util.ForEachChunk(resourceIds, crc.maxDispatchCount, func(resourceIdChunk []string) {
slicez.ForEachChunk(resourceIds, crc.maxDispatchCount, func(resourceIdChunk []string) {
chunkCount++
toDispatch = append(toDispatch, directDispatch{
resourceType: rr,
Expand Down Expand Up @@ -581,7 +580,7 @@ func (cc *ConcurrentChecker) checkTupleToUserset(ctx context.Context, crc curren
toDispatch := make([]directDispatch, 0, subjectsToDispatch.Len())
subjectsToDispatch.ForEachType(func(rr *core.RelationReference, resourceIds []string) {
chunkCount := 0.0
util.ForEachChunk(resourceIds, crc.maxDispatchCount, func(resourceIdChunk []string) {
slicez.ForEachChunk(resourceIds, crc.maxDispatchCount, func(resourceIdChunk []string) {
chunkCount++
toDispatch = append(toDispatch, directDispatch{
resourceType: rr,
Expand Down
4 changes: 2 additions & 2 deletions internal/graph/computed/computecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/authzed/spicedb/internal/dispatch"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/genutil/slicez"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/util"
)

// DebugOption defines the various debug level options for Checks.
Expand Down Expand Up @@ -95,7 +95,7 @@ func computeCheck(ctx context.Context,
resultMetadatas := make(map[string]*v1.ResponseMeta, len(resourceIDs))

// TODO(jschorr): Should we make this run in parallel via the preloadedTaskRunner?
_, err := util.ForEachChunkUntil(resourceIDs, datastore.FilterMaximumIDCount, func(resourceIDsToCheck []string) (bool, error) {
_, err := slicez.ForEachChunkUntil(resourceIDs, datastore.FilterMaximumIDCount, func(resourceIDsToCheck []string) (bool, error) {
checkResult, err := d.DispatchCheck(ctx, &v1.DispatchCheckRequest{
ResourceRelation: params.ResourceType,
ResourceIds: resourceIDsToCheck,
Expand Down
4 changes: 2 additions & 2 deletions internal/graph/lookupsubjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/authzed/spicedb/internal/namespace"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/genutil/mapz"
"github.com/authzed/spicedb/pkg/genutil/slicez"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/util"
)

// ValidatedLookupSubjectsRequest represents a request after it has been validated and parsed for internal
Expand Down Expand Up @@ -417,7 +417,7 @@ func (cl *ConcurrentLookupSubjects) dispatchTo(
}

// Dispatch the found subjects as the resources of the next step.
util.ForEachChunk(resourceIds, maxDispatchChunkSize, func(resourceIdChunk []string) {
slicez.ForEachChunk(resourceIds, maxDispatchChunkSize, func(resourceIdChunk []string) {
g.Go(func() error {
return cl.d.DispatchLookupSubjects(&v1.DispatchLookupSubjectsRequest{
ResourceRelation: resourceType,
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/chunking.go → pkg/genutil/slicez/chunking.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package util
package slicez

import (
"github.com/authzed/spicedb/internal/logging"
)

// ForEachChunk executes the given handler for each chunk of items in the slice.
func ForEachChunk[T any](data []T, chunkSize uint16, handler func(items []T)) {
_, _ = ForEachChunkUntil[T](data, chunkSize, func(items []T) (bool, error) {
_, _ = ForEachChunkUntil(data, chunkSize, func(items []T) (bool, error) {
handler(items)
return true, nil
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package slicez

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions pkg/validationfile/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/authzed/spicedb/internal/namespace"
"github.com/authzed/spicedb/internal/relationships"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/genutil/slicez"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/util"
)

// PopulatedValidationFile contains the fully parsed information from a validation file.
Expand Down Expand Up @@ -144,7 +144,7 @@ func PopulateFromFilesContents(ctx context.Context, ds datastore.Datastore, file
return err
})

util.ForEachChunk(updates, 500, func(chunked []*core.RelationTupleUpdate) {
slicez.ForEachChunk(updates, 500, func(chunked []*core.RelationTupleUpdate) {
if err != nil {
return
}
Expand Down

0 comments on commit 92b696c

Please sign in to comment.