From a5e50872603cd3ffef18bb08eb4553947dde6be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rold=C3=A1n=20Betancort?= Date: Fri, 29 Nov 2024 15:39:49 +0000 Subject: [PATCH 1/2] repro: namespace/caveat changes cause an incorrect delete event --- pkg/datastore/test/watch.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pkg/datastore/test/watch.go b/pkg/datastore/test/watch.go index e36b02973b..40b9e7aa58 100644 --- a/pkg/datastore/test/watch.go +++ b/pkg/datastore/test/watch.go @@ -537,6 +537,7 @@ func WatchSchemaTest(t *testing.T, tester DatastoreTester) { changes, errchan := ds.Watch(ctx, lowestRevision, datastore.WatchJustSchema()) require.Zero(len(errchan)) + // Addition // Write an updated schema and ensure the changes are returned. _, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error { err := rwt.WriteNamespaces(ctx, &core.NamespaceDefinition{ @@ -561,6 +562,37 @@ func WatchSchemaTest(t *testing.T, tester DatastoreTester) { }, }, changes, errchan, false) + // Changed + _, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error { + err := rwt.WriteNamespaces(ctx, &core.NamespaceDefinition{ + Name: "somenewnamespace", + Relation: []*core.Relation{ + { + Name: "anotherrelation", + }, + }, + }) + if err != nil { + return err + } + + return rwt.WriteCaveats(ctx, []*core.CaveatDefinition{ + { + Name: "somenewcaveat", + SerializedExpression: []byte("123"), + }, + }) + }) + require.NoError(err) + + verifyMixedUpdates(require, [][]string{ + { + "changed:somenewnamespace", + "changed:somenewcaveat", + }, + }, changes, errchan, false) + + // Removed // Delete some namespaces and caveats. _, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error { err := rwt.DeleteNamespaces(ctx, "somenewnamespace") From 369286ec067123040ca7dc3872bfe793647225a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rold=C3=A1n=20Betancort?= Date: Fri, 29 Nov 2024 15:41:25 +0000 Subject: [PATCH 2/2] fix: namespace/caveat changes cause an incorrect delete event the computation of schema change events was incorrect due to the use of the wrong snapshot XID for changed namespaces / caveats. It manifested itself as a change event, followed by a delete event. --- internal/datastore/postgres/watch.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/datastore/postgres/watch.go b/internal/datastore/postgres/watch.go index d91e2fa78c..d8a8253bdc 100644 --- a/internal/datastore/postgres/watch.go +++ b/internal/datastore/postgres/watch.go @@ -455,7 +455,7 @@ func (pgd *pgDatastore) loadNamespaceChanges(ctx context.Context, xmin uint64, x } if _, found := filter[createdXID.Uint64]; found { - err := tracked.AddChangedDefinition(ctx, txidToRevision[deletedXID.Uint64], loaded) + err := tracked.AddChangedDefinition(ctx, txidToRevision[createdXID.Uint64], loaded) if err != nil { return err } @@ -514,7 +514,7 @@ func (pgd *pgDatastore) loadCaveatChanges(ctx context.Context, xmin uint64, xmax } if _, found := filter[createdXID.Uint64]; found { - err := tracked.AddChangedDefinition(ctx, txidToRevision[deletedXID.Uint64], loaded) + err := tracked.AddChangedDefinition(ctx, txidToRevision[createdXID.Uint64], loaded) if err != nil { return err }