forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extent_cache.go
164 lines (149 loc) · 3.87 KB
/
extent_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
// Copyright 2018 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 storage
import (
"container/list"
"sync"
)
// ExtentMapItem stores the extent entity pointer and the element
// pointer of the extent entity in a cache list.
type ExtentMapItem struct {
e *Extent
element *list.Element
}
// ExtentCache is an implementation of the ExtentCache with LRU support.
type ExtentCache struct {
extentMap map[uint64]*ExtentMapItem
extentList *list.List
tinyExtents map[uint64]*Extent
tinyLock sync.RWMutex
lock sync.RWMutex
capacity int
}
// NewExtentCache creates and returns a new ExtentCache instance.
func NewExtentCache(capacity int) *ExtentCache {
return &ExtentCache{
extentMap: make(map[uint64]*ExtentMapItem),
extentList: list.New(),
capacity: capacity,
tinyExtents: make(map[uint64]*Extent),
}
}
// Put puts an extent object into the cache.
func (cache *ExtentCache) Put(e *Extent) {
if IsTinyExtent(e.extentID) {
cache.tinyLock.Lock()
cache.tinyExtents[e.extentID] = e
cache.tinyLock.Unlock()
return
}
cache.lock.Lock()
defer cache.lock.Unlock()
item := &ExtentMapItem{
e: e,
element: cache.extentList.PushBack(e),
}
cache.extentMap[e.extentID] = item
cache.evict()
}
// Get gets the extent from the cache.
func (cache *ExtentCache) Get(extentID uint64) (e *Extent, ok bool) {
if IsTinyExtent(extentID) {
cache.tinyLock.RLock()
e, ok = cache.tinyExtents[extentID]
cache.tinyLock.RUnlock()
return
}
cache.lock.Lock()
defer cache.lock.Unlock()
var (
item *ExtentMapItem
)
if item, ok = cache.extentMap[extentID]; ok {
if !IsTinyExtent(extentID) {
cache.extentList.MoveToBack(item.element)
}
e = item.e
}
return
}
// Del deletes the extent stored in the cache.
func (cache *ExtentCache) Del(extentID uint64) {
if IsTinyExtent(extentID) {
return
}
cache.lock.Lock()
defer cache.lock.Unlock()
var (
item *ExtentMapItem
ok bool
)
if item, ok = cache.extentMap[extentID]; ok {
delete(cache.extentMap, extentID)
cache.extentList.Remove(item.element)
item.e.Close()
}
}
// Clear closes all the extents stored in the cache.
func (cache *ExtentCache) Clear() {
for _, extent := range cache.tinyExtents {
extent.Close()
}
cache.lock.Lock()
defer cache.lock.Unlock()
for e := cache.extentList.Front(); e != nil; {
curr := e
e = e.Next()
ec := curr.Value.(*Extent)
delete(cache.extentMap, ec.extentID)
ec.Close()
cache.extentList.Remove(curr)
}
cache.extentList = list.New()
cache.extentMap = make(map[uint64]*ExtentMapItem)
}
// Size returns number of extents stored in the cache.
func (cache *ExtentCache) Size() int {
cache.lock.RLock()
defer cache.lock.RUnlock()
return cache.extentList.Len()
}
func (cache *ExtentCache) evict() {
if cache.capacity <= 0 {
return
}
needRemove := cache.extentList.Len() - cache.capacity
for i := 0; i < needRemove; i++ {
if e := cache.extentList.Front(); e != nil {
front := e.Value.(*Extent)
if IsTinyExtent(front.extentID) {
continue
}
delete(cache.extentMap, front.extentID)
cache.extentList.Remove(e)
front.Close()
}
}
}
// Flush synchronizes the extent stored in the cache to the disk.
func (cache *ExtentCache) Flush() {
for _, extent := range cache.tinyExtents {
extent.Flush()
}
cache.lock.RLock()
defer cache.lock.RUnlock()
for _, item := range cache.extentMap {
item.e.Flush()
}
}