/
partition_delete_extents.go
354 lines (339 loc) · 9.95 KB
/
partition_delete_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
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
// 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 metanode
import (
"bytes"
"container/list"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/log"
"github.com/chubaofs/chubaofs/util/synclist"
)
const (
prefixDelExtent = "EXTENT_DEL"
prefixDelExtentV2 = "EXTENT_DEL_V2"
maxDeleteExtentSize = 10 * MB
)
var extentsFileHeader = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08}
/// start metapartition delete extents work
///
func (mp *metaPartition) startToDeleteExtents() {
fileList := synclist.New()
go mp.appendDelExtentsToFile(fileList)
go mp.deleteExtentsFromList(fileList)
}
// create extent delete file
func (mp *metaPartition) createExtentDeleteFile(prefix string, idx int64, fileList *synclist.SyncList) (fp *os.File, fileName string, fileSize int64, err error) {
fileName = fmt.Sprintf("%s_%d", prefix, idx)
fp, err = os.OpenFile(path.Join(mp.config.RootDir, fileName),
os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.LogErrorf("[metaPartition] createExtentDeletFile openFile %v %v error %v", mp.config.RootDir, fileName, err)
return
}
if _, err = fp.Write(extentsFileHeader); err != nil {
log.LogErrorf("[metaPartition] createExtentDeletFile Write %v %v error %v", mp.config.RootDir, fileName, err)
}
fileSize = int64(len(extentsFileHeader))
fileList.PushBack(fileName)
return
}
//append delete extents from extDelCh to EXTENT_DEL_N files
func (mp *metaPartition) appendDelExtentsToFile(fileList *synclist.SyncList) {
defer func() {
if r := recover(); r != nil {
log.LogErrorf(fmt.Sprintf("[metaPartition] appendDelExtentsToFile pid(%v) panic (%v)", mp.config.PartitionId, r))
}
}()
var (
fileName string
fileSize int64
idx int64
fp *os.File
err error
)
LOOP:
// scan existed EXTENT_DEL_* files to fill fileList
finfos, err := ioutil.ReadDir(mp.config.RootDir)
if err != nil {
panic(err)
}
for _, info := range finfos {
if strings.HasPrefix(info.Name(), prefixDelExtent) {
fileList.PushBack(info.Name())
fileSize = info.Size()
}
}
// check
lastItem := fileList.Back()
if lastItem == nil {
//if no exist EXTENT_DEL_*, create one
fp, fileName, fileSize, err = mp.createExtentDeleteFile(prefixDelExtentV2, idx, fileList)
if err != nil {
panic(err)
}
} else {
//exist, open last file
fileName = lastItem.Value.(string)
fp, err = os.OpenFile(path.Join(mp.config.RootDir, fileName),
os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
}
extentV2 := false
if strings.HasPrefix(fileName, prefixDelExtentV2) {
extentV2 = true
}
// TODO Unhandled errors
defer fp.Close()
buf := make([]byte, 0)
for {
select {
case <-mp.stopC:
return
case <-mp.extReset:
// TODO Unhandled errors
fp.Close()
// reset fileList
fileList.Init()
goto LOOP
case eks := <-mp.extDelCh:
var data []byte
buf = buf[:0]
for _, ek := range eks {
if extentV2 {
data, err = ek.MarshalBinaryWithCheckSum()
} else {
data, err = ek.MarshalBinary()
}
if err != nil {
log.LogWarnf("[appendDelExtentsToFile] partitionId=%d,"+
" extentKey marshal: %s", mp.config.PartitionId, err.Error())
break
}
buf = append(buf, data...)
}
if err != nil {
mp.extDelCh <- eks
continue
}
if fileSize >= maxDeleteExtentSize {
// TODO Unhandled errors
// close old File
fp.Close()
idx += 1
fp, fileName, fileSize, err = mp.createExtentDeleteFile(prefixDelExtentV2, idx, fileList)
if err != nil {
panic(err)
}
}
// write delete extents into file
if _, err = fp.Write(buf); err != nil {
panic(err)
}
fileSize += int64(len(buf))
}
}
}
// Delete all the extents of a file.
func (mp *metaPartition) deleteExtentsFromList(fileList *synclist.SyncList) {
defer func() {
if r := recover(); r != nil {
log.LogErrorf(fmt.Sprintf("deleteExtentsFromList(%v) deleteExtentsFromList panic (%v)", mp.config.PartitionId, r))
}
}()
var (
element *list.Element
fileName string
file string
fileInfo os.FileInfo
err error
)
for {
time.Sleep(time.Minute)
select {
case <-mp.stopC:
return
default:
}
LOOP:
element = fileList.Front()
if element == nil {
continue
}
fileName = element.Value.(string)
file = path.Join(mp.config.RootDir, fileName)
if fileInfo, err = os.Stat(file); err != nil {
fileList.Remove(element)
goto LOOP
}
//if not leader, ignore delete
if _, ok := mp.IsLeader(); !ok {
log.LogDebugf("[deleteExtentsFromList] partitionId=%d, "+
"not raft leader,please ignore", mp.config.PartitionId)
continue
}
//leader do delete extent for EXTENT_DEL_* file
// read delete extents from file
buf := make([]byte, MB)
fp, err := os.OpenFile(file, os.O_RDWR, 0644)
if err != nil {
log.LogErrorf("[deleteExtentsFromList] openFile %v error: %v", file, err)
fileList.Remove(element)
goto LOOP
}
//get delete extents cursor at file header 8 bytes
if _, err = fp.ReadAt(buf[:8], 0); err != nil {
log.LogWarnf("[deleteExtentsFromList] partitionId=%d, "+
"read cursor least 8bytes, retry later", mp.config.PartitionId)
// TODO Unhandled errors
fp.Close()
continue
}
extentV2 := false
extentKeyLen := uint64(proto.ExtentLength)
if strings.HasPrefix(fileName, prefixDelExtentV2) {
extentV2 = true
extentKeyLen = uint64(proto.ExtentV2Length)
}
cursor := binary.BigEndian.Uint64(buf[:8])
if size := uint64(fileInfo.Size()) - cursor; size < MB {
if size <= 0 {
size = extentKeyLen
} else if size > 0 && size < extentKeyLen {
errStr := fmt.Sprintf(
"[deleteExtentsFromList] partitionId=%d, %s file corrupted!",
mp.config.PartitionId, fileName)
log.LogErrorf(errStr) // FIXME
fileList.Remove(element)
fp.Close()
goto LOOP
}
buf = buf[:size]
}
//read extents from cursor
n, err := fp.ReadAt(buf, int64(cursor))
// TODO Unhandled errors
fp.Close()
if err != nil && err != io.EOF {
panic(err)
} else if err == io.EOF {
err = nil
if fileList.Len() <= 1 {
log.LogDebugf("[deleteExtentsFromList] partitionId=%d, %s"+
" extents delete ok", mp.config.PartitionId, fileName)
} else {
status := mp.raftPartition.Status()
if status.State == "StateLeader" && !status.RestoringSnapshot {
// delete old delete extents file for metapartition
if _, err = mp.submit(opFSMInternalDelExtentFile, []byte(fileName)); err != nil {
log.LogErrorf(
"[deleteExtentsFromList] partitionId=%d,"+
"delete old file: %s,status: %s", mp.config.PartitionId,
fileName, err.Error())
}
log.LogDebugf("[deleteExtentsFromList] partitionId=%d "+
",delete old file: %s, status: %v", mp.config.PartitionId, fileName,
err == nil)
goto LOOP
}
log.LogDebugf("[deleteExtentsFromList] partitionId=%d,delete"+
" old file status: %s", mp.config.PartitionId, status.State)
}
continue
}
buff := bytes.NewBuffer(buf)
cursor += uint64(n)
var deleteCnt uint64
for {
if buff.Len() == 0 {
break
}
if uint64(buff.Len()) < extentKeyLen {
cursor -= uint64(buff.Len())
break
}
batchCount := DeleteBatchCount() * 5
if deleteCnt%batchCount == 0 {
DeleteWorkerSleepMs()
}
ek := proto.ExtentKey{}
if extentV2 {
if err = ek.UnmarshalBinaryWithCheckSum(buff); err != nil {
if err == proto.InvalidKeyHeader || err == proto.InvalidKeyCheckSum {
log.LogErrorf("[deleteExtentsFromList] invalid extent key header %v, %v, %v", fileName, mp.config.PartitionId, err)
continue
}
log.LogErrorf("[deleteExtentsFromList] mp: %v Unmarshal extentkey from %v unresolved error: %v", mp.config.PartitionId, fileName, err)
panic(err)
}
} else {
if err = ek.UnmarshalBinary(buff); err != nil {
panic(err)
}
}
// delete dataPartition
if err = mp.doDeleteMarkedInodes(&ek); err != nil {
eks := make([]proto.ExtentKey, 0)
eks = append(eks, ek)
mp.extDelCh <- eks
log.LogWarnf("[deleteExtentsFromList] mp: %v, extent: %v, %s",
mp.config.PartitionId, ek, err.Error())
}
deleteCnt++
}
buff.Reset()
buff.WriteString(fmt.Sprintf("%s %d", fileName, cursor))
if _, err = mp.submit(opFSMInternalDelExtentCursor, buff.Bytes()); err != nil {
log.LogWarnf("[deleteExtentsFromList] partitionId=%d, %s",
mp.config.PartitionId, err.Error())
}
log.LogDebugf("[deleteExtentsFromList] partitionId=%d, file=%s, cursor=%d",
mp.config.PartitionId, fileName, cursor)
goto LOOP
}
}
func (mp *metaPartition) checkBatchDeleteExtents(allExtents map[uint64][]*proto.ExtentKey) {
for partitionID, deleteExtents := range allExtents {
needDeleteExtents := make([]proto.ExtentKey, len(deleteExtents))
for index, ek := range deleteExtents {
newEx := proto.ExtentKey{
FileOffset: ek.FileOffset,
PartitionId: ek.PartitionId,
ExtentId: ek.ExtentId,
ExtentOffset: ek.ExtentOffset,
Size: ek.Size,
CRC: ek.CRC,
}
needDeleteExtents[index] = newEx
log.LogWritef("mp(%v) deleteExtents(%v)", mp.config.PartitionId, newEx.String())
}
err := mp.doBatchDeleteExtentsByPartition(partitionID, deleteExtents)
if err != nil {
log.LogWarnf(fmt.Sprintf("metaPartition(%v) dataPartitionID(%v)"+
" batchDeleteExtentsByPartition failed(%v)", mp.config.PartitionId, partitionID, err))
mp.extDelCh <- needDeleteExtents
}
DeleteWorkerSleepMs()
}
return
}