forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_cluster.go
145 lines (127 loc) · 4.11 KB
/
range_cluster.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
// 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 schedule
import (
"bytes"
"github.com/pingcap/pd/server/core"
)
// RangeCluster isolates the cluster by range.
type RangeCluster struct {
Cluster
regions *core.RegionsInfo
tolerantSizeRatio float64
}
const scanLimit = 128
// GenRangeCluster gets a range cluster by specifying start key and end key.
func GenRangeCluster(cluster Cluster, startKey, endKey []byte) *RangeCluster {
regions := core.NewRegionsInfo()
scanKey := startKey
loopEnd := false
for !loopEnd {
collect := cluster.ScanRegions(scanKey, scanLimit)
if len(collect) == 0 {
break
}
for _, r := range collect {
if bytes.Compare(r.GetStartKey(), endKey) < 0 {
regions.SetRegion(r)
} else {
loopEnd = true
break
}
if string(r.GetEndKey()) == "" {
loopEnd = true
break
}
scanKey = r.GetEndKey()
}
}
return &RangeCluster{
Cluster: cluster,
regions: regions,
}
}
func (r *RangeCluster) updateStoreInfo(s *core.StoreInfo) {
id := s.GetId()
used := float64(s.Stats.GetUsedSize()) / (1 << 20)
if used == 0 {
return
}
amplification := float64(s.RegionSize) / used
s.LeaderCount = r.regions.GetStoreLeaderCount(id)
s.LeaderSize = r.regions.GetStoreLeaderRegionSize(id)
s.RegionCount = r.regions.GetStoreRegionCount(id)
s.RegionSize = r.regions.GetStoreRegionSize(id)
s.PendingPeerCount = r.regions.GetStorePendingPeerCount(id)
s.Stats.UsedSize = uint64(float64(s.RegionSize)/amplification) * (1 << 20)
s.Stats.Available = s.Stats.GetCapacity() - s.Stats.GetUsedSize()
}
// GetStore searches for a store by ID.
func (r *RangeCluster) GetStore(id uint64) *core.StoreInfo {
s := r.Cluster.GetStore(id)
r.updateStoreInfo(s)
return s
}
// GetStores returns all Stores in the cluster.
func (r *RangeCluster) GetStores() []*core.StoreInfo {
stores := r.Cluster.GetStores()
for _, s := range stores {
r.updateStoreInfo(s)
}
return stores
}
// SetTolerantSizeRatio sets the tolerant size ratio.
func (r *RangeCluster) SetTolerantSizeRatio(ratio float64) {
r.tolerantSizeRatio = ratio
}
// GetTolerantSizeRatio gets the tolerant size ratio.
func (r *RangeCluster) GetTolerantSizeRatio() float64 {
if r.tolerantSizeRatio != 0 {
return r.tolerantSizeRatio
}
return r.Cluster.GetTolerantSizeRatio()
}
// RandFollowerRegion returns a random region that has a follower on the store.
func (r *RangeCluster) RandFollowerRegion(storeID uint64, opts ...core.RegionOption) *core.RegionInfo {
return r.regions.RandFollowerRegion(storeID, opts...)
}
// RandLeaderRegion returns a random region that has leader on the store.
func (r *RangeCluster) RandLeaderRegion(storeID uint64, opts ...core.RegionOption) *core.RegionInfo {
return r.regions.RandLeaderRegion(storeID, opts...)
}
// GetAverageRegionSize returns the average region approximate size.
func (r *RangeCluster) GetAverageRegionSize() int64 {
return r.regions.GetAverageRegionSize()
}
// GetRegionStores returns all stores that contains the region's peer.
func (r *RangeCluster) GetRegionStores(region *core.RegionInfo) []*core.StoreInfo {
stores := r.Cluster.GetRegionStores(region)
for _, s := range stores {
r.updateStoreInfo(s)
}
return stores
}
// GetFollowerStores returns all stores that contains the region's follower peer.
func (r *RangeCluster) GetFollowerStores(region *core.RegionInfo) []*core.StoreInfo {
stores := r.Cluster.GetFollowerStores(region)
for _, s := range stores {
r.updateStoreInfo(s)
}
return stores
}
// GetLeaderStore returns all stores that contains the region's leader peer.
func (r *RangeCluster) GetLeaderStore(region *core.RegionInfo) *core.StoreInfo {
s := r.Cluster.GetLeaderStore(region)
r.updateStoreInfo(s)
return s
}