Skip to content

Commit

Permalink
Merge pull request authzed#1743 from josephschorr/writeschema-memory
Browse files Browse the repository at this point in the history
Reduce memory usage of WriteSchema
  • Loading branch information
josephschorr committed Feb 9, 2024
2 parents f426176 + d3a5378 commit a32213b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
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

0 comments on commit a32213b

Please sign in to comment.