Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBG-3285 fix DefaultDbConfig CompactIntervalDays #6409

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion rest/config_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions rest/config_database_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion rest/server_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
45 changes: 45 additions & 0 deletions rest/server_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}