-
Notifications
You must be signed in to change notification settings - Fork 179
/
herocache.go
460 lines (388 loc) · 19 KB
/
herocache.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
package metrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/network"
)
const subsystemHeroCache = "hero_cache"
var _ module.HeroCacheMetrics = (*HeroCacheCollector)(nil)
type HeroCacheCollector struct {
histogramNormalizedBucketSlotAvailable prometheus.Histogram
countKeyGetSuccess prometheus.Counter
countKeyGetFailure prometheus.Counter
countKeyPutSuccess prometheus.Counter
countKeyPutDrop prometheus.Counter
countKeyPutDeduplicated prometheus.Counter
countKeyPutAttempt prometheus.Counter
countKeyRemoved prometheus.Counter
size prometheus.Gauge
countKeyEjectionDueToFullCapacity prometheus.Counter
countKeyEjectionDueToEmergency prometheus.Counter
}
type HeroCacheMetricsRegistrationFunc func(uint64) module.HeroCacheMetrics
// HeroCacheMetricsFactory is a factory method to create a new HeroCacheCollector for a specific cache
// with a specific namespace and a specific name.
// Args:
// - namespace: the namespace of the cache
// - cacheName: the name of the cache
type HeroCacheMetricsFactory func(namespace string, cacheName string) module.HeroCacheMetrics
// NewHeroCacheMetricsFactory creates a new HeroCacheMetricsFactory for the given registrar. It allows to defer the
// registration of the metrics to the point where the cache is created without exposing the registrar to the cache.
// Args:
// - registrar: the prometheus registrar to register the metrics with
// Returns:
// - a HeroCacheMetricsFactory that can be used to create a new HeroCacheCollector for a specific cache
func NewHeroCacheMetricsFactory(registrar prometheus.Registerer) HeroCacheMetricsFactory {
return func(namespace string, cacheName string) module.HeroCacheMetrics {
return NewHeroCacheCollector(namespace, cacheName, registrar)
}
}
// NewNoopHeroCacheMetricsFactory creates a new HeroCacheMetricsFactory that returns a noop collector.
// This is useful for tests that don't want to register metrics.
// Args:
// - none
// Returns:
// - a HeroCacheMetricsFactory that returns a noop collector
func NewNoopHeroCacheMetricsFactory() HeroCacheMetricsFactory {
return func(string, string) module.HeroCacheMetrics {
return NewNoopCollector()
}
}
func NetworkReceiveCacheMetricsFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingReceiveCache
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func NewSubscriptionRecordCacheMetricsFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingSubscriptionRecordsCache
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
// NewGossipSubApplicationSpecificScoreCacheMetrics is the factory method for creating a new HeroCacheCollector for the
// application specific score cache of the GossipSub peer scoring module. The application specific score cache is used
// to keep track of the application specific score of peers in GossipSub.
// Args:
// - f: the HeroCacheMetricsFactory to create the collector
// Returns:
// - a HeroCacheMetrics for the application specific score cache
func NewGossipSubApplicationSpecificScoreCacheMetrics(f HeroCacheMetricsFactory, networkingType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingGossipSubApplicationSpecificScoreCache
if networkingType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
// DisallowListCacheMetricsFactory is the factory method for creating a new HeroCacheCollector for the disallow list cache.
// The disallow-list cache is used to keep track of peers that are disallow-listed and the reasons for it.
// Args:
// - f: the HeroCacheMetricsFactory to create the collector
// - networkingType: the networking type of the cache, i.e., whether it is used for the public or the private network
// Returns:
// - a HeroCacheMetrics for the disallow list cache
func DisallowListCacheMetricsFactory(f HeroCacheMetricsFactory, networkingType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingDisallowListCache
if networkingType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
// GossipSubSpamRecordCacheMetricsFactory is the factory method for creating a new HeroCacheCollector for the spam record cache.
// The spam record cache is used to keep track of peers that are spamming the network and the reasons for it.
func GossipSubSpamRecordCacheMetricsFactory(f HeroCacheMetricsFactory, networkingType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingGossipSubSpamRecordCache
if networkingType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func NetworkDnsTxtCacheMetricsFactory(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceNetwork, ResourceNetworkingDnsTxtCache, registrar)
}
func NetworkDnsIpCacheMetricsFactory(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceNetwork, ResourceNetworkingDnsIpCache, registrar)
}
func ChunkDataPackRequestQueueMetricsFactory(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceExecution, ResourceChunkDataPackRequests, registrar)
}
func ReceiptRequestsQueueMetricFactory(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceExecution, ResourceReceipt, registrar)
}
func CollectionRequestsQueueMetricFactory(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceCollection, ResourceCollection, registrar)
}
func DisallowListNotificationQueueMetricFactory(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceNetwork, ResourceNetworkingDisallowListNotificationQueue, registrar)
}
func ApplicationLayerSpamRecordCacheMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingApplicationLayerSpamRecordCache
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func DialConfigCacheMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingUnicastDialConfigCache
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func ApplicationLayerSpamRecordQueueMetricsFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingApplicationLayerSpamReportQueue
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func GossipSubRPCInspectorQueueMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
// we don't use the public prefix for the metrics here for sake of backward compatibility of metric name.
r := ResourceNetworkingRpcValidationInspectorQueue
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func GossipSubRPCSentTrackerMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
// we don't use the public prefix for the metrics here for sake of backward compatibility of metric name.
r := ResourceNetworkingRPCSentTrackerCache
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func GossipSubRPCSentTrackerQueueMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
// we don't use the public prefix for the metrics here for sake of backward compatibility of metric name.
r := ResourceNetworkingRPCSentTrackerQueue
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func RpcInspectorNotificationQueueMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingRpcInspectorNotificationQueue
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func GossipSubRPCInspectorClusterPrefixedCacheMetricFactory(f HeroCacheMetricsFactory, networkType network.NetworkingType) module.HeroCacheMetrics {
// we don't use the public prefix for the metrics here for sake of backward compatibility of metric name.
r := ResourceNetworkingRpcClusterPrefixReceivedCache
if networkType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
// GossipSubAppSpecificScoreUpdateQueueMetricFactory is the factory method for creating a new HeroCacheCollector for the
// app-specific score update queue of the GossipSub peer scoring module. The app-specific score update queue is used to
// queue the update requests for the app-specific score of peers. The update requests are queued in a worker pool and
// processed asynchronously.
// Args:
// - f: the HeroCacheMetricsFactory to create the collector
// Returns:
// - a HeroCacheMetrics for the app-specific score update queue.
func GossipSubAppSpecificScoreUpdateQueueMetricFactory(f HeroCacheMetricsFactory, networkingType network.NetworkingType) module.HeroCacheMetrics {
r := ResourceNetworkingAppSpecificScoreUpdateQueue
if networkingType == network.PublicNetwork {
r = PrependPublicPrefix(r)
}
return f(namespaceNetwork, r)
}
func CollectionNodeTransactionsCacheMetrics(registrar prometheus.Registerer, epoch uint64) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceCollection, fmt.Sprintf("%s_%d", ResourceTransaction, epoch), registrar)
}
func FollowerCacheMetrics(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceFollowerEngine, ResourceFollowerPendingBlocksCache, registrar)
}
func AccessNodeExecutionDataCacheMetrics(registrar prometheus.Registerer) *HeroCacheCollector {
return NewHeroCacheCollector(namespaceAccess, ResourceExecutionDataCache, registrar)
}
// PrependPublicPrefix prepends the string "public" to the given string.
// This is used to distinguish between public and private metrics.
// Args:
// - str: the string to prepend, example: "my_metric"
// Returns:
// - the prepended string, example: "public_my_metric"
func PrependPublicPrefix(str string) string {
return fmt.Sprintf("%s_%s", "public", str)
}
func NewHeroCacheCollector(nameSpace string, cacheName string, registrar prometheus.Registerer) *HeroCacheCollector {
histogramNormalizedBucketSlotAvailable := prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
// Note that the notion of "bucket" in HeroCache differs from Prometheus.
// A HeroCache "bucket" is used to group the keys of the entities.
// A Prometheus "bucket" is used to group collected data points within a range.
// This metric represents the histogram of normalized available slots in buckets, where
// a data point set to 1 represents a bucket with all slots available (i.e., a fully empty bucket),
// and a data point set to 0 means a bucket with no available slots (i.e., a completely full bucket).
//
// We generally set total slots of a bucket in HeroCache to 16. Hence:
// Prometheus bucket 1 represents total number of HeroCache buckets with at most 16 available slots.
// Prometheus bucket 0.75 represents total number of HeroCache buckets with at most 12 available slots.
// Prometheus bucket 0.5 represents total number of HeroCache buckets with at most 8 available slots.
// Prometheus bucket 0.25 represents total number of HeroCache buckets with at most 4 available slots.
// Prometheus bucket 0.1 represents total number of HeroCache buckets with at most 1 available slots.
// Prometheus bucket 0 represents total number of HeroCache buckets with no (i.e., zero) available slots.
Buckets: []float64{0, 0.1, 0.25, 0.5, 0.75, 1},
Name: cacheName + "_" + "normalized_bucket_available_slot_count",
Help: "normalized histogram of available slots across all buckets",
})
size := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "items_total",
Help: "total number of items in the cache",
})
countKeyGetSuccess := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "successful_read_count_total",
Help: "total number of successful read queries",
})
countKeyGetFailure := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "unsuccessful_read_count_total",
Help: "total number of unsuccessful read queries",
})
countKeyPutAttempt := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "write_attempt_count_total",
Help: "total number of put queries",
})
countKeyPutDrop := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "write_drop_count_total",
Help: "total number of put queries dropped due to full capacity",
})
countKeyPutSuccess := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "successful_write_count_total",
Help: "total number successful write queries",
})
countKeyPutDeduplicated := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "unsuccessful_write_count_total",
Help: "total number of queries writing an already existing (duplicate) entity to the cache",
})
countKeyRemoved := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "removed_count_total",
Help: "total number of entities removed from the cache",
})
countKeyEjectionDueToFullCapacity := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "full_capacity_entity_ejection_total",
Help: "total number of entities ejected when writing new entities at full capacity",
})
countKeyEjectionDueToEmergency := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: nameSpace,
Subsystem: subsystemHeroCache,
Name: cacheName + "_" + "emergency_key_ejection_total",
Help: "total number of emergency key ejections at bucket level",
})
registrar.MustRegister(
// available slot distribution
histogramNormalizedBucketSlotAvailable,
// size
size,
// read
countKeyGetSuccess,
countKeyGetFailure,
// write
countKeyPutSuccess,
countKeyPutDeduplicated,
countKeyPutDrop,
countKeyPutAttempt,
// remove
countKeyRemoved,
// ejection
countKeyEjectionDueToFullCapacity,
countKeyEjectionDueToEmergency)
return &HeroCacheCollector{
histogramNormalizedBucketSlotAvailable: histogramNormalizedBucketSlotAvailable,
size: size,
countKeyGetSuccess: countKeyGetSuccess,
countKeyGetFailure: countKeyGetFailure,
countKeyPutAttempt: countKeyPutAttempt,
countKeyPutSuccess: countKeyPutSuccess,
countKeyPutDeduplicated: countKeyPutDeduplicated,
countKeyPutDrop: countKeyPutDrop,
countKeyRemoved: countKeyRemoved,
countKeyEjectionDueToFullCapacity: countKeyEjectionDueToFullCapacity,
countKeyEjectionDueToEmergency: countKeyEjectionDueToEmergency,
}
}
// BucketAvailableSlots keeps track of number of available slots in buckets of cache.
func (h *HeroCacheCollector) BucketAvailableSlots(availableSlots uint64, totalSlots uint64) {
normalizedAvailableSlots := float64(availableSlots) / float64(totalSlots)
h.histogramNormalizedBucketSlotAvailable.Observe(normalizedAvailableSlots)
}
// OnKeyPutSuccess is called whenever a new (key, entity) pair is successfully added to the cache.
// size parameter is the current size of the cache post insertion.
func (h *HeroCacheCollector) OnKeyPutSuccess(size uint32) {
h.countKeyPutSuccess.Inc()
h.size.Set(float64(size))
}
// OnKeyPutDeduplicated is tracking the total number of unsuccessful writes caused by adding a duplicate key to the cache.
// A duplicate key is dropped by the cache when it is written to the cache.
// Note: in context of HeroCache, the key corresponds to the identifier of its entity. Hence, a duplicate key corresponds to
// a duplicate entity.
func (h *HeroCacheCollector) OnKeyPutDeduplicated() {
h.countKeyPutDeduplicated.Inc()
}
// OnKeyGetSuccess tracks total number of successful read queries.
// A read query is successful if the entity corresponding to its key is available in the cache.
// Note: in context of HeroCache, the key corresponds to the identifier of its entity.
func (h *HeroCacheCollector) OnKeyGetSuccess() {
h.countKeyGetSuccess.Inc()
}
// OnKeyGetFailure tracks total number of unsuccessful read queries.
// A read query is unsuccessful if the entity corresponding to its key is not available in the cache.
// Note: in context of HeroCache, the key corresponds to the identifier of its entity.
func (h *HeroCacheCollector) OnKeyGetFailure() {
h.countKeyGetFailure.Inc()
}
// OnKeyPutAttempt is called whenever a new (key, value) pair is attempted to be put in cache.
// It does not reflect whether the put was successful or not.
// A (key, value) pair put attempt may fail if the cache is full, or the key already exists.
// size parameter is the current size of the cache prior to the put attempt.
func (h *HeroCacheCollector) OnKeyPutAttempt(size uint32) {
h.countKeyPutAttempt.Inc()
h.size.Set(float64(size))
}
// OnKeyPutDrop is called whenever a new (key, entity) pair is dropped from the cache due to full cache.
func (h *HeroCacheCollector) OnKeyPutDrop() {
h.countKeyPutDrop.Inc()
}
// OnKeyRemoved is called whenever a (key, entity) pair is removed from the cache.
// size parameter is the current size of the cache.
func (h *HeroCacheCollector) OnKeyRemoved(size uint32) {
h.countKeyRemoved.Inc()
h.size.Set(float64(size))
}
// OnEntityEjectionDueToFullCapacity is called whenever adding a new (key, entity) to the cache results in ejection of another (key', entity') pair.
// This normally happens -- and is expected -- when the cache is full.
// Note: in context of HeroCache, the key corresponds to the identifier of its entity.
func (h *HeroCacheCollector) OnEntityEjectionDueToFullCapacity() {
h.countKeyEjectionDueToFullCapacity.Inc()
}
// OnEntityEjectionDueToEmergency is called whenever a bucket is found full and all of its keys are valid, i.e.,
// each key belongs to an existing (key, entity) pair.
// Hence, adding a new key to that bucket will replace the oldest valid key inside that bucket.
// Note: in context of HeroCache, the key corresponds to the identifier of its entity.
func (h *HeroCacheCollector) OnEntityEjectionDueToEmergency() {
h.countKeyEjectionDueToEmergency.Inc()
}