forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operator.go
563 lines (486 loc) · 16.8 KB
/
operator.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
560
561
562
563
// Copyright 2016 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 schedule
import (
"bytes"
"fmt"
"reflect"
"sync/atomic"
"time"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
log "github.com/sirupsen/logrus"
"github.com/pingcap/pd/server/core"
)
const (
// LeaderOperatorWaitTime is the duration that when a leader operator lives
// longer than it, the operator will be considered timeout.
LeaderOperatorWaitTime = 10 * time.Second
// RegionOperatorWaitTime is the duration that when a region operator lives
// longer than it, the operator will be considered timeout.
RegionOperatorWaitTime = 10 * time.Minute
)
// OperatorStep describes the basic scheduling steps that can not be subdivided.
type OperatorStep interface {
fmt.Stringer
IsFinish(region *core.RegionInfo) bool
Influence(opInfluence OpInfluence, region *core.RegionInfo)
}
// TransferLeader is an OperatorStep that transfers a region's leader.
type TransferLeader struct {
FromStore, ToStore uint64
}
func (tl TransferLeader) String() string {
return fmt.Sprintf("transfer leader from store %v to store %v", tl.FromStore, tl.ToStore)
}
// IsFinish checks if current step is finished.
func (tl TransferLeader) IsFinish(region *core.RegionInfo) bool {
return region.GetLeader().GetStoreId() == tl.ToStore
}
// Influence calculates the store difference that current step make
func (tl TransferLeader) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
from := opInfluence.GetStoreInfluence(tl.FromStore)
to := opInfluence.GetStoreInfluence(tl.ToStore)
from.LeaderSize -= region.GetApproximateSize()
from.LeaderCount--
to.LeaderSize += region.GetApproximateSize()
to.LeaderCount++
}
// AddPeer is an OperatorStep that adds a region peer.
type AddPeer struct {
ToStore, PeerID uint64
}
func (ap AddPeer) String() string {
return fmt.Sprintf("add peer %v on store %v", ap.PeerID, ap.ToStore)
}
// IsFinish checks if current step is finished.
func (ap AddPeer) IsFinish(region *core.RegionInfo) bool {
if p := region.GetStoreVoter(ap.ToStore); p != nil {
if p.GetId() != ap.PeerID {
log.Warnf("expect %v, but obtain voter %v", ap.String(), p.GetId())
return false
}
return region.GetPendingVoter(p.GetId()) == nil
}
return false
}
// Influence calculates the store difference that current step make
func (ap AddPeer) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
to := opInfluence.GetStoreInfluence(ap.ToStore)
to.RegionSize += region.GetApproximateSize()
to.RegionCount++
}
// AddLearner is an OperatorStep that adds a region learner peer.
type AddLearner struct {
ToStore, PeerID uint64
}
func (al AddLearner) String() string {
return fmt.Sprintf("add learner peer %v on store %v", al.PeerID, al.ToStore)
}
// IsFinish checks if current step is finished.
func (al AddLearner) IsFinish(region *core.RegionInfo) bool {
if p := region.GetStoreLearner(al.ToStore); p != nil {
if p.GetId() != al.PeerID {
log.Warnf("expect %v, but obtain learner %v", al.String(), p.GetId())
return false
}
return region.GetPendingLearner(p.GetId()) == nil
}
return false
}
// Influence calculates the store difference that current step make
func (al AddLearner) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
to := opInfluence.GetStoreInfluence(al.ToStore)
to.RegionSize += region.GetApproximateSize()
to.RegionCount++
}
// PromoteLearner is an OperatorStep that promotes a region learner peer to normal voter.
type PromoteLearner struct {
ToStore, PeerID uint64
}
func (pl PromoteLearner) String() string {
return fmt.Sprintf("promote learner peer %v on store %v to voter", pl.PeerID, pl.ToStore)
}
// IsFinish checks if current step is finished.
func (pl PromoteLearner) IsFinish(region *core.RegionInfo) bool {
if p := region.GetStoreVoter(pl.ToStore); p != nil {
if p.GetId() != pl.PeerID {
log.Warnf("expect %v, but obtain voter %v", pl.String(), p.GetId())
}
return p.GetId() == pl.PeerID
}
return false
}
// Influence calculates the store difference that current step make
func (pl PromoteLearner) Influence(opInfluence OpInfluence, region *core.RegionInfo) {}
// RemovePeer is an OperatorStep that removes a region peer.
type RemovePeer struct {
FromStore uint64
}
func (rp RemovePeer) String() string {
return fmt.Sprintf("remove peer on store %v", rp.FromStore)
}
// IsFinish checks if current step is finished.
func (rp RemovePeer) IsFinish(region *core.RegionInfo) bool {
return region.GetStorePeer(rp.FromStore) == nil
}
// Influence calculates the store difference that current step make
func (rp RemovePeer) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
from := opInfluence.GetStoreInfluence(rp.FromStore)
from.RegionSize -= region.GetApproximateSize()
from.RegionCount--
}
// MergeRegion is an OperatorStep that merge two regions.
type MergeRegion struct {
FromRegion *metapb.Region
ToRegion *metapb.Region
// there are two regions involved in merge process,
// so to keep them from other scheduler,
// both of them should add MerRegion operatorStep.
// But actually, tikv just need the region want to be merged to get the merge request,
// thus use a IsPssive mark to indicate that
// this region doesn't need to send merge request to tikv.
IsPassive bool
}
func (mr MergeRegion) String() string {
return fmt.Sprintf("merge region %v into region %v", mr.FromRegion.GetId(), mr.ToRegion.GetId())
}
// IsFinish checks if current step is finished
func (mr MergeRegion) IsFinish(region *core.RegionInfo) bool {
if mr.IsPassive {
return !bytes.Equal(region.GetStartKey(), mr.ToRegion.StartKey) || !bytes.Equal(region.GetEndKey(), mr.ToRegion.EndKey)
}
return false
}
// Influence calculates the store difference that current step make
func (mr MergeRegion) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
if mr.IsPassive {
for _, p := range region.GetPeers() {
o := opInfluence.GetStoreInfluence(p.GetStoreId())
o.RegionCount--
if region.GetLeader().GetId() == p.GetId() {
o.LeaderCount--
}
}
}
}
// SplitRegion is an OperatorStep that splits a region.
type SplitRegion struct {
StartKey, EndKey []byte
Policy pdpb.CheckPolicy
}
func (sr SplitRegion) String() string {
return fmt.Sprintf("split region with policy %s", sr.Policy.String())
}
// IsFinish checks if current step is finished.
func (sr SplitRegion) IsFinish(region *core.RegionInfo) bool {
return !bytes.Equal(region.GetStartKey(), sr.StartKey) || !bytes.Equal(region.GetEndKey(), sr.EndKey)
}
// Influence calculates the store difference that current step make.
func (sr SplitRegion) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
for _, p := range region.GetPeers() {
inf := opInfluence.GetStoreInfluence(p.GetStoreId())
inf.RegionCount++
if region.GetLeader().GetId() == p.GetId() {
inf.LeaderCount++
}
}
}
// Operator contains execution steps generated by scheduler.
type Operator struct {
desc string
regionID uint64
regionEpoch *metapb.RegionEpoch
kind OperatorKind
steps []OperatorStep
currentStep int32
createTime time.Time
stepTime int64
level core.PriorityLevel
}
// NewOperator creates a new operator.
func NewOperator(desc string, regionID uint64, regionEpoch *metapb.RegionEpoch, kind OperatorKind, steps ...OperatorStep) *Operator {
return &Operator{
desc: desc,
regionID: regionID,
regionEpoch: regionEpoch,
kind: kind,
steps: steps,
createTime: time.Now(),
stepTime: time.Now().UnixNano(),
level: core.NormalPriority,
}
}
func (o *Operator) String() string {
s := fmt.Sprintf("%s (kind:%s, region:%v(%v,%v), createAt:%s, currentStep:%v, steps:%+v) ", o.desc, o.kind, o.regionID, o.regionEpoch.GetVersion(), o.regionEpoch.GetConfVer(), o.createTime, atomic.LoadInt32(&o.currentStep), o.steps)
if o.IsTimeout() {
s = s + "timeout"
}
if o.IsFinish() {
s = s + "finished"
}
return s
}
// MarshalJSON serialize custom types to JSON
func (o *Operator) MarshalJSON() ([]byte, error) {
return []byte(`"` + o.String() + `"`), nil
}
// Desc returns the operator's short description.
func (o *Operator) Desc() string {
return o.desc
}
// SetDesc sets the description for the operator.
func (o *Operator) SetDesc(desc string) {
o.desc = desc
}
// AttachKind attaches an operator kind for the operator.
func (o *Operator) AttachKind(kind OperatorKind) {
o.kind |= kind
}
// RegionID returns the region that operator is targeted.
func (o *Operator) RegionID() uint64 {
return o.regionID
}
// RegionEpoch returns the region's epoch that is attched to the operator.
func (o *Operator) RegionEpoch() *metapb.RegionEpoch {
return o.regionEpoch
}
// Kind returns operator's kind.
func (o *Operator) Kind() OperatorKind {
return o.kind
}
// ElapsedTime returns duration since it was created.
func (o *Operator) ElapsedTime() time.Duration {
return time.Since(o.createTime)
}
// Len returns the operator's steps count.
func (o *Operator) Len() int {
return len(o.steps)
}
// Step returns the i-th step.
func (o *Operator) Step(i int) OperatorStep {
if i >= 0 && i < len(o.steps) {
return o.steps[i]
}
return nil
}
// Check checks if current step is finished, returns next step to take action.
// It's safe to be called by multiple goroutine concurrently.
func (o *Operator) Check(region *core.RegionInfo) OperatorStep {
for step := atomic.LoadInt32(&o.currentStep); int(step) < len(o.steps); step++ {
if o.steps[int(step)].IsFinish(region) {
operatorStepDuration.WithLabelValues(reflect.TypeOf(o.steps[int(step)]).Name()).
Observe(time.Since(time.Unix(0, atomic.LoadInt64(&o.stepTime))).Seconds())
atomic.StoreInt32(&o.currentStep, step+1)
atomic.StoreInt64(&o.stepTime, time.Now().UnixNano())
} else {
return o.steps[int(step)]
}
}
return nil
}
// SetPriorityLevel set the priority level for operator
func (o *Operator) SetPriorityLevel(level core.PriorityLevel) {
o.level = level
}
// GetPriorityLevel get the priority level
func (o *Operator) GetPriorityLevel() core.PriorityLevel {
return o.level
}
// IsFinish checks if all steps are finished.
func (o *Operator) IsFinish() bool {
return atomic.LoadInt32(&o.currentStep) >= int32(len(o.steps))
}
// IsTimeout checks the operator's create time and determines if it is timeout.
func (o *Operator) IsTimeout() bool {
if o.IsFinish() {
return false
}
if o.kind&OpRegion != 0 {
return time.Since(o.createTime) > RegionOperatorWaitTime
}
return time.Since(o.createTime) > LeaderOperatorWaitTime
}
// Influence calculates the store difference which unfinished operator steps make
func (o *Operator) Influence(opInfluence OpInfluence, region *core.RegionInfo) {
for step := atomic.LoadInt32(&o.currentStep); int(step) < len(o.steps); step++ {
if !o.steps[int(step)].IsFinish(region) {
o.steps[int(step)].Influence(opInfluence, region)
}
}
}
// OperatorHistory is used to log and visualize completed operators.
type OperatorHistory struct {
FinishTime time.Time
From, To uint64
Kind core.ResourceKind
}
// History transfers the operator's steps to operator histories.
func (o *Operator) History() []OperatorHistory {
now := time.Now()
var histories []OperatorHistory
var addPeerStores, removePeerStores []uint64
for _, step := range o.steps {
switch s := step.(type) {
case TransferLeader:
histories = append(histories, OperatorHistory{
FinishTime: now,
From: s.FromStore,
To: s.ToStore,
Kind: core.LeaderKind,
})
case AddPeer:
addPeerStores = append(addPeerStores, s.ToStore)
case AddLearner:
addPeerStores = append(addPeerStores, s.ToStore)
case RemovePeer:
removePeerStores = append(removePeerStores, s.FromStore)
}
}
for i := range addPeerStores {
if i < len(removePeerStores) {
histories = append(histories, OperatorHistory{
FinishTime: now,
From: removePeerStores[i],
To: addPeerStores[i],
Kind: core.RegionKind,
})
}
}
return histories
}
// CreateRemovePeerOperator creates an Operator that removes a peer from region.
func CreateRemovePeerOperator(desc string, cluster Cluster, kind OperatorKind, region *core.RegionInfo, storeID uint64) *Operator {
removeKind, steps := removePeerSteps(cluster, region, storeID)
return NewOperator(desc, region.GetID(), region.GetRegionEpoch(), removeKind|kind, steps...)
}
// CreateMovePeerOperator creates an Operator that replaces an old peer with a new peer.
func CreateMovePeerOperator(desc string, cluster Cluster, region *core.RegionInfo, kind OperatorKind, oldStore, newStore uint64, peerID uint64) *Operator {
removeKind, steps := removePeerSteps(cluster, region, oldStore)
var st []OperatorStep
if cluster.IsRaftLearnerEnabled() {
st = []OperatorStep{
AddLearner{ToStore: newStore, PeerID: peerID},
PromoteLearner{ToStore: newStore, PeerID: peerID},
}
} else {
st = []OperatorStep{
AddPeer{ToStore: newStore, PeerID: peerID},
}
}
steps = append(st, steps...)
return NewOperator(desc, region.GetID(), region.GetRegionEpoch(), removeKind|kind|OpRegion, steps...)
}
// removePeerSteps returns the steps to safely remove a peer. It prevents removing leader by transfer its leadership first.
func removePeerSteps(cluster Cluster, region *core.RegionInfo, storeID uint64) (kind OperatorKind, steps []OperatorStep) {
if region.GetLeader() != nil && region.GetLeader().GetStoreId() == storeID {
for id := range region.GetFollowers() {
follower := cluster.GetStore(id)
if follower != nil && !cluster.CheckLabelProperty(RejectLeader, follower.Labels) {
steps = append(steps, TransferLeader{FromStore: storeID, ToStore: id})
kind = OpLeader
break
}
}
}
steps = append(steps, RemovePeer{FromStore: storeID})
kind |= OpRegion
return
}
// CreateMergeRegionOperator creates an Operator that merge two region into one
func CreateMergeRegionOperator(desc string, cluster Cluster, source *core.RegionInfo, target *core.RegionInfo, kind OperatorKind) (*Operator, *Operator, error) {
steps, kinds, err := matchPeerSteps(cluster, source, target)
if err != nil {
return nil, nil, err
}
steps = append(steps, MergeRegion{
FromRegion: source.GetMeta(),
ToRegion: target.GetMeta(),
IsPassive: false,
})
op1 := NewOperator(desc, source.GetID(), source.GetRegionEpoch(), kinds|kind|OpMerge, steps...)
op2 := NewOperator(desc, target.GetID(), target.GetRegionEpoch(), kind|OpMerge, MergeRegion{
FromRegion: source.GetMeta(),
ToRegion: target.GetMeta(),
IsPassive: true,
})
return op1, op2, nil
}
// matchPeerSteps returns the steps to match the location of peer stores of source region with target's.
func matchPeerSteps(cluster Cluster, source *core.RegionInfo, target *core.RegionInfo) ([]OperatorStep, OperatorKind, error) {
storeIDs := make(map[uint64]struct{})
var steps []OperatorStep
var kind OperatorKind
sourcePeers := source.GetPeers()
targetPeers := target.GetPeers()
for _, peer := range targetPeers {
storeIDs[peer.GetStoreId()] = struct{}{}
}
// Add missing peers.
for id := range storeIDs {
if source.GetStorePeer(id) != nil {
continue
}
peer, err := cluster.AllocPeer(id)
if err != nil {
log.Debugf("peer alloc failed: %v", err)
return nil, kind, err
}
if cluster.IsRaftLearnerEnabled() {
steps = append(steps,
AddLearner{ToStore: id, PeerID: peer.Id},
PromoteLearner{ToStore: id, PeerID: peer.Id},
)
} else {
steps = append(steps, AddPeer{ToStore: id, PeerID: peer.Id})
}
kind |= OpRegion
}
// Check whether to transfer leader or not
intersection := getIntersectionStores(sourcePeers, targetPeers)
leaderID := source.GetLeader().GetStoreId()
isFound := false
for _, storeID := range intersection {
if storeID == leaderID {
isFound = true
break
}
}
if !isFound {
steps = append(steps, TransferLeader{FromStore: source.GetLeader().GetStoreId(), ToStore: target.GetLeader().GetStoreId()})
kind |= OpLeader
}
// Remove redundant peers.
for _, peer := range sourcePeers {
if _, ok := storeIDs[peer.GetStoreId()]; ok {
continue
}
steps = append(steps, RemovePeer{FromStore: peer.GetStoreId()})
kind |= OpRegion
}
return steps, kind, nil
}
// getIntersectionStores returns the stores included in two region's peers.
func getIntersectionStores(a []*metapb.Peer, b []*metapb.Peer) []uint64 {
set := make([]uint64, 0)
hash := make(map[uint64]struct{})
for _, peer := range a {
hash[peer.GetStoreId()] = struct{}{}
}
for _, peer := range b {
if _, found := hash[peer.GetStoreId()]; found {
set = append(set, peer.GetStoreId())
}
}
return set
}