/
extent_cache.go
373 lines (327 loc) · 11 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
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
// 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 stream
import (
"fmt"
"sync"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/btree"
"github.com/chubaofs/chubaofs/util/log"
)
// ExtentRequest defines the struct for the request of read or write an extent.
type ExtentRequest struct {
FileOffset int
Size int
Data []byte
ExtentKey *proto.ExtentKey
}
// String returns the string format of the extent request.
func (er *ExtentRequest) String() string {
return fmt.Sprintf("FileOffset(%v) Size(%v) ExtentKey(%v)", er.FileOffset, er.Size, er.ExtentKey)
}
// NewExtentRequest returns a new extent request.
func NewExtentRequest(offset, size int, data []byte, ek *proto.ExtentKey) *ExtentRequest {
return &ExtentRequest{
FileOffset: offset,
Size: size,
Data: data,
ExtentKey: ek,
}
}
// ExtentCache defines the struct of the extent cache.
type ExtentCache struct {
sync.RWMutex
inode uint64
gen uint64 // generation number
size uint64 // size of the cache
root *btree.BTree
discard *btree.BTree
}
// NewExtentCache returns a new extent cache.
func NewExtentCache(inode uint64) *ExtentCache {
return &ExtentCache{
inode: inode,
root: btree.New(32),
discard: btree.New(32),
}
}
// Refresh refreshes the extent cache.
func (cache *ExtentCache) Refresh(inode uint64, getExtents GetExtentsFunc) error {
gen, size, extents, err := getExtents(inode)
if err != nil {
return err
}
//log.LogDebugf("Local ExtentCache before update: ino(%v) gen(%v) size(%v) extents(%v)", inode, cache.gen, cache.size, cache.List())
cache.update(gen, size, extents)
//log.LogDebugf("Local ExtentCache after update: ino(%v) gen(%v) size(%v) extents(%v)", inode, cache.gen, cache.size, cache.List())
return nil
}
func (cache *ExtentCache) update(gen, size uint64, eks []proto.ExtentKey) {
cache.Lock()
defer cache.Unlock()
log.LogDebugf("ExtentCache update: ino(%v) cache.gen(%v) cache.size(%v) gen(%v) size(%v)", cache.inode, cache.gen, cache.size, gen, size)
// cache.root.Ascend(func(bi btree.Item) bool {
// ek := bi.(*proto.ExtentKey)
// log.LogDebugf("ExtentCache update: local ino(%v) ek(%v)", cache.inode, ek)
// return true
// })
//
// for _, ek := range eks {
// log.LogDebugf("ExtentCache update: remote ino(%v) ek(%v)", cache.inode, ek)
// }
if cache.gen != 0 && cache.gen >= gen {
log.LogDebugf("ExtentCache update: no need to update, ino(%v) gen(%v) size(%v)", cache.inode, gen, size)
return
}
cache.gen = gen
cache.size = size
cache.root.Clear(false)
for _, ek := range eks {
extent := ek
cache.root.ReplaceOrInsert(&extent)
}
}
// Append appends an extent key.
func (cache *ExtentCache) Append(ek *proto.ExtentKey, sync bool) (discardExtents []proto.ExtentKey) {
ekEnd := ek.FileOffset + uint64(ek.Size)
lower := &proto.ExtentKey{FileOffset: ek.FileOffset}
upper := &proto.ExtentKey{FileOffset: ekEnd}
discard := make([]*proto.ExtentKey, 0)
cache.Lock()
defer cache.Unlock()
// When doing the append, we do not care about the data after the file offset.
// Those data will be overwritten by the current extent anyway.
cache.root.AscendRange(lower, upper, func(i btree.Item) bool {
found := i.(*proto.ExtentKey)
discard = append(discard, found)
return true
})
// After deleting the data between lower and upper, we will do the append
for _, key := range discard {
cache.root.Delete(key)
if key.PartitionId != 0 && key.ExtentId != 0 && (key.PartitionId != ek.PartitionId || key.ExtentId != ek.ExtentId || ek.ExtentOffset != key.ExtentOffset) {
if sync || (ek.PartitionId == 0 && ek.ExtentId == 0) {
cache.discard.ReplaceOrInsert(key)
//log.LogDebugf("ExtentCache Append add to discard: ino(%v) ek(%v) discard(%v)", cache.inode, ek, key)
}
}
}
cache.root.ReplaceOrInsert(ek)
if sync {
cache.gen++
discardExtents = make([]proto.ExtentKey, 0, cache.discard.Len())
cache.discard.AscendRange(lower, upper, func(i btree.Item) bool {
found := i.(*proto.ExtentKey)
if found.PartitionId != ek.PartitionId || found.ExtentId != ek.ExtentId || found.ExtentOffset != ek.ExtentOffset {
discardExtents = append(discardExtents, *found)
}
return true
})
}
if ekEnd > cache.size {
cache.size = ekEnd
}
log.LogDebugf("ExtentCache Append: ino(%v) sync(%v) ek(%v) local discard(%v) discardExtents(%v)", cache.inode, sync, ek, discard, discardExtents)
return
}
func (cache *ExtentCache) RemoveDiscard(discardExtents []proto.ExtentKey) {
cache.Lock()
defer cache.Unlock()
for _, ek := range discardExtents {
cache.discard.Delete(&ek)
//log.LogDebugf("ExtentCache ClearDiscard: ino(%v) discard(%v)", cache.inode, ek)
}
}
// Max returns the max extent key in the cache.
func (cache *ExtentCache) Max() *proto.ExtentKey {
cache.RLock()
defer cache.RUnlock()
ek := cache.root.Max().(*proto.ExtentKey)
return ek
}
// Size returns the size of the cache.
func (cache *ExtentCache) Size() (size int, gen uint64) {
cache.RLock()
defer cache.RUnlock()
return int(cache.size), cache.gen
}
// SetSize set the size of the cache.
func (cache *ExtentCache) SetSize(size uint64, sync bool) {
cache.Lock()
defer cache.Unlock()
cache.size = size
if sync {
cache.gen++
}
}
// List returns a list of the extents in the cache.
func (cache *ExtentCache) List() []*proto.ExtentKey {
cache.RLock()
root := cache.root.Clone()
cache.RUnlock()
extents := make([]*proto.ExtentKey, 0, root.Len())
root.Ascend(func(i btree.Item) bool {
ek := i.(*proto.ExtentKey)
extents = append(extents, ek)
return true
})
return extents
}
// Get returns the extent key based on the given offset.
func (cache *ExtentCache) Get(offset uint64) (ret *proto.ExtentKey) {
pivot := &proto.ExtentKey{FileOffset: offset}
cache.RLock()
defer cache.RUnlock()
cache.root.DescendLessOrEqual(pivot, func(i btree.Item) bool {
ek := i.(*proto.ExtentKey)
//log.LogDebugf("ExtentCache GetConnect: ino(%v) ek(%v) offset(%v)", cache.inode, ek, offset)
if offset >= ek.FileOffset && offset < ek.FileOffset+uint64(ek.Size) {
ret = ek
}
return false
})
return ret
}
// PrepareReadRequests classifies the incoming request.
func (cache *ExtentCache) PrepareReadRequests(offset, size int, data []byte) []*ExtentRequest {
requests := make([]*ExtentRequest, 0)
pivot := &proto.ExtentKey{FileOffset: uint64(offset)}
upper := &proto.ExtentKey{FileOffset: uint64(offset + size)}
start := offset
end := offset + size
cache.RLock()
defer cache.RUnlock()
lower := &proto.ExtentKey{}
cache.root.DescendLessOrEqual(pivot, func(i btree.Item) bool {
ek := i.(*proto.ExtentKey)
lower.FileOffset = ek.FileOffset
return false
})
cache.root.AscendRange(lower, upper, func(i btree.Item) bool {
ek := i.(*proto.ExtentKey)
ekStart := int(ek.FileOffset)
ekEnd := int(ek.FileOffset) + int(ek.Size)
log.LogDebugf("PrepareReadRequests: ino(%v) start(%v) end(%v) ekStart(%v) ekEnd(%v)", cache.inode, start, end, ekStart, ekEnd)
if start < ekStart {
if end <= ekStart {
return false
} else if end < ekEnd {
// add hole (start, ekStart)
req := NewExtentRequest(start, ekStart-start, data[start-offset:ekStart-offset], nil)
requests = append(requests, req)
// add non-hole (ekStart, end)
req = NewExtentRequest(ekStart, end-ekStart, data[ekStart-offset:end-offset], ek)
requests = append(requests, req)
start = end
return false
} else {
// add hole (start, ekStart)
req := NewExtentRequest(start, ekStart-start, data[start-offset:ekStart-offset], nil)
requests = append(requests, req)
// add non-hole (ekStart, ekEnd)
req = NewExtentRequest(ekStart, ekEnd-ekStart, data[ekStart-offset:ekEnd-offset], ek)
requests = append(requests, req)
start = ekEnd
return true
}
} else if start < ekEnd {
if end <= ekEnd {
// add non-hole (start, end)
req := NewExtentRequest(start, end-start, data[start-offset:end-offset], ek)
requests = append(requests, req)
start = end
return false
} else {
// add non-hole (start, ekEnd), start = ekEnd
req := NewExtentRequest(start, ekEnd-start, data[start-offset:ekEnd-offset], ek)
requests = append(requests, req)
start = ekEnd
return true
}
} else {
return true
}
})
log.LogDebugf("PrepareReadRequests: ino(%v) start(%v) end(%v)", cache.inode, start, end)
if start < end {
// add hole (start, end)
req := NewExtentRequest(start, end-start, data[start-offset:end-offset], nil)
requests = append(requests, req)
}
return requests
}
// PrepareWriteRequests TODO explain
func (cache *ExtentCache) PrepareWriteRequests(offset, size int, data []byte) []*ExtentRequest {
requests := make([]*ExtentRequest, 0)
pivot := &proto.ExtentKey{FileOffset: uint64(offset)}
upper := &proto.ExtentKey{FileOffset: uint64(offset + size)}
start := offset
end := offset + size
cache.RLock()
defer cache.RUnlock()
lower := &proto.ExtentKey{}
cache.root.DescendLessOrEqual(pivot, func(i btree.Item) bool {
ek := i.(*proto.ExtentKey)
lower.FileOffset = ek.FileOffset
return false
})
cache.root.AscendRange(lower, upper, func(i btree.Item) bool {
ek := i.(*proto.ExtentKey)
ekStart := int(ek.FileOffset)
ekEnd := int(ek.FileOffset) + int(ek.Size)
log.LogDebugf("PrepareWriteRequests: ino(%v) start(%v) end(%v) ekStart(%v) ekEnd(%v)", cache.inode, start, end, ekStart, ekEnd)
if start <= ekStart {
if end <= ekStart {
return false
} else if end < ekEnd {
var req *ExtentRequest
if start < ekStart {
// add hole (start, ekStart)
req = NewExtentRequest(start, ekStart-start, data[start-offset:ekStart-offset], nil)
requests = append(requests, req)
}
// add non-hole (ekStart, end)
req = NewExtentRequest(ekStart, end-ekStart, data[ekStart-offset:end-offset], ek)
requests = append(requests, req)
start = end
return false
} else {
return true
}
} else if start < ekEnd {
if end <= ekEnd {
// add non-hole (start, end)
req := NewExtentRequest(start, end-start, data[start-offset:end-offset], ek)
requests = append(requests, req)
start = end
return false
} else {
// add non-hole (start, ekEnd), start = ekEnd
req := NewExtentRequest(start, ekEnd-start, data[start-offset:ekEnd-offset], ek)
requests = append(requests, req)
start = ekEnd
return true
}
} else {
return true
}
})
log.LogDebugf("PrepareWriteRequests: ino(%v) start(%v) end(%v)", cache.inode, start, end)
if start < end {
// add hole (start, end)
req := NewExtentRequest(start, end-start, data[start-offset:end-offset], nil)
requests = append(requests, req)
}
return requests
}