diff --git a/db/database.go b/db/database.go index 024081e7df..36e7e7d5e8 100644 --- a/db/database.go +++ b/db/database.go @@ -58,6 +58,7 @@ const ( DefaultPurgeInterval = 30 * 24 * time.Hour DefaultSGReplicateEnabled = true DefaultSGReplicateWebsocketPingInterval = time.Minute * 5 + DefaultCompactInterval = 24 * time.Hour ) // Default values for delta sync @@ -66,7 +67,6 @@ var ( DefaultDeltaSyncRevMaxAge = uint32(60 * 60 * 24) // 24 hours in seconds ) -var DefaultCompactInterval = uint32(60 * 60 * 24) // Default compact interval in seconds = 1 Day var ( DefaultQueryPaginationLimit = 5000 ) diff --git a/rest/config_database.go b/rest/config_database.go index 10a50fff3c..2af0dfed5d 100644 --- a/rest/config_database.go +++ b/rest/config_database.go @@ -169,7 +169,7 @@ func DefaultDbConfig(sc *StartupConfig) *DbConfig { Enabled: base.BoolPtr(db.DefaultDeltaSyncEnabled), RevMaxAgeSeconds: base.Uint32Ptr(db.DefaultDeltaSyncRevMaxAge), }, - CompactIntervalDays: base.Float32Ptr(float32(db.DefaultCompactInterval)), + CompactIntervalDays: base.Float32Ptr(float32(db.DefaultCompactInterval.Hours() / 24)), SGReplicateEnabled: base.BoolPtr(db.DefaultSGReplicateEnabled), SGReplicateWebsocketPingInterval: base.IntPtr(int(db.DefaultSGReplicateWebsocketPingInterval.Seconds())), Replications: nil, diff --git a/rest/config_database_test.go b/rest/config_database_test.go new file mode 100644 index 0000000000..930833bab8 --- /dev/null +++ b/rest/config_database_test.go @@ -0,0 +1,23 @@ +// Copyright 2023-Present Couchbase, Inc. +// +// Use of this software is governed by the Business Source License included +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified +// in that file, in accordance with the Business Source License, use of this +// software will be governed by the Apache License, Version 2.0, included in +// the file licenses/APL2.txt. + +package rest + +import ( + "testing" + "time" + + "github.com/couchbase/sync_gateway/db" + "github.com/stretchr/testify/require" +) + +func TestDefaultDbConfig(t *testing.T) { + sc := DefaultStartupConfig("") + compactIntervalDays := *(DefaultDbConfig(&sc).CompactIntervalDays) + require.Equal(t, db.DefaultCompactInterval, time.Duration(compactIntervalDays)*time.Hour*24) +} diff --git a/rest/server_context.go b/rest/server_context.go index 4186202f52..ced750b0b0 100644 --- a/rest/server_context.go +++ b/rest/server_context.go @@ -1069,7 +1069,7 @@ func dbcOptionsFromConfig(ctx context.Context, sc *ServerContext, config *DbConf } base.InfofCtx(ctx, base.KeyAll, "delta_sync enabled=%t with rev_max_age_seconds=%d for database %s", deltaSyncOptions.Enabled, deltaSyncOptions.RevMaxAgeSeconds, dbName) - compactIntervalSecs := db.DefaultCompactInterval + compactIntervalSecs := uint32(db.DefaultCompactInterval.Seconds()) if config.CompactIntervalDays != nil { compactIntervalSecs = uint32(*config.CompactIntervalDays * 60 * 60 * 24) } diff --git a/rest/server_context_test.go b/rest/server_context_test.go index 418ba02b78..b189d1d5cc 100644 --- a/rest/server_context_test.go +++ b/rest/server_context_test.go @@ -875,3 +875,48 @@ func TestOfflineDatabaseStartup(t *testing.T) { require.NoError(t, err) assert.Equal(t, int64(1), rt.GetDatabase().DbStats.SharedBucketImport().ImportCount.Value()) } + +func TestCompactIntervalFromConfig(t *testing.T) { + testCases := []struct { + name string + compactIntervalDays *float32 + expectedCompactIntervalSecs uint32 + }{ + { + name: "compact interval days not set", + compactIntervalDays: nil, + expectedCompactIntervalSecs: uint32(db.DefaultCompactInterval.Seconds()), + }, + { + name: "explicit 1", + compactIntervalDays: base.Float32Ptr(1), + expectedCompactIntervalSecs: uint32((24 * time.Hour).Seconds()), + }, + { + name: "1.5", + compactIntervalDays: base.Float32Ptr(1.5), + expectedCompactIntervalSecs: uint32((1.5 * 24 * time.Hour).Seconds()), + }, + { + name: "2", + compactIntervalDays: base.Float32Ptr(2), + expectedCompactIntervalSecs: uint32((2 * 24 * time.Hour).Seconds()), + }, + } + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + + ctx := base.TestCtx(t) + startupConfig := &StartupConfig{} + sc := NewServerContext(ctx, startupConfig, false) + defer sc.Close(ctx) + config := &DbConfig{ + CompactIntervalDays: test.compactIntervalDays, + Unsupported: &db.UnsupportedOptions{}, + } + opts, err := dbcOptionsFromConfig(base.TestCtx(t), sc, config, "fakedb") + require.NoError(t, err) + require.Equal(t, int(test.expectedCompactIntervalSecs), int(opts.CompactInterval)) + }) + } +}