/
range_cluster.go
144 lines (128 loc) · 4.75 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
// 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 (
"github.com/gogo/protobuf/proto"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/v3/server/core"
"github.com/pingcap/pd/v3/server/schedule/opt"
)
// RangeCluster isolates the cluster by range.
type RangeCluster struct {
opt.Cluster
subCluster *core.BasicCluster // Collect all regions belong to the range.
tolerantSizeRatio float64
}
// GenRangeCluster gets a range cluster by specifying start key and end key.
// The cluster can only know the regions within [startKey, endKey].
func GenRangeCluster(cluster opt.Cluster, startKey, endKey []byte) *RangeCluster {
subCluster := core.NewBasicCluster()
for _, r := range cluster.ScanRegions(startKey, endKey, -1) {
subCluster.Regions.AddRegion(r)
}
return &RangeCluster{
Cluster: cluster,
subCluster: subCluster,
}
}
func (r *RangeCluster) updateStoreInfo(s *core.StoreInfo) *core.StoreInfo {
id := s.GetID()
used := float64(s.GetUsedSize()) / (1 << 20)
if used == 0 {
return s
}
amplification := float64(s.GetRegionSize()) / used
leaderCount := r.subCluster.GetStoreLeaderCount(id)
leaderSize := r.subCluster.GetStoreLeaderRegionSize(id)
regionCount := r.subCluster.GetStoreRegionCount(id)
regionSize := r.subCluster.GetStoreRegionSize(id)
pendingPeerCount := r.subCluster.GetStorePendingPeerCount(id)
newStats := proto.Clone(s.GetStoreStats()).(*pdpb.StoreStats)
newStats.UsedSize = uint64(float64(regionSize)/amplification) * (1 << 20)
newStats.Available = s.GetCapacity() - newStats.UsedSize
newStore := s.Clone(
core.SetStoreStats(newStats),
core.SetLeaderCount(leaderCount),
core.SetRegionCount(regionCount),
core.SetPendingPeerCount(pendingPeerCount),
core.SetLeaderSize(leaderSize),
core.SetRegionSize(regionSize),
)
return newStore
}
// GetStore searches for a store by ID.
func (r *RangeCluster) GetStore(id uint64) *core.StoreInfo {
s := r.Cluster.GetStore(id)
if s == nil {
return nil
}
return r.updateStoreInfo(s)
}
// GetStores returns all Stores in the cluster.
func (r *RangeCluster) GetStores() []*core.StoreInfo {
stores := r.Cluster.GetStores()
newStores := make([]*core.StoreInfo, 0, len(stores))
for _, s := range stores {
newStores = append(newStores, r.updateStoreInfo(s))
}
return newStores
}
// 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, ranges []core.KeyRange, opts ...core.RegionOption) *core.RegionInfo {
return r.subCluster.RandFollowerRegion(storeID, ranges, opts...)
}
// RandLeaderRegion returns a random region that has leader on the store.
func (r *RangeCluster) RandLeaderRegion(storeID uint64, ranges []core.KeyRange, opts ...core.RegionOption) *core.RegionInfo {
return r.subCluster.RandLeaderRegion(storeID, ranges, opts...)
}
// GetAverageRegionSize returns the average region approximate size.
func (r *RangeCluster) GetAverageRegionSize() int64 {
return r.subCluster.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)
newStores := make([]*core.StoreInfo, 0, len(stores))
for _, s := range stores {
newStores = append(newStores, r.updateStoreInfo(s))
}
return newStores
}
// 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)
newStores := make([]*core.StoreInfo, 0, len(stores))
for _, s := range stores {
newStores = append(newStores, r.updateStoreInfo(s))
}
return newStores
}
// 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)
if s != nil {
return r.updateStoreInfo(s)
}
return s
}