-
Notifications
You must be signed in to change notification settings - Fork 1
/
hot_peer_cache.go
335 lines (296 loc) · 9.46 KB
/
hot_peer_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
package statistics
import (
"math"
"time"
"github.com/deepfabric/prophet/core"
"github.com/deepfabric/prophet/pb/metapb"
"github.com/deepfabric/prophet/util/movingaverage"
)
const (
topNN = 60
topNTTL = 3 * ResourceHeartBeatReportInterval * time.Second
hotThresholdRatio = 0.8
rollingWindowsSize = 5
hotResourceReportMinInterval = 3
hotResourceAntiCount = 2
)
var (
minHotThresholds = [2][dimLen]float64{
WriteFlow: {
byteDim: 1 * 1024,
keyDim: 32,
},
ReadFlow: {
byteDim: 8 * 1024,
keyDim: 128,
},
}
)
// hotPeerCache saves the hot peer's statistics.
type hotPeerCache struct {
kind FlowKind
peersOfContainer map[uint64]*TopN // containerID -> hot peers
containersOfResource map[uint64]map[uint64]struct{} // resourceID -> containerIDs
}
func newHotContainersStats(kind FlowKind) *hotPeerCache {
return &hotPeerCache{
kind: kind,
peersOfContainer: make(map[uint64]*TopN),
containersOfResource: make(map[uint64]map[uint64]struct{}),
}
}
// ResourceStats returns hot items
func (f *hotPeerCache) ResourceStats() map[uint64][]*HotPeerStat {
res := make(map[uint64][]*HotPeerStat)
for containerID, peers := range f.peersOfContainer {
values := peers.GetAll()
stat := make([]*HotPeerStat, len(values))
res[containerID] = stat
for i := range values {
stat[i] = values[i].(*HotPeerStat)
}
}
return res
}
// Update updates the items in statistics.
func (f *hotPeerCache) Update(item *HotPeerStat) {
if item.IsNeedDelete() {
if peers, ok := f.peersOfContainer[item.ContainerID]; ok {
peers.Remove(item.ResourceID)
}
if containers, ok := f.containersOfResource[item.ResourceID]; ok {
delete(containers, item.ContainerID)
}
} else {
peers, ok := f.peersOfContainer[item.ContainerID]
if !ok {
peers = NewTopN(dimLen, topNN, topNTTL)
f.peersOfContainer[item.ContainerID] = peers
}
peers.Put(item)
containers, ok := f.containersOfResource[item.ResourceID]
if !ok {
containers = make(map[uint64]struct{})
f.containersOfResource[item.ResourceID] = containers
}
containers[item.ContainerID] = struct{}{}
}
}
func (f *hotPeerCache) collectResourceMetrics(byteRate, keyRate float64, interval uint64) {
resourceHeartbeatIntervalHist.Observe(float64(interval))
if interval == 0 {
return
}
if f.kind == ReadFlow {
readByteHist.Observe(byteRate)
readKeyHist.Observe(keyRate)
}
if f.kind == WriteFlow {
writeByteHist.Observe(byteRate)
writeKeyHist.Observe(keyRate)
}
}
// CheckResourceFlow checks the flow information of resource.
func (f *hotPeerCache) CheckResourceFlow(res *core.CachedResource) (ret []*HotPeerStat) {
bytes := float64(f.getResourceBytes(res))
keys := float64(f.getResourceKeys(res))
reportInterval := res.GetInterval()
interval := reportInterval.GetEnd() - reportInterval.GetStart()
byteRate := bytes / float64(interval)
keyRate := keys / float64(interval)
f.collectResourceMetrics(byteRate, keyRate, interval)
// old resource is in the front and new resource is in the back
// which ensures it will hit the cache if moving peer or transfer leader occurs with the same replica number
var tmpItem *HotPeerStat
containerIDs := f.getAllContainerIDs(res)
for _, containerID := range containerIDs {
isExpired := f.isResourceExpired(res, containerID) // transfer read leader or remove write peer
oldItem := f.getOldHotPeerStat(res.Meta.ID(), containerID)
if isExpired && oldItem != nil { // it may has been moved to other container, we save it to tmpItem
tmpItem = oldItem
}
// This is used for the simulator. Ignore if report too fast.
if !isExpired && Denoising && interval < hotResourceReportMinInterval {
continue
}
newItem := &HotPeerStat{
ContainerID: containerID,
ResourceID: res.Meta.ID(),
Kind: f.kind,
ByteRate: byteRate,
KeyRate: keyRate,
LastUpdateTime: time.Now(),
needDelete: isExpired,
isLeader: res.GetLeader().GetContainerID() == containerID,
}
if oldItem == nil {
if tmpItem != nil { // use the tmpItem cached from the container where this resource was in before
oldItem = tmpItem
} else { // new item is new peer after adding replica
for _, containerID := range containerIDs {
oldItem = f.getOldHotPeerStat(res.Meta.ID(), containerID)
if oldItem != nil {
break
}
}
}
}
newItem = f.updateHotPeerStat(newItem, oldItem, bytes, keys, time.Duration(interval))
if newItem != nil {
ret = append(ret, newItem)
}
}
return ret
}
func (f *hotPeerCache) IsResourceHot(res *core.CachedResource, hotDegree int) bool {
switch f.kind {
case WriteFlow:
return f.isResourceHotWithAnyPeers(res, hotDegree)
case ReadFlow:
return f.isResourceHotWithPeer(res, res.GetLeader(), hotDegree)
}
return false
}
func (f *hotPeerCache) CollectMetrics(typ string) {
for containerID, peers := range f.peersOfContainer {
container := containerTag(containerID)
thresholds := f.calcHotThresholds(containerID)
hotCacheStatusGauge.WithLabelValues("total_length", container, typ).Set(float64(peers.Len()))
hotCacheStatusGauge.WithLabelValues("byte-rate-threshold", container, typ).Set(thresholds[byteDim])
hotCacheStatusGauge.WithLabelValues("key-rate-threshold", container, typ).Set(thresholds[keyDim])
// for compatibility
hotCacheStatusGauge.WithLabelValues("hotThreshold", container, typ).Set(thresholds[byteDim])
}
}
func (f *hotPeerCache) getResourceBytes(res *core.CachedResource) uint64 {
switch f.kind {
case WriteFlow:
return res.GetBytesWritten()
case ReadFlow:
return res.GetBytesRead()
}
return 0
}
func (f *hotPeerCache) getResourceKeys(res *core.CachedResource) uint64 {
switch f.kind {
case WriteFlow:
return res.GetKeysWritten()
case ReadFlow:
return res.GetKeysRead()
}
return 0
}
func (f *hotPeerCache) getOldHotPeerStat(resID, containerID uint64) *HotPeerStat {
if hotPeers, ok := f.peersOfContainer[containerID]; ok {
if v := hotPeers.Get(resID); v != nil {
return v.(*HotPeerStat)
}
}
return nil
}
func (f *hotPeerCache) isResourceExpired(res *core.CachedResource, containerID uint64) bool {
switch f.kind {
case WriteFlow:
_, ok := res.GetContainerPeer(containerID)
return !ok
case ReadFlow:
return res.GetLeader().GetContainerID() != containerID
}
return false
}
func (f *hotPeerCache) calcHotThresholds(containerID uint64) [dimLen]float64 {
minThresholds := minHotThresholds[f.kind]
tn, ok := f.peersOfContainer[containerID]
if !ok || tn.Len() < topNN {
return minThresholds
}
ret := [dimLen]float64{
byteDim: tn.GetTopNMin(byteDim).(*HotPeerStat).GetByteRate(),
keyDim: tn.GetTopNMin(keyDim).(*HotPeerStat).GetKeyRate(),
}
for k := 0; k < dimLen; k++ {
ret[k] = math.Max(ret[k]*hotThresholdRatio, minThresholds[k])
}
return ret
}
// gets the containerIDs, including old resource and new resource
func (f *hotPeerCache) getAllContainerIDs(res *core.CachedResource) []uint64 {
containerIDs := make(map[uint64]struct{})
ret := make([]uint64, 0, len(res.Meta.Peers()))
// old containers
ids, ok := f.containersOfResource[res.Meta.ID()]
if ok {
for containerID := range ids {
containerIDs[containerID] = struct{}{}
ret = append(ret, containerID)
}
}
// new containers
for _, peer := range res.Meta.Peers() {
// ReadFlow no need consider the followers.
if f.kind == ReadFlow && peer.ContainerID != res.GetLeader().GetContainerID() {
continue
}
if _, ok := containerIDs[peer.ContainerID]; !ok {
containerIDs[peer.ContainerID] = struct{}{}
ret = append(ret, peer.ContainerID)
}
}
return ret
}
func (f *hotPeerCache) isResourceHotWithAnyPeers(res *core.CachedResource, hotDegree int) bool {
for _, peer := range res.Meta.Peers() {
if f.isResourceHotWithPeer(res, &peer, hotDegree) {
return true
}
}
return false
}
func (f *hotPeerCache) isResourceHotWithPeer(res *core.CachedResource, peer *metapb.Peer, hotDegree int) bool {
if peer == nil {
return false
}
containerID := peer.GetContainerID()
if peers, ok := f.peersOfContainer[containerID]; ok {
if stat := peers.Get(res.Meta.ID()); stat != nil {
return stat.(*HotPeerStat).HotDegree >= hotDegree
}
}
return false
}
func (f *hotPeerCache) getDefaultTimeMedian() *movingaverage.TimeMedian {
return movingaverage.NewTimeMedian(DefaultAotSize, rollingWindowsSize, ResourceHeartBeatReportInterval)
}
func (f *hotPeerCache) updateHotPeerStat(newItem, oldItem *HotPeerStat, bytes, keys float64, interval time.Duration) *HotPeerStat {
thresholds := f.calcHotThresholds(newItem.ContainerID)
isHot := newItem.ByteRate >= thresholds[byteDim] || // if interval is zero, rate will be NaN, isHot will be false
newItem.KeyRate >= thresholds[keyDim]
if newItem.needDelete {
return newItem
}
if oldItem != nil {
newItem.rollingByteRate = oldItem.rollingByteRate
newItem.rollingKeyRate = oldItem.rollingKeyRate
if isHot {
newItem.HotDegree = oldItem.HotDegree + 1
newItem.AntiCount = hotResourceAntiCount
} else if interval != 0 {
newItem.HotDegree = oldItem.HotDegree - 1
newItem.AntiCount = oldItem.AntiCount - 1
if newItem.AntiCount <= 0 {
newItem.needDelete = true
}
}
} else {
if !isHot {
return nil
}
newItem.rollingByteRate = f.getDefaultTimeMedian()
newItem.rollingKeyRate = f.getDefaultTimeMedian()
newItem.AntiCount = hotResourceAntiCount
newItem.isNew = true
}
newItem.rollingByteRate.Add(bytes, interval*time.Second)
newItem.rollingKeyRate.Add(keys, interval*time.Second)
return newItem
}