From d26ad3152657c473269f571e4b754c17a27a70ac Mon Sep 17 00:00:00 2001 From: Tor Colvin Date: Fri, 30 Jun 2023 16:47:04 -0400 Subject: [PATCH] (3.1.1 backport) CBG-3108 don't log non SG indexes (#6317) (#6330) - add context based logging to include bucket and collection information --- base/bucket_n1ql_test.go | 126 +++++++++++++++++++-------------- base/cluster_n1ql.go | 24 +++---- base/collection_n1ql.go | 25 ++++--- base/collection_n1ql_common.go | 78 ++++++++++---------- base/util_testing.go | 4 +- db/database.go | 2 +- db/functions/function_test.go | 5 +- db/functions/graphql_test.go | 2 +- db/indexes.go | 36 +++++----- db/indexes_test.go | 18 ++--- db/indextest/indextest_test.go | 2 +- db/util_testing.go | 2 +- rest/api_collections_test.go | 5 +- rest/utilities_testing.go | 2 +- 14 files changed, 176 insertions(+), 155 deletions(-) diff --git a/base/bucket_n1ql_test.go b/base/bucket_n1ql_test.go index 0e5796c4d9..69894bd3a9 100644 --- a/base/bucket_n1ql_test.go +++ b/base/bucket_n1ql_test.go @@ -11,6 +11,7 @@ licenses/APL2.txt. package base import ( + "context" "errors" "fmt" "testing" @@ -53,20 +54,21 @@ func TestN1qlQuery(t *testing.T) { assert.True(t, added, "AddRaw returned added=false, expected true") } + ctx := TestCtx(t) indexExpression := "val" - err := n1qlStore.CreateIndex("testIndex_value", indexExpression, "", testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndex_value", indexExpression, "", testN1qlOptions) if err != nil && err != ErrAlreadyExists { t.Errorf("Error creating index: %s", err) } // Wait for index readiness - onlineErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex_value"}, false) + onlineErr := n1qlStore.WaitForIndexesOnline(ctx, []string{"testIndex_value"}, false) if onlineErr != nil { t.Fatalf("Error waiting for index to come online: %v", err) } // Check index state - exists, state, stateErr := n1qlStore.GetIndexMeta("testIndex_value") + exists, state, stateErr := n1qlStore.GetIndexMeta(ctx, "testIndex_value") assert.NoError(t, stateErr, "Error validating index state") assert.True(t, state != nil, "No state returned for index") assert.Equal(t, "online", state.State) @@ -75,13 +77,13 @@ func TestN1qlQuery(t *testing.T) { // Defer index teardown defer func() { // Drop the index - err = n1qlStore.DropIndex("testIndex_value") + err = n1qlStore.DropIndex(ctx, "testIndex_value") if err != nil { t.Fatalf("Error dropping index: %s", err) } }() - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex_value"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(ctx, []string{"testIndex_value"}, false) require.NoError(t, readyErr, "Error validating index online") // Query the index @@ -161,21 +163,23 @@ func TestN1qlFilterExpression(t *testing.T) { assert.True(t, added, "AddRaw returned added=false, expected true") } + ctx := TestCtx(t) + indexExpression := "val" filterExpression := "val < 3" - err := n1qlStore.CreateIndex("testIndex_filtered_value", indexExpression, filterExpression, testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndex_filtered_value", indexExpression, filterExpression, testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } // Wait for index readiness - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex_filtered_value"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(ctx, []string{"testIndex_filtered_value"}, false) require.NoError(t, readyErr, "Error validating index online") // Defer index teardown defer func() { // Drop the index - err = n1qlStore.DropIndex("testIndex_filtered_value") + err = n1qlStore.DropIndex(ctx, "testIndex_filtered_value") if err != nil { t.Fatalf("Error dropping index: %s", err) } @@ -230,31 +234,32 @@ func TestIndexMeta(t *testing.T) { t.Fatalf("Requires bucket to be N1QLStore") } + ctx := TestCtx(t) // Check index state pre-creation - exists, meta, err := n1qlStore.GetIndexMeta("testIndex_value") + exists, meta, err := n1qlStore.GetIndexMeta(ctx, "testIndex_value") assert.False(t, exists) assert.NoError(t, err, "Error getting meta for non-existent index") indexExpression := "val" - err = n1qlStore.CreateIndex("testIndex_value", indexExpression, "", testN1qlOptions) + err = n1qlStore.CreateIndex(ctx, "testIndex_value", indexExpression, "", testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex_value"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(ctx, []string{"testIndex_value"}, false) require.NoError(t, readyErr, "Error validating index online") // Defer index teardown defer func() { // Drop the index - err = n1qlStore.DropIndex("testIndex_value") + err = n1qlStore.DropIndex(ctx, "testIndex_value") if err != nil { t.Fatalf("Error dropping index: %s", err) } }() // Check index state post-creation - exists, meta, err = n1qlStore.GetIndexMeta("testIndex_value") + exists, meta, err = n1qlStore.GetIndexMeta(ctx, "testIndex_value") assert.True(t, exists) assert.Equal(t, "online", meta.State) assert.NoError(t, err, "Error retrieving index state") @@ -288,19 +293,21 @@ func TestMalformedN1qlQuery(t *testing.T) { assert.True(t, added, "AddRaw returned added=false, expected true") } + ctx := TestCtx(t) + indexExpression := "val" - err := n1qlStore.CreateIndex("testIndex_value_malformed", indexExpression, "", testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndex_value_malformed", indexExpression, "", testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex_value_malformed"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(ctx, []string{"testIndex_value_malformed"}, false) assert.NoError(t, readyErr, "Error validating index online") // Defer index teardown defer func() { // Drop the index - err = n1qlStore.DropIndex("testIndex_value_malformed") + err = n1qlStore.DropIndex(ctx, "testIndex_value_malformed") if err != nil { t.Fatalf("Error dropping index: %s", err) } @@ -352,16 +359,18 @@ func TestCreateAndDropIndex(t *testing.T) { t.Fatalf("Requires bucket to be N1QLStore") } + ctx := TestCtx(t) + createExpression := SyncPropertyName + ".sequence" - err := n1qlStore.CreateIndex("testIndex_sequence", createExpression, "", testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndex_sequence", createExpression, "", testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex_sequence"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(ctx, []string{"testIndex_sequence"}, false) assert.NoError(t, readyErr, "Error validating index online") // Drop the index - err = n1qlStore.DropIndex("testIndex_sequence") + err = n1qlStore.DropIndex(ctx, "testIndex_sequence") if err != nil { t.Fatalf("Error dropping index: %s", err) } @@ -381,22 +390,23 @@ func TestCreateDuplicateIndex(t *testing.T) { if !ok { t.Fatalf("Requires bucket to be N1QLStore") } + ctx := TestCtx(t) createExpression := SyncPropertyName + ".sequence" - err := n1qlStore.CreateIndex("testIndexDuplicateSequence", createExpression, "", testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndexDuplicateSequence", createExpression, "", testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndexDuplicateSequence"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(TestCtx(t), []string{"testIndexDuplicateSequence"}, false) assert.NoError(t, readyErr, "Error validating index online") // Attempt to create duplicate, validate duplicate error - duplicateErr := n1qlStore.CreateIndex("testIndexDuplicateSequence", createExpression, "", testN1qlOptions) + duplicateErr := n1qlStore.CreateIndex(ctx, "testIndexDuplicateSequence", createExpression, "", testN1qlOptions) assert.Equal(t, ErrAlreadyExists, duplicateErr) // Drop the index - err = n1qlStore.DropIndex("testIndexDuplicateSequence") + err = n1qlStore.DropIndex(ctx, "testIndexDuplicateSequence") if err != nil { t.Fatalf("Error dropping index: %s", err) } @@ -417,17 +427,19 @@ func TestCreateAndDropIndexSpecialCharacters(t *testing.T) { t.Fatalf("Requires bucket to be N1QLStore") } + ctx := TestCtx(t) + createExpression := SyncPropertyName + ".sequence" - err := n1qlStore.CreateIndex("testIndex-sequence", createExpression, "", testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndex-sequence", createExpression, "", testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } - readyErr := n1qlStore.WaitForIndexesOnline([]string{"testIndex-sequence"}, false) + readyErr := n1qlStore.WaitForIndexesOnline(TestCtx(t), []string{"testIndex-sequence"}, false) assert.NoError(t, readyErr, "Error validating index online") // Drop the index - err = n1qlStore.DropIndex("testIndex-sequence") + err = n1qlStore.DropIndex(ctx, "testIndex-sequence") if err != nil { t.Fatalf("Error dropping index: %s", err) } @@ -449,7 +461,8 @@ func TestDeferredCreateIndex(t *testing.T) { } indexName := "testIndexDeferred" - assert.NoError(t, tearDownTestIndex(n1qlStore, indexName), "Error in pre-test cleanup") + ctx := TestCtx(t) + assert.NoError(t, tearDownTestIndex(ctx, n1qlStore, indexName), "Error in pre-test cleanup") deferN1qlOptions := &N1qlIndexOptions{ NumReplica: 0, @@ -457,23 +470,23 @@ func TestDeferredCreateIndex(t *testing.T) { } createExpression := SyncPropertyName + ".sequence" - err := n1qlStore.CreateIndex(indexName, createExpression, "", deferN1qlOptions) + err := n1qlStore.CreateIndex(ctx, indexName, createExpression, "", deferN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } // Drop the index defer func() { - err = n1qlStore.DropIndex(indexName) + err = n1qlStore.DropIndex(ctx, indexName) if err != nil { t.Fatalf("Error dropping index: %s", err) } }() - buildErr := buildIndexes(n1qlStore, []string{indexName}) + buildErr := buildIndexes(ctx, n1qlStore, []string{indexName}) assert.NoError(t, buildErr, "Error building indexes") - readyErr := n1qlStore.WaitForIndexesOnline([]string{indexName}, false) + readyErr := n1qlStore.WaitForIndexesOnline(ctx, []string{indexName}, false) assert.NoError(t, readyErr, "Error validating index online") } @@ -493,10 +506,11 @@ func TestBuildDeferredIndexes(t *testing.T) { t.Fatalf("Requires bucket to be N1QLStore") } + ctx := TestCtx(t) deferredIndexName := "testIndexDeferred" nonDeferredIndexName := "testIndexNonDeferred" - assert.NoError(t, tearDownTestIndex(n1qlStore, deferredIndexName), "Error in pre-test cleanup") - assert.NoError(t, tearDownTestIndex(n1qlStore, nonDeferredIndexName), "Error in pre-test cleanup") + assert.NoError(t, tearDownTestIndex(ctx, n1qlStore, deferredIndexName), "Error in pre-test cleanup") + assert.NoError(t, tearDownTestIndex(ctx, n1qlStore, nonDeferredIndexName), "Error in pre-test cleanup") deferN1qlOptions := &N1qlIndexOptions{ NumReplica: 0, @@ -505,44 +519,44 @@ func TestBuildDeferredIndexes(t *testing.T) { // Create a deferred and a non-deferred index createExpression := SyncPropertyName + ".sequence" - err := n1qlStore.CreateIndex(deferredIndexName, createExpression, "", deferN1qlOptions) + err := n1qlStore.CreateIndex(ctx, deferredIndexName, createExpression, "", deferN1qlOptions) if err != nil { t.Errorf("Error creating index: %s", err) } createExpression = SyncPropertyName + ".rev" - err = n1qlStore.CreateIndex(nonDeferredIndexName, createExpression, "", &N1qlIndexOptions{NumReplica: 0}) + err = n1qlStore.CreateIndex(ctx, nonDeferredIndexName, createExpression, "", &N1qlIndexOptions{NumReplica: 0}) if err != nil { t.Fatalf("Error creating index: %s", err) } // Drop the indexes defer func() { - err = n1qlStore.DropIndex(deferredIndexName) + err = n1qlStore.DropIndex(ctx, deferredIndexName) if err != nil { t.Fatalf("Error dropping deferred index: %s", err) } }() defer func() { - err = n1qlStore.DropIndex(nonDeferredIndexName) + err = n1qlStore.DropIndex(ctx, nonDeferredIndexName) if err != nil { t.Fatalf("Error dropping non-deferred index: %s", err) } }() - buildErr := n1qlStore.BuildDeferredIndexes([]string{deferredIndexName, nonDeferredIndexName}) + buildErr := n1qlStore.BuildDeferredIndexes(TestCtx(t), []string{deferredIndexName, nonDeferredIndexName}) assert.NoError(t, buildErr, "Error building indexes") - readyErr := n1qlStore.WaitForIndexesOnline([]string{deferredIndexName}, false) + readyErr := n1qlStore.WaitForIndexesOnline(TestCtx(t), []string{deferredIndexName}, false) assert.NoError(t, readyErr, "Error validating index online") - readyErr = n1qlStore.WaitForIndexesOnline([]string{nonDeferredIndexName}, false) + readyErr = n1qlStore.WaitForIndexesOnline(TestCtx(t), []string{nonDeferredIndexName}, false) assert.NoError(t, readyErr, "Error validating index online") // Ensure no errors from no-op scenarios - alreadyBuiltErr := n1qlStore.BuildDeferredIndexes([]string{deferredIndexName, nonDeferredIndexName}) + alreadyBuiltErr := n1qlStore.BuildDeferredIndexes(TestCtx(t), []string{deferredIndexName, nonDeferredIndexName}) assert.NoError(t, alreadyBuiltErr, "Error building already built indexes") - emptySetErr := n1qlStore.BuildDeferredIndexes([]string{}) + emptySetErr := n1qlStore.BuildDeferredIndexes(TestCtx(t), []string{}) assert.NoError(t, emptySetErr, "Error building empty set") } @@ -560,54 +574,55 @@ func TestCreateAndDropIndexErrors(t *testing.T) { if !ok { t.Fatalf("Requires bucket to be N1QLStore") } + ctx := TestCtx(t) // Malformed expression createExpression := "_sync sequence" - err := n1qlStore.CreateIndex("testIndex_malformed", createExpression, "", testN1qlOptions) + err := n1qlStore.CreateIndex(ctx, "testIndex_malformed", createExpression, "", testN1qlOptions) if err == nil { t.Fatalf("Expected error for malformed index expression") } // Create index createExpression = "_sync.sequence" - err = n1qlStore.CreateIndex("testIndex_sequence", createExpression, "", testN1qlOptions) + err = n1qlStore.CreateIndex(ctx, "testIndex_sequence", createExpression, "", testN1qlOptions) if err != nil { t.Fatalf("Error creating index: %s", err) } // Attempt to recreate duplicate index - err = n1qlStore.CreateIndex("testIndex_sequence", createExpression, "", testN1qlOptions) + err = n1qlStore.CreateIndex(ctx, "testIndex_sequence", createExpression, "", testN1qlOptions) if err == nil { t.Fatalf("Expected error attempting to recreate already existing index") } // Drop non-existent index - err = n1qlStore.DropIndex("testIndex_not_found") + err = n1qlStore.DropIndex(ctx, "testIndex_not_found") if err == nil { t.Fatalf("Expected error attempting to drop non-existent index") } // Drop the index - err = n1qlStore.DropIndex("testIndex_sequence") + err = n1qlStore.DropIndex(ctx, "testIndex_sequence") if err != nil { t.Fatalf("Error dropping index: %s", err) } // Drop index that's already been dropped - err = n1qlStore.DropIndex("testIndex_sequence") + err = n1qlStore.DropIndex(ctx, "testIndex_sequence") if err == nil { t.Fatalf("Expected error attempting to drop index twice") } } -func tearDownTestIndex(n1qlStore N1QLStore, indexName string) (err error) { +func tearDownTestIndex(ctx context.Context, n1qlStore N1QLStore, indexName string) (err error) { - exists, _, err := n1qlStore.GetIndexMeta(indexName) + exists, _, err := n1qlStore.GetIndexMeta(ctx, indexName) if err != nil { return err } if exists { - return n1qlStore.DropIndex(indexName) + return n1qlStore.DropIndex(ctx, indexName) } else { return nil } @@ -638,22 +653,23 @@ func TestWaitForBucketExistence(t *testing.T) { ) var options = &N1qlIndexOptions{NumReplica: 0} + ctx := TestCtx(t) go func() { indexExists, _, err := getIndexMetaWithoutRetry(n1qlStore, indexName) assert.NoError(t, err, "No error while trying to fetch the index metadata") if indexExists { - err := n1qlStore.DropIndex(indexName) + err := n1qlStore.DropIndex(ctx, indexName) assert.NoError(t, err, "Index should be removed from the bucket") } - err = n1qlStore.CreateIndex(indexName, expression, filterExpression, options) + err = n1qlStore.CreateIndex(ctx, indexName, expression, filterExpression, options) assert.NoError(t, err, "Index should be created in the bucket") }() - assert.NoError(t, n1qlStore.WaitForIndexesOnline([]string{indexName}, false)) + assert.NoError(t, n1qlStore.WaitForIndexesOnline(TestCtx(t), []string{indexName}, false)) // Drop the index; - err := n1qlStore.DropIndex(indexName) + err := n1qlStore.DropIndex(ctx, indexName) assert.NoError(t, err, "Index should be removed from the bucket") } diff --git a/base/cluster_n1ql.go b/base/cluster_n1ql.go index 89eda8ed16..6b22173f56 100644 --- a/base/cluster_n1ql.go +++ b/base/cluster_n1ql.go @@ -64,24 +64,24 @@ func (cl *ClusterOnlyN1QLStore) BucketName() string { return cl.bucketName } -func (cl *ClusterOnlyN1QLStore) BuildDeferredIndexes(indexSet []string) error { - return BuildDeferredIndexes(cl, indexSet) +func (cl *ClusterOnlyN1QLStore) BuildDeferredIndexes(ctx context.Context, indexSet []string) error { + return BuildDeferredIndexes(ctx, cl, indexSet) } -func (cl *ClusterOnlyN1QLStore) CreateIndex(indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error { - return CreateIndex(cl, indexName, expression, filterExpression, options) +func (cl *ClusterOnlyN1QLStore) CreateIndex(ctx context.Context, indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error { + return CreateIndex(ctx, cl, indexName, expression, filterExpression, options) } -func (cl *ClusterOnlyN1QLStore) CreatePrimaryIndex(indexName string, options *N1qlIndexOptions) error { - return CreatePrimaryIndex(cl, indexName, options) +func (cl *ClusterOnlyN1QLStore) CreatePrimaryIndex(ctx context.Context, indexName string, options *N1qlIndexOptions) error { + return CreatePrimaryIndex(ctx, cl, indexName, options) } func (cl *ClusterOnlyN1QLStore) ExplainQuery(statement string, params map[string]interface{}) (plan map[string]interface{}, err error) { return ExplainQuery(cl, statement, params) } -func (cl *ClusterOnlyN1QLStore) DropIndex(indexName string) error { - return DropIndex(cl, indexName) +func (cl *ClusterOnlyN1QLStore) DropIndex(ctx context.Context, indexName string) error { + return DropIndex(ctx, cl, indexName) } // IndexMetaKeyspaceID returns the value of keyspace_id for the system:indexes table for the collection. @@ -190,12 +190,12 @@ func (cl *ClusterOnlyN1QLStore) runQuery(statement string, n1qlOptions *gocb.Que return queryResults, err } -func (cl *ClusterOnlyN1QLStore) WaitForIndexesOnline(indexNames []string, failfast bool) error { - return WaitForIndexesOnline(cl.cluster, cl.bucketName, cl.scopeName, cl.collectionName, indexNames, failfast) +func (cl *ClusterOnlyN1QLStore) WaitForIndexesOnline(ctx context.Context, indexNames []string, failfast bool) error { + return WaitForIndexesOnline(ctx, cl.cluster, cl.bucketName, cl.scopeName, cl.collectionName, indexNames, failfast) } -func (cl *ClusterOnlyN1QLStore) GetIndexMeta(indexName string) (exists bool, meta *IndexMeta, err error) { - return GetIndexMeta(cl, indexName) +func (cl *ClusterOnlyN1QLStore) GetIndexMeta(ctx context.Context, indexName string) (exists bool, meta *IndexMeta, err error) { + return GetIndexMeta(ctx, cl, indexName) } func (cl *ClusterOnlyN1QLStore) IsErrNoResults(err error) bool { diff --git a/base/collection_n1ql.go b/base/collection_n1ql.go index 15f3fbab3c..d1aff1027d 100644 --- a/base/collection_n1ql.go +++ b/base/collection_n1ql.go @@ -117,31 +117,30 @@ func (c *Collection) ExplainQuery(statement string, params map[string]interface{ return ExplainQuery(c, statement, params) } -func (c *Collection) CreateIndex(indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error { - return CreateIndex(c, indexName, expression, filterExpression, options) +func (c *Collection) CreateIndex(ctx context.Context, indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error { + return CreateIndex(ctx, c, indexName, expression, filterExpression, options) } -func (c *Collection) CreatePrimaryIndex(indexName string, options *N1qlIndexOptions) error { - return CreatePrimaryIndex(c, indexName, options) +func (c *Collection) CreatePrimaryIndex(ctx context.Context, indexName string, options *N1qlIndexOptions) error { + return CreatePrimaryIndex(ctx, c, indexName, options) } -// WaitForIndexesOnline takes set of indexes and watches them till they're online. -func (c *Collection) WaitForIndexesOnline(indexNames []string, failfast bool) error { - return WaitForIndexesOnline(c.Bucket.cluster, c.BucketName(), c.ScopeName(), c.CollectionName(), indexNames, failfast) +func (c *Collection) WaitForIndexesOnline(ctx context.Context, indexNames []string, failfast bool) error { + return WaitForIndexesOnline(ctx, c.Bucket.cluster, c.BucketName(), c.ScopeName(), c.CollectionName(), indexNames, failfast) } -func (c *Collection) GetIndexMeta(indexName string) (exists bool, meta *IndexMeta, err error) { - return GetIndexMeta(c, indexName) +func (c *Collection) GetIndexMeta(ctx context.Context, indexName string) (exists bool, meta *IndexMeta, err error) { + return GetIndexMeta(ctx, c, indexName) } // DropIndex drops the specified index from the current bucket. -func (c *Collection) DropIndex(indexName string) error { - return DropIndex(c, indexName) +func (c *Collection) DropIndex(ctx context.Context, indexName string) error { + return DropIndex(ctx, c, indexName) } // Issues a build command for any deferred sync gateway indexes associated with the bucket. -func (c *Collection) BuildDeferredIndexes(indexSet []string) error { - return BuildDeferredIndexes(c, indexSet) +func (c *Collection) BuildDeferredIndexes(ctx context.Context, indexSet []string) error { + return BuildDeferredIndexes(ctx, c, indexSet) } func (b *GocbV2Bucket) runQuery(statement string, n1qlOptions *gocb.QueryOptions) (*gocb.QueryResult, error) { diff --git a/base/collection_n1ql_common.go b/base/collection_n1ql_common.go index 4e4eca7a6c..34b9d9c220 100644 --- a/base/collection_n1ql_common.go +++ b/base/collection_n1ql_common.go @@ -16,6 +16,8 @@ import ( "strings" "time" + "golang.org/x/exp/slices" + "github.com/couchbase/gocb/v2" sgbucket "github.com/couchbase/sg-bucket" pkgerrors "github.com/pkg/errors" @@ -36,12 +38,12 @@ const ( // N1QLStore defines the set of operations Sync Gateway uses to manage and interact with N1QL type N1QLStore interface { GetName() string - BuildDeferredIndexes(indexSet []string) error - CreateIndex(indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error - CreatePrimaryIndex(indexName string, options *N1qlIndexOptions) error - DropIndex(indexName string) error + BuildDeferredIndexes(ctx context.Context, indexSet []string) error + CreateIndex(ctx context.Context, indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error + CreatePrimaryIndex(ctx context.Context, indexName string, options *N1qlIndexOptions) error + DropIndex(ctx context.Context, indexName string) error ExplainQuery(statement string, params map[string]interface{}) (plan map[string]interface{}, err error) - GetIndexMeta(indexName string) (exists bool, meta *IndexMeta, err error) + GetIndexMeta(ctx context.Context, indexName string) (exists bool, meta *IndexMeta, err error) Query(statement string, params map[string]interface{}, consistency ConsistencyMode, adhoc bool) (results sgbucket.QueryResultIterator, err error) IsErrNoResults(error) bool EscapedKeyspace() string @@ -49,7 +51,7 @@ type N1QLStore interface { IndexMetaScopeID() string IndexMetaKeyspaceID() string BucketName() string - WaitForIndexesOnline(indexNames []string, failfast bool) error + WaitForIndexesOnline(ctx context.Context, indexNames []string, failfast bool) error // executeQuery performs the specified query without any built-in retry handling and returns the resultset executeQuery(statement string) (sgbucket.QueryResultIterator, error) @@ -90,7 +92,7 @@ func ExplainQuery(store N1QLStore, statement string, params map[string]interface // // CreateIndex("myIndex", "field1, field2, nested.field", "field1 > 0", N1qlIndexOptions{numReplica:1}) // CREATE INDEX myIndex on myBucket(field1, field2, nested.field) WHERE field1 > 0 WITH {"numReplica":1} -func CreateIndex(store N1QLStore, indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error { +func CreateIndex(ctx context.Context, store N1QLStore, indexName string, expression string, filterExpression string, options *N1qlIndexOptions) error { createStatement := fmt.Sprintf("CREATE INDEX `%s` ON %s(%s)", indexName, store.EscapedKeyspace(), expression) // Add filter expression, when present @@ -101,7 +103,7 @@ func CreateIndex(store N1QLStore, indexName string, expression string, filterExp // Replace any KeyspaceQueryToken references in the index expression createStatement = strings.Replace(createStatement, KeyspaceQueryToken, store.EscapedKeyspace(), -1) - createErr := createIndex(store, indexName, createStatement, options) + createErr := createIndex(ctx, store, indexName, createStatement, options) if createErr != nil { if strings.Contains(createErr.Error(), "already exists") || strings.Contains(createErr.Error(), "duplicate index name") { return ErrAlreadyExists @@ -110,12 +112,12 @@ func CreateIndex(store N1QLStore, indexName string, expression string, filterExp return createErr } -func CreatePrimaryIndex(store N1QLStore, indexName string, options *N1qlIndexOptions) error { +func CreatePrimaryIndex(ctx context.Context, store N1QLStore, indexName string, options *N1qlIndexOptions) error { createStatement := fmt.Sprintf("CREATE PRIMARY INDEX `%s` ON %s", indexName, store.EscapedKeyspace()) - return createIndex(store, indexName, createStatement, options) + return createIndex(ctx, store, indexName, createStatement, options) } -func createIndex(store N1QLStore, indexName string, createStatement string, options *N1qlIndexOptions) error { +func createIndex(ctx context.Context, store N1QLStore, indexName string, createStatement string, options *N1qlIndexOptions) error { if options != nil { withClause, marshalErr := JSONMarshal(options) @@ -125,7 +127,7 @@ func createIndex(store N1QLStore, indexName string, createStatement string, opti createStatement = fmt.Sprintf(`%s with %s`, createStatement, withClause) } - DebugfCtx(context.TODO(), KeyQuery, "Attempting to create index using statement: [%s]", UD(createStatement)) + DebugfCtx(ctx, KeyQuery, "Attempting to create index using statement: [%s]", UD(createStatement)) err := store.executeStatement(createStatement) if err == nil { @@ -133,27 +135,27 @@ func createIndex(store N1QLStore, indexName string, createStatement string, opti } if IsIndexerRetryIndexError(err) { - InfofCtx(context.TODO(), KeyQuery, "Indexer error creating index - waiting for server background retry. Error:%v", err) + InfofCtx(ctx, KeyQuery, "Indexer error creating index - waiting for server background retry. Error:%v", err) // Wait for bucket to be created in background before returning - return waitForIndexExistence(store, indexName, true) + return waitForIndexExistence(ctx, store, indexName, true) } if IsCreateDuplicateIndexError(err) { - InfofCtx(context.TODO(), KeyQuery, "Duplicate index creation in progress - waiting for index readiness. Error:%v", err) + InfofCtx(ctx, KeyQuery, "Duplicate index creation in progress - waiting for index readiness. Error:%v", err) // Wait for bucket to be created in background before returning - return waitForIndexExistence(store, indexName, true) + return waitForIndexExistence(ctx, store, indexName, true) } return pkgerrors.WithStack(RedactErrorf("Error creating index with statement: %s. Error: %v", UD(createStatement), err)) } // Waits for index to exist/not exist. Used in response to background create/drop processing by server. -func waitForIndexExistence(store N1QLStore, indexName string, shouldExist bool) error { +func waitForIndexExistence(ctx context.Context, store N1QLStore, indexName string, shouldExist bool) error { worker := func() (shouldRetry bool, err error, value interface{}) { // GetIndexMeta has its own error retry handling, // but keep the retry logic up here for checking if the index exists. - exists, _, err := store.GetIndexMeta(indexName) + exists, _, err := store.GetIndexMeta(ctx, indexName) if err != nil { return false, err, nil } @@ -175,7 +177,7 @@ func waitForIndexExistence(store N1QLStore, indexName string, shouldExist bool) } // BuildDeferredIndexes issues a build command for any deferred sync gateway indexes associated with the bucket. -func BuildDeferredIndexes(s N1QLStore, indexSet []string) error { +func BuildDeferredIndexes(ctx context.Context, s N1QLStore, indexSet []string) error { if len(indexSet) == 0 { return nil @@ -218,15 +220,15 @@ func BuildDeferredIndexes(s N1QLStore, indexSet []string) error { return nil } - InfofCtx(context.TODO(), KeyQuery, "Building deferred indexes: %v", deferredIndexes) - buildErr := buildIndexes(s, deferredIndexes) + InfofCtx(ctx, KeyQuery, "Building deferred indexes: %v", deferredIndexes) + buildErr := buildIndexes(ctx, s, deferredIndexes) return buildErr } // BuildIndexes executes a BUILD INDEX statement in the current bucket, using the form: // // BUILD INDEX ON `bucket.Name`(`index1`, `index2`, ...) -func buildIndexes(s N1QLStore, indexNames []string) error { +func buildIndexes(ctx context.Context, s N1QLStore, indexNames []string) error { if len(indexNames) == 0 { return nil } @@ -241,7 +243,7 @@ func buildIndexes(s N1QLStore, indexNames []string) error { if IsIndexerRetryBuildError(err) { InfofCtx(context.TODO(), KeyQuery, "Indexer error creating index - waiting for background build. Error:%v", err) // Wait for bucket to be created in background before returning - return s.WaitForIndexesOnline(indexNames, false) + return s.WaitForIndexesOnline(ctx, indexNames, false) } return err @@ -263,13 +265,13 @@ type getIndexMetaRetryValues struct { meta *IndexMeta } -func GetIndexMeta(store N1QLStore, indexName string) (exists bool, meta *IndexMeta, err error) { +func GetIndexMeta(ctx context.Context, store N1QLStore, indexName string) (exists bool, meta *IndexMeta, err error) { worker := func() (shouldRetry bool, err error, value interface{}) { exists, meta, err := getIndexMetaWithoutRetry(store, indexName) if err != nil { // retry - WarnfCtx(context.TODO(), "Error from GetIndexMeta for index %s: %v will retry", indexName, err) + WarnfCtx(ctx, "Error from GetIndexMeta for index %s: %v will retry", indexName, err) return true, err, nil } return false, nil, getIndexMetaRetryValues{ @@ -318,7 +320,7 @@ func getIndexMetaWithoutRetry(store N1QLStore, indexName string) (exists bool, m } // DropIndex drops the specified index from the current bucket. -func DropIndex(store N1QLStore, indexName string) error { +func DropIndex(ctx context.Context, store N1QLStore, indexName string) error { statement := fmt.Sprintf("DROP INDEX default:%s.`%s`", store.EscapedKeyspace(), indexName) err := store.executeStatement(statement) @@ -327,9 +329,9 @@ func DropIndex(store N1QLStore, indexName string) error { } if IsIndexerRetryIndexError(err) { - InfofCtx(context.TODO(), KeyQuery, "Indexer error dropping index - waiting for server background retry. Error:%v", err) + InfofCtx(ctx, KeyQuery, "Indexer error dropping index - waiting for server background retry. Error:%v", err) // Wait for bucket to be dropped in background before returning - return waitForIndexExistence(store, indexName, false) + return waitForIndexExistence(ctx, store, indexName, false) } return err @@ -516,8 +518,7 @@ func IndexMetaKeyspaceID(bucketName, scopeName, collectionName string) string { } // WaitForIndexesOnline takes set of indexes and watches them till they're online. -func WaitForIndexesOnline(cluster *gocb.Cluster, bucketName, scopeName, collectionName string, indexNames []string, failfast bool) error { - logCtx := context.TODO() +func WaitForIndexesOnline(ctx context.Context, cluster *gocb.Cluster, bucketName, scopeName, collectionName string, indexNames []string, failfast bool) error { mgr := cluster.QueryIndexes() maxNumAttempts := 180 if failfast { @@ -542,17 +543,22 @@ func WaitForIndexesOnline(cluster *gocb.Cluster, bucketName, scopeName, collecti } // check each of the current indexes state, add to map once finished to make sure each index online is only being logged once for i := 0; i < len(currIndexes); i++ { - if currIndexes[i].State == IndexStateOnline { - if !onlineIndexes[currIndexes[i].Name] { - InfofCtx(logCtx, KeyAll, "Index %s is online", MD(currIndexes[i].Name)) - onlineIndexes[currIndexes[i].Name] = true + name := currIndexes[i].Name + // use slices.Contains since the number of indexes is expected to be small + if currIndexes[i].State == IndexStateOnline && slices.Contains(indexNames, name) { + if !onlineIndexes[name] { + InfofCtx(ctx, KeyAll, "Index %s is online", MD(name)) + onlineIndexes[name] = true } } } // check online index against indexes we watch to have online, increase counter as each comes online + var offlineIndexes []string for _, listVal := range indexNames { if onlineIndexes[listVal] { watchedOnlineIndexCount++ + } else { + offlineIndexes = append(offlineIndexes, listVal) } } @@ -562,9 +568,9 @@ func WaitForIndexesOnline(cluster *gocb.Cluster, bucketName, scopeName, collecti retryCount++ shouldContinue, sleepMs := retrySleeper(retryCount) if !shouldContinue { - return fmt.Errorf("error waiting for indexes for bucket %s....", MD(bucketName)) + return fmt.Errorf("error waiting for indexes %s ...", strings.Join(offlineIndexes, ", ")) } - InfofCtx(logCtx, KeyAll, "Indexes for bucket %s not ready - retrying...", MD(bucketName)) + InfofCtx(ctx, KeyAll, "Indexes %s not ready - retrying...", strings.Join(offlineIndexes, ", ")) time.Sleep(time.Millisecond * time.Duration(sleepMs)) } } diff --git a/base/util_testing.go b/base/util_testing.go index 8d01b89016..a483aad07d 100644 --- a/base/util_testing.go +++ b/base/util_testing.go @@ -359,10 +359,10 @@ func DropAllIndexes(ctx context.Context, n1QLStore N1QLStore) error { defer wg.Done() InfofCtx(ctx, KeySGTest, "Dropping index %s on bucket %s...", indexToDrop, n1QLStore.GetName()) - dropErr := n1QLStore.DropIndex(indexToDrop) + dropErr := n1QLStore.DropIndex(ctx, indexToDrop) if dropErr != nil { // Retry dropping index if first try fails before returning error - dropRetry := n1QLStore.DropIndex(indexToDrop) + dropRetry := n1QLStore.DropIndex(ctx, indexToDrop) if dropRetry != nil { asyncErrors <- dropErr ErrorfCtx(ctx, "...failed to drop index %s on bucket %s: %s", indexToDrop, n1QLStore.GetName(), dropErr) diff --git a/db/database.go b/db/database.go index 6d6268c6fa..99ab8504e2 100644 --- a/db/database.go +++ b/db/database.go @@ -767,7 +767,7 @@ func (dbCtx *DatabaseContext) RemoveObsoleteIndexes(ctx context.Context, preview errs = errs.Append(errors.New(err)) continue } - collectionRemovedIndexes, err := removeObsoleteIndexes(n1qlStore, previewOnly, dbCtx.UseXattrs(), dbCtx.UseViews(), sgIndexes) + collectionRemovedIndexes, err := removeObsoleteIndexes(ctx, n1qlStore, previewOnly, dbCtx.UseXattrs(), dbCtx.UseViews(), sgIndexes) if err != nil { errs = errs.Append(err) continue diff --git a/db/functions/function_test.go b/db/functions/function_test.go index b983d588e9..1d36e48e9c 100644 --- a/db/functions/function_test.go +++ b/db/functions/function_test.go @@ -684,12 +684,13 @@ func setupTestDBForBucketWithOptions(t testing.TB, tBucket base.Bucket, dbcOptio // createPrimaryIndex returns true if there was no index created before func createPrimaryIndex(t *testing.T, n1qlStore base.N1QLStore) bool { - hasPrimary, _, err := base.GetIndexMeta(n1qlStore, base.PrimaryIndexName) + ctx := base.TestCtx(t) + hasPrimary, _, err := base.GetIndexMeta(ctx, n1qlStore, base.PrimaryIndexName) assert.NoError(t, err) if hasPrimary { return false } - err = n1qlStore.CreatePrimaryIndex(base.PrimaryIndexName, nil) + err = n1qlStore.CreatePrimaryIndex(ctx, base.PrimaryIndexName, nil) assert.NoError(t, err) return true } diff --git a/db/functions/graphql_test.go b/db/functions/graphql_test.go index 006b7601e2..910e6dde17 100644 --- a/db/functions/graphql_test.go +++ b/db/functions/graphql_test.go @@ -439,7 +439,7 @@ func TestUserGraphQLWithN1QL(t *testing.T) { if createdPrimaryIdx { defer func() { - err := n1qlStore.DropIndex(base.PrimaryIndexName) + err := n1qlStore.DropIndex(base.TestCtx(t), base.PrimaryIndexName) require.NoError(t, err) }() diff --git a/db/indexes.go b/db/indexes.go index c88ed8086b..bd683e6398 100644 --- a/db/indexes.go +++ b/db/indexes.go @@ -271,11 +271,11 @@ func (i *SGIndex) shouldCreate(options InitializeIndexOptions) bool { // Creates index associated with specified SGIndex if not already present. Always defers build - a subsequent BUILD INDEX // will need to be invoked for any created indexes. -func (i *SGIndex) createIfNeeded(bucket base.N1QLStore, options InitializeIndexOptions) (isDeferred bool, err error) { +func (i *SGIndex) createIfNeeded(ctx context.Context, bucket base.N1QLStore, options InitializeIndexOptions) (isDeferred bool, err error) { indexName := i.fullIndexName(options.UseXattrs) - exists, indexMeta, metaErr := bucket.GetIndexMeta(indexName) + exists, indexMeta, metaErr := bucket.GetIndexMeta(ctx, indexName) if metaErr != nil { return false, metaErr } @@ -291,9 +291,9 @@ func (i *SGIndex) createIfNeeded(bucket base.N1QLStore, options InitializeIndexO // 2. SG previously crashed between index creation and index build. // GSI doesn't like concurrent build requests, so wait and recheck index state before treating as option 2. // (see known issue documented https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/build-index.html) - base.InfofCtx(context.TODO(), base.KeyQuery, "Index %s already in deferred state - waiting 10s to re-evaluate before issuing build to avoid concurrent build requests.", indexName) + base.InfofCtx(ctx, base.KeyQuery, "Index %s already in deferred state - waiting 10s to re-evaluate before issuing build to avoid concurrent build requests.", indexName) time.Sleep(10 * time.Second) - _, indexMeta, metaErr = bucket.GetIndexMeta(indexName) + _, indexMeta, metaErr = bucket.GetIndexMeta(ctx, indexName) if metaErr != nil || indexMeta == nil { return false, fmt.Errorf("Error retrieving index metadata after defer wait. IndexMeta: %v Error:%v", indexMeta, metaErr) } @@ -304,10 +304,8 @@ func (i *SGIndex) createIfNeeded(bucket base.N1QLStore, options InitializeIndexO return false, nil } - logCtx := context.TODO() - // Create index - base.InfofCtx(logCtx, base.KeyQuery, "Index %s doesn't exist, creating...", indexName) + base.InfofCtx(ctx, base.KeyQuery, "Index %s doesn't exist, creating...", indexName) isDeferred = true indexExpression := replaceSyncTokensIndex(i.expression, options.UseXattrs) filterExpression := replaceSyncTokensIndex(i.filterExpression, options.UseXattrs) @@ -323,7 +321,7 @@ func (i *SGIndex) createIfNeeded(bucket base.N1QLStore, options InitializeIndexO // start a retry loop to create index, worker := func() (shouldRetry bool, err error, value interface{}) { - err = bucket.CreateIndex(indexName, indexExpression, filterExpression, n1qlOptions) + err = bucket.CreateIndex(ctx, indexName, indexExpression, filterExpression, n1qlOptions) if err != nil { // If index has already been created (race w/ other SG node), return without error if err == base.ErrAlreadyExists { @@ -333,7 +331,7 @@ func (i *SGIndex) createIfNeeded(bucket base.N1QLStore, options InitializeIndexO if strings.Contains(err.Error(), "not enough indexer nodes") { return false, fmt.Errorf("Unable to create indexes with the specified number of replicas (%d). Increase the number of index nodes, or modify 'num_index_replicas' in your Sync Gateway database config.", options.NumReplicas), nil } - base.WarnfCtx(logCtx, "Error creating index %s: %v - will retry.", indexName, err) + base.WarnfCtx(ctx, "Error creating index %s: %v - will retry.", indexName, err) } return err != nil, err, nil } @@ -345,7 +343,7 @@ func (i *SGIndex) createIfNeeded(bucket base.N1QLStore, options InitializeIndexO return false, pkgerrors.Wrapf(err, "Error installing Couchbase index: %v", indexName) } - base.InfofCtx(logCtx, base.KeyQuery, "Index %s created successfully", indexName) + base.InfofCtx(ctx, base.KeyQuery, "Index %s created successfully", indexName) return isDeferred, nil } @@ -383,7 +381,7 @@ func InitializeIndexes(ctx context.Context, n1QLStore base.N1QLStore, options In fullIndexName := sgIndex.fullIndexName(options.UseXattrs) - isDeferred, err := sgIndex.createIfNeeded(n1QLStore, options) + isDeferred, err := sgIndex.createIfNeeded(ctx, n1QLStore, options) if err != nil { return base.RedactErrorf("Unable to install index %s: %v", base.MD(sgIndex.simpleName), err) } @@ -395,7 +393,7 @@ func InitializeIndexes(ctx context.Context, n1QLStore base.N1QLStore, options In // Issue BUILD INDEX for any deferred indexes. if len(deferredIndexes) > 0 { - buildErr := base.BuildDeferredIndexes(n1QLStore, deferredIndexes) + buildErr := base.BuildDeferredIndexes(ctx, n1QLStore, deferredIndexes) if buildErr != nil { base.InfofCtx(ctx, base.KeyQuery, "Error building deferred indexes. Error: %v", buildErr) return buildErr @@ -419,7 +417,7 @@ func waitForIndexes(ctx context.Context, bucket base.N1QLStore, options Initiali indexes = append(indexes, fullIndexName) } - err := bucket.WaitForIndexesOnline(indexes, options.FailFast) + err := bucket.WaitForIndexesOnline(ctx, indexes, options.FailFast) if err != nil { return err } @@ -437,7 +435,7 @@ func isIndexerError(err error) bool { // Iterates over the index set, removing obsolete indexes: // - indexes based on the inverse value of xattrs being used by the database // - indexes associated with previous versions of the index, for either xattrs=true or xattrs=false -func removeObsoleteIndexes(bucket base.N1QLStore, previewOnly bool, useXattrs bool, useViews bool, indexMap map[SGIndexType]SGIndex) (removedIndexes []string, err error) { +func removeObsoleteIndexes(ctx context.Context, bucket base.N1QLStore, previewOnly bool, useXattrs bool, useViews bool, indexMap map[SGIndexType]SGIndex) (removedIndexes []string, err error) { removedIndexes = make([]string, 0) // Build set of candidates for cleanup @@ -458,9 +456,9 @@ func removeObsoleteIndexes(bucket base.N1QLStore, previewOnly bool, useXattrs bo // Attempt removal of candidates, adding to set of removedIndexes when found for _, indexName := range removalCandidates { - removed, err := removeObsoleteIndex(bucket, indexName, previewOnly) + removed, err := removeObsoleteIndex(ctx, bucket, indexName, previewOnly) if err != nil { - base.WarnfCtx(context.TODO(), "Unexpected error when removing index %q: %s", indexName, err) + base.WarnfCtx(ctx, "Unexpected error when removing index %q: %s", indexName, err) } if removed { removedIndexes = append(removedIndexes, indexName) @@ -471,16 +469,16 @@ func removeObsoleteIndexes(bucket base.N1QLStore, previewOnly bool, useXattrs bo } // Removes an obsolete index from the database. In preview mode, checks for existence of the index only. -func removeObsoleteIndex(bucket base.N1QLStore, indexName string, previewOnly bool) (removed bool, err error) { +func removeObsoleteIndex(ctx context.Context, bucket base.N1QLStore, indexName string, previewOnly bool) (removed bool, err error) { if previewOnly { // Check for index existence - exists, _, getMetaErr := bucket.GetIndexMeta(indexName) + exists, _, getMetaErr := bucket.GetIndexMeta(ctx, indexName) if getMetaErr != nil { return false, getMetaErr } return exists, nil } else { - err = bucket.DropIndex(indexName) + err = bucket.DropIndex(ctx, indexName) // If no error, add to set of removed indexes and return if err == nil { return true, nil diff --git a/db/indexes_test.go b/db/indexes_test.go index 1f3103ad6f..138f1d9c8c 100644 --- a/db/indexes_test.go +++ b/db/indexes_test.go @@ -153,19 +153,19 @@ func TestPostUpgradeIndexesSimple(t *testing.T) { require.NoError(t, err) // Running w/ opposite xattrs flag should preview removal of the indexes associated with this db context - removedIndexes, removeErr := removeObsoleteIndexes(n1qlStore, true, !db.UseXattrs(), db.UseViews(), sgIndexes) + removedIndexes, removeErr := removeObsoleteIndexes(ctx, n1qlStore, true, !db.UseXattrs(), db.UseViews(), sgIndexes) sort.Strings(removedIndexes) require.EqualValues(t, expectedRemovedIndexes, removedIndexes) require.NoError(t, removeErr, "Unexpected error running removeObsoleteIndexes in preview mode") // Running again w/ preview=false to perform cleanup - removedIndexes, removeErr = removeObsoleteIndexes(n1qlStore, false, !db.UseXattrs(), db.UseViews(), sgIndexes) + removedIndexes, removeErr = removeObsoleteIndexes(ctx, n1qlStore, false, !db.UseXattrs(), db.UseViews(), sgIndexes) sort.Strings(removedIndexes) require.Equal(t, expectedRemovedIndexes, removedIndexes) require.NoError(t, removeErr, "Unexpected error running removeObsoleteIndexes in non-preview mode") // One more time to make sure they are actually gone - removedIndexes, removeErr = removeObsoleteIndexes(n1qlStore, false, !db.UseXattrs(), db.UseViews(), sgIndexes) + removedIndexes, removeErr = removeObsoleteIndexes(ctx, n1qlStore, false, !db.UseXattrs(), db.UseViews(), sgIndexes) require.Len(t, removedIndexes, 0) require.NoError(t, removeErr, "Unexpected error running removeObsoleteIndexes in post-cleanup no-op") @@ -199,7 +199,7 @@ func TestPostUpgradeIndexesVersionChange(t *testing.T) { copiedIndexes := copySGIndexes(sgIndexes) // Validate that removeObsoleteIndexes is a no-op for the default case - removedIndexes, removeErr := removeObsoleteIndexes(n1qlStore, true, db.UseXattrs(), db.UseViews(), copiedIndexes) + removedIndexes, removeErr := removeObsoleteIndexes(ctx, n1qlStore, true, db.UseXattrs(), db.UseViews(), copiedIndexes) log.Printf("removedIndexes: %+v", removedIndexes) assert.Equal(t, 0, len(removedIndexes)) assert.NoError(t, removeErr, "Unexpected error running removeObsoleteIndexes in no-op case") @@ -216,7 +216,7 @@ func TestPostUpgradeIndexesVersionChange(t *testing.T) { copiedIndexes[IndexAccess] = accessIndex // Validate that removeObsoleteIndexes now triggers removal of one index - removedIndexes, removeErr = removeObsoleteIndexes(n1qlStore, true, db.UseXattrs(), db.UseViews(), copiedIndexes) + removedIndexes, removeErr = removeObsoleteIndexes(ctx, n1qlStore, true, db.UseXattrs(), db.UseViews(), copiedIndexes) log.Printf("removedIndexes: %+v", removedIndexes) assert.Equal(t, 1, len(removedIndexes)) assert.NoError(t, removeErr, "Unexpected error running removeObsoleteIndexes with hacked sgIndexes") @@ -352,11 +352,11 @@ func TestRemoveIndexesUseViewsTrueAndFalse(t *testing.T) { } } - removedIndexes, removeErr := removeObsoleteIndexes(n1QLStore, false, db.UseXattrs(), true, copiedIndexes) + removedIndexes, removeErr := removeObsoleteIndexes(ctx, n1QLStore, false, db.UseXattrs(), true, copiedIndexes) require.NoError(t, removeErr) require.Len(t, removedIndexes, expectedIndexes) - removedIndexes, removeErr = removeObsoleteIndexes(n1QLStore, false, db.UseXattrs(), false, copiedIndexes) + removedIndexes, removeErr = removeObsoleteIndexes(ctx, n1QLStore, false, db.UseXattrs(), false, copiedIndexes) require.NoError(t, removeErr) require.Len(t, removedIndexes, 0) @@ -407,7 +407,7 @@ func TestRemoveObsoleteIndexOnError(t *testing.T) { n1qlStore, ok := base.AsN1QLStore(dataStore) require.True(t, ok) - removedIndex, removeErr := removeObsoleteIndexes(n1qlStore, false, db.UseXattrs(), db.UseViews(), testIndexes) + removedIndex, removeErr := removeObsoleteIndexes(ctx, n1qlStore, false, db.UseXattrs(), db.UseViews(), testIndexes) assert.NoError(t, removeErr) if base.TestUseXattrs() { @@ -440,7 +440,7 @@ func dropAndInitializeIndexes(ctx context.Context, n1qlStore base.N1QLStore, opt } // Recreate the primary index required by the test bucket pooling framework - err := n1qlStore.CreatePrimaryIndex(base.PrimaryIndexName, nil) + err := n1qlStore.CreatePrimaryIndex(ctx, base.PrimaryIndexName, nil) if err != nil { return err } diff --git a/db/indextest/indextest_test.go b/db/indextest/indextest_test.go index 08c2ffc263..11b0ad52fb 100644 --- a/db/indextest/indextest_test.go +++ b/db/indextest/indextest_test.go @@ -454,7 +454,7 @@ var clearIndexes resetN1QLStoreFn = func(n1QLStores []base.N1QLStore, isServerle var err error for _, n1QLStore := range n1QLStores { for _, index := range indexes { - newErr := n1QLStore.DropIndex(index) + newErr := n1QLStore.DropIndex(context.TODO(), index) if newErr != nil && strings.Contains(newErr.Error(), "Index not exist") { err = errors.Wrap(err, newErr.Error()) } diff --git a/db/util_testing.go b/db/util_testing.go index 8d943bf4b0..9737e618c3 100644 --- a/db/util_testing.go +++ b/db/util_testing.go @@ -384,7 +384,7 @@ var viewsAndGSIBucketInit base.TBPBucketInitFunc = func(ctx context.Context, b b return err } - err = n1qlStore.CreatePrimaryIndex(base.PrimaryIndexName, nil) + err = n1qlStore.CreatePrimaryIndex(ctx, base.PrimaryIndexName, nil) if err != nil { return err } diff --git a/rest/api_collections_test.go b/rest/api_collections_test.go index 1855f9f652..c6b7089922 100644 --- a/rest/api_collections_test.go +++ b/rest/api_collections_test.go @@ -516,8 +516,9 @@ func TestCollectionsBasicIndexQuery(t *testing.T) { require.True(t, ok) idxName := t.Name() + "_primary" - require.NoError(t, n1qlStore.CreatePrimaryIndex(idxName, nil)) - require.NoError(t, n1qlStore.WaitForIndexesOnline([]string{idxName}, false)) + ctx := base.TestCtx(t) + require.NoError(t, n1qlStore.CreatePrimaryIndex(ctx, idxName, nil)) + require.NoError(t, n1qlStore.WaitForIndexesOnline(ctx, []string{idxName}, false)) res, err := n1qlStore.Query("SELECT keyspace_id, bucket_id, scope_id from system:indexes WHERE name = $idxName", map[string]interface{}{"idxName": idxName}, base.RequestPlus, true) diff --git a/rest/utilities_testing.go b/rest/utilities_testing.go index bff3217d57..7c968ffb60 100644 --- a/rest/utilities_testing.go +++ b/rest/utilities_testing.go @@ -2506,6 +2506,6 @@ func dropAllNonPrimaryIndexes(t *testing.T, dataStore base.DataStore) { ctx := base.TestCtx(t) dropErr := base.DropAllIndexes(ctx, n1qlStore) require.NoError(t, dropErr) - err := n1qlStore.CreatePrimaryIndex(base.PrimaryIndexName, nil) + err := n1qlStore.CreatePrimaryIndex(ctx, base.PrimaryIndexName, nil) require.NoError(t, err, "Unable to recreate primary index") }