From 03a3f3d8242fcd43cdf3cf03d9ef408264d3049a Mon Sep 17 00:00:00 2001 From: Ben Brooks Date: Fri, 20 Sep 2024 14:53:14 +0100 Subject: [PATCH] CBG3026 prereq: More low-risk cleanup/refactoring (#7120) * More low-risk cleanup/refactoring - Move `DatabaseLogCtx` earlier in `_getOrAddDatabaseFromConfig` so we can identify which database init-related logging is for - `t.Run`->`rt.Run` fixes (prevents panics with failing subtests) - Minor rename and TODO comment for upcoming work * rearrange code based on review --- db/design_doc.go | 6 +++--- db/indexes.go | 1 + rest/indextest/index_test.go | 4 ++-- rest/server_context.go | 24 ++++++++++++------------ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/db/design_doc.go b/db/design_doc.go index 9383c4aa67..666a8eff40 100644 --- a/db/design_doc.go +++ b/db/design_doc.go @@ -344,9 +344,9 @@ func stripSyncProperty(row *sgbucket.ViewRow) { } func InitializeViews(ctx context.Context, ds sgbucket.DataStore) error { - collection, ok := ds.(*base.Collection) - if ok && !collection.IsDefaultScopeCollection() { - return fmt.Errorf("Can not initialize views on a non default collection") + collection, isCBServerCollection := ds.(*base.Collection) + if isCBServerCollection && !collection.IsDefaultScopeCollection() { + return fmt.Errorf("Can not initialize views on a non-default Couchbase Server collection") } viewStore, ok := ds.(sgbucket.ViewStore) diff --git a/db/indexes.go b/db/indexes.go index da3c730d3c..2a604a5c4d 100644 --- a/db/indexes.go +++ b/db/indexes.go @@ -384,6 +384,7 @@ func InitializeIndexes(ctx context.Context, n1QLStore base.N1QLStore, options In deferredIndexes := make([]string, 0) for _, sgIndex := range sgIndexes { + // TODO CBG-2838: build list of indexes to create (with one GetIndexMeta call) if !sgIndex.shouldCreate(options) { base.DebugfCtx(ctx, base.KeyAll, "Skipping index: %s ...", sgIndex.simpleName) continue diff --git a/rest/indextest/index_test.go b/rest/indextest/index_test.go index f7423037b3..86fdbdb509 100644 --- a/rest/indextest/index_test.go +++ b/rest/indextest/index_test.go @@ -80,7 +80,7 @@ func TestSyncGatewayStartupIndexes(t *testing.T) { } // tests sg_users index - t.Run("testUserQueries", func(t *testing.T) { + rt.Run("testUserQueries", func(t *testing.T) { users := []string{"alice", "bob"} for _, user := range users { @@ -96,7 +96,7 @@ func TestSyncGatewayStartupIndexes(t *testing.T) { }) // tests sg_roles index - t.Run("testRoleQueries", func(t *testing.T) { + rt.Run("testRoleQueries", func(t *testing.T) { roles := []string{"roleA", "roleB"} for _, role := range roles { diff --git a/rest/server_context.go b/rest/server_context.go index 347f9f3cae..9bd041fa9a 100644 --- a/rest/server_context.go +++ b/rest/server_context.go @@ -608,10 +608,6 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config return nil, err } - if spec.Server == "" { - spec.Server = sc.Config.Bootstrap.Server - } - previousDatabase := sc.databases_[dbName] if previousDatabase != nil { if options.useExisting { @@ -622,6 +618,18 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config "Duplicate database name %q", dbName) } + // Generate database context options from config and server context + contextOptions, err := dbcOptionsFromConfig(ctx, sc, &config.DbConfig, dbName) + if err != nil { + return nil, err + } + // set this early so we have dbName available in db-init related logging, before we have an actual database + ctx = base.DatabaseLogCtx(ctx, dbName, contextOptions.LoggingConfig) + + if spec.Server == "" { + spec.Server = sc.Config.Bootstrap.Server + } + // Connect to bucket base.InfofCtx(ctx, base.KeyAll, "Opening db /%s as bucket %q, pool %q, server <%s>", base.MD(dbName), base.MD(spec.BucketName), base.SD(base.DefaultPool), base.SD(spec.Server)) @@ -777,14 +785,6 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config return nil, err } - // Generate database context options from config and server context - contextOptions, err := dbcOptionsFromConfig(ctx, sc, &config.DbConfig, dbName) - if err != nil { - return nil, err - } - - ctx = base.DatabaseLogCtx(ctx, dbName, contextOptions.LoggingConfig) - contextOptions.UseViews = useViews javascriptTimeout := getJavascriptTimeout(&config.DbConfig)