From cadc177975cf534c732af09a8fec59cab01f0a11 Mon Sep 17 00:00:00 2001 From: Alex Ababii Date: Fri, 27 Nov 2026 11:22:50 +0100 Subject: [PATCH] fix(activitylog): version key into stream metadata --- services/activitylog/pkg/command/server.go | 30 ++++++++- .../service/activitylog/activitylog_test.go | 61 +++++++++++++++++++ .../pkg/service/activitylog/migrations.go | 12 ++-- 3 files changed, 97 insertions(+), 6 deletions(-) diff --git a/services/activitylog/pkg/command/server.go b/services/activitylog/pkg/command/server.go index 9a3abec71a..0f2ea527b0 100644 --- a/services/activitylog/pkg/command/server.go +++ b/services/activitylog/pkg/command/server.go @@ -276,20 +276,46 @@ func ConnectNatsKV(cfg config.Store, logger log.Logger) (nats.KeyValue, error) { Msg("could not read stream info of the existing bucket, continuing with its current TTL") return kv, nil } - if info.Config.MaxAge != cfg.TTL { + + // keep a copy of the version key in the stream metadata, applying a TTL can purge the key + version := "" + if info.Config.Metadata[activitylog.ActivityLogVersionMetadataKey] == "" { + entry, err := kv.Get(activitylog.ActivityLogVersionKey) + switch { + case err == nil: + version = string(entry.Value()) + case errors.Is(err, nats.ErrKeyNotFound): + default: + logger.Error(). + Err(err). + Str("bucket", cfg.Database). + Str("stream", streamName). + Dur("ttl", cfg.TTL). + Msg("could not read the version key of the existing bucket, continuing with its current settings") + return kv, nil + } + } + + if info.Config.MaxAge != cfg.TTL || version != "" { streamCfg := info.Config streamCfg.MaxAge = cfg.TTL // the duplicate window must not exceed the max age if cfg.TTL > 0 && streamCfg.Duplicates > cfg.TTL { streamCfg.Duplicates = cfg.TTL } + if version != "" { + if streamCfg.Metadata == nil { + streamCfg.Metadata = map[string]string{} + } + streamCfg.Metadata[activitylog.ActivityLogVersionMetadataKey] = version + } if _, err := js.UpdateStream(&streamCfg); err != nil { logger.Error(). Err(err). Str("bucket", cfg.Database). Str("stream", streamName). Dur("ttl", cfg.TTL). - Msg("could not update the TTL of the existing bucket, continuing with its current TTL") + Msg("could not update the stream config of the existing bucket, continuing with its current settings") } } diff --git a/services/activitylog/pkg/service/activitylog/activitylog_test.go b/services/activitylog/pkg/service/activitylog/activitylog_test.go index a9e6e603bc..85f02690ab 100644 --- a/services/activitylog/pkg/service/activitylog/activitylog_test.go +++ b/services/activitylog/pkg/service/activitylog/activitylog_test.go @@ -274,6 +274,12 @@ var _ = Describe("ConnectNatsKV", func() { return info.Config.MaxAge } + metadata := func() map[string]string { + info, err := js.StreamInfo("KV_" + db) + Expect(err).ToNot(HaveOccurred()) + return info.Config.Metadata + } + BeforeEach(func() { db = "activitylog-ttl-test-" + uuid.New().String() cfg = config.Store{ @@ -348,6 +354,61 @@ var _ = Describe("ConnectNatsKV", func() { Expect(maxAge()).To(Equal(time.Duration(0))) } }) + + It("copies the version key to the stream metadata of an existing bucket", func() { + kv, err := command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + _, err = kv.PutString(activitylog.ActivityLogVersionKey, "1") + Expect(err).ToNot(HaveOccurred()) + + _, err = command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + Expect(metadata()).To(HaveKeyWithValue(activitylog.ActivityLogVersionMetadataKey, "1")) + Expect(maxAge()).To(Equal(time.Duration(0))) + + entry, err := kv.Get(activitylog.ActivityLogVersionKey) + Expect(err).ToNot(HaveOccurred()) + Expect(string(entry.Value())).To(Equal("1")) + }) + + It("copies the version key to the stream metadata while applying a TTL", func() { + kv, err := command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + _, err = kv.PutString(activitylog.ActivityLogVersionKey, "1") + Expect(err).ToNot(HaveOccurred()) + + cfg.TTL = time.Hour + _, err = command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + Expect(metadata()).To(HaveKeyWithValue(activitylog.ActivityLogVersionMetadataKey, "1")) + Expect(maxAge()).To(Equal(time.Hour)) + }) + + It("keeps the version stream metadata when the version key is missing", func() { + _, err := command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + info, err := js.StreamInfo("KV_" + db) + Expect(err).ToNot(HaveOccurred()) + streamCfg := info.Config + streamCfg.Metadata = map[string]string{activitylog.ActivityLogVersionMetadataKey: "1"} + _, err = js.UpdateStream(&streamCfg) + Expect(err).ToNot(HaveOccurred()) + + cfg.TTL = time.Hour + _, err = command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + Expect(metadata()).To(HaveKeyWithValue(activitylog.ActivityLogVersionMetadataKey, "1")) + Expect(maxAge()).To(Equal(time.Hour)) + }) + + It("does not set the version stream metadata when there is no version", func() { + _, err := command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + + _, err = command.ConnectNatsKV(cfg, log.NopLogger()) + Expect(err).ToNot(HaveOccurred()) + Expect(metadata()).ToNot(HaveKey(activitylog.ActivityLogVersionMetadataKey)) + }) }) func activitites(acts ...any) []activitylog.RawActivity { diff --git a/services/activitylog/pkg/service/activitylog/migrations.go b/services/activitylog/pkg/service/activitylog/migrations.go index dca612e6b6..784049096e 100644 --- a/services/activitylog/pkg/service/activitylog/migrations.go +++ b/services/activitylog/pkg/service/activitylog/migrations.go @@ -10,13 +10,17 @@ import ( "github.com/vmihailenco/msgpack/v5" ) -const activitylogVersionKey = "activitylog.version" +// VersionKey is the KV key holding the activitylog data version. +const ActivityLogVersionKey = "activitylog.version" + +// VersionMetadataKey is the stream metadata key holding a copy of the activitylog data version. +const ActivityLogVersionMetadataKey = "opencloud.activitylog.version" const currentMigrationVersion = "1" // RunMigrations checks the activitylog data version and runs migrations if necessary. // It should be called during service startup, after the NATS KeyValue store is initialized. func (a *ActivityLog) runMigrations(ctx context.Context, kv nats.KeyValue) error { - entry, err := kv.Get(activitylogVersionKey) + entry, err := kv.Get(ActivityLogVersionKey) if err == nats.ErrKeyNotFound { a.log.Info().Msg("activitylog version key not found. Running migration to V1...") return a.migrateToV1(ctx, kv) @@ -60,7 +64,7 @@ func (a *ActivityLog) migrateToV1(_ context.Context, kv nats.KeyValue) error { } for key := range keyChan { - if key == activitylogVersionKey { + if key == ActivityLogVersionKey { skippedCount++ continue // Skip the version key itself } @@ -120,7 +124,7 @@ func (a *ActivityLog) migrateToV1(_ context.Context, kv nats.KeyValue) error { } // Set the activitylog version to "1" after migration - if _, err := kv.PutString(activitylogVersionKey, currentMigrationVersion); err != nil { + if _, err := kv.PutString(ActivityLogVersionKey, currentMigrationVersion); err != nil { return fmt.Errorf("migrateToV1: failed to set activitylog version key to '%s' in NATS KV store: %w", currentMigrationVersion, err) }