fix(activitylog): version key into stream metadata

This commit is contained in:
Alex Ababii committed 2026-11-27 11:22:50 +01:00
1 parent a901bd3b65
commit cadc177975
3 files changed
+97 -6

No files matched your search

+28 -2
View File
@@ -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")
}
}
@@ -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 {
@@ -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)
}