-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathregion_scatterer.go
559 lines (523 loc) · 19.9 KB
/
region_scatterer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright 2017 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scatter
import (
"context"
"fmt"
"math"
"sync"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/cache"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/core/constant"
"github.com/tikv/pd/pkg/errs"
sche "github.com/tikv/pd/pkg/schedule/core"
"github.com/tikv/pd/pkg/schedule/filter"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/placement"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.uber.org/zap"
)
const regionScatterName = "region-scatter"
var (
gcInterval = time.Minute
gcTTL = time.Minute * 3
// WithLabelValues is a heavy operation, define variable to avoid call it every time.
scatterSkipEmptyRegionCounter = scatterCounter.WithLabelValues("skip", "empty-region")
scatterSkipNoRegionCounter = scatterCounter.WithLabelValues("skip", "no-region")
scatterSkipNoLeaderCounter = scatterCounter.WithLabelValues("skip", "no-leader")
scatterSkipHotRegionCounter = scatterCounter.WithLabelValues("skip", "hot")
scatterSkipNotReplicatedCounter = scatterCounter.WithLabelValues("skip", "not-replicated")
scatterUnnecessaryCounter = scatterCounter.WithLabelValues("unnecessary", "")
scatterFailCounter = scatterCounter.WithLabelValues("fail", "")
scatterSuccessCounter = scatterCounter.WithLabelValues("success", "")
)
const (
maxSleepDuration = time.Minute
initialSleepDuration = 100 * time.Millisecond
maxRetryLimit = 30
)
type selectedStores struct {
mu syncutil.RWMutex
groupDistribution *cache.TTLString // value type: map[uint64]uint64, group -> StoreID -> count
}
func newSelectedStores(ctx context.Context) *selectedStores {
return &selectedStores{
groupDistribution: cache.NewStringTTL(ctx, gcInterval, gcTTL),
}
}
// Put plus count by storeID and group
func (s *selectedStores) Put(id uint64, group string) {
s.mu.Lock()
defer s.mu.Unlock()
distribution, ok := s.getDistributionByGroupLocked(group)
if !ok {
distribution = map[uint64]uint64{}
distribution[id] = 0
}
distribution[id]++
s.groupDistribution.Put(group, distribution)
}
// Get the count by storeID and group
func (s *selectedStores) Get(id uint64, group string) uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
distribution, ok := s.getDistributionByGroupLocked(group)
if !ok {
return 0
}
count, ok := distribution[id]
if !ok {
return 0
}
return count
}
// GetGroupDistribution get distribution group by `group`
func (s *selectedStores) GetGroupDistribution(group string) (map[uint64]uint64, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.getDistributionByGroupLocked(group)
}
// TotalCountByStore counts the total count by store
func (s *selectedStores) TotalCountByStore(storeID uint64) uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
groups := s.groupDistribution.GetAllID()
totalCount := uint64(0)
for _, group := range groups {
storeDistribution, ok := s.getDistributionByGroupLocked(group)
if !ok {
continue
}
count, ok := storeDistribution[storeID]
if !ok {
continue
}
totalCount += count
}
return totalCount
}
// getDistributionByGroupLocked should be called with lock
func (s *selectedStores) getDistributionByGroupLocked(group string) (map[uint64]uint64, bool) {
if result, ok := s.groupDistribution.Get(group); ok {
return result.(map[uint64]uint64), true
}
return nil, false
}
// RegionScatterer scatters regions.
type RegionScatterer struct {
ctx context.Context
name string
cluster sche.SharedCluster
ordinaryEngine engineContext
specialEngines sync.Map
opController *operator.Controller
addSuspectRegions func(regionIDs ...uint64)
}
// NewRegionScatterer creates a region scatterer.
// RegionScatter is used for the `Lightning`, it will scatter the specified regions before import data.
func NewRegionScatterer(ctx context.Context, cluster sche.SharedCluster, opController *operator.Controller, addSuspectRegions func(regionIDs ...uint64)) *RegionScatterer {
return &RegionScatterer{
ctx: ctx,
name: regionScatterName,
cluster: cluster,
opController: opController,
addSuspectRegions: addSuspectRegions,
ordinaryEngine: newEngineContext(ctx, func() filter.Filter {
return filter.NewEngineFilter(regionScatterName, filter.NotSpecialEngines)
}),
}
}
type filterFunc func() filter.Filter
type engineContext struct {
filterFuncs []filterFunc
selectedPeer *selectedStores
selectedLeader *selectedStores
}
func newEngineContext(ctx context.Context, filterFuncs ...filterFunc) engineContext {
filterFuncs = append(filterFuncs, func() filter.Filter {
return &filter.StoreStateFilter{ActionScope: regionScatterName, MoveRegion: true, ScatterRegion: true, OperatorLevel: constant.High}
})
return engineContext{
filterFuncs: filterFuncs,
selectedPeer: newSelectedStores(ctx),
selectedLeader: newSelectedStores(ctx),
}
}
// ScatterRegionsByRange directly scatter regions by ScatterRegions
func (r *RegionScatterer) ScatterRegionsByRange(startKey, endKey []byte, group string, retryLimit int) (int, map[uint64]error, error) {
regions := r.cluster.ScanRegions(startKey, endKey, -1)
if len(regions) < 1 {
scatterSkipEmptyRegionCounter.Inc()
return 0, nil, errors.New("empty region")
}
failures := make(map[uint64]error, len(regions))
regionMap := make(map[uint64]*core.RegionInfo, len(regions))
for _, region := range regions {
regionMap[region.GetID()] = region
}
// If there existed any region failed to relocated after retry, add it into unProcessedRegions
opsCount, err := r.scatterRegions(regionMap, failures, group, retryLimit, false)
if err != nil {
return 0, nil, err
}
return opsCount, failures, nil
}
// ScatterRegionsByID directly scatter regions by ScatterRegions
func (r *RegionScatterer) ScatterRegionsByID(regionsID []uint64, group string, retryLimit int, skipStoreLimit bool) (int, map[uint64]error, error) {
if len(regionsID) < 1 {
scatterSkipEmptyRegionCounter.Inc()
return 0, nil, errors.New("empty region")
}
failures := make(map[uint64]error, len(regionsID))
regions := make([]*core.RegionInfo, 0, len(regionsID))
for _, id := range regionsID {
region := r.cluster.GetRegion(id)
if region == nil {
scatterSkipNoRegionCounter.Inc()
log.Warn("failed to find region during scatter", zap.Uint64("region-id", id))
failures[id] = errors.New(fmt.Sprintf("failed to find region %v", id))
continue
}
regions = append(regions, region)
}
regionMap := make(map[uint64]*core.RegionInfo, len(regions))
for _, region := range regions {
regionMap[region.GetID()] = region
}
// If there existed any region failed to relocated after retry, add it into unProcessedRegions
opsCount, err := r.scatterRegions(regionMap, failures, group, retryLimit, skipStoreLimit)
if err != nil {
return 0, nil, err
}
return opsCount, failures, nil
}
// scatterRegions relocates the regions. If the group is defined, the regions' leader with the same group would be scattered
// in a group level instead of cluster level.
// RetryTimes indicates the retry times if any of the regions failed to relocate during scattering. There will be
// time.Sleep between each retry.
// Failures indicates the regions which are failed to be relocated, the key of the failures indicates the regionID
// and the value of the failures indicates the failure error.
func (r *RegionScatterer) scatterRegions(regions map[uint64]*core.RegionInfo, failures map[uint64]error, group string, retryLimit int, skipStoreLimit bool) (int, error) {
if len(regions) < 1 {
scatterSkipEmptyRegionCounter.Inc()
return 0, errors.New("empty region")
}
if retryLimit > maxRetryLimit {
retryLimit = maxRetryLimit
}
opsCount := 0
for currentRetry := 0; currentRetry <= retryLimit; currentRetry++ {
for _, region := range regions {
op, err := r.Scatter(region, group, skipStoreLimit)
failpoint.Inject("scatterFail", func() {
if region.GetID() == 1 {
err = errors.New("mock error")
}
})
if err != nil {
failures[region.GetID()] = err
continue
}
delete(regions, region.GetID())
opsCount++
if op != nil {
if ok := r.opController.AddOperator(op); !ok {
// If there existed any operator failed to be added into Operator Controller, add its regions into unProcessedRegions
failures[op.RegionID()] = fmt.Errorf("region %v failed to add operator", op.RegionID())
continue
}
failpoint.Inject("scatterHbStreamsDrain", func() {
r.opController.GetHBStreams().Drain(1)
r.opController.RemoveOperator(op, operator.AdminStop)
})
}
delete(failures, region.GetID())
}
// all regions have been relocated, break the loop.
if len(regions) < 1 {
break
}
// Wait for a while if there are some regions failed to be relocated
time.Sleep(typeutil.MinDuration(maxSleepDuration, time.Duration(math.Pow(2, float64(currentRetry)))*initialSleepDuration))
}
return opsCount, nil
}
// Scatter relocates the region. If the group is defined, the regions' leader with the same group would be scattered
// in a group level instead of cluster level.
func (r *RegionScatterer) Scatter(region *core.RegionInfo, group string, skipStoreLimit bool) (*operator.Operator, error) {
if !filter.IsRegionReplicated(r.cluster, region) {
r.addSuspectRegions(region.GetID())
scatterSkipNotReplicatedCounter.Inc()
log.Warn("region not replicated during scatter", zap.Uint64("region-id", region.GetID()))
return nil, errors.Errorf("region %d is not fully replicated", region.GetID())
}
if region.GetLeader() == nil {
scatterSkipNoLeaderCounter.Inc()
log.Warn("region no leader during scatter", zap.Uint64("region-id", region.GetID()))
return nil, errors.Errorf("region %d has no leader", region.GetID())
}
if r.cluster.IsRegionHot(region) {
scatterSkipHotRegionCounter.Inc()
log.Warn("region too hot during scatter", zap.Uint64("region-id", region.GetID()))
return nil, errors.Errorf("region %d is hot", region.GetID())
}
return r.scatterRegion(region, group, skipStoreLimit), nil
}
func (r *RegionScatterer) scatterRegion(region *core.RegionInfo, group string, skipStoreLimit bool) *operator.Operator {
engineFilter := filter.NewEngineFilter(r.name, filter.NotSpecialEngines)
ordinaryPeers := make(map[uint64]*metapb.Peer, len(region.GetPeers()))
specialPeers := make(map[string]map[uint64]*metapb.Peer)
oldFit := r.cluster.GetRuleManager().FitRegion(r.cluster, region)
// Group peers by the engine of their stores
for _, peer := range region.GetPeers() {
store := r.cluster.GetStore(peer.GetStoreId())
if store == nil {
return nil
}
if engineFilter.Target(r.cluster.GetSharedConfig(), store).IsOK() {
ordinaryPeers[peer.GetStoreId()] = peer
} else {
engine := store.GetLabelValue(core.EngineKey)
if _, ok := specialPeers[engine]; !ok {
specialPeers[engine] = make(map[uint64]*metapb.Peer)
}
specialPeers[engine][peer.GetStoreId()] = peer
}
}
targetPeers := make(map[uint64]*metapb.Peer, len(region.GetPeers())) // StoreID -> Peer
selectedStores := make(map[uint64]struct{}, len(region.GetPeers())) // selected StoreID set
leaderCandidateStores := make([]uint64, 0, len(region.GetPeers())) // StoreID allowed to become Leader
scatterWithSameEngine := func(peers map[uint64]*metapb.Peer, context engineContext) { // peers: StoreID -> Peer
for _, peer := range peers {
if _, ok := selectedStores[peer.GetStoreId()]; ok {
if allowLeader(oldFit, peer) {
leaderCandidateStores = append(leaderCandidateStores, peer.GetStoreId())
}
// It is both sourcePeer and targetPeer itself, no need to select.
continue
}
for {
candidates := r.selectCandidates(region, oldFit, peer.GetStoreId(), selectedStores, context)
newPeer := r.selectStore(group, peer, peer.GetStoreId(), candidates, context)
targetPeers[newPeer.GetStoreId()] = newPeer
selectedStores[newPeer.GetStoreId()] = struct{}{}
// If the selected peer is a peer other than origin peer in this region,
// it is considered that the selected peer select itself.
// This origin peer re-selects.
if _, ok := peers[newPeer.GetStoreId()]; !ok || peer.GetStoreId() == newPeer.GetStoreId() {
selectedStores[peer.GetStoreId()] = struct{}{}
if allowLeader(oldFit, peer) {
leaderCandidateStores = append(leaderCandidateStores, newPeer.GetStoreId())
}
break
}
}
}
}
scatterWithSameEngine(ordinaryPeers, r.ordinaryEngine)
// FIXME: target leader only considers the ordinary stores, maybe we need to consider the
// special engine stores if the engine supports to become a leader. But now there is only
// one engine, tiflash, which does not support the leader, so don't consider it for now.
targetLeader := r.selectAvailableLeaderStore(group, region, leaderCandidateStores, r.ordinaryEngine)
if targetLeader == 0 {
scatterSkipNoLeaderCounter.Inc()
return nil
}
for engine, peers := range specialPeers {
ctx, ok := r.specialEngines.Load(engine)
if !ok {
ctx = newEngineContext(r.ctx, func() filter.Filter {
return filter.NewEngineFilter(r.name, placement.LabelConstraint{Key: core.EngineKey, Op: placement.In, Values: []string{engine}})
})
r.specialEngines.Store(engine, ctx)
}
scatterWithSameEngine(peers, ctx.(engineContext))
}
if isSameDistribution(region, targetPeers, targetLeader) {
scatterUnnecessaryCounter.Inc()
r.Put(targetPeers, targetLeader, group)
return nil
}
op, err := operator.CreateScatterRegionOperator("scatter-region", r.cluster, region, targetPeers, targetLeader, skipStoreLimit)
if err != nil {
scatterFailCounter.Inc()
for _, peer := range region.GetPeers() {
targetPeers[peer.GetStoreId()] = peer
}
r.Put(targetPeers, region.GetLeader().GetStoreId(), group)
log.Debug("fail to create scatter region operator", errs.ZapError(err))
return nil
}
if op != nil {
scatterSuccessCounter.Inc()
r.Put(targetPeers, targetLeader, group)
op.SetPriorityLevel(constant.High)
}
return op
}
func allowLeader(fit *placement.RegionFit, peer *metapb.Peer) bool {
switch peer.GetRole() {
case metapb.PeerRole_Learner, metapb.PeerRole_DemotingVoter:
return false
}
if peer.IsWitness {
return false
}
peerFit := fit.GetRuleFit(peer.GetId())
if peerFit == nil || peerFit.Rule == nil || peerFit.Rule.IsWitness {
return false
}
switch peerFit.Rule.Role {
case placement.Voter, placement.Leader:
return true
}
return false
}
func isSameDistribution(region *core.RegionInfo, targetPeers map[uint64]*metapb.Peer, targetLeader uint64) bool {
peers := region.GetPeers()
for _, peer := range peers {
if _, ok := targetPeers[peer.GetStoreId()]; !ok {
return false
}
}
return region.GetLeader().GetStoreId() == targetLeader
}
func (r *RegionScatterer) selectCandidates(region *core.RegionInfo, oldFit *placement.RegionFit, sourceStoreID uint64, selectedStores map[uint64]struct{}, context engineContext) []uint64 {
sourceStore := r.cluster.GetStore(sourceStoreID)
if sourceStore == nil {
log.Error("failed to get the store", zap.Uint64("store-id", sourceStoreID), errs.ZapError(errs.ErrGetSourceStore))
return nil
}
filters := []filter.Filter{
filter.NewExcludedFilter(r.name, nil, selectedStores),
}
scoreGuard := filter.NewPlacementSafeguard(r.name, r.cluster.GetSharedConfig(), r.cluster.GetBasicCluster(), r.cluster.GetRuleManager(), region, sourceStore, oldFit)
for _, filterFunc := range context.filterFuncs {
filters = append(filters, filterFunc())
}
filters = append(filters, scoreGuard)
stores := r.cluster.GetStores()
candidates := make([]uint64, 0)
maxStoreTotalCount := uint64(0)
minStoreTotalCount := uint64(math.MaxUint64)
for _, store := range stores {
count := context.selectedPeer.TotalCountByStore(store.GetID())
if count > maxStoreTotalCount {
maxStoreTotalCount = count
}
if count < minStoreTotalCount {
minStoreTotalCount = count
}
}
for _, store := range stores {
storeCount := context.selectedPeer.TotalCountByStore(store.GetID())
// If storeCount is equal to the maxStoreTotalCount, we should skip this store as candidate.
// If the storeCount are all the same for the whole cluster(maxStoreTotalCount == minStoreTotalCount), any store
// could be selected as candidate.
if storeCount < maxStoreTotalCount || maxStoreTotalCount == minStoreTotalCount {
if filter.Target(r.cluster.GetSharedConfig(), store, filters) {
candidates = append(candidates, store.GetID())
}
}
}
return candidates
}
func (r *RegionScatterer) selectStore(group string, peer *metapb.Peer, sourceStoreID uint64, candidates []uint64, context engineContext) *metapb.Peer {
if len(candidates) < 1 {
return peer
}
var newPeer *metapb.Peer
minCount := uint64(math.MaxUint64)
for _, storeID := range candidates {
count := context.selectedPeer.Get(storeID, group)
if count < minCount {
minCount = count
newPeer = &metapb.Peer{
StoreId: storeID,
Role: peer.GetRole(),
}
}
}
// if the source store have the least count, we don't need to scatter this peer
for _, storeID := range candidates {
if storeID == sourceStoreID && context.selectedPeer.Get(sourceStoreID, group) <= minCount {
return peer
}
}
if newPeer == nil {
return peer
}
return newPeer
}
// selectAvailableLeaderStore select the target leader store from the candidates. The candidates would be collected by
// the existed peers store depended on the leader counts in the group level. Please use this func before scatter spacial engines.
func (r *RegionScatterer) selectAvailableLeaderStore(group string, region *core.RegionInfo, leaderCandidateStores []uint64, context engineContext) uint64 {
sourceStore := r.cluster.GetStore(region.GetLeader().GetStoreId())
if sourceStore == nil {
log.Error("failed to get the store", zap.Uint64("store-id", region.GetLeader().GetStoreId()), errs.ZapError(errs.ErrGetSourceStore))
return 0
}
minStoreGroupLeader := uint64(math.MaxUint64)
id := uint64(0)
for _, storeID := range leaderCandidateStores {
store := r.cluster.GetStore(storeID)
if store == nil {
continue
}
storeGroupLeaderCount := context.selectedLeader.Get(storeID, group)
if minStoreGroupLeader > storeGroupLeaderCount {
minStoreGroupLeader = storeGroupLeaderCount
id = storeID
}
}
return id
}
// Put put the final distribution in the context no matter the operator was created
func (r *RegionScatterer) Put(peers map[uint64]*metapb.Peer, leaderStoreID uint64, group string) {
engineFilter := filter.NewEngineFilter(r.name, filter.NotSpecialEngines)
// Group peers by the engine of their stores
for _, peer := range peers {
storeID := peer.GetStoreId()
store := r.cluster.GetStore(storeID)
if store == nil {
continue
}
if engineFilter.Target(r.cluster.GetSharedConfig(), store).IsOK() {
r.ordinaryEngine.selectedPeer.Put(storeID, group)
scatterDistributionCounter.WithLabelValues(
fmt.Sprintf("%v", storeID),
fmt.Sprintf("%v", false),
core.EngineTiKV).Inc()
} else {
engine := store.GetLabelValue(core.EngineKey)
ctx, _ := r.specialEngines.Load(engine)
ctx.(engineContext).selectedPeer.Put(storeID, group)
scatterDistributionCounter.WithLabelValues(
fmt.Sprintf("%v", storeID),
fmt.Sprintf("%v", false),
engine).Inc()
}
}
r.ordinaryEngine.selectedLeader.Put(leaderStoreID, group)
scatterDistributionCounter.WithLabelValues(
fmt.Sprintf("%v", leaderStoreID),
fmt.Sprintf("%v", true),
core.EngineTiKV).Inc()
}