-
Notifications
You must be signed in to change notification settings - Fork 42
/
split_checker.go
187 lines (159 loc) · 4.5 KB
/
split_checker.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
// Copyright 2021 MatrixOrigin.
//
// 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 raftstore
import (
"bytes"
"fmt"
"sync"
"github.com/lni/goutils/syncutil"
"github.com/matrixorigin/matrixcube/components/log"
"github.com/matrixorigin/matrixcube/components/prophet/pb/metapb"
"go.uber.org/zap"
)
type splitCheckFunc func(shard Shard, size uint64) (currentApproximateSize uint64,
currentApproximateKeys uint64, splitKeys [][]byte, ctx []byte, err error)
type splitChecker struct {
shardCapacityBytes uint64
replicaGetter replicaGetter
checkFuncFactory func(group uint64) splitCheckFunc
stopper *syncutil.Stopper
shardsC chan Shard
mu struct {
sync.Mutex
running bool
}
}
func newSplitChecker(maxWaitToCheck int,
shardCapacityBytes uint64,
replicaGetter replicaGetter,
checkFuncFactory func(group uint64) splitCheckFunc) *splitChecker {
return &splitChecker{
stopper: syncutil.NewStopper(),
replicaGetter: replicaGetter,
checkFuncFactory: checkFuncFactory,
shardCapacityBytes: shardCapacityBytes,
shardsC: make(chan Shard, maxWaitToCheck),
}
}
func (sc *splitChecker) start() {
sc.mu.Lock()
defer sc.mu.Unlock()
if sc.mu.running {
return
}
sc.mu.running = true
go func() {
for {
select {
case <-sc.stopper.ShouldStop():
close(sc.shardsC)
return
case shard := <-sc.shardsC:
sc.doChecker(shard)
}
}
}()
}
func (sc *splitChecker) doChecker(shard Shard) bool {
pr, ok := sc.replicaGetter.getReplica(shard.ID)
if !ok {
return false
}
pr.logger.Info("start split check job")
epoch := shard.Epoch
current := pr.getShard()
if current.Epoch.Version != epoch.Version {
pr.logger.Info("epoch changed, need re-check later",
log.EpochField("current-epoch", current.Epoch),
log.EpochField("check-epoch", epoch))
return false
}
fn := sc.checkFuncFactory(shard.Group)
size, keys, splitKeys, ctx, err := fn(shard, sc.shardCapacityBytes)
if err != nil {
pr.logger.Fatal("fail to scan split key",
zap.Error(err))
}
pr.logger.Debug("split check result",
log.ShardField("metadata", shard),
zap.Uint64("size", size),
zap.Uint64("capacity", sc.shardCapacityBytes),
zap.Uint64("keys", keys),
zap.ByteStrings("split-keys", splitKeys))
if len(splitKeys) > 0 {
for idx, key := range splitKeys {
if checkKeyInShard(key, shard) != nil {
pr.logger.Fatal("invalid split key",
log.HexField("key", key),
log.HexField("shard-start", shard.Start),
log.HexField("shard-end", shard.End))
}
if idx > 0 && bytes.Compare(key, splitKeys[idx-1]) <= 0 {
pr.logger.Fatal("invalid split key",
log.HexField("key", key),
log.HexField("prev-split-key", splitKeys[idx-1]))
}
}
}
act := action{
actionType: splitAction,
epoch: current.Epoch,
}
act.splitCheckData.ctx = ctx
act.splitCheckData.keys = keys
act.splitCheckData.size = size
act.splitCheckData.splitKeys = splitKeys
// need to exec split request
if len(splitKeys) > 0 {
// Suppose we have a shard A with range [0,10), after checking, we need to split Shard A into 2 Shards B and C
// in the range of [0, 5) and [5,10) at the point of 5.
// Note. After the split is complete, Shard A will no longer be used
newShardsCount := len(splitKeys) + 1
newIDs, err := pr.prophetClient.AskBatchSplit(NewResourceAdapterWithShard(current), uint32(newShardsCount))
if err != nil {
pr.logger.Error("fail to ask batch split",
zap.Error(err))
return false
}
if len(newIDs) != newShardsCount {
panic(fmt.Sprintf("expect %d new splitIDs, got %d", newShardsCount, len(newIDs)))
}
act.splitCheckData.splitIDs = newIDs
}
pr.addAction(act)
return true
}
func (sc *splitChecker) close() {
sc.mu.Lock()
defer sc.mu.Unlock()
if !sc.mu.running {
return
}
sc.stopper.Stop()
sc.mu.running = false
}
func (sc *splitChecker) add(shard Shard) {
sc.mu.Lock()
defer sc.mu.Unlock()
if !sc.mu.running {
return
}
if shard.State == metapb.ResourceState_Destroying ||
shard.State == metapb.ResourceState_Destroyed {
return
}
select {
case sc.shardsC <- shard:
default:
}
}