forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
575 lines (503 loc) · 15.9 KB
/
cache.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
564
565
566
567
568
569
570
571
572
573
574
575
// 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 server
import (
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gogo/protobuf/proto"
"github.com/juju/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/server/cache"
"github.com/pingcap/pd/server/core"
)
var (
errRegionNotFound = func(regionID uint64) error {
return errors.Errorf("region %v not found", regionID)
}
errRegionIsStale = func(region *metapb.Region, origin *metapb.Region) error {
return errors.Errorf("region is stale: region %v origin %v", region, origin)
}
)
type clusterInfo struct {
sync.RWMutex
id core.IDAllocator
kv *core.KV
meta *metapb.Cluster
stores *core.StoresInfo
regions *core.RegionsInfo
activeRegions int
writeStatistics cache.Cache
readStatistics cache.Cache
}
func newClusterInfo(id core.IDAllocator) *clusterInfo {
return &clusterInfo{
id: id,
stores: core.NewStoresInfo(),
regions: core.NewRegionsInfo(),
writeStatistics: cache.NewCache(statCacheMaxLen, cache.TwoQueueCache),
readStatistics: cache.NewCache(statCacheMaxLen, cache.TwoQueueCache),
}
}
// Return nil if cluster is not bootstrapped.
func loadClusterInfo(id core.IDAllocator, kv *core.KV) (*clusterInfo, error) {
c := newClusterInfo(id)
c.kv = kv
c.meta = &metapb.Cluster{}
ok, err := kv.LoadMeta(c.meta)
if err != nil {
return nil, errors.Trace(err)
}
if !ok {
return nil, nil
}
start := time.Now()
if err := kv.LoadStores(c.stores, kvRangeLimit); err != nil {
return nil, errors.Trace(err)
}
log.Infof("load %v stores cost %v", c.stores.GetStoreCount(), time.Since(start))
start = time.Now()
if err := kv.LoadRegions(c.regions, kvRangeLimit); err != nil {
return nil, errors.Trace(err)
}
log.Infof("load %v regions cost %v", c.regions.GetRegionCount(), time.Since(start))
return c, nil
}
func (c *clusterInfo) allocID() (uint64, error) {
return c.id.Alloc()
}
// AllocPeer allocs a new peer on a store.
func (c *clusterInfo) AllocPeer(storeID uint64) (*metapb.Peer, error) {
peerID, err := c.allocID()
if err != nil {
log.Errorf("failed to alloc peer: %v", err)
return nil, errors.Trace(err)
}
peer := &metapb.Peer{
Id: peerID,
StoreId: storeID,
}
return peer, nil
}
func (c *clusterInfo) getClusterID() uint64 {
c.RLock()
defer c.RUnlock()
return c.meta.GetId()
}
func (c *clusterInfo) getMeta() *metapb.Cluster {
c.RLock()
defer c.RUnlock()
return proto.Clone(c.meta).(*metapb.Cluster)
}
func (c *clusterInfo) putMeta(meta *metapb.Cluster) error {
c.Lock()
defer c.Unlock()
return c.putMetaLocked(proto.Clone(meta).(*metapb.Cluster))
}
func (c *clusterInfo) putMetaLocked(meta *metapb.Cluster) error {
if c.kv != nil {
if err := c.kv.SaveMeta(meta); err != nil {
return errors.Trace(err)
}
}
c.meta = meta
return nil
}
// GetStore searches for a store by ID.
func (c *clusterInfo) GetStore(storeID uint64) *core.StoreInfo {
c.RLock()
defer c.RUnlock()
return c.stores.GetStore(storeID)
}
func (c *clusterInfo) putStore(store *core.StoreInfo) error {
c.Lock()
defer c.Unlock()
return c.putStoreLocked(store.Clone())
}
func (c *clusterInfo) putStoreLocked(store *core.StoreInfo) error {
if c.kv != nil {
if err := c.kv.SaveStore(store.Store); err != nil {
return errors.Trace(err)
}
}
c.stores.SetStore(store)
return nil
}
// BlockStore stops balancer from selecting the store.
func (c *clusterInfo) BlockStore(storeID uint64) error {
c.Lock()
defer c.Unlock()
return errors.Trace(c.stores.BlockStore(storeID))
}
// UnblockStore allows balancer to select the store.
func (c *clusterInfo) UnblockStore(storeID uint64) {
c.Lock()
defer c.Unlock()
c.stores.UnblockStore(storeID)
}
// GetStores returns all stores in the cluster.
func (c *clusterInfo) GetStores() []*core.StoreInfo {
c.RLock()
defer c.RUnlock()
return c.stores.GetStores()
}
func (c *clusterInfo) getMetaStores() []*metapb.Store {
c.RLock()
defer c.RUnlock()
return c.stores.GetMetaStores()
}
func (c *clusterInfo) getStoreCount() int {
c.RLock()
defer c.RUnlock()
return c.stores.GetStoreCount()
}
func (c *clusterInfo) getStoresWriteStat() map[uint64]uint64 {
c.RLock()
defer c.RUnlock()
return c.stores.GetStoresWriteStat()
}
func (c *clusterInfo) getStoresReadStat() map[uint64]uint64 {
c.RLock()
defer c.RUnlock()
return c.stores.GetStoresReadStat()
}
// GetRegions searches for a region by ID.
func (c *clusterInfo) GetRegion(regionID uint64) *core.RegionInfo {
c.RLock()
defer c.RUnlock()
return c.regions.GetRegion(regionID)
}
// updateWriteStatCache updates statistic for a region if it's hot, or remove it from statistics if it cools down
func (c *clusterInfo) updateWriteStatCache(region *core.RegionInfo, hotRegionThreshold uint64) {
var v *core.RegionStat
key := region.GetId()
value, isExist := c.writeStatistics.Peek(key)
newItem := &core.RegionStat{
RegionID: region.GetId(),
FlowBytes: region.WrittenBytes,
LastUpdateTime: time.Now(),
StoreID: region.Leader.GetStoreId(),
Version: region.GetRegionEpoch().GetVersion(),
AntiCount: hotRegionAntiCount,
}
if isExist {
v = value.(*core.RegionStat)
newItem.HotDegree = v.HotDegree + 1
}
if region.WrittenBytes < hotRegionThreshold {
if !isExist {
return
}
if v.AntiCount <= 0 {
c.writeStatistics.Remove(key)
return
}
// eliminate some noise
newItem.HotDegree = v.HotDegree - 1
newItem.AntiCount = v.AntiCount - 1
newItem.FlowBytes = v.FlowBytes
}
c.writeStatistics.Put(key, newItem)
}
// updateReadStatCache updates statistic for a region if it's hot, or remove it from statistics if it cools down
func (c *clusterInfo) updateReadStatCache(region *core.RegionInfo, hotRegionThreshold uint64) {
var v *core.RegionStat
key := region.GetId()
value, isExist := c.readStatistics.Peek(key)
newItem := &core.RegionStat{
RegionID: region.GetId(),
FlowBytes: region.ReadBytes,
LastUpdateTime: time.Now(),
StoreID: region.Leader.GetStoreId(),
Version: region.GetRegionEpoch().GetVersion(),
AntiCount: hotRegionAntiCount,
}
if isExist {
v = value.(*core.RegionStat)
newItem.HotDegree = v.HotDegree + 1
}
if region.ReadBytes < hotRegionThreshold {
if !isExist {
return
}
if v.AntiCount <= 0 {
c.readStatistics.Remove(key)
return
}
// eliminate some noise
newItem.HotDegree = v.HotDegree - 1
newItem.AntiCount = v.AntiCount - 1
newItem.FlowBytes = v.FlowBytes
}
c.readStatistics.Put(key, newItem)
}
// RegionWriteStats returns hot region's write stats.
func (c *clusterInfo) RegionWriteStats() []*core.RegionStat {
elements := c.writeStatistics.Elems()
stats := make([]*core.RegionStat, len(elements))
for i := range elements {
stats[i] = elements[i].Value.(*core.RegionStat)
}
return stats
}
// RegionReadStats returns hot region's read stats.
func (c *clusterInfo) RegionReadStats() []*core.RegionStat {
elements := c.readStatistics.Elems()
stats := make([]*core.RegionStat, len(elements))
for i := range elements {
stats[i] = elements[i].Value.(*core.RegionStat)
}
return stats
}
// IsRegionHot checks if a region is in hot state.
func (c *clusterInfo) IsRegionHot(id uint64) bool {
c.RLock()
defer c.RUnlock()
if stat, ok := c.writeStatistics.Peek(id); ok {
return stat.(*core.RegionStat).HotDegree >= hotRegionLowThreshold
}
return false
}
func (c *clusterInfo) searchRegion(regionKey []byte) *core.RegionInfo {
c.RLock()
defer c.RUnlock()
return c.regions.SearchRegion(regionKey)
}
func (c *clusterInfo) putRegion(region *core.RegionInfo) error {
c.Lock()
defer c.Unlock()
return c.putRegionLocked(region.Clone())
}
func (c *clusterInfo) putRegionLocked(region *core.RegionInfo) error {
if c.kv != nil {
if err := c.kv.SaveRegion(region.Region); err != nil {
return errors.Trace(err)
}
}
c.regions.SetRegion(region)
return nil
}
func (c *clusterInfo) getRegions() []*core.RegionInfo {
c.RLock()
defer c.RUnlock()
return c.regions.GetRegions()
}
func (c *clusterInfo) randomRegion() *core.RegionInfo {
c.RLock()
defer c.RUnlock()
return c.regions.RandRegion()
}
func (c *clusterInfo) getMetaRegions() []*metapb.Region {
c.RLock()
defer c.RUnlock()
return c.regions.GetMetaRegions()
}
func (c *clusterInfo) getRegionCount() int {
c.RLock()
defer c.RUnlock()
return c.regions.GetRegionCount()
}
func (c *clusterInfo) getStoreRegionCount(storeID uint64) int {
c.RLock()
defer c.RUnlock()
return c.regions.GetStoreRegionCount(storeID)
}
func (c *clusterInfo) getStoreLeaderCount(storeID uint64) int {
c.RLock()
defer c.RUnlock()
return c.regions.GetStoreLeaderCount(storeID)
}
// RandLeaderRegion returns a random region that has leader on the store.
func (c *clusterInfo) RandLeaderRegion(storeID uint64) *core.RegionInfo {
c.RLock()
defer c.RUnlock()
return c.regions.RandLeaderRegion(storeID)
}
// RandFolowerRegion returns a random region that has a follower on the store.
func (c *clusterInfo) RandFollowerRegion(storeID uint64) *core.RegionInfo {
c.RLock()
defer c.RUnlock()
return c.regions.RandFollowerRegion(storeID)
}
// GetRegionStores returns all stores that contains the region's peer.
func (c *clusterInfo) GetRegionStores(region *core.RegionInfo) []*core.StoreInfo {
c.RLock()
defer c.RUnlock()
var stores []*core.StoreInfo
for id := range region.GetStoreIds() {
if store := c.stores.GetStore(id); store != nil {
stores = append(stores, store)
}
}
return stores
}
// GetRegionStores returns all stores that contains the region's leader peer.
func (c *clusterInfo) GetLeaderStore(region *core.RegionInfo) *core.StoreInfo {
c.RLock()
defer c.RUnlock()
return c.stores.GetStore(region.Leader.GetStoreId())
}
// GetRegionStores returns all stores that contains the region's follower peer.
func (c *clusterInfo) GetFollowerStores(region *core.RegionInfo) []*core.StoreInfo {
c.RLock()
defer c.RUnlock()
var stores []*core.StoreInfo
for id := range region.GetFollowers() {
if store := c.stores.GetStore(id); store != nil {
stores = append(stores, store)
}
}
return stores
}
// isPrepared if the cluster information is collected
func (c *clusterInfo) isPrepared() bool {
c.RLock()
defer c.RUnlock()
return float64(c.regions.Length())*collectFactor <= float64(c.activeRegions)
}
// handleStoreHeartbeat updates the store status.
func (c *clusterInfo) handleStoreHeartbeat(stats *pdpb.StoreStats) error {
c.Lock()
defer c.Unlock()
storeID := stats.GetStoreId()
store := c.stores.GetStore(storeID)
if store == nil {
return errors.Trace(core.ErrStoreNotFound(storeID))
}
store.Stats = proto.Clone(stats).(*pdpb.StoreStats)
store.LastHeartbeatTS = time.Now()
c.stores.SetStore(store)
return nil
}
func (c *clusterInfo) updateStoreStatus(id uint64) {
c.stores.SetLeaderCount(id, c.regions.GetStoreLeaderCount(id))
c.stores.SetRegionCount(id, c.regions.GetStoreRegionCount(id))
}
// handleRegionHeartbeat updates the region information.
func (c *clusterInfo) handleRegionHeartbeat(region *core.RegionInfo) error {
region = region.Clone()
c.RLock()
origin := c.regions.GetRegion(region.GetId())
c.RUnlock()
// Save to KV if meta is updated.
// Save to cache if meta or leader is updated, or contains any down/pending peer.
// Mark isNew if the region in cache does not have leader.
var saveKV, saveCache, isNew bool
if origin == nil {
log.Infof("[region %d] Insert new region {%v}", region.GetId(), region)
saveKV, saveCache, isNew = true, true, true
} else {
r := region.GetRegionEpoch()
o := origin.GetRegionEpoch()
// Region meta is stale, return an error.
if r.GetVersion() < o.GetVersion() || r.GetConfVer() < o.GetConfVer() {
return errors.Trace(errRegionIsStale(region.Region, origin.Region))
}
if r.GetVersion() > o.GetVersion() {
log.Infof("[region %d] %s, Version changed from {%d} to {%d}", region.GetId(), core.DiffRegionKeyInfo(origin, region), o.GetVersion(), r.GetVersion())
saveKV, saveCache = true, true
}
if r.GetConfVer() > o.GetConfVer() {
log.Infof("[region %d] %s, ConfVer changed from {%d} to {%d}", region.GetId(), core.DiffRegionPeersInfo(origin, region), o.GetConfVer(), r.GetConfVer())
saveKV, saveCache = true, true
}
if region.Leader.GetId() != origin.Leader.GetId() {
log.Infof("[region %d] Leader changed from {%v} to {%v}", region.GetId(), origin.GetPeer(origin.Leader.GetId()), region.GetPeer(region.Leader.GetId()))
if origin.Leader.GetId() == 0 {
isNew = true
}
saveCache = true
}
if len(region.DownPeers) > 0 || len(region.PendingPeers) > 0 {
saveCache = true
}
if len(origin.DownPeers) > 0 || len(origin.PendingPeers) > 0 {
saveCache = true
}
}
if saveKV && c.kv != nil {
if err := c.kv.SaveRegion(region.Region); err != nil {
// Not successfully saved to kv is not fatal, it only leads to longer warm-up
// after restart. Here we only log the error then go on updating cache.
log.Errorf("[region %d] fail to save region %v: %v", region.GetId(), region, err)
}
}
c.Lock()
defer c.Unlock()
if isNew {
c.activeRegions++
}
if saveCache {
c.regions.SetRegion(region)
// Update related stores.
if origin != nil {
for _, p := range origin.Peers {
c.updateStoreStatus(p.GetStoreId())
}
}
for _, p := range region.Peers {
c.updateStoreStatus(p.GetStoreId())
}
}
c.updateWriteStatus(region)
c.updateReadStatus(region)
return nil
}
func (c *clusterInfo) updateWriteStatus(region *core.RegionInfo) {
var WrittenBytesPerSec uint64
v, isExist := c.writeStatistics.Peek(region.GetId())
if isExist {
interval := time.Since(v.(*core.RegionStat).LastUpdateTime).Seconds()
if interval < minHotRegionReportInterval {
return
}
WrittenBytesPerSec = uint64(float64(region.WrittenBytes) / interval)
} else {
WrittenBytesPerSec = uint64(float64(region.WrittenBytes) / float64(regionHeartBeatReportInterval))
}
region.WrittenBytes = WrittenBytesPerSec
// hotRegionThreshold is use to pick hot region
// suppose the number of the hot regions is statCacheMaxLen
// and we use total written Bytes past storeHeartBeatReportInterval seconds to divide the number of hot regions
// divide 2 because the store reports data about two times than the region record write to rocksdb
divisor := float64(statCacheMaxLen) * 2 * storeHeartBeatReportInterval
hotRegionThreshold := uint64(float64(c.stores.TotalWrittenBytes()) / divisor)
if hotRegionThreshold < hotWriteRegionMinFlowRate {
hotRegionThreshold = hotWriteRegionMinFlowRate
}
c.updateWriteStatCache(region, hotRegionThreshold)
}
func (c *clusterInfo) updateReadStatus(region *core.RegionInfo) {
var ReadBytesPerSec uint64
v, isExist := c.readStatistics.Peek(region.GetId())
if isExist {
interval := time.Now().Sub(v.(*core.RegionStat).LastUpdateTime).Seconds()
if interval < minHotRegionReportInterval {
return
}
ReadBytesPerSec = uint64(float64(region.ReadBytes) / interval)
} else {
ReadBytesPerSec = uint64(float64(region.ReadBytes) / float64(regionHeartBeatReportInterval))
}
region.ReadBytes = ReadBytesPerSec
// hotRegionThreshold is use to pick hot region
// suppose the number of the hot regions is statLRUMaxLen
// and we use total written Bytes past storeHeartBeatReportInterval seconds to divide the number of hot regions
divisor := float64(statCacheMaxLen) * storeHeartBeatReportInterval
hotRegionThreshold := uint64(float64(c.stores.TotalReadBytes()) / divisor)
if hotRegionThreshold < hotReadRegionMinFlowRate {
hotRegionThreshold = hotReadRegionMinFlowRate
}
c.updateReadStatCache(region, hotRegionThreshold)
}