-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
- Loading branch information
Showing
16 changed files
with
485 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2020 TiKV Project Authors. | ||
// | ||
// 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 checker | ||
|
||
import ( | ||
"github.com/pingcap/log" | ||
"github.com/tikv/pd/server/core" | ||
"github.com/tikv/pd/server/schedule/filter" | ||
"github.com/tikv/pd/server/schedule/opt" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// ReplicaStrategy collects some utilities to manipulate region peers. It | ||
// exists to allow replica_checker and rule_checker to reuse common logics. | ||
type ReplicaStrategy struct { | ||
checkerName string // replica-checker / rule-checker | ||
cluster opt.Cluster | ||
locationLabels []string | ||
isolationLevel string | ||
region *core.RegionInfo | ||
extraFilters []filter.Filter | ||
} | ||
|
||
// SelectStoreToAdd returns the store to add a replica to a region. | ||
// `coLocationStores` are the stores used to compare location with target | ||
// store. | ||
// `extraFilters` is used to set up more filters based on the context that | ||
// calling this method. | ||
// | ||
// For example, to select a target store to replace a region's peer, we can use | ||
// the peer list with the peer removed as `coLocationStores`. | ||
// Meanwhile, we need to provide more constraints to ensure that the isolation | ||
// level cannot be reduced after replacement. | ||
func (s *ReplicaStrategy) SelectStoreToAdd(coLocationStores []*core.StoreInfo, extraFilters ...filter.Filter) uint64 { | ||
// The selection process uses a two-stage fashion. The first stage | ||
// ignores the temporary state of the stores and selects the stores | ||
// with the highest score according to the location label. The second | ||
// stage considers all temporary states and capacity factors to select | ||
// the most suitable target. | ||
// | ||
// The reason for it is to prevent the non-optimal replica placement due | ||
// to the short-term state, resulting in redundant scheduling. | ||
filters := []filter.Filter{ | ||
filter.NewExcludedFilter(s.checkerName, nil, s.region.GetStoreIds()), | ||
filter.NewStorageThresholdFilter(s.checkerName), | ||
filter.NewSpecialUseFilter(s.checkerName), | ||
&filter.StoreStateFilter{ActionScope: s.checkerName, MoveRegion: true, AllowTemporaryStates: true}, | ||
} | ||
if len(s.locationLabels) > 0 && s.isolationLevel != "" { | ||
filters = append(filters, filter.NewIsolationFilter(s.checkerName, s.isolationLevel, s.locationLabels, coLocationStores)) | ||
} | ||
if len(extraFilters) > 0 { | ||
filters = append(filters, extraFilters...) | ||
} | ||
if len(s.extraFilters) > 0 { | ||
filters = append(filters, s.extraFilters...) | ||
} | ||
|
||
isolationComparer := filter.IsolationComparer(s.locationLabels, coLocationStores) | ||
strictStateFilter := &filter.StoreStateFilter{ActionScope: s.checkerName, MoveRegion: true} | ||
target := filter.NewCandidates(s.cluster.GetStores()). | ||
FilterTarget(s.cluster.GetOpts(), filters...). | ||
Sort(isolationComparer).Reverse().Top(isolationComparer). // greater isolation score is better | ||
Sort(filter.RegionScoreComparer(s.cluster.GetOpts())). // less region score is better | ||
FilterTarget(s.cluster.GetOpts(), strictStateFilter).PickFirst() // the filter does not ignore temp states | ||
if target == nil { | ||
return 0 | ||
} | ||
return target.GetID() | ||
} | ||
|
||
// SelectStoreToReplace returns a store to replace oldStore. The location | ||
// placement after scheduling should be not worse than original. | ||
func (s *ReplicaStrategy) SelectStoreToReplace(coLocationStores []*core.StoreInfo, old uint64) uint64 { | ||
// trick to avoid creating a slice with `old` removed. | ||
s.swapStoreToFirst(coLocationStores, old) | ||
safeGuard := filter.NewLocationSafeguard(s.checkerName, s.locationLabels, coLocationStores, s.cluster.GetStore(old)) | ||
return s.SelectStoreToAdd(coLocationStores[1:], safeGuard) | ||
} | ||
|
||
// SelectStoreToImprove returns a store to replace oldStore. The location | ||
// placement after scheduling should be better than original. | ||
func (s *ReplicaStrategy) SelectStoreToImprove(coLocationStores []*core.StoreInfo, old uint64) uint64 { | ||
// trick to avoid creating a slice with `old` removed. | ||
s.swapStoreToFirst(coLocationStores, old) | ||
filters := []filter.Filter{ | ||
filter.NewLocationImprover(s.checkerName, s.locationLabels, coLocationStores, s.cluster.GetStore(old)), | ||
} | ||
if len(s.locationLabels) > 0 && s.isolationLevel != "" { | ||
filters = append(filters, filter.NewIsolationFilter(s.checkerName, s.isolationLevel, s.locationLabels, coLocationStores[1:])) | ||
} | ||
return s.SelectStoreToAdd(coLocationStores[1:], filters...) | ||
} | ||
|
||
func (s *ReplicaStrategy) swapStoreToFirst(stores []*core.StoreInfo, id uint64) { | ||
for i, s := range stores { | ||
if s.GetID() == id { | ||
stores[0], stores[i] = stores[i], stores[0] | ||
return | ||
} | ||
} | ||
} | ||
|
||
// SelectStoreToRemove returns the best option to remove from the region. | ||
func (s *ReplicaStrategy) SelectStoreToRemove(coLocationStores []*core.StoreInfo) uint64 { | ||
isolationComparer := filter.IsolationComparer(s.locationLabels, coLocationStores) | ||
source := filter.NewCandidates(coLocationStores). | ||
FilterSource(s.cluster.GetOpts(), &filter.StoreStateFilter{ActionScope: replicaCheckerName, MoveRegion: true}). | ||
Sort(isolationComparer).Top(isolationComparer). | ||
Sort(filter.RegionScoreComparer(s.cluster.GetOpts())).Reverse(). | ||
PickFirst() | ||
if source == nil { | ||
log.Debug("no removable store", zap.Uint64("region-id", s.region.GetID())) | ||
return 0 | ||
} | ||
return source.GetID() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.