-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
482 lines (426 loc) · 10 KB
/
bucket.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package cbytecache
import (
"encoding/binary"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/koykov/byteconv"
"github.com/koykov/cbytebuf"
)
// Cache bucket.
type bucket struct {
config *Config
idx uint32
ids string
status uint32
maxCap uint32
size bucketSize
mux sync.RWMutex
// Internal buffer.
buf *cbytebuf.CByteBuf
// Entry index. Value point to the index in entry array.
index map[uint64]uint32
// Entries storage.
entry []entry
// Memory arenas.
queue arenaQueue
lastEvc, lastVac time.Time
}
// Make and init new bucket.
func newBucket(id uint32, config *Config, maxCap uint64) *bucket {
b := bucket{
config: config,
idx: id,
ids: strconv.Itoa(int(id)),
maxCap: uint32(maxCap),
buf: cbytebuf.NewCByteBuf(),
index: make(map[uint64]uint32),
}
b.queue.setHead(nil).setAct(nil).setTail(nil)
return &b
}
// Set p to bucket by h hash.
func (b *bucket) set(key string, h uint64, p []byte) (err error) {
if err = b.checkStatus(); err != nil {
return
}
b.mux.Lock()
err = b.setLF(key, h, p, 0)
b.mux.Unlock()
return
}
// Set m to bucket by h hash.
func (b *bucket) setm(key string, h uint64, m MarshallerTo) (err error) {
if err = b.checkStatus(); err != nil {
return
}
b.mux.Lock()
defer b.mux.Unlock()
// Use internal buffer to convert m to bytes before set.
b.buf.ResetLen()
if _, err = b.buf.WriteMarshallerTo(m); err != nil {
return
}
err = b.setLF(key, h, b.buf.Bytes(), 0)
return
}
// Internal setter. It works in lock-free mode thus need to guarantee thread-safety outside.
func (b *bucket) setLF(key string, h uint64, p []byte, expire uint32) (err error) {
var (
idx, pl uint32
e *entry
ok bool
stm = b.nowT()
)
defer b.buf.ResetLen()
// Try to get already existed entry.
if idx, ok = b.index[h]; ok {
if idx < b.elen() {
e = &b.entry[idx]
}
}
// Extend entry data with collision control data.
if p, pl, err = b.c7n(key, p); err != nil {
return
}
// Cache doesn't support of overwrite existing entries, but it's handy to use known existing entry for collision check.
if e != nil {
if b.config.CollisionCheck {
// Prepare space in buffer to get potentially collided entry.
bl := b.buf.Len()
if err = b.buf.GrowLen(bl + int(e.length)); err != nil {
return
}
p = b.buf.Bytes()[:bl] // update p due to possible buffer grow
dst := b.buf.Bytes()[bl:]
// Read entry key and payload.
var key1 string
if key1, dst, err = b.getLF(dst[:0], e, dummyMetrics); err != nil {
return
}
if key1 != key {
// Keys don't match - collision caught.
if b.l() != nil {
b.l().Printf("collision %d: keys \"%s\" and \"%s\"", h, key, key1)
}
b.mw().Collision(b.ids)
err = ErrEntryCollision
return
}
}
// Exit anyway.
err = ErrEntryExists
return
}
// Init alloc.
if b.queue.len() == 0 {
a := b.queue.alloc(nil, b.acap())
b.queue.setHead(a).setAct(a)
b.mw().Alloc(b.ids, b.acap())
b.size.snap(snapAlloc, b.acap())
}
// Get current arena.
a := b.queue.act()
startArena := a
arenaOffset, arenaRest := a.offset(), a.rest()
rest := uint32(len(p))
if arenaRest >= rest {
// Arena has enough space to write the entry.
a.write(p)
} else {
// Arena hasn't enough space - need share entry among arenas.
// New arena allocation will need, so check if it's possible.
_, full, _ := b.queue.stat()
if b.maxCap > 0 && full*b.acap()+b.acap() > b.maxCap {
// Allocation denied, thus stop write at all.
b.mw().NoSpace(b.ids)
return ErrNoSpace
}
// Share entry among current and next arenas.
mustWrite := arenaRest
for {
// Write entry bytes that fits to arena free space.
a.write(p[:mustWrite])
p = p[mustWrite:]
if rest -= mustWrite; rest == 0 {
// All entry bytes written.
break
}
// Switch to the next arena.
prev := a
a = a.next()
b.queue.setAct(a)
b.mw().Fill(b.ids, b.acap())
// Alloc new arena if needed.
if a == nil {
a = b.queue.alloc(prev, b.acap())
b.mw().Alloc(b.ids, b.acap())
prev.setNext(a)
b.queue.setAct(a).setTail(a)
b.size.snap(snapAlloc, b.acap())
}
// Calculate rest of bytes to write.
mustWrite = umin32(rest, b.acap())
}
}
// Create and register new entry.
e1 := entry{
hash: h,
offset: arenaOffset,
length: pl,
expire: expire,
aid: startArena.id,
qp: b.queue.ptr(),
}
if e1.expire == 0 {
e1.expire = uint32(b.config.Clock.Now().Add(b.config.ExpireInterval).Unix())
}
b.entry = append(b.entry, e1)
b.index[h] = b.elen() - 1
b.size.snap(snapSet, pl)
b.mw().Set(b.ids, b.nowT().Sub(stm))
return ErrOK
}
// Get entry by h hash.
func (b *bucket) get(dst []byte, h uint64, del bool) ([]byte, error) {
if err := b.checkStatus(); err != nil {
return dst, err
}
if del {
b.mux.Lock()
defer b.mux.Unlock()
} else {
b.mux.RLock()
defer b.mux.RUnlock()
}
var (
idx uint32
ok bool
stm = b.nowT()
)
if idx, ok = b.index[h]; !ok {
b.mw().Miss(b.ids)
return dst, ErrNotFound
}
if idx >= b.elen() {
b.mw().Miss(b.ids)
return dst, ErrNotFound
}
e := &b.entry[idx]
if e.expire < b.now() {
b.mw().Expire(b.ids)
return dst, ErrNotFound
}
var err error
_, dst, err = b.getLF(dst, e, b.mw())
if err == nil {
b.mw().Hit(b.ids, b.nowT().Sub(stm))
}
if del {
err = b.delLF(h)
}
return dst, err
}
// Internal getter. It works in lock-free mode thus need to guarantee thread-safety outside.
func (b *bucket) getLF(dst []byte, entry *entry, mw MetricsWriter) (string, []byte, error) {
// Get starting arena.
arenaOffset := entry.offset
a := entry.arena()
if a == nil {
mw.Miss(b.ids)
return "", dst, ErrNotFound
}
arenaRest := b.acap() - arenaOffset
if entry.offset+entry.length < b.acap() {
// Good case: entry doesn't share among arenas.
dst = append(dst, a.read(arenaOffset, entry.length)...)
} else {
// Walk over arenas and collect entry by parts.
rest := entry.length
for rest > 0 {
dst = append(dst, a.read(arenaOffset, arenaRest)...)
if rest -= arenaRest; rest == 0 {
break
}
a = a.next()
if a == nil {
mw.Corrupt(b.ids)
return "", dst, ErrEntryCorrupt
}
arenaOffset = 0
arenaRest = umin32(rest, b.acap())
}
}
if len(dst) < keySizeBytes {
return "", dst, ErrEntryCorrupt
}
l := len(dst)
kl := binary.LittleEndian.Uint16(dst[l-keySizeBytes:])
if l-2 <= int(kl) {
return "", dst, ErrEntryCorrupt
}
key := byteconv.B2S(dst[l-int(kl)-keySizeBytes : l-keySizeBytes])
return key, dst[:l-int(kl)-keySizeBytes], ErrOK
}
// Extend entry with collision control data.
func (b *bucket) c7n(key string, p []byte) ([]byte, uint32, error) {
pl := uint32(len(p))
var err error
// Write payload at start.
if _, err = b.buf.Write(p); err != nil {
return p, pl, err
}
// Write key.
if _, err = b.buf.WriteString(key); err != nil {
return p, pl, err
}
// Write key length to the last two bytes.
bl := b.buf.Len()
if err = b.buf.GrowLen(bl + keySizeBytes); err != nil {
return p, pl, err
}
kl := uint16(len(key))
binary.LittleEndian.PutUint16(b.buf.Bytes()[bl:], kl)
// p extends now with collision control data.
p = b.buf.Bytes()
pl = uint32(len(p))
return p, pl, err
}
// Delete entry from bucket index.
//
// Entry data will keep in the arenas and will
func (b *bucket) del(h uint64) error {
if err := b.checkStatus(); err != nil {
return err
}
b.mux.Lock()
defer b.mux.Unlock()
return b.delLF(h)
}
// Delete entry in lock-free mode.
func (b *bucket) delLF(h uint64) error {
idx, ok := b.index[h]
if !ok {
return nil
}
if idx >= b.elen() {
return nil
}
b.entry[idx].destroy()
delete(b.index, h)
b.mw().Del(b.ids)
return nil
}
// Reset bucket data.
//
// All allocated data and buffer will keep for further use.
func (b *bucket) reset() error {
if err := b.checkStatus(); err != nil {
return err
}
b.svcLock()
defer b.svcUnlock()
b.buf.ResetLen()
b.evictRange(len(b.entry))
b.entry = b.entry[:0]
return ErrOK
}
// Release bucket data.
//
// All allocated data (arenas and buffer) will release.
func (b *bucket) release() error {
if err := b.checkStatus(); err != nil {
return err
}
var c int
b.svcLock()
defer func() {
if b.l() != nil {
b.l().Printf("bucket #%d: release %d arenas", b.idx, c)
}
b.svcUnlock()
}()
b.buf.Release()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if b.elen() == 0 {
return
}
b.evictRange(len(b.entry))
b.entry = b.entry[:0]
}()
wg.Add(1)
go func() {
defer wg.Done()
a := b.queue.head()
for a != nil {
if a.full() {
a.reset()
b.mw().Reset(b.ids, b.acap())
}
c++
a.release()
b.mw().Release(b.ids, b.acap())
b.size.snap(snapRelease, b.acap())
a = a.next()
}
b.queue.setHead(nil).setAct(nil).setTail(nil)
}()
wg.Wait()
return ErrOK
}
// Lock bucket for service operation.
func (b *bucket) svcLock() {
atomic.StoreUint32(&b.status, bucketStatusService)
b.mux.Lock()
}
// Unlock bucket after service operation.
func (b *bucket) svcUnlock() {
b.mux.Unlock()
atomic.StoreUint32(&b.status, bucketStatusActive)
}
// Check bucket status.
//
// Possible errors are: bucket under service or bucket corrupt.
func (b *bucket) checkStatus() error {
if status := atomic.LoadUint32(&b.status); status != bucketStatusActive {
if status == bucketStatusService {
return ErrBucketService
}
if status == bucketStatusCorrupt {
return ErrBucketCorrupt
}
}
return ErrOK
}
// Return timestamp as uint32 value.
func (b *bucket) now() uint32 {
return uint32(b.config.Clock.Now().Unix())
}
// Return current time.
func (b *bucket) nowT() time.Time {
return b.config.Clock.Now()
}
// Return length of currently used arenas.
func (b *bucket) alen() uint32 {
return uint32(b.queue.len())
}
// Return length of collected entries.
func (b *bucket) elen() uint32 {
return uint32(len(b.entry))
}
// Shorthand metrics writer method.
func (b *bucket) mw() MetricsWriter {
return b.config.MetricsWriter
}
// Shorthand arena capacity method.
func (b *bucket) acap() uint32 {
return uint32(b.config.ArenaCapacity)
}
// Shorthand logger method.
func (b *bucket) l() Logger {
return b.config.Logger
}