-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft.go
307 lines (279 loc) · 7.8 KB
/
raft.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
// 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 simulator
import (
"context"
"math/rand"
"sort"
"sync"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/tools/pd-simulator/simulator/cases"
"github.com/pingcap/pd/tools/pd-simulator/simulator/simutil"
"github.com/pkg/errors"
)
// RaftEngine records all raft infomations.
type RaftEngine struct {
sync.RWMutex
regionsInfo *core.RegionsInfo
conn *Connection
regionChange map[uint64][]uint64
schedulerStats *schedulerStatistics
regionSplitSize int64
regionSplitKeys int64
storeConfig *SimConfig
}
// NewRaftEngine creates the initialized raft with the configuration.
func NewRaftEngine(conf *cases.Case, conn *Connection, storeConfig *SimConfig) *RaftEngine {
r := &RaftEngine{
regionsInfo: core.NewRegionsInfo(),
conn: conn,
regionChange: make(map[uint64][]uint64),
schedulerStats: newSchedulerStatistics(),
regionSplitSize: conf.RegionSplitSize,
regionSplitKeys: conf.RegionSplitKeys,
storeConfig: storeConfig,
}
splitKeys := generateKeys(len(conf.Regions) - 1)
for i, region := range conf.Regions {
meta := &metapb.Region{
Id: region.ID,
Peers: region.Peers,
RegionEpoch: &metapb.RegionEpoch{ConfVer: 1, Version: 1},
}
if i > 0 {
meta.StartKey = []byte(splitKeys[i-1])
}
if i < len(conf.Regions)-1 {
meta.EndKey = []byte(splitKeys[i])
}
regionInfo := core.NewRegionInfo(
meta,
region.Leader,
core.SetApproximateSize(region.Size),
core.SetApproximateKeys(region.Keys),
)
r.SetRegion(regionInfo)
peers := region.Peers
regionSize := uint64(region.Size)
for _, peer := range peers {
r.conn.Nodes[peer.StoreId].incUsedSize(regionSize)
}
}
for _, node := range conn.Nodes {
node.raftEngine = r
}
return r
}
func (r *RaftEngine) stepRegions() {
regions := r.GetRegions()
for _, region := range regions {
r.stepLeader(region)
r.stepSplit(region)
}
}
func (r *RaftEngine) stepLeader(region *core.RegionInfo) {
if region.GetLeader() != nil && r.conn.nodeHealth(region.GetLeader().GetStoreId()) {
return
}
newLeader := r.electNewLeader(region)
newRegion := region.Clone(core.WithLeader(newLeader))
if newLeader == nil {
r.SetRegion(newRegion)
simutil.Logger.Infof("[region %d] no leader", region.GetID())
return
}
simutil.Logger.Infof("[region %d] elect new leader: %+v,old leader: %+v", region.GetID(), newLeader, region.GetLeader())
r.SetRegion(newRegion)
r.recordRegionChange(newRegion)
}
func (r *RaftEngine) stepSplit(region *core.RegionInfo) {
if region.GetLeader() == nil {
return
}
if !r.NeedSplit(region.GetApproximateSize(), region.GetApproximateKeys()) {
return
}
ids := make([]uint64, 1+len(region.GetPeers()))
for i := range ids {
var err error
ids[i], err = r.allocID(region.GetLeader().GetStoreId())
if err != nil {
simutil.Logger.Infof("alloc id failed: %s", err)
return
}
}
splitKey := generateSplitKey(region.GetStartKey(), region.GetEndKey())
left := region.Clone(
core.WithNewRegionID(ids[len(ids)-1]),
core.WithNewPeerIds(ids[0:len(ids)-1]...),
core.WithIncVersion(),
core.SetApproximateKeys(region.GetApproximateKeys()/2),
core.SetApproximateSize(region.GetApproximateSize()/2),
core.WithPendingPeers(nil),
core.WithDownPeers(nil),
core.WithEndKey(splitKey),
)
right := region.Clone(
core.WithIncVersion(),
core.SetApproximateKeys(region.GetApproximateKeys()/2),
core.SetApproximateSize(region.GetApproximateSize()/2),
core.WithStartKey(splitKey),
)
r.SetRegion(right)
r.SetRegion(left)
r.recordRegionChange(left)
r.recordRegionChange(right)
}
// NeedSplit checks whether the region needs to split according its size
// and number of keys.
func (r *RaftEngine) NeedSplit(size, rows int64) bool {
if r.regionSplitSize != 0 && size >= r.regionSplitSize {
return true
}
if r.regionSplitKeys != 0 && rows >= r.regionSplitKeys {
return true
}
return false
}
func (r *RaftEngine) recordRegionChange(region *core.RegionInfo) {
n := region.GetLeader().GetStoreId()
r.regionChange[n] = append(r.regionChange[n], region.GetID())
}
func (r *RaftEngine) updateRegionStore(region *core.RegionInfo, size int64) {
newRegion := region.Clone(
core.SetApproximateSize(region.GetApproximateSize()+size),
core.SetWrittenBytes(uint64(size)),
)
storeIDs := region.GetStoreIds()
for storeID := range storeIDs {
r.conn.Nodes[storeID].incUsedSize(uint64(size))
}
r.SetRegion(newRegion)
}
func (r *RaftEngine) updateRegionReadBytes(readBytes map[uint64]int64) {
for id, bytes := range readBytes {
region := r.GetRegion(id)
if region == nil {
simutil.Logger.Errorf("region %d not found", id)
continue
}
newRegion := region.Clone(core.SetReadBytes(uint64(bytes)))
r.SetRegion(newRegion)
}
}
func (r *RaftEngine) electNewLeader(region *core.RegionInfo) *metapb.Peer {
var (
unhealth int
newLeaderStoreID uint64
)
ids := region.GetStoreIds()
for id := range ids {
if r.conn.nodeHealth(id) {
newLeaderStoreID = id
} else {
unhealth++
}
}
if unhealth > len(ids)/2 {
return nil
}
for _, peer := range region.GetPeers() {
if peer.GetStoreId() == newLeaderStoreID {
return peer
}
}
return nil
}
// GetRegion returns the RegionInfo with regionID
func (r *RaftEngine) GetRegion(regionID uint64) *core.RegionInfo {
r.RLock()
defer r.RUnlock()
return r.regionsInfo.GetRegion(regionID)
}
// GetRegions gets all RegionInfo from regionMap
func (r *RaftEngine) GetRegions() []*core.RegionInfo {
r.RLock()
defer r.RUnlock()
return r.regionsInfo.GetRegions()
}
// SetRegion sets the RegionInfo with regionID
func (r *RaftEngine) SetRegion(region *core.RegionInfo) []*metapb.Region {
r.Lock()
defer r.Unlock()
return r.regionsInfo.SetRegion(region)
}
// SearchRegion searches the RegionInfo from regionTree
func (r *RaftEngine) SearchRegion(regionKey []byte) *core.RegionInfo {
r.RLock()
defer r.RUnlock()
return r.regionsInfo.SearchRegion(regionKey)
}
// RandRegion gets a region by random
func (r *RaftEngine) RandRegion() *core.RegionInfo {
r.RLock()
defer r.RUnlock()
return r.regionsInfo.RandRegion()
}
func (r *RaftEngine) allocID(storeID uint64) (uint64, error) {
node, ok := r.conn.Nodes[storeID]
if !ok {
return 0, errors.Errorf("node %d not found", storeID)
}
id, err := node.client.AllocID(context.Background())
return id, errors.WithStack(err)
}
const (
// 26^10 ~= 1.4e+14, should be enough.
keyChars = "abcdefghijklmnopqrstuvwxyz"
keyLen = 10
)
// generate ordered, unique strings.
func generateKeys(size int) []string {
m := make(map[string]struct{}, size)
for len(m) < size {
k := make([]byte, keyLen)
for i := range k {
k[i] = keyChars[rand.Intn(len(keyChars))]
}
m[string(k)] = struct{}{}
}
v := make([]string, 0, size)
for k := range m {
v = append(v, k)
}
sort.Strings(v)
return v
}
func generateSplitKey(start, end []byte) []byte {
var key []byte
// lessThanEnd is set as true when the key is already less than end key.
lessThanEnd := len(end) == 0
for i, s := range start {
e := byte('z')
if !lessThanEnd {
e = end[i]
}
c := (s + e) / 2
key = append(key, c)
// case1: s = c < e. Continue with lessThanEnd=true.
// case2: s < c < e. return key.
// case3: s = c = e. Continue with lessThanEnd=false.
lessThanEnd = c < e
if c > s && c < e {
return key
}
}
key = append(key, ('a'+'z')/2)
return key
}