mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
fix(activitylog): fall back to the version key when the stream info cannot be read
This commit is contained in:
1 parent
c341330902
commit
fda7232556
2 files changed
+30
-4
No files matched your search
@@ -500,6 +500,32 @@ var _ = Describe("migrations", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("falls back to the version key when the stream info cannot be read", func() {
|
||||
_, err := kv.PutString(activitylog.ActivityLogVersionKey, "1")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// a JetStream context of a closed connection makes the stream info lookup fail
|
||||
conn, err := nats.Connect(server.Addr().String())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
brokenJS, err := conn.JetStream()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
conn.Close()
|
||||
|
||||
// other packages can raise the global zerolog level above warn
|
||||
globalLevel := zerolog.GlobalLevel()
|
||||
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
||||
DeferCleanup(zerolog.SetGlobalLevel, globalLevel)
|
||||
|
||||
var logs bytes.Buffer
|
||||
_, err = activitylog.New(kv, activitylog.JetStream(brokenJS), activitylog.Logger(log.Logger{Logger: zerolog.New(&logs)}))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = kv.Get(legacyKey)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(logs.String()).To(ContainSubstring(`"level":"warn"`))
|
||||
Expect(logs.String()).To(ContainSubstring("could not read the stream info"))
|
||||
})
|
||||
|
||||
It("migrates when there is no version", func() {
|
||||
_, err := activitylog.New(kv, activitylog.JetStream(js), activitylog.Logger(log.NopLogger()))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
@@ -41,14 +41,14 @@ func (a *ActivityLog) runMigrations(ctx context.Context, kv nats.KeyValue) error
|
||||
}
|
||||
|
||||
// storedVersion returns the activitylog data version, or an empty string if none is stored.
|
||||
// The stream metadata is preferred, the version key of the KV store is the fallback.
|
||||
// The stream metadata is preferred, the version key of the KV store is the fallback
|
||||
// (also used when the stream info cannot be read).
|
||||
func (a *ActivityLog) storedVersion(kv nats.KeyValue) (string, error) {
|
||||
if a.js != nil {
|
||||
info, err := a.js.StreamInfo("KV_" + kv.Bucket())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get activitylog version from NATS stream info: %w", err)
|
||||
}
|
||||
if version := info.Config.Metadata[ActivityLogVersionMetadataKey]; version != "" {
|
||||
a.log.Warn().Err(err).Str("bucket", kv.Bucket()).Msg("could not read the stream info, falling back to the activitylog version key")
|
||||
} else if version := info.Config.Metadata[ActivityLogVersionMetadataKey]; version != "" {
|
||||
return version, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user