diff --git a/base/stats.go b/base/stats.go index dfe5d74108..3995572a78 100644 --- a/base/stats.go +++ b/base/stats.go @@ -359,6 +359,7 @@ type DbStats struct { SecurityStats *SecurityStats `json:"security,omitempty"` SharedBucketImportStats *SharedBucketImportStats `json:"shared_bucket_import,omitempty"` CollectionStats map[string]*CollectionStats `json:"per_collection,omitempty"` + dbReplicatorStatsMutex sync.Mutex } type CacheStats struct { @@ -1096,7 +1097,8 @@ func (s *SgwStats) NewDBStats(name string, deltaSyncEnabled bool, importEnabled s.dbStatsMapMutex.Lock() defer s.dbStatsMapMutex.Unlock() dbStats := &DbStats{ - dbName: name, + dbName: name, + DbReplicatorStats: make(map[string]*DbReplicatorStats), } // These have a pretty good chance of being used so we'll initialise these for every database stat struct created @@ -1823,6 +1825,7 @@ func (d *DbStats) initSecurityStats() error { } func (d *DbStats) unregisterReplicationStats(replicationID string) { + if d.DbReplicatorStats[replicationID] == nil { return } @@ -1957,132 +1960,131 @@ func (d *DbStats) InitCollectionStats(scopeAndCollectionNames ...string) error { } func (d *DbStats) DBReplicatorStats(replicationID string) (*DbReplicatorStats, error) { + d.dbReplicatorStatsMutex.Lock() + defer d.dbReplicatorStatsMutex.Unlock() + + if _, ok := d.DbReplicatorStats[replicationID]; ok { + return d.DbReplicatorStats[replicationID], nil + } var err error resUtil := &DbReplicatorStats{} - if d.DbReplicatorStats == nil { - d.DbReplicatorStats = map[string]*DbReplicatorStats{} - } - - if _, ok := d.DbReplicatorStats[replicationID]; !ok { - labelKeys := []string{DatabaseLabelKey, ReplicationLabelKey} - labelVals := []string{d.dbName, replicationID} - - resUtil.NumAttachmentBytesPushed, err = NewIntStat(SubsystemReplication, "sgr_num_attachment_bytes_pushed", StatUnitBytes, SGRNumAttachmentBytesPushedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumAttachmentPushed, err = NewIntStat(SubsystemReplication, "sgr_num_attachments_pushed", StatUnitNoUnits, SGRNumAttachmentsPushedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumDocPushed, err = NewIntStat(SubsystemReplication, "sgr_num_docs_pushed", StatUnitNoUnits, SGRNumDocsPushedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumDocsFailedToPush, err = NewIntStat(SubsystemReplication, "sgr_num_docs_failed_to_push", StatUnitNoUnits, SGRNumDocsFailedToPushDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.PushConflictCount, err = NewIntStat(SubsystemReplication, "sgr_push_conflict_count", StatUnitNoUnits, SGRPushConflictCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.PushRejectedCount, err = NewIntStat(SubsystemReplication, "sgr_push_rejected_count", StatUnitNoUnits, SGRPushRejectedCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.PushDeltaSentCount, err = NewIntStat(SubsystemReplication, "sgr_deltas_sent", StatUnitNoUnits, SGRDeltasSentDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.DocsCheckedSent, err = NewIntStat(SubsystemReplication, "sgr_docs_checked_sent", StatUnitNoUnits, SGRDocsCheckedSentDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumConnectAttemptsPush, err = NewIntStat(SubsystemReplication, "sgr_num_connect_attempts_push", StatUnitNoUnits, SGRNumConnectAttemptsPushDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumReconnectsAbortedPush, err = NewIntStat(SubsystemReplication, "sgr_num_reconnects_aborted_push", StatUnitNoUnits, SGRNumReconnectsAbortedPushDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumAttachmentBytesPulled, err = NewIntStat(SubsystemReplication, "sgr_num_attachment_bytes_pulled", StatUnitBytes, SGRNumAttachmentBytesPulledDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumAttachmentsPulled, err = NewIntStat(SubsystemReplication, "sgr_num_attachments_pulled", StatUnitNoUnits, SGRNumAttachmentsPulledDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.PulledCount, err = NewIntStat(SubsystemReplication, "sgr_num_docs_pulled", StatUnitNoUnits, SGRNumDocsPulledDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.PurgedCount, err = NewIntStat(SubsystemReplication, "sgr_num_docs_purged", StatUnitNoUnits, SGRNumDocsPurgedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.FailedToPullCount, err = NewIntStat(SubsystemReplication, "sgr_num_docs_failed_to_pull", StatUnitNoUnits, SGRNumDocsFailedToPullDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.DeltaReceivedCount, err = NewIntStat(SubsystemReplication, "sgr_deltas_recv", StatUnitNoUnits, SGRDeltasRecvDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.DeltaRequestedCount, err = NewIntStat(SubsystemReplication, "sgr_deltas_requested", StatUnitNoUnits, SGRDeltasRequestedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.DocsCheckedReceived, err = NewIntStat(SubsystemReplication, "sgr_docs_checked_recv", StatUnitNoUnits, SGRDocsCheckedRecvDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ConflictResolvedLocalCount, err = NewIntStat(SubsystemReplication, "sgr_conflict_resolved_local_count", StatUnitNoUnits, SGRConflictResolvedLocalCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ConflictResolvedRemoteCount, err = NewIntStat(SubsystemReplication, "sgr_conflict_resolved_remote_count", StatUnitNoUnits, SGRConflictResolvedRemoteCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ConflictResolvedMergedCount, err = NewIntStat(SubsystemReplication, "sgr_conflict_resolved_merge_count", StatUnitNoUnits, SGRConflictResolvedMergeCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumConnectAttemptsPull, err = NewIntStat(SubsystemReplication, "sgr_num_connect_attempts_pull", StatUnitNoUnits, SGRNumConnectAttemptsPullDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumReconnectsAbortedPull, err = NewIntStat(SubsystemReplication, "sgr_num_reconnects_aborted_pull", StatUnitNoUnits, SGRNumReconnectsAbortedPullDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.NumHandlersPanicked, err = NewIntStat(SubsystemReplication, "sgr_num_handlers_panicked", StatUnitNoUnits, SGRNumHandlersPanickedDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ExpectedSequenceLen, err = NewIntStat(SubsystemReplication, "expected_sequence_len", StatUnitNoUnits, SGRExpectedSequenceLengthDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ExpectedSequenceLenPostCleanup, err = NewIntStat(SubsystemReplication, "expected_sequence_len_post_cleanup", StatUnitNoUnits, SGRExpectedSequenceLengthPostCleanupDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ProcessedSequenceLen, err = NewIntStat(SubsystemReplication, "processed_sequence_len", StatUnitNoUnits, SGRProcessedSequenceLength, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - resUtil.ProcessedSequenceLenPostCleanup, err = NewIntStat(SubsystemReplication, "processed_sequence_len_post_cleanup", StatUnitNoUnits, SGRProcessedSequenceLengthPostCleanupDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) - if err != nil { - return nil, err - } - - d.DbReplicatorStats[replicationID] = resUtil + labelKeys := []string{DatabaseLabelKey, ReplicationLabelKey} + labelVals := []string{d.dbName, replicationID} + resUtil.NumAttachmentBytesPushed, err = NewIntStat(SubsystemReplication, "sgr_num_attachment_bytes_pushed", StatUnitBytes, SGRNumAttachmentBytesPushedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumAttachmentPushed, err = NewIntStat(SubsystemReplication, "sgr_num_attachments_pushed", StatUnitNoUnits, SGRNumAttachmentsPushedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumDocPushed, err = NewIntStat(SubsystemReplication, "sgr_num_docs_pushed", StatUnitNoUnits, SGRNumDocsPushedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumDocsFailedToPush, err = NewIntStat(SubsystemReplication, "sgr_num_docs_failed_to_push", StatUnitNoUnits, SGRNumDocsFailedToPushDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.PushConflictCount, err = NewIntStat(SubsystemReplication, "sgr_push_conflict_count", StatUnitNoUnits, SGRPushConflictCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.PushRejectedCount, err = NewIntStat(SubsystemReplication, "sgr_push_rejected_count", StatUnitNoUnits, SGRPushRejectedCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.PushDeltaSentCount, err = NewIntStat(SubsystemReplication, "sgr_deltas_sent", StatUnitNoUnits, SGRDeltasSentDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.DocsCheckedSent, err = NewIntStat(SubsystemReplication, "sgr_docs_checked_sent", StatUnitNoUnits, SGRDocsCheckedSentDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumConnectAttemptsPush, err = NewIntStat(SubsystemReplication, "sgr_num_connect_attempts_push", StatUnitNoUnits, SGRNumConnectAttemptsPushDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumReconnectsAbortedPush, err = NewIntStat(SubsystemReplication, "sgr_num_reconnects_aborted_push", StatUnitNoUnits, SGRNumReconnectsAbortedPushDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumAttachmentBytesPulled, err = NewIntStat(SubsystemReplication, "sgr_num_attachment_bytes_pulled", StatUnitBytes, SGRNumAttachmentBytesPulledDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumAttachmentsPulled, err = NewIntStat(SubsystemReplication, "sgr_num_attachments_pulled", StatUnitNoUnits, SGRNumAttachmentsPulledDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.PulledCount, err = NewIntStat(SubsystemReplication, "sgr_num_docs_pulled", StatUnitNoUnits, SGRNumDocsPulledDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.PurgedCount, err = NewIntStat(SubsystemReplication, "sgr_num_docs_purged", StatUnitNoUnits, SGRNumDocsPurgedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err } + resUtil.FailedToPullCount, err = NewIntStat(SubsystemReplication, "sgr_num_docs_failed_to_pull", StatUnitNoUnits, SGRNumDocsFailedToPullDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.DeltaReceivedCount, err = NewIntStat(SubsystemReplication, "sgr_deltas_recv", StatUnitNoUnits, SGRDeltasRecvDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.DeltaRequestedCount, err = NewIntStat(SubsystemReplication, "sgr_deltas_requested", StatUnitNoUnits, SGRDeltasRequestedDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.DocsCheckedReceived, err = NewIntStat(SubsystemReplication, "sgr_docs_checked_recv", StatUnitNoUnits, SGRDocsCheckedRecvDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ConflictResolvedLocalCount, err = NewIntStat(SubsystemReplication, "sgr_conflict_resolved_local_count", StatUnitNoUnits, SGRConflictResolvedLocalCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ConflictResolvedRemoteCount, err = NewIntStat(SubsystemReplication, "sgr_conflict_resolved_remote_count", StatUnitNoUnits, SGRConflictResolvedRemoteCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ConflictResolvedMergedCount, err = NewIntStat(SubsystemReplication, "sgr_conflict_resolved_merge_count", StatUnitNoUnits, SGRConflictResolvedMergeCountDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumConnectAttemptsPull, err = NewIntStat(SubsystemReplication, "sgr_num_connect_attempts_pull", StatUnitNoUnits, SGRNumConnectAttemptsPullDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumReconnectsAbortedPull, err = NewIntStat(SubsystemReplication, "sgr_num_reconnects_aborted_pull", StatUnitNoUnits, SGRNumReconnectsAbortedPullDesc, StatAddedVersion3dot0dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.NumHandlersPanicked, err = NewIntStat(SubsystemReplication, "sgr_num_handlers_panicked", StatUnitNoUnits, SGRNumHandlersPanickedDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ExpectedSequenceLen, err = NewIntStat(SubsystemReplication, "expected_sequence_len", StatUnitNoUnits, SGRExpectedSequenceLengthDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ExpectedSequenceLenPostCleanup, err = NewIntStat(SubsystemReplication, "expected_sequence_len_post_cleanup", StatUnitNoUnits, SGRExpectedSequenceLengthPostCleanupDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ProcessedSequenceLen, err = NewIntStat(SubsystemReplication, "processed_sequence_len", StatUnitNoUnits, SGRProcessedSequenceLength, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + resUtil.ProcessedSequenceLenPostCleanup, err = NewIntStat(SubsystemReplication, "processed_sequence_len_post_cleanup", StatUnitNoUnits, SGRProcessedSequenceLengthPostCleanupDesc, StatAddedVersion3dot1dot0, StatDeprecatedVersionNotDeprecated, StatStabilityCommitted, labelKeys, labelVals, prometheus.CounterValue, 0) + if err != nil { + return nil, err + } + + d.DbReplicatorStats[replicationID] = resUtil return d.DbReplicatorStats[replicationID], nil } diff --git a/db/import_pindex.go b/db/import_pindex.go index 3ba2cee6df..5e50164571 100644 --- a/db/import_pindex.go +++ b/db/import_pindex.go @@ -14,15 +14,20 @@ import ( "context" "errors" "fmt" + "sync" "github.com/couchbase/cbgt" "github.com/couchbase/sync_gateway/base" ) +// registerImportPindexImplMutex locks access to cbgt.RegisterImportPindexImpl. +var registerImportPindexImplMutex = sync.Mutex{} + // RegisterImportPindexImpl registers the PIndex type definition. This is invoked by cbgt when a Pindex (collection of // vbuckets) is assigned to this node. - func RegisterImportPindexImpl(ctx context.Context, configGroup string) { + registerImportPindexImplMutex.Lock() + defer registerImportPindexImplMutex.Unlock() // Since RegisterPIndexImplType is a global var without synchronization, index type needs to be // config group scoped. The associated importListener within the context is retrieved based on the diff --git a/rest/adminapitest/admin_api_test.go b/rest/adminapitest/admin_api_test.go index e089d89616..7d26a5660a 100644 --- a/rest/adminapitest/admin_api_test.go +++ b/rest/adminapitest/admin_api_test.go @@ -3108,7 +3108,6 @@ func TestNotExistentDBRequest(t *testing.T) { func TestConfigsIncludeDefaults(t *testing.T) { base.RequireNumTestBuckets(t, 2) - base.SetUpTestLogging(t, base.LevelInfo, base.KeyHTTP) ctx := base.TestCtx(t) @@ -3120,6 +3119,7 @@ func TestConfigsIncludeDefaults(t *testing.T) { config := rest.BootstrapStartupConfigForTest(t) config.Logging.Console.LogKeys = []string{base.KeyDCP.String()} config.Logging.Console.LogLevel.Set(base.LevelDebug) + config.Logging.Console.Enabled = base.BoolPtr(true) // only necessary for tests since they avoid InitLogging, normally this is inferred by InitLogging sc, closeFn := rest.StartServerWithConfig(t, &config) defer closeFn() diff --git a/rest/config.go b/rest/config.go index aaff62c9c4..bf03ebaf67 100644 --- a/rest/config.go +++ b/rest/config.go @@ -25,6 +25,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "syscall" "time" @@ -47,6 +48,9 @@ var ( // The value of defaultLogFilePath is populated by -defaultLogFilePath by command line flag from service scripts. defaultLogFilePath string + + // serverContextGlobalsInitialized is set the first time that a server context has been initialized + serverContextGlobalsInitialized = atomic.Bool{} ) const ( @@ -1344,26 +1348,31 @@ func (sc *StartupConfig) Validate(ctx context.Context, isEnterpriseEdition bool) // SetupServerContext creates a new ServerContext given its configuration and performs the context validation. func SetupServerContext(ctx context.Context, config *StartupConfig, persistentConfig bool) (*ServerContext, error) { - // Logging config will now have been loaded from command line - // or from a sync_gateway config file so we can validate the - // configuration and setup logging now - if err := config.SetupAndValidateLogging(ctx); err != nil { - // If we didn't set up logging correctly, we *probably* can't log via normal means... - // as a best-effort, last-ditch attempt, we'll log to stderr as well. - log.Printf("[ERR] Error setting up logging: %v", err) - return nil, fmt.Errorf("error setting up logging: %v", err) + // If SetupServerContext is called while any other go routines that might use logging are running, it will + // cause a data race, therefore only initialize logging and other globals on the first call. From a main + // program, there is only one ServerContext. + if serverContextGlobalsInitialized.CompareAndSwap(false, true) { + // Logging config will now have been loaded from command line + // or from a sync_gateway config file so we can validate the + // configuration and setup logging now + + if err := config.SetupAndValidateLogging(ctx); err != nil { + // If we didn't set up logging correctly, we *probably* can't log via normal means... + // as a best-effort, last-ditch attempt, we'll log to stderr as well. + log.Printf("[ERR] Error setting up logging: %v", err) + return nil, fmt.Errorf("error setting up logging: %v", err) + } + base.FlushLoggerBuffers() + + if err := setGlobalConfig(ctx, config); err != nil { + return nil, err + } } - base.FlushLoggerBuffers() - base.InfofCtx(ctx, base.KeyAll, "Logging: Console level: %v", base.ConsoleLogLevel()) base.InfofCtx(ctx, base.KeyAll, "Logging: Console keys: %v", base.ConsoleLogKey().EnabledLogKeys()) base.InfofCtx(ctx, base.KeyAll, "Logging: Redaction level: %s", config.Logging.RedactionLevel) - if err := setGlobalConfig(ctx, config); err != nil { - return nil, err - } - if err := config.Validate(ctx, base.IsEnterpriseEdition()); err != nil { return nil, err }