Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: fix WaitGroup data race #1273

Merged
merged 2 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ func (c *RaftCluster) runSyncRegions() {

func (c *RaftCluster) runCoordinator() {
defer logutil.LogPanic()

defer c.wg.Done()
defer c.coordinator.wg.Wait()
c.coordinator.run()
c.wg.Done()
<-c.coordinator.ctx.Done()
log.Info("coordinator: Stopped coordinator")
}

func (c *RaftCluster) stop() {
Expand Down
2 changes: 0 additions & 2 deletions server/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ func (c *coordinator) run() {
select {
case <-ticker.C:
case <-c.ctx.Done():
log.Info("coordinator: Stopped coordinator")
return
}
}
Expand Down Expand Up @@ -258,7 +257,6 @@ func (c *coordinator) run() {

func (c *coordinator) stop() {
c.cancel()
c.wg.Wait()
}

// Hack to retrive info from scheduler.
Expand Down
12 changes: 12 additions & 0 deletions server/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (s *testCoordinatorSuite) TestDispatch(c *C) {

co := newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)
co.run()
defer co.wg.Wait()
defer co.stop()

// Transfer peer from store 4 to store 1.
Expand Down Expand Up @@ -298,12 +299,14 @@ func (s *testCoordinatorSuite) TestCheckRegion(c *C) {
tc.putRegion(r)
c.Assert(co.checkRegion(tc.GetRegion(1)), IsFalse)
co.stop()
co.wg.Wait()

// new cluster with learner disabled
cfg.DisableLearner = true
tc = newTestClusterInfo(opt)
co = newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)
co.run()
defer co.wg.Wait()
defer co.stop()

tc.addRegionStore(4, 4)
Expand Down Expand Up @@ -334,6 +337,7 @@ func (s *testCoordinatorSuite) TestReplica(c *C) {

co := newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)
co.run()
defer co.wg.Wait()
defer co.stop()

tc.addRegionStore(1, 1)
Expand Down Expand Up @@ -395,6 +399,7 @@ func (s *testCoordinatorSuite) TestPeerState(c *C) {

co := newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)
co.run()
defer co.wg.Wait()
defer co.stop()

// Transfer peer from store 4 to store 1.
Expand Down Expand Up @@ -493,6 +498,7 @@ func (s *testCoordinatorSuite) TestAddScheduler(c *C) {
defer hbStreams.Close()
co := newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)
co.run()
defer co.wg.Wait()
defer co.stop()

c.Assert(co.schedulers, HasLen, 4)
Expand Down Expand Up @@ -571,6 +577,7 @@ func (s *testCoordinatorSuite) TestPersistScheduler(c *C) {
c.Assert(co.schedulers, HasLen, 2)
c.Assert(co.cluster.opt.persist(co.cluster.kv), IsNil)
co.stop()
co.wg.Wait()
// make a new coordinator for testing
// whether the schedulers added or removed in dynamic way are recorded in opt
_, newOpt := newTestScheduleConfig()
Expand All @@ -587,6 +594,7 @@ func (s *testCoordinatorSuite) TestPersistScheduler(c *C) {
co.run()
c.Assert(co.schedulers, HasLen, 3)
co.stop()
co.wg.Wait()
// suppose restart PD again
_, newOpt = newTestScheduleConfig()
newOpt.reload(tc.kv)
Expand All @@ -610,13 +618,15 @@ func (s *testCoordinatorSuite) TestPersistScheduler(c *C) {
c.Assert(co.schedulers, HasLen, 4)
c.Assert(co.cluster.opt.persist(co.cluster.kv), IsNil)
co.stop()
co.wg.Wait()

_, newOpt = newTestScheduleConfig()
newOpt.reload(co.cluster.kv)
tc.clusterInfo.opt = newOpt
co = newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)

co.run()
defer co.wg.Wait()
defer co.stop()
c.Assert(co.schedulers, HasLen, 4)
c.Assert(co.removeScheduler("grant-leader-scheduler-2"), IsNil)
Expand Down Expand Up @@ -650,6 +660,7 @@ func (s *testCoordinatorSuite) TestRestart(c *C) {
dispatchHeartbeat(c, co, region, stream)
region = waitPromoteLearner(c, stream, region, 2)
co.stop()
co.wg.Wait()

// Recreate coodinator then add another replica on store 3.
co = newCoordinator(tc.clusterInfo, hbStreams, namespace.DefaultClassifier)
Expand All @@ -659,6 +670,7 @@ func (s *testCoordinatorSuite) TestRestart(c *C) {
dispatchHeartbeat(c, co, region, stream)
waitPromoteLearner(c, stream, region, 3)
co.stop()
co.wg.Wait()
}

func waitOperator(c *C, co *coordinator, regionID uint64) {
Expand Down