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

cluster: reset stores cache before loading cluster info #4942

Merged
merged 6 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func (c *RaftCluster) LoadClusterInfo() (*RaftCluster, error) {
return nil, nil
}

c.core.ResetStores()
start := time.Now()
if err := c.storage.LoadStores(c.core.PutStore); err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions server/core/basic_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ func (bc *BasicCluster) PutStore(store *StoreInfo) {
bc.Stores.SetStore(store)
}

// ResetStores resets the store cache.
func (bc *BasicCluster) ResetStores() {
bc.Lock()
defer bc.Unlock()
bc.Stores = NewStoresInfo()
}

// DeleteStore deletes a store.
func (bc *BasicCluster) DeleteStore(store *StoreInfo) {
bc.Lock()
Expand Down
57 changes: 57 additions & 0 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,3 +1343,60 @@ func (s *clusterTestSuite) TestMinResolvedTS(c *C) {
ts = rc.GetMinResolvedTS()
c.Assert(ts, Equals, store5TS)
}

// See https://github.com/tikv/pd/issues/4941
func (s *clusterTestSuite) TestTransferLeaderBack(c *C) {
tc, err := tests.NewTestCluster(s.ctx, 2)
defer tc.Destroy()
c.Assert(err, IsNil)
err = tc.RunInitialServers()
c.Assert(err, IsNil)
tc.WaitLeader()
leaderServer := tc.GetServer(tc.GetLeader())
svr := leaderServer.GetServer()
rc := cluster.NewRaftCluster(s.ctx, svr.ClusterID(), syncer.NewRegionSyncer(svr), svr.GetClient(), svr.GetHTTPClient())
rc.InitCluster(svr.GetAllocator(), svr.GetPersistOptions(), svr.GetStorage(), svr.GetBasicCluster())
storage := rc.GetStorage()
meta := &metapb.Cluster{Id: 123}
c.Assert(storage.SaveMeta(meta), IsNil)
n := 4
stores := make([]*metapb.Store, 0, n)
for i := 1; i <= n; i++ {
store := &metapb.Store{Id: uint64(i), State: metapb.StoreState_Up}
stores = append(stores, store)
}

for _, store := range stores {
c.Assert(storage.SaveStore(store), IsNil)
}
rc, err = rc.LoadClusterInfo()
c.Assert(err, IsNil)
c.Assert(rc, NotNil)
// offline a store
c.Assert(rc.RemoveStore(1, false), IsNil)
c.Assert(rc.GetStore(1).GetState(), Equals, metapb.StoreState_Offline)

// transfer PD leader to another PD
tc.ResignLeader()
tc.WaitLeader()
leaderServer = tc.GetServer(tc.GetLeader())
svr1 := leaderServer.GetServer()
rc1 := svr1.GetRaftCluster()
c.Assert(err, IsNil)
c.Assert(rc1, NotNil)
// tombstone a store, and remove its record
c.Assert(rc1.BuryStore(1, false), IsNil)
c.Assert(rc1.RemoveTombStoneRecords(), IsNil)

// transfer PD leader back to the previous PD
tc.ResignLeader()
tc.WaitLeader()
leaderServer = tc.GetServer(tc.GetLeader())
svr = leaderServer.GetServer()
rc = svr.GetRaftCluster()
c.Assert(rc, NotNil)

// check store count
c.Assert(rc.GetMetaCluster(), DeepEquals, meta)
c.Assert(rc.GetStoreCount(), Equals, 3)
}