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: try to replace the down replicas instead of removing directly #1222

Merged
merged 6 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 9 additions & 8 deletions pkg/faketikv/cases/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func (a *idAllocator) nextID() uint64 {

// ConfMap is a mapping of the cases to the their corresponding initialize functions.
var ConfMap = map[string]func() *Conf{
"balance-leader": newBalanceLeader,
"add-nodes": newAddNodes,
"add-nodes-dynamic": newAddNodesDynamic,
"delete-nodes": newDeleteNodes,
"region-split": newRegionSplit,
"region-merge": newRegionMerge,
"hot-read": newHotRead,
"hot-write": newHotWrite,
"balance-leader": newBalanceLeader,
"add-nodes": newAddNodes,
"add-nodes-dynamic": newAddNodesDynamic,
"delete-nodes": newDeleteNodes,
"region-split": newRegionSplit,
"region-merge": newRegionMerge,
"hot-read": newHotRead,
"hot-write": newHotWrite,
"makeup-down-replicas": newMakeupDownReplicas,
}

// NewConf creates a config to initialize simulator cluster.
Expand Down
2 changes: 1 addition & 1 deletion pkg/faketikv/cases/delete_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newDeleteNodes() *Conf {
numNodes := 8
e := &DeleteNodesInner{}
e.Step = func(tick int64) uint64 {
if tick%100 == 0 && numNodes > 7 {
if numNodes > 7 && tick%100 == 0 {
idx := rand.Intn(numNodes)
numNodes--
nodeID := ids[idx]
Expand Down
93 changes: 93 additions & 0 deletions pkg/faketikv/cases/makeup_down_replica.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// // http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package cases

import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/pkg/faketikv/simutil"
"github.com/pingcap/pd/server/core"
)

func newMakeupDownReplicas() *Conf {
var conf Conf
var id idAllocator

for i := 1; i <= 4; i++ {
conf.Stores = append(conf.Stores, &Store{
ID: id.nextID(),
Status: metapb.StoreState_Up,
Capacity: 1 * TB,
Available: 900 * GB,
Version: "2.1.0",
})
}

for i := 0; i < 400; i++ {
peers := []*metapb.Peer{
{Id: id.nextID(), StoreId: uint64(i)%4 + 1},
{Id: id.nextID(), StoreId: uint64(i+1)%4 + 1},
{Id: id.nextID(), StoreId: uint64(i+2)%4 + 1},
}
conf.Regions = append(conf.Regions, Region{
ID: id.nextID(),
Peers: peers,
Leader: peers[0],
Size: 96 * MB,
Keys: 960000,
})
}
conf.MaxID = id.maxID

numNodes := 4
down := false
e := &DeleteNodesInner{}
e.Step = func(tick int64) uint64 {
if numNodes > 3 && tick%100 == 0 {
numNodes--
return uint64(1)
}
if tick == 300 {
down = true
}
return 0
}
conf.Events = []EventInner{e}

conf.Checker = func(regions *core.RegionsInfo) bool {
sum := 0
regionCounts := make([]int, 0, 3)
for i := 1; i <= 4; i++ {
regionCount := regions.GetStoreRegionCount(uint64(i))
if i != 1 {
regionCounts = append(regionCounts, regionCount)
}
sum += regionCount
}
simutil.Logger.Infof("region counts: %v", regionCounts)

if down && sum < 1200 {
// only need to print once
down = false
simutil.Logger.Error("making up replicas don't start immediately")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we abstract a statistic to evaluate different strategies instead of just print a log message? For example, we may count the number of regions with insufficient replicas in the scheduling process.

return false
}

for _, regionCount := range regionCounts {
if regionCount != 400 {
return false
}
}
return true
}
return &conf
}
4 changes: 1 addition & 3 deletions server/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,10 @@ func (s *testCoordinatorSuite) TestReplica(c *C) {
core.WithDownPeers(append(region.GetDownPeers(), downPeer)),
)
dispatchHeartbeat(c, co, region, stream)
waitRemovePeer(c, stream, region, 3)
region = region.Clone(core.WithDownPeers(nil))
dispatchHeartbeat(c, co, region, stream)
waitAddLearner(c, stream, region, 4)
dispatchHeartbeat(c, co, region, stream)
waitPromoteLearner(c, stream, region, 4)
region = region.Clone(core.WithDownPeers(nil))
dispatchHeartbeat(c, co, region, stream)
waitNoResponse(c, stream)

Expand Down
59 changes: 35 additions & 24 deletions server/schedule/replica_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package schedule

import (
"fmt"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
Expand Down Expand Up @@ -169,7 +171,8 @@ func (r *ReplicaChecker) checkDownPeer(region *core.RegionInfo) *Operator {
if stats.GetDownSeconds() < uint64(r.cluster.GetMaxStoreDownTime().Seconds()) {
continue
}
return CreateRemovePeerOperator("removeDownReplica", r.cluster, OpReplica, region, peer.GetStoreId())

return r.fixPeer(region, peer, "Down")
}
return nil
}
Expand All @@ -194,29 +197,7 @@ func (r *ReplicaChecker) checkOfflinePeer(region *core.RegionInfo) *Operator {
continue
}

// Check the number of replicas first.
if len(region.GetPeers()) > r.cluster.GetMaxReplicas() {
return CreateRemovePeerOperator("removeExtraOfflineReplica", r.cluster, OpReplica, region, peer.GetStoreId())
}

// Consider we have 3 peers (A, B, C), we set the store that contains C to
// offline while C is pending. If we generate an operator that adds a replica
// D then removes C, D will not be successfully added util C is normal again.
// So it's better to remove C directly.
if region.GetPendingPeer(peer.GetId()) != nil {
return CreateRemovePeerOperator("removePendingOfflineReplica", r.cluster, OpReplica, region, peer.GetStoreId())
}

storeID, _ := r.SelectBestReplacementStore(region, peer, NewStorageThresholdFilter())
if storeID == 0 {
log.Debugf("[region %d] no best store to add replica", region.GetID())
return nil
}
newPeer, err := r.cluster.AllocPeer(storeID)
if err != nil {
return nil
}
return CreateMovePeerOperator("replaceOfflineReplica", r.cluster, region, OpReplica, peer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
return r.fixPeer(region, peer, "Offline")
}

return nil
Expand Down Expand Up @@ -250,3 +231,33 @@ func (r *ReplicaChecker) checkBestReplacement(region *core.RegionInfo) *Operator
checkerCounter.WithLabelValues("replica_checker", "new_operator").Inc()
return CreateMovePeerOperator("moveToBetterLocation", r.cluster, region, OpReplica, oldPeer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
}

func (r *ReplicaChecker) fixPeer(region *core.RegionInfo, peer *metapb.Peer, status string) *Operator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does fixPeer mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means this peer needs to be removed in order to be made up in another store or just simply be replaced. The name before addressing the comment is handleReplica. There could be a better naming about this.

removeExtra := fmt.Sprintf("removeExtra%sReplica", status)
// Check the number of replicas first.
if len(region.GetPeers()) > r.cluster.GetMaxReplicas() {
return CreateRemovePeerOperator(removeExtra, r.cluster, OpReplica, region, peer.GetStoreId())
}

removePending := fmt.Sprintf("removePending%sReplica", status)
// Consider we have 3 peers (A, B, C), we set the store that contains C to
// offline/down while C is pending. If we generate an operator that adds a replica
// D then removes C, D will not be successfully added util C is normal again.
// So it's better to remove C directly.
if region.GetPendingPeer(peer.GetId()) != nil {
return CreateRemovePeerOperator(removePending, r.cluster, OpReplica, region, peer.GetStoreId())
}

storeID, _ := r.SelectBestReplacementStore(region, peer, NewStorageThresholdFilter())
if storeID == 0 {
log.Debugf("[region %d] no best store to add replica", region.GetID())
return nil
}
newPeer, err := r.cluster.AllocPeer(storeID)
if err != nil {
return nil
}

replace := fmt.Sprintf("replace%sReplica", status)
return CreateMovePeerOperator(replace, r.cluster, region, OpReplica, peer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
}
10 changes: 4 additions & 6 deletions server/schedulers/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,9 @@ func (s *testReplicaCheckerSuite) TestBasic(c *C) {
Peer: region.GetStorePeer(2),
DownSeconds: 24 * 60 * 60,
}
region = region.Clone(
core.WithRemoveStorePeer(1),
core.WithDownPeers(append(region.GetDownPeers(), downPeer)),
)
testutil.CheckRemovePeer(c, rc.Check(region), 2)

region = region.Clone(core.WithDownPeers(append(region.GetDownPeers(), downPeer)))
testutil.CheckTransferPeer(c, rc.Check(region), schedule.OpReplica, 2, 1)
region = region.Clone(core.WithDownPeers(nil))
c.Assert(rc.Check(region), IsNil)

Expand Down Expand Up @@ -796,7 +794,7 @@ func (s *testReplicaCheckerSuite) TestOpts(c *C) {
}))
tc.SetStoreOffline(2)
// RemoveDownReplica has higher priority than replaceOfflineReplica.
testutil.CheckRemovePeer(c, rc.Check(region), 1)
testutil.CheckTransferPeer(c, rc.Check(region), schedule.OpReplica, 1, 4)
opt.DisableRemoveDownReplica = true
testutil.CheckTransferPeer(c, rc.Check(region), schedule.OpReplica, 2, 4)
opt.DisableReplaceOfflineReplica = true
Expand Down