-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom_filter.go
196 lines (168 loc) · 4.74 KB
/
bloom_filter.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
package multi_tier_caching
import (
"log"
"sync"
"time"
"github.com/bits-and-blooms/bloom/v3"
"github.com/prometheus/client_golang/prometheus"
)
type BloomFilter struct {
debug bool
filter *bloom.BloomFilter
mu sync.Mutex
analytics *CacheAnalytics
increaseFactor float64
decreaseFactor float64
maxSize uint
minSize uint
lastAdjustment time.Time
stopChan chan struct{}
}
func NewBloomFilter(size uint, hashFuncs uint, debug bool, analytics *CacheAnalytics) *BloomFilter {
prometheus.MustRegister(
bloomCapacityGauge,
bloomCountGauge,
bloomHashFunctionsGauge,
bloomFalsePositiveGauge,
bloomLoadFactorGauge,
bloomLastAdjustmentGauge,
)
bloomFilter := &BloomFilter{
filter: bloom.New(size, hashFuncs),
debug: debug,
analytics: analytics,
increaseFactor: 1.5,
decreaseFactor: 0.7,
maxSize: 100000,
minSize: 1000,
lastAdjustment: time.Now(),
stopChan: make(chan struct{}),
}
go bloomFilter.metricsUpdater()
return bloomFilter
}
func (b *BloomFilter) Add(key string) {
b.mu.Lock()
defer b.mu.Unlock()
b.filter.AddString(key)
if b.debug {
log.Printf("[BLOOM] Added key: %s", key)
}
// Checking the load and changing the filter size
b.adjustFilterSize()
}
func (b *BloomFilter) Exists(key string) bool {
b.mu.Lock()
defer b.mu.Unlock()
exists := b.filter.TestString(key)
if b.debug {
log.Printf("[BLOOM] Check key=%s, exists=%v", key, exists)
}
return exists
}
func (b *BloomFilter) adjustFilterSize() {
if time.Since(b.lastAdjustment) < 20*time.Second {
if b.debug {
log.Printf("[BLOOM] Skipping adjustment, lastAdjustment was %v", b.lastAdjustment)
}
return // Adjust maximum once per minute
}
currentSize := b.filter.Cap()
metrics := b.analytics.GetFrequencyPerMinute()
totalRequests := 0
for _, v := range metrics {
totalRequests += v
}
// Check cache miss rate
misses := getCounterValue(b.analytics.cacheMisses)
hits := getCounterValueVec(b.analytics.cacheHits)
// Calculate desired size based on metrics
desiredSize := uint(float64(currentSize) * b.calculateAdjustmentFactor(totalRequests, misses, hits))
// Apply boundaries
if desiredSize > b.maxSize {
desiredSize = b.maxSize
} else if desiredSize < b.minSize {
desiredSize = b.minSize
}
if desiredSize != currentSize {
b.resizeFilter(desiredSize, metrics)
b.lastAdjustment = time.Now()
if b.debug {
log.Printf("[BLOOM] Resized filter to %d, lastAdjustment updated to %s", desiredSize, b.lastAdjustment.Format("15:04:05"))
}
}
}
func (b *BloomFilter) calculateAdjustmentFactor(totalRequests int, misses float64, hits float64) float64 {
if totalRequests == 0 {
return 1.0
}
missRate := misses / (hits + misses)
loadFactor := float64(len(b.analytics.keys)) / float64(b.filter.Cap())
// Increase size if high miss rate or load factor > 0.75
if missRate > 0.1 || loadFactor > 0.75 {
return b.increaseFactor
}
// Decrease size if low utilization and miss rate
if loadFactor < 0.25 && missRate < 0.05 {
return b.decreaseFactor
}
return 1.0
}
func (b *BloomFilter) resizeFilter(newSize uint, frequencies map[string]int) {
newFilter := bloom.New(newSize, b.filter.K())
// Dynamic threshold based on average number of requests
total := 0
for _, count := range frequencies {
total += count
}
avg := total / len(frequencies)
threshold := avg // or another coefficient, for example, avg / 2
for key, count := range frequencies {
if count > threshold {
newFilter.AddString(key)
}
}
b.mu.Lock()
defer b.mu.Unlock()
b.filter = newFilter
if b.debug {
log.Printf("[BLOOM] Resized filter from %d to %d, kept %d keys",
b.filter.Cap(), newSize, len(frequencies))
}
}
func (b *BloomFilter) metricsUpdater() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
//b.adjustFilterSize()
b.UpdateMetrics()
case <-b.stopChan:
return
}
}
}
func (b *BloomFilter) UpdateMetrics() {
b.mu.Lock()
defer b.mu.Unlock()
capacity := float64(b.filter.Cap())
count := float64(b.filter.ApproximatedSize())
hashFuncs := float64(b.filter.K())
falsePositiveRate := estimateFalsePositiveRate(b.filter.K(), b.filter.Cap(), uint(count))
loadFactor := count / capacity
lastAdjustmentTS := float64(b.lastAdjustment.Unix())
bloomCapacityGauge.Set(capacity)
bloomCountGauge.Set(count)
bloomHashFunctionsGauge.Set(hashFuncs)
bloomFalsePositiveGauge.Set(falsePositiveRate)
bloomLoadFactorGauge.Set(loadFactor)
bloomLastAdjustmentGauge.Set(lastAdjustmentTS)
if b.debug {
log.Printf("[BLOOM] Metrics updated - "+
"Capacity: %.0f, Count: %.0f, Hashes: %.0f, "+
"FPR: %.6f, Load: %.4f, LastAdj: %d",
capacity, count, hashFuncs,
falsePositiveRate, loadFactor, int64(lastAdjustmentTS))
}
}