-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegment.go
435 lines (362 loc) · 9.84 KB
/
segment.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
package forget
import (
"errors"
"sync"
"time"
)
type segment struct {
mx *sync.RWMutex
readDoneCond *sync.Cond
memory *memory
deleteQueue *list
lruLookup map[string]*list
lruRotate *list
itemLookup [][]*item
closed bool
stats *segmentStats
}
var errAllocationFailed = errors.New("allocation for key failed")
func newSegment(chunkCount, chunkSize int, notify *notify) *segment {
return &segment{
mx: &sync.RWMutex{},
readDoneCond: sync.NewCond(&sync.Mutex{}),
memory: newMemory(chunkCount, chunkSize),
lruLookup: make(map[string]*list),
lruRotate: &list{},
deleteQueue: &list{},
itemLookup: make([][]*item, chunkCount), // there cannot be more entries than chunks
stats: newSegmentStats(chunkSize, chunkCount*chunkSize, notify),
}
}
// returns the index of the lookup bucket based on an item's hash
func (s *segment) bucketIndex(hash uint64) int {
return int(hash % uint64(len(s.itemLookup)))
}
// finds an item based on the hash, keyspace and key
func (s *segment) lookup(hash uint64, keyspace, key string) (*item, bool) {
for _, i := range s.itemLookup[s.bucketIndex(hash)] {
if i.keyspace == keyspace && i.keyEquals(key) {
return i, true
}
}
return nil, false
}
// stores an item in the right lookup bucket
func (s *segment) addLookup(hash uint64, i *item) {
index := s.bucketIndex(hash)
s.itemLookup[index] = append(s.itemLookup[index], i)
}
// deletes the lookup entry of an item
func (s *segment) deleteLookup(i *item) {
bi := s.bucketIndex(i.hash)
bucket := s.itemLookup[bi]
for bii, ii := range bucket {
if ii == i {
last := len(bucket) - 1
bucket[last], bucket[bii], bucket = nil, bucket[last], bucket[:last]
s.itemLookup[bi] = bucket
return
}
}
}
// returns the LRU list for a keyspace. If it doesn't exist, creates it and stores it
func (s *segment) keyspaceLRU(keyspace string) *list {
lru := s.lruLookup[keyspace]
if lru == nil {
lru = &list{}
s.lruLookup[keyspace] = lru
s.lruRotate.insert(lru, nil)
}
return lru
}
// removes an LRU list
func (s *segment) removeKeyspaceLRU(lru *list, keyspace string) {
delete(s.lruLookup, keyspace)
s.lruRotate.remove(lru)
}
// deletes an item from an LRU list
func (s *segment) deleteFromLRU(i *item) {
lru := s.lruLookup[i.keyspace]
lru.remove(i)
if lru.empty() {
s.removeKeyspaceLRU(lru, i.keyspace)
}
}
// checks if an item can be deleted immediately or needs to be marked for delete
func canDelete(i *item) bool {
return !i.writeComplete || i.readers == 0
}
// releases the memory used by an item
func (s *segment) freeMemory(i *item) {
first, last := i.data()
if first != nil {
s.memory.free(first, last)
}
}
// deletes an item or marks it for delete
func (s *segment) deleteItem(i *item) bool {
s.deleteLookup(i)
s.deleteFromLRU(i)
if canDelete(i) {
s.freeMemory(i)
i.close()
return true
}
s.deleteQueue.insert(i, nil)
return false
}
// checks if an item can be evicted
func canEvict(i *item) bool {
return i.size > 0 && (!i.writeComplete || i.readers == 0)
}
// evicts one item from the items marked for delete if possible
func (s *segment) evictFromDeleted() bool {
current := s.deleteQueue.first
for current != nil {
i := current.(*item)
if !canEvict(i) {
current = current.next()
continue
}
s.deleteQueue.remove(i)
s.freeMemory(i)
i.close()
s.stats.deleteItem(i)
s.stats.decMarkedDeleted(i.keyspace)
s.stats.notifyEvict(i.keyspace, i.size)
return true
}
return false
}
// tries to evict one item from an lru list, other than the one in the args
func (s *segment) evictFromLRU(lru *list, skip *item) (*item, bool) {
current := lru.first
for current != nil {
i := current.(*item)
if i == skip || !canEvict(i) {
current = current.next()
continue
}
s.deleteLookup(i)
s.deleteFromLRU(i)
s.freeMemory(i)
i.close()
return i, true
}
return nil, false
}
// tries to evict one item from its own keyspace, other than the one in the args
func (s *segment) evictFromOwnKeyspace(i *item) bool {
keyspaceLRU, ok := s.lruLookup[i.keyspace]
if !ok {
return false
}
evictedItem, ok := s.evictFromLRU(keyspaceLRU, i)
if !ok {
return false
}
s.stats.deleteItem(evictedItem)
s.stats.notifyEvict(evictedItem.keyspace, evictedItem.size)
return true
}
// round-robin over the rest of the LRUs to evict one item, other than the item's own LRU, to evict one item
func (s *segment) evictFromOtherKeyspaces(i *item) bool {
own := s.lruLookup[i.keyspace]
lru, _ := s.lruRotate.first.(*list)
for lru != nil {
if lru == own {
lru, _ = lru.next().(*list)
continue
}
evictedItem, ok := s.evictFromLRU(lru, nil)
if !ok {
lru, _ = lru.next().(*list)
continue
}
s.stats.deleteItem(evictedItem)
s.stats.notifyEvict(evictedItem.keyspace, evictedItem.size)
var rotateAt node = lru
if lru.empty() {
// if empty, it was removed, too
rotateAt = rotateAt.prev()
}
s.lruRotate.rotate(rotateAt)
return true
}
return false
}
// tries to evict one item, other than the one in the args. First tries from the items marked for delete,
// checking if no reader is blocking them anymore. Next tries in the item's own keyspace. Last tries all other
// keyspaces in a round-robin fashion
func (s *segment) evictForItem(i *item) bool {
return s.evictFromDeleted() ||
s.evictFromOwnKeyspace(i) ||
s.evictFromOtherKeyspaces(i)
}
// tries to allocate a single chunk in the free memory space. If it fails, tries to evict an item. When
// allocated, aligns the chunk with the item's existing chunks.
func (s *segment) allocateFor(i *item) error {
c, ok := s.memory.allocate()
if !ok {
if s.evictForItem(i) {
c, _ = s.memory.allocate()
} else {
s.stats.notifyAllocFailed(i.keyspace)
return errAllocationFailed
}
}
_, last := i.data()
if last != nil && c != last.next() {
s.memory.move(c, last.next())
}
i.appendChunk(c)
return nil
}
// moves an item in its keyspace's LRU list to the end, meaning that it was the most recently used item in the
// keyspace
func (s *segment) touchItem(i *item) {
s.lruLookup[i.keyspace].move(i, nil)
}
// tries to find an item based on its hash, key and keyspace. If found but expired, deletes it. When finds a
// valid item, it sets the item as the most recently used one in the LRU list of the keyspace
func (s *segment) get(hash uint64, keyspace, key string) (*Reader, bool) {
s.mx.Lock()
defer s.mx.Unlock()
if s.closed {
return nil, false
}
i, ok := s.lookup(hash, keyspace, key)
if !ok {
s.stats.notifyMiss(keyspace, key)
return nil, false
}
if i.expired() {
if s.deleteItem(i) {
s.stats.deleteItem(i)
s.stats.notifyExpireDelete(keyspace, key, i.size)
return nil, false
}
s.stats.incMarkedDeleted(i.keyspace)
s.stats.notifyExpire(keyspace, key)
return nil, false
}
s.touchItem(i)
i.readers++
s.stats.notifyHit(keyspace, key)
s.stats.incReaders(keyspace)
return newReader(s, i), true
}
// allocates 0 or more chunks to fit the key with the item and saves the key bytes in memory
func (s *segment) writeKey(i *item, key string) error {
p := []byte(key)
for len(p) > 0 {
if err := s.allocateFor(i); err != nil {
return err
}
n, _ := i.write(p)
p = p[n:]
}
return nil
}
// tries to create a new item. If one exists with the same keyspace and key, it deletes it first. Stores the
// item in the lookup table and the LRU list of the keyspace. If memory cannot be allocated for the item key due
// to active readers holding too many existing items, it fails
func (s *segment) trySet(hash uint64, keyspace, key string, ttl time.Duration) (*Writer, error) {
s.mx.Lock()
defer s.mx.Unlock()
if s.closed {
return nil, ErrCacheClosed
}
if i, ok := s.lookup(hash, keyspace, key); ok {
if s.deleteItem(i) {
s.stats.deleteItem(i)
s.stats.notifyDelete(keyspace, key, i.size)
} else {
s.stats.incMarkedDeleted(i.keyspace)
s.stats.notifyDelete(keyspace, key, 0)
}
}
i := newItem(hash, keyspace, len(key), ttl)
if err := s.writeKey(i, key); err != nil {
first, last := i.data()
if first != nil {
s.memory.free(first, last)
}
return nil, err
}
s.addLookup(hash, i)
lru := s.keyspaceLRU(i.keyspace)
lru.insert(i, nil)
s.stats.addItem(i)
s.stats.notifySet(keyspace, key, i.keySize)
s.stats.incWriters(keyspace)
return newWriter(s, i), nil
}
// creates a new item. If the item key cannot be stored due to active readers holding too many existing items,
// it blocks, and waits for the next reader to signal that it's done
func (s *segment) set(hash uint64, keyspace, key string, ttl time.Duration) (*Writer, bool) {
for {
if w, err := s.trySet(hash, keyspace, key, ttl); err == errAllocationFailed {
// waiting to be able to store the key
// condition checking happens during writing they key
s.readDoneCond.L.Lock()
s.readDoneCond.Wait()
s.readDoneCond.L.Unlock()
} else if err != nil {
return nil, false
} else {
return w, true
}
}
}
// deletes an item if it can be found in the segment
func (s *segment) del(hash uint64, keyspace, key string) {
s.mx.Lock()
defer s.mx.Unlock()
if s.closed {
return
}
i, ok := s.lookup(hash, keyspace, key)
if !ok {
return
}
if s.deleteItem(i) {
s.stats.deleteItem(i)
s.stats.notifyDelete(keyspace, key, i.size)
return
}
s.stats.incMarkedDeleted(i.keyspace)
s.stats.notifyDelete(keyspace, key, 0)
}
func (s *segment) getStats() *segmentStats {
s.mx.Lock()
defer s.mx.Unlock()
return s.stats.clone()
}
// closes all items in a list
func closeAll(l *list) {
i := l.first
for i != nil {
i.(*item).close()
i = i.next()
}
}
// closes the segment. It releases the allocated memory
func (s *segment) close() {
s.mx.Lock()
defer s.mx.Unlock()
if s.closed {
return
}
closeAll(s.deleteQueue)
lru := s.lruRotate.first
for lru != nil {
closeAll(lru.(*list))
lru = lru.next()
}
s.memory = nil
s.deleteQueue = nil
s.lruLookup = nil
s.itemLookup = nil
s.closed = true
}