forked from true1064/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition_item.go
486 lines (445 loc) · 12.4 KB
/
partition_item.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
// 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 metanode
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"reflect"
"strings"
"sync"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/util/log"
)
// MetaItem defines the structure of the metadata operations.
type MetaItem struct {
Op uint32 `json:"Op"`
K []byte `json:"k"`
V []byte `json:"v"`
}
// MarshalJson
func (s *MetaItem) MarshalJson() ([]byte, error) {
return json.Marshal(s)
}
// MarshalBinary marshals MetaItem to binary data.
// Binary frame structure:
// +------+----+------+------+------+------+
// | Item | Op | LenK | K | LenV | V |
// +------+----+------+------+------+------+
// | byte | 4 | 4 | LenK | 4 | LenV |
// +------+----+------+------+------+------+
func (s *MetaItem) MarshalBinary() (result []byte, err error) {
buff := bytes.NewBuffer(make([]byte, 0))
buff.Grow(4 + len(s.K) + len(s.V))
if err = binary.Write(buff, binary.BigEndian, s.Op); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, uint32(len(s.K))); err != nil {
return
}
if _, err = buff.Write(s.K); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, uint32(len(s.V))); err != nil {
return
}
if _, err = buff.Write(s.V); err != nil {
return
}
result = buff.Bytes()
return
}
// UnmarshalJson unmarshals binary data to MetaItem.
func (s *MetaItem) UnmarshalJson(data []byte) error {
return json.Unmarshal(data, s)
}
// MarshalBinary unmarshal this MetaItem entity from binary data.
// Binary frame structure:
// +------+----+------+------+------+------+
// | Item | Op | LenK | K | LenV | V |
// +------+----+------+------+------+------+
// | byte | 4 | 4 | LenK | 4 | LenV |
// +------+----+------+------+------+------+
func (s *MetaItem) UnmarshalBinary(raw []byte) (err error) {
var (
lenK uint32
lenV uint32
)
buff := bytes.NewBuffer(raw)
if err = binary.Read(buff, binary.BigEndian, &s.Op); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &lenK); err != nil {
return
}
s.K = make([]byte, lenK)
if _, err = buff.Read(s.K); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &lenV); err != nil {
return
}
s.V = make([]byte, lenV)
if _, err = buff.Read(s.V); err != nil {
return
}
return
}
// NewMetaItem returns a new MetaItem.
func NewMetaItem(op uint32, key, value []byte) *MetaItem {
return &MetaItem{
Op: op,
K: key,
V: value,
}
}
type fileData struct {
filename string
data []byte
}
const (
// initial version
SnapFormatVersion_0 uint32 = iota
// version since transaction feature, added formatVersion, txId and cursor in MetaItemIterator struct
SnapFormatVersion_1
)
// MetaItemIterator defines the iterator of the MetaItem.
type MetaItemIterator struct {
fileRootDir string
SnapFormatVersion uint32
applyID uint64
uniqID uint64
txId uint64
cursor uint64
inodeTree *BTree
dentryTree *BTree
extendTree *BTree
multipartTree *BTree
txTree *BTree
txRbInodeTree *BTree
txRbDentryTree *BTree
uniqChecker *uniqChecker
filenames []string
dataCh chan interface{}
errorCh chan error
err error
closeCh chan struct{}
closeOnce sync.Once
}
// SnapItemWrapper key definition
const (
SiwKeySnapFormatVer uint32 = iota
SiwKeyApplyId
SiwKeyTxId
SiwKeyCursor
SiwKeyUniqId
)
type SnapItemWrapper struct {
key uint32
value interface{}
}
func (siw *SnapItemWrapper) MarshalKey() (k []byte) {
k = make([]byte, 8)
binary.BigEndian.PutUint32(k, siw.key)
return
}
func (siw *SnapItemWrapper) UnmarshalKey(k []byte) (err error) {
siw.key = binary.BigEndian.Uint32(k)
return
}
// newMetaItemIterator returns a new MetaItemIterator.
func newMetaItemIterator(mp *metaPartition) (si *MetaItemIterator, err error) {
si = new(MetaItemIterator)
si.fileRootDir = mp.config.RootDir
si.SnapFormatVersion = mp.manager.metaNode.raftSyncSnapFormatVersion
mp.nonIdempotent.Lock()
si.applyID = mp.getApplyID()
si.txId = mp.txProcessor.txManager.txIdAlloc.getTransactionID()
si.cursor = mp.GetCursor()
si.uniqID = mp.GetUniqId()
si.inodeTree = mp.inodeTree.GetTree()
si.dentryTree = mp.dentryTree.GetTree()
si.extendTree = mp.extendTree.GetTree()
si.multipartTree = mp.multipartTree.GetTree()
si.txTree = mp.txProcessor.txManager.txTree.GetTree()
si.txRbInodeTree = mp.txProcessor.txResource.txRbInodeTree.GetTree()
si.txRbDentryTree = mp.txProcessor.txResource.txRbDentryTree.GetTree()
si.uniqChecker = mp.uniqChecker.clone()
mp.nonIdempotent.Unlock()
si.dataCh = make(chan interface{})
si.errorCh = make(chan error, 1)
si.closeCh = make(chan struct{})
// collect extend del files
var filenames = make([]string, 0)
var fileInfos []os.DirEntry
if fileInfos, err = os.ReadDir(mp.config.RootDir); err != nil {
return
}
for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() && strings.HasPrefix(fileInfo.Name(), prefixDelExtent) {
filenames = append(filenames, fileInfo.Name())
}
if !fileInfo.IsDir() && strings.HasPrefix(fileInfo.Name(), prefixDelExtentV2) {
filenames = append(filenames, fileInfo.Name())
}
if !fileInfo.IsDir() && strings.HasPrefix(fileInfo.Name(), prefixMultiVer) {
filenames = append(filenames, fileInfo.Name())
}
}
si.filenames = filenames
// start data producer
go func(iter *MetaItemIterator) {
defer func() {
close(iter.dataCh)
close(iter.errorCh)
}()
var produceItem = func(item interface{}) (success bool) {
select {
case iter.dataCh <- item:
return true
case <-iter.closeCh:
return false
}
}
var produceError = func(err error) {
select {
case iter.errorCh <- err:
default:
}
}
var checkClose = func() (closed bool) {
select {
case <-iter.closeCh:
return true
default:
return false
}
}
if si.SnapFormatVersion == SnapFormatVersion_0 {
// process index ID
produceItem(si.applyID)
log.LogDebugf("newMetaItemIterator: SnapFormatVersion_0, partitionId(%v), applyID(%v)",
mp.config.PartitionId, si.applyID)
} else if si.SnapFormatVersion == SnapFormatVersion_1 {
// process snapshot format version
snapFormatVerWrapper := SnapItemWrapper{SiwKeySnapFormatVer, si.SnapFormatVersion}
produceItem(snapFormatVerWrapper)
// process apply index ID
applyIdWrapper := SnapItemWrapper{SiwKeyApplyId, si.applyID}
produceItem(applyIdWrapper)
// process txId
txIdWrapper := SnapItemWrapper{SiwKeyTxId, si.txId}
produceItem(txIdWrapper)
// process cursor
cursorWrapper := SnapItemWrapper{SiwKeyCursor, si.cursor}
produceItem(cursorWrapper)
log.LogDebugf("newMetaItemIterator: SnapFormatVersion_1, partitionId(%v) applyID(%v) txId(%v) cursor(%v) uniqID(%v)",
mp.config.PartitionId, si.applyID, si.txId, si.cursor, si.uniqID)
if si.uniqID != 0 {
// process uniqId
uniqIdWrapper := SnapItemWrapper{SiwKeyUniqId, si.uniqID}
produceItem(uniqIdWrapper)
}
} else {
panic(fmt.Sprintf("invalid raftSyncSnapFormatVersione: %v", si.SnapFormatVersion))
}
// process inodes
iter.inodeTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
// process dentries
iter.dentryTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
// process extends
iter.extendTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
// process multiparts
iter.multipartTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
if si.SnapFormatVersion == SnapFormatVersion_1 {
iter.txTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
iter.txRbInodeTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
iter.txRbDentryTree.Ascend(func(i BtreeItem) bool {
return produceItem(i)
})
if checkClose() {
return
}
if si.uniqID != 0 {
produceItem(si.uniqChecker)
if checkClose() {
return
}
}
}
// process extent del files
var err error
var raw []byte
for _, filename := range iter.filenames {
if raw, err = ioutil.ReadFile(path.Join(iter.fileRootDir, filename)); err != nil {
produceError(err)
return
}
if !produceItem(&fileData{filename: filename, data: raw}) {
return
}
}
}(si)
return
}
// ApplyIndex returns the applyID of the iterator.
func (si *MetaItemIterator) ApplyIndex() uint64 {
return si.applyID
}
// Close closes the iterator.
func (si *MetaItemIterator) Close() {
si.closeOnce.Do(func() {
close(si.closeCh)
})
return
}
// Next returns the next item.
func (si *MetaItemIterator) Next() (data []byte, err error) {
if si.err != nil {
err = si.err
return
}
var item interface{}
var open bool
select {
case item, open = <-si.dataCh:
case err, open = <-si.errorCh:
}
if item == nil || !open {
err, si.err = io.EOF, io.EOF
si.Close()
return
}
if err != nil {
si.err = err
si.Close()
return
}
var snap *MetaItem
switch typedItem := item.(type) {
case uint64:
applyIDBuf := make([]byte, 8)
binary.BigEndian.PutUint64(applyIDBuf, si.applyID)
data = applyIDBuf
return
case SnapItemWrapper:
if typedItem.key == SiwKeySnapFormatVer {
snapFormatVerBuf := make([]byte, 8)
binary.BigEndian.PutUint32(snapFormatVerBuf, si.SnapFormatVersion)
snap = NewMetaItem(opFSMSnapFormatVersion, typedItem.MarshalKey(), snapFormatVerBuf)
} else if typedItem.key == SiwKeyApplyId {
applyIDBuf := make([]byte, 8)
binary.BigEndian.PutUint64(applyIDBuf, si.applyID)
snap = NewMetaItem(opFSMApplyId, typedItem.MarshalKey(), applyIDBuf)
} else if typedItem.key == SiwKeyTxId {
txIDBuf := make([]byte, 8)
binary.BigEndian.PutUint64(txIDBuf, si.txId)
snap = NewMetaItem(opFSMTxId, typedItem.MarshalKey(), txIDBuf)
} else if typedItem.key == SiwKeyCursor {
cursor := typedItem.value.(uint64)
cursorBuf := make([]byte, 8)
binary.BigEndian.PutUint64(cursorBuf, cursor)
snap = NewMetaItem(opFSMCursor, typedItem.MarshalKey(), cursorBuf)
} else if typedItem.key == SiwKeyUniqId {
uniqId := typedItem.value.(uint64)
uniqIdBuf := make([]byte, 8)
binary.BigEndian.PutUint64(uniqIdBuf, uniqId)
snap = NewMetaItem(opFSMUniqIDSnap, typedItem.MarshalKey(), uniqIdBuf)
} else {
panic(fmt.Sprintf("MetaItemIterator.Next: unknown SnapItemWrapper key: %v", typedItem.key))
}
case *Inode:
snap = NewMetaItem(opFSMCreateInode, typedItem.MarshalKey(), typedItem.MarshalValue())
case *Dentry:
snap = NewMetaItem(opFSMCreateDentry, typedItem.MarshalKey(), typedItem.MarshalValue())
case *Extend:
var raw []byte
if raw, err = typedItem.Bytes(); err != nil {
si.err = err
si.Close()
return
}
snap = NewMetaItem(opFSMSetXAttr, nil, raw)
case *Multipart:
var raw []byte
if raw, err = typedItem.Bytes(); err != nil {
si.err = err
si.Close()
return
}
snap = NewMetaItem(opFSMCreateMultipart, nil, raw)
case *proto.TransactionInfo:
val, _ := typedItem.Marshal()
snap = NewMetaItem(opFSMTxSnapshot, []byte(typedItem.TxID), val)
case *TxRollbackInode:
val, _ := typedItem.Marshal()
snap = NewMetaItem(opFSMTxRbInodeSnapshot, typedItem.inode.MarshalKey(), val)
case *TxRollbackDentry:
val, _ := typedItem.Marshal()
snap = NewMetaItem(opFSMTxRbDentrySnapshot, []byte(typedItem.txDentryInfo.GetKey()), val)
case *fileData:
snap = NewMetaItem(opExtentFileSnapshot, []byte(typedItem.filename), typedItem.data)
case *uniqChecker:
var raw []byte
if raw, _, err = typedItem.Marshal(); err != nil {
si.err = err
si.Close()
return
}
snap = NewMetaItem(opFSMUniqCheckerSnap, nil, raw)
default:
panic(fmt.Sprintf("unknown item type: %v", reflect.TypeOf(item).Name()))
}
if data, err = snap.MarshalBinary(); err != nil {
si.err = err
si.Close()
return
}
return
}