-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
431 lines (363 loc) · 8.83 KB
/
cache.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
package feature
import (
"bytes"
"container/list"
"hash/maphash"
"os"
"path/filepath"
"sort"
"sync"
)
// Cache is an in-memory view of feature mount point on a file system.
//
// The cache is optimized for fast lookups of gates, and fast test of gate
// open states for an id. The cache is also immutable, and therefore safe to
// use concurrently from multiple goroutines.
//
// The cache is designed to minimize the memory footprint. The underlying files
// containing the id collections are memory mapped so multiple programs are able
// to share the memory pages.
type Cache struct {
cache lruCache
mutex sync.RWMutex
tiers []cachedTier
}
func (c *Cache) swap(x *Cache) *Cache {
c.mutex.Lock()
defer c.mutex.Unlock()
c.tiers, x.tiers = x.tiers, c.tiers
c.cache.clear()
return x
}
// Close releases resources held by the cache.
func (c *Cache) Close() error {
c.mutex.Lock()
defer c.mutex.Unlock()
for i := range c.tiers {
for _, c := range c.tiers[i].collections {
c.unmap()
}
}
c.tiers = nil
c.cache.clear()
return nil
}
// GateOpen returns true if a gate is opened for a given id.
//
// The method does not retain any of the strings passed as arguments.
func (c *Cache) GateOpen(family, gate, collection, id string) bool {
g := c.LookupGates(family, collection, id)
i := sort.Search(len(g), func(i int) bool {
return g[i] >= gate
})
return i < len(g) && g[i] == gate
}
// LookupGates returns the list of open gates in a family for a given id.
//
// The method does not retain any of the strings passed as arguments.
func (c *Cache) LookupGates(family, collection, id string) []string {
key := lruCacheKey{
family: family,
collection: collection,
id: id,
}
if v := c.cache.lookup(key); v != nil && v.key == key {
return v.gates
}
buf := family + collection + id
key = lruCacheKey{
family: buf[:len(family)],
collection: buf[len(family) : len(family)+len(collection)],
id: buf[len(family)+len(collection):],
}
disabled := make(map[string]struct{})
gates := make([]string, 0, 8)
defer func() {
c.cache.insert(key, gates, 4096)
}()
h := acquireBufferedHash64()
defer releaseBufferedHash64(h)
c.mutex.RLock()
defer c.mutex.RUnlock()
for i := range c.tiers {
t := &c.tiers[i]
c := t.collections[collection]
exists := c != nil && c.contains(id)
for _, g := range t.gates[family] {
if g.collection == collection {
if exists {
if openGate(id, g.salt, g.volume, h) {
gates = append(gates, g.name)
} else {
disabled[g.name] = struct{}{}
}
} else if g.open {
gates = append(gates, g.name)
}
}
}
}
if len(gates) == 0 {
gates = nil
} else {
sort.Strings(gates)
gates = deduplicate(gates)
gates = strip(gates, disabled)
// Safe guard in case the program appends to the slice, it will force
// the reallocation and copy.
gates = gates[:len(gates):len(gates)]
}
return gates
}
func deduplicate(s []string) []string {
n := 0
for i := 1; i < len(s); i++ {
if s[i] != s[n] {
n++
s[n] = s[i]
}
}
for i := n + 1; i < len(s); i++ {
s[i] = ""
}
return s[:n+1]
}
func strip(s []string, disabled map[string]struct{}) []string {
n := 0
for _, x := range s {
if _, skip := disabled[x]; !skip {
s[n] = x
n++
}
}
for i := n; i < len(s); i++ {
s[i] = ""
}
return s[:n]
}
type cachedTier struct {
group string
name string
collections map[string]*collection
gates map[string][]cachedGate
}
type cachedGate struct {
name string
collection string
salt string
volume float64
open bool
}
// The Load method loads the features at the mount point it is called on,
// returning a Cache object exposing the state.
//
// The returned cache holds operating system resources and therefore must be
// closed when the program does not need it anymore.
func (path MountPoint) Load() (*Cache, error) {
// Resolves symlinks first so we know that the underlying directory
// structure will not change across reads from the file system when
// loading the cache.
p, err := filepath.EvalSymlinks(string(path))
if err != nil {
return nil, err
}
path = MountPoint(p)
// To minimize the memory footprint of the cache, strings are deduplicated
// using this map, so we only retain only one copy of each string value.
strings := stringCache{}
tiers := make([]cachedTier, 0)
if err := Scan(path.Groups(), func(group string) error {
return Scan(path.Tiers(group), func(tier string) error {
t, err := path.OpenTier(group, tier)
if err != nil {
return err
}
defer t.Close()
c := cachedTier{
group: strings.load(group),
name: strings.load(tier),
collections: make(map[string]*collection),
gates: make(map[string][]cachedGate),
}
if err := Scan(t.Families(), func(family string) error {
return Scan(t.Gates(family), func(gate string) error {
f := strings.load(family)
d := readdir(t.gatePath(family, gate))
defer d.close()
for d.next() {
open, salt, volume, err := t.ReadGate(family, gate, d.name())
if err != nil {
return err
}
c.gates[f] = append(c.gates[f], cachedGate{
name: strings.load(gate),
collection: strings.load(d.name()),
salt: salt,
volume: volume,
open: open,
})
}
return nil
})
}); err != nil {
return err
}
if err := Scan(t.Collections(), func(collection string) error {
col, err := mmapCollection(t.collectionPath(collection))
if err != nil {
return err
}
c.collections[strings.load(collection)] = col
return nil
}); err != nil {
return err
}
tiers = append(tiers, c)
return nil
})
}); err != nil {
return nil, err
}
for _, tier := range tiers {
for _, gates := range tier.gates {
sort.Slice(gates, func(i, j int) bool {
return gates[i].name < gates[j].name
})
}
}
return &Cache{tiers: tiers}, nil
}
type slice struct {
offset uint32
length uint32
}
type collection struct {
memory []byte
index []slice
}
func (col *collection) at(i int) []byte {
slice := col.index[i]
return col.memory[slice.offset : slice.offset+slice.length]
}
func (col *collection) contains(id string) bool {
i := sort.Search(len(col.index), func(i int) bool {
return string(col.at(i)) >= id
})
return i < len(col.index) && string(col.at(i)) == id
}
func (col *collection) unmap() {
munmap(col.memory)
col.memory, col.index = nil, nil
}
func (col *collection) Len() int { return len(col.index) }
func (col *collection) Less(i, j int) bool { return string(col.at(i)) < string(col.at(j)) }
func (col *collection) Swap(i, j int) { col.index[i], col.index[j] = col.index[j], col.index[i] }
func mmapCollection(path string) (*collection, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
m, err := mmap(f)
if err != nil {
return nil, err
}
count := 0
forEachLine(m, func(int, int) { count++ })
index := make([]slice, 0, count)
forEachLine(m, func(off, len int) {
index = append(index, slice{
offset: uint32(off),
length: uint32(len),
})
})
col := &collection{memory: m, index: index}
if !sort.IsSorted(col) {
sort.Sort(col)
}
return col, nil
}
func forEachLine(b []byte, do func(off, len int)) {
for i := 0; i < len(b); {
n := bytes.IndexByte(b[i:], '\n')
if n < 0 {
n = len(b) - i
}
do(i, n)
i += n + 1
}
}
type stringCache map[string]string
func (c stringCache) load(s string) string {
v, ok := c[s]
if ok {
return v
}
c[s] = s
return s
}
var lruCacheSeed = maphash.MakeSeed()
type lruCacheKey struct {
family string
collection string
id string
}
func (k *lruCacheKey) hash(h *maphash.Hash) uint64 {
h.WriteString(k.family)
h.WriteString(k.collection)
h.WriteString(k.id)
return h.Sum64()
}
type lruCacheValue struct {
key lruCacheKey
gates []string
}
type lruCache struct {
mutex sync.Mutex
queue list.List
cache map[uint64]*list.Element
}
func (c *lruCache) clear() {
c.mutex.Lock()
c.queue = list.List{}
for key := range c.cache {
delete(c.cache, key)
}
c.mutex.Unlock()
}
func (c *lruCache) insert(key lruCacheKey, gates []string, limit int) {
m := maphash.Hash{}
m.SetSeed(lruCacheSeed)
h := key.hash(&m)
v := &lruCacheValue{
key: key,
gates: gates,
}
c.mutex.Lock()
defer c.mutex.Unlock()
if c.cache == nil {
c.cache = make(map[uint64]*list.Element)
}
e := c.queue.PushBack(v)
c.cache[h] = e
for limit > 0 && len(c.cache) > limit {
e := c.queue.Back()
v := e.Value.(*lruCacheValue)
c.queue.Remove(e)
m.Reset()
delete(c.cache, v.key.hash(&m))
}
}
func (c *lruCache) lookup(key lruCacheKey) *lruCacheValue {
m := maphash.Hash{}
m.SetSeed(lruCacheSeed)
h := key.hash(&m)
c.mutex.Lock()
defer c.mutex.Unlock()
e := c.cache[h]
if e != nil {
c.queue.MoveToFront(e)
return e.Value.(*lruCacheValue)
}
return nil
}