forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicache.go
170 lines (151 loc) · 4.26 KB
/
icache.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
// Copyright 2018 The Chubao 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 fs
import (
"container/list"
"sync"
"time"
)
const (
// MinInodeCacheEvictNum is used in the foreground eviction.
// When clearing the inodes from the cache, it stops as soon as 10 inodes have been evicted.
MinInodeCacheEvictNum = 10
// MaxInodeCacheEvictNum is used in the back ground. We can evict 200000 inodes at max.
MaxInodeCacheEvictNum = 200000
BgEvictionInterval = 2 * time.Minute
)
// InodeCache defines the structure of the inode cache.
type InodeCache struct {
sync.RWMutex
cache map[uint64]*list.Element
lruList *list.List
expiration time.Duration
maxElements int
}
// NewInodeCache returns a new inode cache.
func NewInodeCache(exp time.Duration, maxElements int) *InodeCache {
ic := &InodeCache{
cache: make(map[uint64]*list.Element),
lruList: list.New(),
expiration: exp,
maxElements: maxElements,
}
go ic.backgroundEviction()
return ic
}
// Put puts the given inode into the inode cache.
func (ic *InodeCache) Put(inode *Inode) {
ic.Lock()
old, ok := ic.cache[inode.ino]
if ok {
ic.lruList.Remove(old)
delete(ic.cache, inode.ino)
}
if ic.lruList.Len() >= ic.maxElements {
ic.evict(true)
}
inode.setExpiration(ic.expiration)
element := ic.lruList.PushFront(inode)
ic.cache[inode.ino] = element
ic.Unlock()
//log.LogDebugf("InodeCache PutConnect: inode(%v)", inode)
}
// Get returns the inode based on the given inode ID.
func (ic *InodeCache) Get(ino uint64) *Inode {
ic.RLock()
element, ok := ic.cache[ino]
if !ok {
ic.RUnlock()
return nil
}
inode := element.Value.(*Inode)
if inode.expired() {
ic.RUnlock()
//log.LogDebugf("InodeCache GetConnect expired: now(%v) inode(%v)", time.Now().Format(LogTimeFormat), inode)
return nil
}
ic.RUnlock()
return inode
}
// Delete deletes the inode based on the given inode ID.
func (ic *InodeCache) Delete(ino uint64) {
//log.LogDebugf("InodeCache Delete: ino(%v)", ino)
ic.Lock()
element, ok := ic.cache[ino]
if ok {
ic.lruList.Remove(element)
delete(ic.cache, ino)
}
ic.Unlock()
}
// Foreground eviction cares more about the speed.
// Background eviction evicts all expired items from the cache.
// The caller should grab the WRITE lock of the inode cache.
func (ic *InodeCache) evict(foreground bool) {
var count int
//defer func() {
// log.LogInfof("InodeCache: evict count(%v)", count)
//}()
for i := 0; i < MinInodeCacheEvictNum; i++ {
element := ic.lruList.Back()
if element == nil {
return
}
// For background eviction, if all expired items have been evicted, just return
// But for foreground eviction, we need to evict at least MinInodeCacheEvictNum inodes.
// The foreground eviction, does not need to care if the inode has expired or not.
inode := element.Value.(*Inode)
if !foreground && !inode.expired() {
return
}
ic.lruList.Remove(element)
delete(ic.cache, inode.ino)
//log.LogInfof("InodeCache: evict inode(%v)", inode)
count++
}
// For background eviction, we need to continue evict all expired items from the cache
if foreground {
return
}
for i := 0; i < MaxInodeCacheEvictNum; i++ {
element := ic.lruList.Back()
if element == nil {
break
}
inode := element.Value.(*Inode)
if !inode.expired() {
break
}
ic.lruList.Remove(element)
delete(ic.cache, inode.ino)
//log.LogInfof("InodeCache: evict inode(%v)", inode)
count++
}
}
func (ic *InodeCache) backgroundEviction() {
t := time.NewTicker(BgEvictionInterval)
defer t.Stop()
for {
select {
case <-t.C:
//log.LogInfof("InodeCache: start BG evict")
//start := time.Now()
ic.Lock()
ic.evict(false)
ic.Unlock()
//elapsed := time.Since(start)
//log.LogInfof("InodeCache: done BG evict, cost (%v)ns", elapsed.Nanoseconds())
}
}
}