-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathslidingbloomfilter.go
445 lines (359 loc) · 11.3 KB
/
slidingbloomfilter.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
package rueidisprob
import (
"context"
"errors"
"strconv"
"time"
"github.com/redis/rueidis"
)
const (
slidingBloomFilterInitializeScript = `
local filterKey = KEYS[1]
local nextFilterKey = KEYS[2]
local counterKey = KEYS[3]
local nextCounterKey = KEYS[4]
local lastRotationKey = KEYS[5]
local windowHalf = tonumber(ARGV[1])
if redis.call('EXISTS', filterKey, nextFilterKey, counterKey, nextCounterKey, lastRotationKey) == 0 then
local time = redis.call('TIME')
local current_time = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
redis.call('MSET', filterKey, "", counterKey, 0, nextFilterKey, "", nextCounterKey, 0)
redis.call('SET', lastRotationKey, tostring(current_time), 'PX', windowHalf, 'NX')
end
return 1
`
slidingBloomFilterAddMultiScript = `
local hashIterations = tonumber(ARGV[1])
local windowHalf = tonumber(ARGV[2])
local numElements = tonumber(#ARGV) - 2
local filterKey = KEYS[1]
local nextFilterKey = KEYS[2]
local counterKey = KEYS[3]
local nextCounterKey = KEYS[4]
local lastRotationKey = KEYS[5]
local time = redis.call('TIME')
local current_time = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2])/1000)
local acquiredLock = redis.call('SET', lastRotationKey, tostring(current_time), 'PX', windowHalf, 'NX')
if acquiredLock then
redis.call('RENAME', nextFilterKey, filterKey)
redis.call('RENAME', nextCounterKey, counterKey)
redis.call('SET', nextFilterKey, "")
redis.call('SET', nextCounterKey, 0)
end
local counter = 0
local oneBits = 0
for i=1, numElements do
local bitset = redis.call('BITFIELD', filterKey, 'SET', 'u1', ARGV[i+2], '1')
redis.call('BITFIELD', nextFilterKey, 'SET', 'u1', ARGV[i+2], '1')
oneBits = oneBits + bitset[1]
if i % hashIterations == 0 then
if oneBits ~= hashIterations then
counter = counter + 1
end
oneBits = 0
end
end
redis.call('INCRBY', nextCounterKey, counter)
return redis.call('INCRBY', counterKey, counter)
`
slidingBloomFilterExistsMultiScript = `
local hashIterations = tonumber(ARGV[1])
local windowHalf = tonumber(ARGV[2])
local numElements = tonumber(#ARGV) - 2
local filterKey = KEYS[1]
local nextFilterKey = KEYS[2]
local counterKey = KEYS[3]
local nextCounterKey = KEYS[4]
local lastRotationKey = KEYS[5]
local time = redis.call('TIME')
local current_time = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2])/1000)
local acquiredLock = redis.call('SET', lastRotationKey, tostring(current_time), 'PX', windowHalf, 'NX')
if acquiredLock then
redis.call('RENAME', nextFilterKey, filterKey)
redis.call('RENAME', nextCounterKey, counterKey)
redis.call('SET', nextFilterKey, "")
redis.call('SET', nextCounterKey, 0)
end
local result = {}
local oneBits = 0
for i=1, numElements do
local index = tonumber(ARGV[i+2])
local bitset = redis.call('BITFIELD', filterKey, 'GET', 'u1', index)
oneBits = oneBits + bitset[1]
if i % hashIterations == 0 then
table.insert(result, oneBits == hashIterations)
oneBits = 0
end
end
return result
`
slidingBloomFilterExistsReadOnlyMultiScript = `
local hashIterations = tonumber(ARGV[1])
local windowHalf = tonumber(ARGV[2])
local numElements = tonumber(#ARGV) - 2
local filterKey = KEYS[1]
local nextFilterKey = KEYS[2]
local counterKey = KEYS[3]
local nextCounterKey = KEYS[4]
local lastRotationKey = KEYS[5]
local time = redis.call('TIME')
local current_time = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2])/1000)
local acquiredLock = redis.call('SET', lastRotationKey, tostring(current_time), 'PX', windowHalf, 'NX')
if acquiredLock then
redis.call('RENAME', nextFilterKey, filterKey)
redis.call('RENAME', nextCounterKey, counterKey)
redis.call('SET', nextFilterKey, "")
redis.call('SET', nextCounterKey, 0)
end
local result = {}
local oneBits = 0
for i=1, numElements do
local index = tonumber(ARGV[i+2])
local bitset = redis.call('BITFIELD_RO', filterKey, 'GET', 'u1', index)
oneBits = oneBits + bitset[1]
if i % hashIterations == 0 then
table.insert(result, oneBits == hashIterations)
oneBits = 0
end
end
return result
`
slidingBloomFilterResetScript = `
local filterKey = KEYS[1]
local nextFilterKey = KEYS[2]
local counterKey = KEYS[3]
local nextCounterKey = KEYS[4]
redis.call('RENAME', nextFilterKey, filterKey)
redis.call('RENAME', nextCounterKey, counterKey)
redis.call('SET', nextFilterKey, "")
redis.call('SET', nextCounterKey, 0)
`
// Redis key suffixes
counterSuffix = ":c"
nextFilterSuffix = ":n"
nextCounterSuffix = ":nc"
lastRotationSuffix = ":lr"
)
var (
_ BloomFilter = (*slidingBloomFilter)(nil)
ErrWindowSizeLessThanOneSecond = errors.New("window size cannot be less than 1 second")
)
type SlidingBloomFilterOptions struct {
enableReadOperation bool
}
type SlidingBloomFilterOptionFunc func(o *SlidingBloomFilterOptions)
func WithReadOnlyExists(enableReadOperations bool) SlidingBloomFilterOptionFunc {
return func(o *SlidingBloomFilterOptions) {
o.enableReadOperation = enableReadOperations
}
}
type slidingBloomFilter struct {
client rueidis.Client
addMultiScript *rueidis.Lua
existsMultiScript *rueidis.Lua
// Pre-calculated window half in milliseconds
windowHalfMs string
// name is the name of the sliding Bloom filter.
// It is used as a key in the Redis.
name string
// counter is the name of the counter.
counter string
hashIterationString string
addMultiKeys []string
existsMultiKeys []string
// window is the duration of the sliding window.
window time.Duration
// hashIterations is the number of hash functions to use.
hashIterations uint
// size is the number of bits to use.
size uint
}
// NewSlidingBloomFilter creates a new sliding window Bloom filter.
// NOTE: 'name:c' is used as a counter key in the Redis
// 'name:n' is used as a next filter key in the Redis
// 'name:nc' is used as a next counter key in the Redis
// 'name:lr' is used as a last rotation key in the Redis
// to keep track of the items in the window.
func NewSlidingBloomFilter(
redisClient rueidis.Client,
name string,
expectedNumberOfItems uint,
falsePositiveRate float64,
windowSize time.Duration,
opts ...SlidingBloomFilterOptionFunc,
) (BloomFilter, error) {
if len(name) == 0 {
return nil, ErrEmptyName
}
if falsePositiveRate <= 0 {
return nil, ErrFalsePositiveRateLessThanEqualZero
}
if falsePositiveRate > 1 {
return nil, ErrFalsePositiveRateGreaterThanOne
}
if windowSize < time.Second {
return nil, ErrWindowSizeLessThanOneSecond
}
size := numberOfBloomFilterBits(expectedNumberOfItems, falsePositiveRate)
if size == 0 {
return nil, ErrBitsSizeZero
}
if size > maxSize {
return nil, ErrBitsSizeTooLarge
}
hashIterations := numberOfBloomFilterHashFunctions(size, expectedNumberOfItems)
options := &SlidingBloomFilterOptions{}
for _, opt := range opts {
opt(options)
}
var existsMultiScript string
if options.enableReadOperation {
existsMultiScript = slidingBloomFilterExistsReadOnlyMultiScript
} else {
existsMultiScript = slidingBloomFilterExistsMultiScript
}
// NOTE: https://redis.io/docs/reference/cluster-spec/#hash-tags
bfName := "{" + name + "}"
counterName := bfName + counterSuffix
nextFilterName := bfName + nextFilterSuffix
nextCounterName := bfName + nextCounterSuffix
lastRotationName := bfName + lastRotationSuffix
s := &slidingBloomFilter{
client: redisClient,
name: bfName,
counter: counterName,
window: windowSize,
windowHalfMs: strconv.FormatInt(windowSize.Milliseconds()/2, 10),
hashIterations: hashIterations,
hashIterationString: strconv.FormatUint(uint64(hashIterations), 10),
size: size,
addMultiScript: rueidis.NewLuaScript(slidingBloomFilterAddMultiScript),
addMultiKeys: []string{bfName, nextFilterName, counterName, nextCounterName, lastRotationName},
existsMultiScript: rueidis.NewLuaScript(existsMultiScript),
existsMultiKeys: []string{bfName, nextFilterName, counterName, nextCounterName, lastRotationName},
}
err := s.initialize()
if err != nil {
return nil, err
}
return s, nil
}
func (s *slidingBloomFilter) initialize() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
initializeScript := rueidis.NewLuaScript(slidingBloomFilterInitializeScript)
resp := initializeScript.Exec(ctx, s.client, s.addMultiKeys, []string{s.windowHalfMs})
if resp.Error() != nil && !rueidis.IsRedisNil(resp.Error()) {
return resp.Error()
}
v, err := resp.AsInt64()
if err != nil {
return err
}
if v != 1 {
return errors.New("failed to initialize sliding Bloom filter")
}
return nil
}
func (s *slidingBloomFilter) Add(ctx context.Context, key string) error {
return s.AddMulti(ctx, []string{key})
}
func (s *slidingBloomFilter) AddMulti(ctx context.Context, keys []string) error {
if len(keys) == 0 {
return nil
}
buf := bytesPool.Get(0, len(keys)*int(s.hashIterations)*8)
defer bytesPool.Put(buf)
indexes := s.indexes(keys, &buf.s)
args := make([]string, 0, len(indexes)+2)
args = append(args, s.hashIterationString)
args = append(args, s.windowHalfMs)
args = append(args, indexes...)
resp := s.addMultiScript.Exec(ctx, s.client, s.addMultiKeys, args)
return resp.Error()
}
func (s *slidingBloomFilter) indexes(keys []string, buf *[]byte) []string {
allIndexes := make([]string, 0, len(keys)*int(s.hashIterations))
size := uint64(s.size)
for _, key := range keys {
h1, h2 := hash([]byte(key))
for i := uint(0); i < s.hashIterations; i++ {
offset := len(*buf)
*buf = strconv.AppendUint(*buf, index(h1, h2, i, size), 10)
allIndexes = append(allIndexes, rueidis.BinaryString((*buf)[offset:]))
}
}
return allIndexes
}
func (s *slidingBloomFilter) Exists(ctx context.Context, key string) (bool, error) {
exists, err := s.ExistsMulti(ctx, []string{key})
if err != nil {
return false, err
}
return exists[0], nil
}
func (s *slidingBloomFilter) ExistsMulti(ctx context.Context, keys []string) ([]bool, error) {
if len(keys) == 0 {
return nil, nil
}
buf := bytesPool.Get(0, len(keys)*int(s.hashIterations)*8)
defer bytesPool.Put(buf)
indexes := s.indexes(keys, &buf.s)
args := make([]string, 0, len(indexes)+2)
args = append(args, s.hashIterationString)
args = append(args, s.windowHalfMs)
args = append(args, indexes...)
resp := s.existsMultiScript.Exec(ctx, s.client, s.existsMultiKeys, args)
if resp.Error() != nil {
return nil, resp.Error()
}
arr, err := resp.ToArray()
if err != nil {
return nil, err
}
result := make([]bool, len(keys))
for i, el := range arr {
v, err := el.AsBool()
if err != nil {
if rueidis.IsRedisNil(err) {
result[i] = false
continue
}
return nil, err
}
result[i] = v
}
return result, nil
}
func (s *slidingBloomFilter) Reset(ctx context.Context) error {
resp := s.client.Do(ctx,
s.client.B().
Eval().
Script(slidingBloomFilterResetScript).
Numkeys(4).
Key(s.addMultiKeys...).
Build(),
)
return resp.Error()
}
func (s *slidingBloomFilter) Delete(ctx context.Context) error {
resp := s.client.Do(ctx, s.client.B().Del().Key(s.addMultiKeys...).Build())
return resp.Error()
}
func (s *slidingBloomFilter) Count(ctx context.Context) (uint64, error) {
resp := s.client.Do(
ctx,
s.client.B().
Get().
Key(s.counter).
Build(),
)
count, err := resp.AsUint64()
if err != nil {
if rueidis.IsRedisNil(err) {
return 0, nil
}
return 0, err
}
return count, nil
}