Skip to content

Commit

Permalink
lib/master: optimize for recovering with no worker (pingcap#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzix committed Apr 18, 2022
1 parent 84c0ac6 commit fa9e885
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/master/worker_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ func (m *WorkerManager) InitAfterRecover(ctx context.Context) error {
return err
}

if len(allPersistedWorkers) == 0 {
// Fast path when there is no worker.
m.mu.Lock()
m.state = workerManagerReady
m.mu.Unlock()
return nil
}

m.mu.Lock()
for workerID, status := range allPersistedWorkers {
entry := newWaitingWorkerEntry(workerID, status)
Expand Down
16 changes: 16 additions & 0 deletions lib/master/worker_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,19 @@ func TestRecoverAfterFailoverFast(t *testing.T) {
require.Contains(t, suite.manager.GetWorkers(), "worker-1")
suite.Close()
}

func TestRecoverWithNoWorker(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

suite := NewWorkerManageTestSuite(false)

// Since there is no worker info in the metastore,
// recovering should be very fast.
// Since we are using a mock clock, and we are NOT advancing it,
// InitAfterRecover returning at all would indicate a successful test.
err := suite.manager.InitAfterRecover(ctx)
require.NoError(t, err)

suite.Close()
}

0 comments on commit fa9e885

Please sign in to comment.