forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_cache.go
545 lines (463 loc) · 13.3 KB
/
meta_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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2019 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package objectnode
import (
"container/list"
"os"
"strconv"
"sync"
"time"
"github.com/cubefs/cubefs/metanode"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/util/log"
)
const (
BackGroundEvictDurFactor = 2
BackGroundEvictMaxNumFactor = 5
)
type AccessStat struct {
accessNum uint64
miss uint64
validHit uint64
}
type AttrItem struct {
proto.XAttrInfo
expiredTime int64
}
func (attr *AttrItem) IsExpired() bool {
if attr.expiredTime < time.Now().Unix() {
return true
}
return false
}
//VolumeInodeAttrsCache caches Attrs for Inodes
type VolumeInodeAttrsCache struct {
sync.RWMutex
cache map[uint64]*list.Element
lruList *list.List
maxElements int64
accessStat AccessStat
}
func NewVolumeInodeAttrsCache(maxElements int64) *VolumeInodeAttrsCache {
vac := &VolumeInodeAttrsCache{
cache: make(map[uint64]*list.Element),
lruList: list.New(),
maxElements: maxElements,
}
return vac
}
func (vac *VolumeInodeAttrsCache) SetMaxElements(maxElements int64) {
vac.Lock()
defer vac.Unlock()
vac.maxElements = maxElements
}
func (vac *VolumeInodeAttrsCache) putAttr(attr *AttrItem) {
vac.Lock()
defer vac.Unlock()
old, ok := vac.cache[attr.Inode]
if ok {
oldAttr := old.Value.(*AttrItem)
log.LogDebugf("replace old attr: inode(%v) attr(%v) with new attr(%v)", attr.Inode, oldAttr, attr)
vac.lruList.Remove(old)
delete(vac.cache, attr.Inode)
}
if int64(vac.lruList.Len()) > vac.maxElements {
vac.evictAttr(int64(vac.lruList.Len())-vac.maxElements, false)
}
element := vac.lruList.PushFront(attr)
vac.cache[attr.Inode] = element
}
func (vac *VolumeInodeAttrsCache) mergeAttr(attr *AttrItem) {
vac.RLock()
old, ok := vac.cache[attr.Inode]
if ok {
oldAttrs := old.Value.(*AttrItem)
for key, val := range oldAttrs.XAttrs {
_, exist := attr.XAttrs[key]
if !exist {
attr.XAttrs[key] = val
}
}
}
vac.RUnlock()
vac.putAttr(attr)
}
func (vac *VolumeInodeAttrsCache) getAttr(inode uint64) *AttrItem {
var miss bool
defer func() {
vac.Lock()
vac.accessStat.accessNum++
if miss {
vac.accessStat.miss++
} else {
vac.accessStat.validHit++
}
vac.Unlock()
}()
vac.RLock()
element, ok := vac.cache[inode]
if !ok {
log.LogDebugf("cache Get inode(%v) attr not found", inode)
miss = true
vac.RUnlock()
return nil
}
attrs := element.Value.(*AttrItem)
vac.RUnlock()
vac.Lock()
vac.lruList.MoveToFront(element)
vac.Unlock()
log.LogDebugf("cache Get inode(%v) attrs(%v)", inode, attrs)
return attrs
}
func (vac *VolumeInodeAttrsCache) deleteAttr(inode uint64) {
vac.Lock()
defer vac.Unlock()
element, ok := vac.cache[inode]
if ok {
attr := element.Value.(*AttrItem)
log.LogDebugf("delete attr in cache: inode(%v) attr(%v) ", inode, attr)
vac.lruList.Remove(element)
delete(vac.cache, inode)
}
}
func (vac *VolumeInodeAttrsCache) deleteAttrWithKey(inode uint64, key string) {
vac.Lock()
var attr *AttrItem
element, ok := vac.cache[inode]
if ok {
attr = element.Value.(*AttrItem)
log.LogDebugf("delete key: %v in attrs of inode(%v) ", key, inode)
delete(attr.XAttrs, key)
}
vac.Unlock()
if attr != nil {
vac.putAttr(attr)
}
}
func (vac *VolumeInodeAttrsCache) evictAttr(evictNum int64, backGround bool) {
if evictNum <= 0 {
return
}
for i := int64(0); i < evictNum; i++ {
element := vac.lruList.Back()
attr := element.Value.(*AttrItem)
if !backGround || (backGround && attr.IsExpired()) {
vac.lruList.Remove(element)
delete(vac.cache, attr.Inode)
}
}
}
func (vac *VolumeInodeAttrsCache) TotalNum() int {
vac.RLock()
defer vac.RUnlock()
return len(vac.cache)
}
func (vac *VolumeInodeAttrsCache) GetAccessStat() (accssNum, validHit, miss uint64) {
return vac.accessStat.accessNum, vac.accessStat.validHit, vac.accessStat.miss
}
//type DentryItem metanode.Dentry
type DentryItem struct {
metanode.Dentry
expiredTime int64
}
func (di *DentryItem) Key() string {
return strconv.FormatUint(di.ParentId, 10) + pathSep + di.Name
}
func (di *DentryItem) IsExpired() bool {
if di.expiredTime < time.Now().Unix() {
return true
}
return false
}
//VolumeDentryCache accelerates translating S3 path to posix-compatible file system metadata within a volume
type VolumeDentryCache struct {
sync.RWMutex
cache map[string]*list.Element
lruList *list.List
maxElements int64
aStat AccessStat
}
func NewVolumeDentryCache(maxElements int64) *VolumeDentryCache {
vdc := &VolumeDentryCache{
cache: make(map[string]*list.Element),
lruList: list.New(),
maxElements: maxElements,
}
return vdc
}
func (vdc *VolumeDentryCache) setMaxElements(maxElements int64) {
vdc.Lock()
defer vdc.Unlock()
vdc.maxElements = maxElements
}
func (vdc *VolumeDentryCache) putDentry(dentry *DentryItem) {
vdc.Lock()
defer vdc.Unlock()
key := dentry.Key()
old, ok := vdc.cache[key]
if ok {
oldDentry := old.Value.(*DentryItem)
log.LogDebugf("replace old dentry: parentID(%v) inode(%v) name(%v) mode(%v) "+
"with new one: parentID(%v) inode(%v) name(%v) mode(%v)",
oldDentry.ParentId, oldDentry.Inode, oldDentry.Name, os.FileMode(oldDentry.Type),
dentry.ParentId, dentry.Inode, dentry.Name, os.FileMode(dentry.Type))
vdc.lruList.Remove(old)
delete(vdc.cache, key)
}
if int64(vdc.lruList.Len()) > vdc.maxElements {
vdc.evictDentry(int64(vdc.lruList.Len())-vdc.maxElements, false)
}
element := vdc.lruList.PushFront(dentry)
vdc.cache[key] = element
}
func (vdc *VolumeDentryCache) getDentry(key string) *DentryItem {
var miss bool
defer func() {
vdc.Lock()
vdc.aStat.accessNum++
if miss {
vdc.aStat.miss++
} else {
vdc.aStat.validHit++
}
vdc.Unlock()
}()
vdc.RLock()
element, ok := vdc.cache[key]
if !ok {
miss = true
vdc.RUnlock()
return nil
}
item := element.Value.(*DentryItem)
vdc.RUnlock()
vdc.Lock()
vdc.lruList.MoveToFront(element)
vdc.Unlock()
return item
}
func (vdc *VolumeDentryCache) deleteDentry(key string) {
vdc.Lock()
defer vdc.Unlock()
element, ok := vdc.cache[key]
if ok {
dentry := element.Value.(*DentryItem)
log.LogDebugf("delete dentry in cache: key(%v) dentry(%v) ", key, dentry)
vdc.lruList.Remove(element)
delete(vdc.cache, key)
}
}
func (vdc *VolumeDentryCache) evictDentry(evictNum int64, backGround bool) {
if evictNum <= 0 {
return
}
for i := int64(0); i < evictNum; i++ {
element := vdc.lruList.Back()
dentry := element.Value.(*DentryItem)
if !backGround || (backGround && dentry.IsExpired()) {
vdc.lruList.Remove(element)
delete(vdc.cache, dentry.Key())
}
}
}
func (vdc *VolumeDentryCache) TotalNum() int {
vdc.RLock()
defer vdc.RUnlock()
return len(vdc.cache)
}
func (vdc *VolumeDentryCache) GetAccessStat() (accssNum, validHit, miss uint64) {
return vdc.aStat.accessNum, vdc.aStat.validHit, vdc.aStat.miss
}
type ObjMetaCache struct {
sync.RWMutex
volumeDentryCache map[string]*VolumeDentryCache // volume --> VolumeDentryCache
volumeInodeAttrsCache map[string]*VolumeInodeAttrsCache // volume --> VolumeInodeAttrsCache
maxDentryNum int64 // maxDentryNum that all volume share
maxInodeAttrNum int64 // maxInodeAttrNum that all volume share
refreshIntervalSec uint64 // dentry/attr cache expiration time
}
func NewObjMetaCache(maxDentryNum, maxInodeAttrNum int64, refreshInterval uint64) *ObjMetaCache {
omc := &ObjMetaCache{
volumeDentryCache: make(map[string]*VolumeDentryCache),
volumeInodeAttrsCache: make(map[string]*VolumeInodeAttrsCache),
maxDentryNum: maxDentryNum,
maxInodeAttrNum: maxInodeAttrNum,
refreshIntervalSec: refreshInterval,
}
go omc.backGroundEvictItem()
return omc
}
func (omc *ObjMetaCache) backGroundEvictItem() {
t := time.NewTicker(time.Duration(BackGroundEvictDurFactor*omc.refreshIntervalSec) * time.Second)
defer t.Stop()
for range t.C {
log.LogDebugf("ObjMetaCache: start backGround evict")
start := time.Now()
omc.RLock()
for _, vac := range omc.volumeInodeAttrsCache {
vac.Lock()
evictNum := len(vac.cache) / BackGroundEvictMaxNumFactor
vac.evictAttr(int64(evictNum), true)
vac.Unlock()
}
for _, vdc := range omc.volumeDentryCache {
vdc.Lock()
evictNum := len(vdc.cache) / BackGroundEvictMaxNumFactor
vdc.evictDentry(int64(evictNum), true)
vdc.Unlock()
}
elapsed := time.Since(start)
log.LogDebugf("ObjMetaCache: finish backGround evict, dentryCache, cost(%d)ms", elapsed.Milliseconds())
omc.RUnlock()
}
}
func (omc *ObjMetaCache) PutAttr(volume string, item *AttrItem) {
omc.Lock()
vac, exist := omc.volumeInodeAttrsCache[volume]
if !exist {
volumeNum := len(omc.volumeInodeAttrsCache)
avgMaxNum := omc.maxInodeAttrNum / (int64(volumeNum) + 1)
vac = NewVolumeInodeAttrsCache(avgMaxNum)
omc.volumeInodeAttrsCache[volume] = vac
for _, v := range omc.volumeInodeAttrsCache {
v.SetMaxElements(avgMaxNum)
}
log.LogDebugf("NewVolumeInodeAttrsCache: volume(%v), volumeNum(%v), avgMaxNum(%v)", volume, volumeNum, avgMaxNum)
}
omc.Unlock()
item.expiredTime = time.Now().Unix() + int64(omc.refreshIntervalSec)
vac.putAttr(item)
log.LogDebugf("ObjMetaCache PutAttr: volume(%v) attr(%v)", volume, item)
}
func (omc *ObjMetaCache) MergeAttr(volume string, item *AttrItem) {
omc.Lock()
vac, exist := omc.volumeInodeAttrsCache[volume]
if !exist {
volumeNum := len(omc.volumeInodeAttrsCache)
avgMaxNum := omc.maxInodeAttrNum / (int64(volumeNum) + 1)
vac = NewVolumeInodeAttrsCache(avgMaxNum)
omc.volumeInodeAttrsCache[volume] = vac
for _, v := range omc.volumeInodeAttrsCache {
v.SetMaxElements(avgMaxNum)
}
log.LogDebugf("NewVolumeInodeAttrsCache: volume(%v), volumeNum(%v), avgMaxNum(%v)", volume, volumeNum, avgMaxNum)
}
omc.Unlock()
log.LogDebugf("ObjMetaCache MergeAttr: volume(%v) attr(%v)", volume, item)
vac.mergeAttr(item)
}
func (omc *ObjMetaCache) GetAttr(volume string, inode uint64) (attr *AttrItem, needRefresh bool) {
omc.RLock()
vac, exist := omc.volumeInodeAttrsCache[volume]
omc.RUnlock()
if !exist {
log.LogDebugf("ObjMetaCache GetAttr: fail, volume(%v) inode(%v) ", volume, inode)
return nil, false
}
attr = vac.getAttr(inode)
if attr == nil {
return nil, false
}
log.LogDebugf("ObjMetaCache GetAttr: volume(%v) inode(%v) attr(%v)", volume, inode, attr)
return attr, attr.IsExpired()
}
func (omc *ObjMetaCache) DeleteAttr(volume string, inode uint64) {
omc.RLock()
vac, exist := omc.volumeInodeAttrsCache[volume]
omc.RUnlock()
if !exist {
return
}
log.LogDebugf("ObjMetaCache DeleteAttr: volume(%v), inode(%v)", volume, inode)
vac.deleteAttr(inode)
}
func (omc *ObjMetaCache) DeleteAttrWithKey(volume string, inode uint64, key string) {
omc.RLock()
vac, exist := omc.volumeInodeAttrsCache[volume]
omc.RUnlock()
if !exist {
return
}
log.LogDebugf("ObjMetaCache DeleteAttr: volume(%v), inode(%v)", volume, inode)
vac.deleteAttrWithKey(inode, key)
}
func (omc *ObjMetaCache) TotalAttrNum() int {
var total int
var vacs []*VolumeInodeAttrsCache
omc.RLock()
for _, vac := range omc.volumeInodeAttrsCache {
vacs = append(vacs, vac)
}
omc.RUnlock()
for _, vac := range vacs {
total += vac.TotalNum()
}
return total
}
func (omc *ObjMetaCache) PutDentry(volume string, item *DentryItem) {
omc.Lock()
vdc, exist := omc.volumeDentryCache[volume]
if !exist {
volumeNum := len(omc.volumeDentryCache)
avgMaxNum := omc.maxDentryNum / (int64(volumeNum) + 1)
vdc = NewVolumeDentryCache(avgMaxNum)
omc.volumeDentryCache[volume] = vdc
for _, v := range omc.volumeDentryCache {
v.setMaxElements(avgMaxNum)
}
log.LogDebugf("NewVolumeDentryCache: volume(%v), volumeNum(%v), avgMaxNum(%v)", volume, volumeNum, avgMaxNum)
}
omc.Unlock()
item.expiredTime = time.Now().Unix() + int64(omc.refreshIntervalSec)
vdc.putDentry(item)
log.LogDebugf("ObjMetaCache PutDentry: volume(%v), DentryItem(%v)", volume, item)
}
func (omc *ObjMetaCache) GetDentry(volume string, key string) (dentry *DentryItem, needRefresh bool) {
omc.RLock()
vdc, exist := omc.volumeDentryCache[volume]
omc.RUnlock()
if !exist {
return nil, false
}
dentry = vdc.getDentry(key)
if dentry == nil {
return nil, false
}
log.LogDebugf("ObjMetaCache GetDentry: volume(%v), key(%v), dentry:(%v)", volume, key, dentry)
return dentry, dentry.IsExpired()
}
func (omc *ObjMetaCache) DeleteDentry(volume string, key string) {
omc.RLock()
vdc, exist := omc.volumeDentryCache[volume]
omc.RUnlock()
if !exist {
return
}
log.LogDebugf("ObjMetaCache DeleteDentry: volume(%v), key(%v)", volume, key)
vdc.deleteDentry(key)
}
func (omc *ObjMetaCache) TotalDentryNum() int {
var total int
var vdcs []*VolumeDentryCache
omc.RLock()
for _, vdc := range omc.volumeDentryCache {
vdcs = append(vdcs, vdc)
}
omc.RUnlock()
for _, vdc := range vdcs {
total += vdc.TotalNum()
}
return total
}