From 7869a2381c69728636c4f0248470210d3e593716 Mon Sep 17 00:00:00 2001 From: Neil Macneale IV Date: Tue, 21 Oct 2025 10:18:24 -0700 Subject: [PATCH] Only init branch activity services for sql-server --- go/cmd/dolt/commands/sqlserver/server.go | 8 ++++++ .../doltcore/doltdb/branch_activity.go | 27 +++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/go/cmd/dolt/commands/sqlserver/server.go b/go/cmd/dolt/commands/sqlserver/server.go index 9a39336ac1..3792905e7b 100644 --- a/go/cmd/dolt/commands/sqlserver/server.go +++ b/go/cmd/dolt/commands/sqlserver/server.go @@ -763,6 +763,14 @@ func ConfigureServices( } controller.Register(RunClusterRemoteSrv) + branchActivityInit := &svcs.AnonService{ + InitF: func(ctx context.Context) error { + doltdb.BranchActivityInit(ctx) + return nil + }, + } + controller.Register(branchActivityInit) + // We still have some startup to do from this point, and we do not run // the SQL server until we are fully booted. We also want to stop the // SQL server as the first thing we stop. However, if startup fails diff --git a/go/libraries/doltcore/doltdb/branch_activity.go b/go/libraries/doltcore/doltdb/branch_activity.go index 3f8d666f0c..9ba93467ea 100644 --- a/go/libraries/doltcore/doltdb/branch_activity.go +++ b/go/libraries/doltcore/doltdb/branch_activity.go @@ -51,21 +51,23 @@ var ( branchReadTimes map[string]time.Time branchWriteTimes map[string]time.Time systemStartTime time.Time - activityChan chan branchActivityEvent + activityChan *chan branchActivityEvent ) -func init() { +func BranchActivityInit(ctx context.Context) { systemStartTime = time.Now() branchReadTimes = make(map[string]time.Time) branchWriteTimes = make(map[string]time.Time) - activityChan = make(chan branchActivityEvent, 64) // lifetime in buffer will be very short. + ac := make(chan branchActivityEvent, 64) // lifetime in buffer will be very short. + activityChan = &ac // Start background goroutine to process events go func() { - ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() for { select { - case event := <-activityChan: + case event := <-(*activityChan): branchActivityMutex.Lock() if event.eventType == READ { if existing, exists := branchReadTimes[event.branch]; !exists || event.timestamp.After(existing) { @@ -86,12 +88,15 @@ func init() { // BranchActivityReadEvent records when a branch is read/accessed func BranchActivityReadEvent(ctx context.Context, branch string) { + if activityChan == nil { + return + } if ctx.Value(StatsSessionContextKey) != nil { return } select { - case activityChan <- branchActivityEvent{ + case (*activityChan) <- branchActivityEvent{ branch: branch, timestamp: time.Now(), eventType: READ, @@ -103,12 +108,15 @@ func BranchActivityReadEvent(ctx context.Context, branch string) { // BranchActivityWriteEvent records when a branch is written/updated func BranchActivityWriteEvent(ctx context.Context, branch string) { + if activityChan == nil { + return + } if ctx.Value(StatsSessionContextKey) != nil { return } select { - case activityChan <- branchActivityEvent{ + case (*activityChan) <- branchActivityEvent{ branch: branch, timestamp: time.Now(), eventType: WRITE, @@ -120,10 +128,13 @@ func BranchActivityWriteEvent(ctx context.Context, branch string) { // GetBranchActivity returns activity data for all branches (tracked and untracked) func GetBranchActivity(ctx context.Context, ddb *DoltDB) ([]BranchActivityData, error) { + if activityChan == nil { + return nil, nil + } + branchActivityMutex.RLock() defer branchActivityMutex.RUnlock() - // Get all branches from the database branchRefs, err := ddb.GetBranches(ctx) if err != nil { return nil, err