mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
fix(activitylog): ttl for kv buckets
This commit is contained in:
1 parent
5b9c898f07
commit
a901bd3b65
2 files changed
+138
-3
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/olekukonko/errors"
|
||||
@@ -98,7 +99,7 @@ func Server(cfg *config.Config) *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
kv, err := ConnectNatsKV(cfg.Store)
|
||||
kv, err := ConnectNatsKV(cfg.Store, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -222,7 +223,19 @@ func Server(cfg *config.Config) *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func ConnectNatsKV(cfg config.Store) (nats.KeyValue, error) {
|
||||
// minTTL is the smallest positive max age the NATS server accepts for a stream.
|
||||
const minTTL = 100 * time.Millisecond
|
||||
|
||||
func ConnectNatsKV(cfg config.Store, logger log.Logger) (nats.KeyValue, error) {
|
||||
// NATS rejects negative and too small positive max ages, fall back to no expiry
|
||||
if cfg.TTL < 0 || (cfg.TTL > 0 && cfg.TTL < minTTL) {
|
||||
logger.Warn().
|
||||
Str("bucket", cfg.Database).
|
||||
Dur("ttl", cfg.TTL).
|
||||
Msg("invalid TTL configured, falling back to the default (no expiry)")
|
||||
cfg.TTL = 0
|
||||
}
|
||||
|
||||
// Connect to NATS servers
|
||||
secureOption := natspkg.Secure(cfg.EnableTLS, cfg.TLSInsecure, cfg.TLSRootCACertificate)
|
||||
conn, err := nats.Connect(strings.Join(cfg.Nodes, ","), secureOption, nats.UserInfo(cfg.AuthUsername, cfg.AuthPassword))
|
||||
@@ -243,10 +256,41 @@ func ConnectNatsKV(cfg config.Store) (nats.KeyValue, error) {
|
||||
|
||||
kv, err = js.CreateKeyValue(&nats.KeyValueConfig{
|
||||
Bucket: cfg.Database,
|
||||
TTL: cfg.TTL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Failed to create bucket (%s)", cfg.Database)
|
||||
}
|
||||
return kv, nil
|
||||
}
|
||||
|
||||
// the bucket already exists, make sure its TTL matches the configured one
|
||||
streamName := "KV_" + cfg.Database
|
||||
info, err := js.StreamInfo(streamName)
|
||||
if err != nil {
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Str("bucket", cfg.Database).
|
||||
Str("stream", streamName).
|
||||
Dur("ttl", cfg.TTL).
|
||||
Msg("could not read stream info of the existing bucket, continuing with its current TTL")
|
||||
return kv, nil
|
||||
}
|
||||
if info.Config.MaxAge != cfg.TTL {
|
||||
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 _, 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")
|
||||
}
|
||||
}
|
||||
|
||||
return kv, nil
|
||||
|
||||
@@ -10,8 +10,10 @@ import (
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
"github.com/google/uuid"
|
||||
nserver "github.com/nats-io/nats-server/v2/server"
|
||||
"github.com/nats-io/nats.go"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/opencloud-eu/opencloud/pkg/log"
|
||||
"github.com/opencloud-eu/opencloud/services/activitylog/pkg/command"
|
||||
"github.com/opencloud-eu/opencloud/services/activitylog/pkg/config"
|
||||
"github.com/opencloud-eu/opencloud/services/activitylog/pkg/service/activitylog"
|
||||
@@ -75,7 +77,7 @@ var _ = Describe("ActivitylogService", func() {
|
||||
kv, err := command.ConnectNatsKV(config.Store{
|
||||
Nodes: []string{server.Addr().String()},
|
||||
Database: db,
|
||||
})
|
||||
}, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
alog, err = activitylog.New(
|
||||
@@ -259,6 +261,95 @@ var _ = Describe("ActivitylogService", func() {
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("ConnectNatsKV", func() {
|
||||
var (
|
||||
db string
|
||||
cfg config.Store
|
||||
js nats.JetStreamContext
|
||||
)
|
||||
|
||||
maxAge := func() time.Duration {
|
||||
info, err := js.StreamInfo("KV_" + db)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
return info.Config.MaxAge
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
db = "activitylog-ttl-test-" + uuid.New().String()
|
||||
cfg = config.Store{
|
||||
Nodes: []string{server.Addr().String()},
|
||||
Database: db,
|
||||
}
|
||||
|
||||
conn, err := nats.Connect(server.Addr().String())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
DeferCleanup(conn.Close)
|
||||
js, err = conn.JetStream()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates a new bucket without expiry by default", func() {
|
||||
_, err := command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Duration(0)))
|
||||
})
|
||||
|
||||
It("applies the TTL to a new bucket", func() {
|
||||
cfg.TTL = time.Hour
|
||||
_, err := command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Hour))
|
||||
})
|
||||
|
||||
It("updates the TTL of an existing bucket", func() {
|
||||
_, err := command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Duration(0)))
|
||||
|
||||
cfg.TTL = time.Hour
|
||||
_, err = command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Hour))
|
||||
|
||||
cfg.TTL = time.Second
|
||||
_, err = command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Second))
|
||||
|
||||
cfg.TTL = 0
|
||||
_, err = command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Duration(0)))
|
||||
})
|
||||
|
||||
It("falls back to no expiry for an invalid TTL on a new bucket", func() {
|
||||
for _, ttl := range []time.Duration{-time.Hour, 50 * time.Millisecond} {
|
||||
db = "activitylog-ttl-test-" + uuid.New().String()
|
||||
cfg.Database = db
|
||||
cfg.TTL = ttl
|
||||
_, err := command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Duration(0)))
|
||||
}
|
||||
})
|
||||
|
||||
It("falls back to no expiry for an invalid TTL on an existing bucket", func() {
|
||||
for _, ttl := range []time.Duration{-time.Hour, 50 * time.Millisecond} {
|
||||
db = "activitylog-ttl-test-" + uuid.New().String()
|
||||
cfg.Database = db
|
||||
cfg.TTL = time.Hour
|
||||
_, err := command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Hour))
|
||||
|
||||
cfg.TTL = ttl
|
||||
_, err = command.ConnectNatsKV(cfg, log.NopLogger())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(maxAge()).To(Equal(time.Duration(0)))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
func activitites(acts ...any) []activitylog.RawActivity {
|
||||
var activities []activitylog.RawActivity
|
||||
act := activitylog.RawActivity{}
|
||||
|
||||
Reference in new issue
Block a user