/
sorted_extents.go
281 lines (239 loc) · 6.21 KB
/
sorted_extents.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
package metanode
import (
"bytes"
"encoding/json"
"sync"
"github.com/chubaofs/chubaofs/proto"
)
type SortedExtents struct {
sync.RWMutex
eks []proto.ExtentKey
}
func NewSortedExtents() *SortedExtents {
return &SortedExtents{
eks: make([]proto.ExtentKey, 0),
}
}
func (se *SortedExtents) String() string {
se.RLock()
data, err := json.Marshal(se.eks)
se.RUnlock()
if err != nil {
return ""
}
return string(data)
}
func (se *SortedExtents) MarshalBinary() ([]byte, error) {
var data []byte
se.RLock()
defer se.RUnlock()
for _, ek := range se.eks {
ekdata, err := ek.MarshalBinary()
if err != nil {
return nil, err
}
data = append(data, ekdata...)
}
return data, nil
}
func (se *SortedExtents) UnmarshalBinary(data []byte) error {
var ek proto.ExtentKey
se.Lock()
defer se.Unlock()
buf := bytes.NewBuffer(data)
for {
if buf.Len() == 0 {
break
}
if err := ek.UnmarshalBinary(buf); err != nil {
return err
}
// Don't use se.Append here, since we need to retain the raw ek order.
se.eks = append(se.eks, ek)
}
return nil
}
func (se *SortedExtents) Append(ek proto.ExtentKey) (deleteExtents []proto.ExtentKey) {
endOffset := ek.FileOffset + uint64(ek.Size)
se.Lock()
defer se.Unlock()
if len(se.eks) <= 0 {
se.eks = append(se.eks, ek)
return
}
lastKey := se.eks[len(se.eks)-1]
if lastKey.FileOffset+uint64(lastKey.Size) <= ek.FileOffset {
se.eks = append(se.eks, ek)
return
}
firstKey := se.eks[0]
if firstKey.FileOffset >= endOffset {
eks := se.doCopyExtents()
se.eks = se.eks[:0]
se.eks = append(se.eks, ek)
se.eks = append(se.eks, eks...)
return
}
var startIndex, endIndex int
invalidExtents := make([]proto.ExtentKey, 0)
for idx, key := range se.eks {
if ek.FileOffset > key.FileOffset {
startIndex = idx + 1
continue
}
if endOffset >= key.FileOffset+uint64(key.Size) {
invalidExtents = append(invalidExtents, key)
continue
}
break
}
endIndex = startIndex + len(invalidExtents)
upperExtents := make([]proto.ExtentKey, len(se.eks)-endIndex)
copy(upperExtents, se.eks[endIndex:])
se.eks = se.eks[:startIndex]
se.eks = append(se.eks, ek)
se.eks = append(se.eks, upperExtents...)
// check if ek and key are the same extent file with size extented
deleteExtents = make([]proto.ExtentKey, 0, len(invalidExtents))
for _, key := range invalidExtents {
if key.PartitionId != ek.PartitionId || key.ExtentId != ek.ExtentId {
deleteExtents = append(deleteExtents, key)
}
}
return
}
func (se *SortedExtents) AppendWithCheck(ek proto.ExtentKey, discard []proto.ExtentKey) (deleteExtents []proto.ExtentKey, status uint8) {
status = proto.OpOk
endOffset := ek.FileOffset + uint64(ek.Size)
se.Lock()
defer se.Unlock()
if len(se.eks) <= 0 {
se.eks = append(se.eks, ek)
return
}
lastKey := se.eks[len(se.eks)-1]
if lastKey.FileOffset+uint64(lastKey.Size) <= ek.FileOffset {
se.eks = append(se.eks, ek)
return
}
firstKey := se.eks[0]
if firstKey.FileOffset >= endOffset {
eks := se.doCopyExtents()
se.eks = se.eks[:0]
se.eks = append(se.eks, ek)
se.eks = append(se.eks, eks...)
return
}
var startIndex, endIndex int
invalidExtents := make([]proto.ExtentKey, 0)
for idx, key := range se.eks {
if ek.FileOffset > key.FileOffset {
startIndex = idx + 1
continue
}
if endOffset >= key.FileOffset+uint64(key.Size) {
invalidExtents = append(invalidExtents, key)
continue
}
break
}
// Makes the request idempotent, just in case client retries.
if len(invalidExtents) == 1 && invalidExtents[0] == ek {
return
}
// check if ek and key are the same extent file with size extented
deleteExtents = make([]proto.ExtentKey, 0, len(invalidExtents))
for _, key := range invalidExtents {
if key.PartitionId != ek.PartitionId || key.ExtentId != ek.ExtentId || key.ExtentOffset != ek.ExtentOffset {
deleteExtents = append(deleteExtents, key)
}
}
//log.LogInfof("invalidExtents(%v) deleteExtents(%v) discardExtents(%v)", invalidExtents, deleteExtents, discard)
if discard != nil {
if len(deleteExtents) != len(discard) {
return deleteExtents, proto.OpConflictExtentsErr
}
for i := 0; i < len(discard); i++ {
if deleteExtents[i].PartitionId != discard[i].PartitionId || deleteExtents[i].ExtentId != discard[i].ExtentId || deleteExtents[i].ExtentOffset != discard[i].ExtentOffset {
return deleteExtents, proto.OpConflictExtentsErr
}
}
} else if len(deleteExtents) != 0 {
return deleteExtents, proto.OpConflictExtentsErr
}
endIndex = startIndex + len(invalidExtents)
upperExtents := make([]proto.ExtentKey, len(se.eks)-endIndex)
copy(upperExtents, se.eks[endIndex:])
se.eks = se.eks[:startIndex]
se.eks = append(se.eks, ek)
se.eks = append(se.eks, upperExtents...)
return
}
func (se *SortedExtents) Truncate(offset uint64) (deleteExtents []proto.ExtentKey) {
var endIndex int
se.Lock()
defer se.Unlock()
endIndex = -1
for idx, key := range se.eks {
if key.FileOffset >= offset {
endIndex = idx
break
}
}
if endIndex < 0 {
deleteExtents = make([]proto.ExtentKey, 0)
} else {
deleteExtents = make([]proto.ExtentKey, len(se.eks)-endIndex)
copy(deleteExtents, se.eks[endIndex:])
se.eks = se.eks[:endIndex]
}
numKeys := len(se.eks)
if numKeys > 0 {
lastKey := &se.eks[numKeys-1]
if lastKey.FileOffset+uint64(lastKey.Size) > offset {
lastKey.Size = uint32(offset - lastKey.FileOffset)
}
}
return
}
func (se *SortedExtents) Len() int {
se.RLock()
defer se.RUnlock()
return len(se.eks)
}
// Returns the file size
func (se *SortedExtents) Size() uint64 {
se.RLock()
defer se.RUnlock()
last := len(se.eks)
if last <= 0 {
return uint64(0)
}
return se.eks[last-1].FileOffset + uint64(se.eks[last-1].Size)
}
func (se *SortedExtents) Range(f func(ek proto.ExtentKey) bool) {
se.RLock()
defer se.RUnlock()
for _, ek := range se.eks {
if !f(ek) {
break
}
}
}
func (se *SortedExtents) Clone() *SortedExtents {
newSe := NewSortedExtents()
se.RLock()
defer se.RUnlock()
newSe.eks = se.doCopyExtents()
return newSe
}
func (se *SortedExtents) CopyExtents() []proto.ExtentKey {
se.RLock()
defer se.RUnlock()
return se.doCopyExtents()
}
func (se *SortedExtents) doCopyExtents() []proto.ExtentKey {
eks := make([]proto.ExtentKey, len(se.eks))
copy(eks, se.eks)
return eks
}