Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory usage of WriteSchema #1743

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions internal/services/shared/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (
"github.com/authzed/spicedb/pkg/genutil/mapz"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/schemadsl/compiler"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/typesystem"
)

// ValidatedSchemaChanges is a set of validated schema changes that can be applied to the datastore.
type ValidatedSchemaChanges struct {
compiled *compiler.CompiledSchema
newCaveatDefNames *mapz.Set[string]
newObjectDefNames *mapz.Set[string]
additiveOnly bool
compiled *compiler.CompiledSchema
validatedTypeSystems map[string]*typesystem.ValidatedNamespaceTypeSystem
newCaveatDefNames *mapz.Set[string]
newObjectDefNames *mapz.Set[string]
additiveOnly bool
}

// ValidateSchemaChanges validates the schema found in the compiled schema and returns a
Expand All @@ -39,6 +41,8 @@ func ValidateSchemaChanges(ctx context.Context, compiled *compiler.CompiledSchem

// 2) Validate the namespaces defined.
newObjectDefNames := mapz.NewSet[string]()
validatedTypeSystems := make(map[string]*typesystem.ValidatedNamespaceTypeSystem, len(compiled.ObjectDefinitions))

for _, nsdef := range compiled.ObjectDefinitions {
ts, err := typesystem.NewNamespaceTypeSystem(nsdef,
typesystem.ResolverForPredefinedDefinitions(typesystem.PredefinedElements{
Expand All @@ -54,18 +58,16 @@ func ValidateSchemaChanges(ctx context.Context, compiled *compiler.CompiledSchem
return nil, err
}

if err := namespace.AnnotateNamespace(vts); err != nil {
return nil, err
}

validatedTypeSystems[nsdef.Name] = vts
newObjectDefNames.Insert(nsdef.Name)
}

return &ValidatedSchemaChanges{
compiled: compiled,
newCaveatDefNames: newCaveatDefNames,
newObjectDefNames: newObjectDefNames,
additiveOnly: additiveOnly,
compiled: compiled,
validatedTypeSystems: validatedTypeSystems,
newCaveatDefNames: newCaveatDefNames,
newObjectDefNames: newObjectDefNames,
additiveOnly: additiveOnly,
}, nil
}

Expand Down Expand Up @@ -156,6 +158,15 @@ func ApplySchemaChangesOverExisting(

if len(diff.Deltas()) > 0 {
objectDefsWithChanges = append(objectDefsWithChanges, nsdef)

vts, ok := validated.validatedTypeSystems[nsdef.Name]
if !ok {
return nil, spiceerrors.MustBugf("validated type system not found for namespace `%s`", nsdef.Name)
}

if err := namespace.AnnotateNamespace(vts); err != nil {
return nil, err
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion internal/services/shared/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func TestApplySchemaChanges(t *testing.T) {
SchemaString: `
definition user {}

definition organization {}
definition organization {
relation member: user
permission admin = member
}

caveat catchTwentyTwo(value int) {
value == 22
Expand All @@ -58,6 +61,13 @@ func TestApplySchemaChanges(t *testing.T) {
require.Equal(applied.RemovedObjectDefNames, []string{"document"})
require.Equal(applied.NewCaveatDefNames, []string{"catchTwentyTwo"})
require.Equal(applied.RemovedCaveatDefNames, []string{"hasFortyTwo"})

orgDef, err := rwt.LookupNamespacesWithNames(ctx, []string{"organization"})
require.NoError(err)
require.Len(orgDef, 1)

require.NotEmpty(orgDef[0].Definition.Relation[0].CanonicalCacheKey)
require.NotEmpty(orgDef[0].Definition.Relation[1].CanonicalCacheKey)
return nil
})
require.NoError(err)
Expand Down
Loading