/
segment.go
475 lines (396 loc) · 11.1 KB
/
segment.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
// Copyright 2018 The kingbus 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 (
"encoding/binary"
"fmt"
"hash/crc32"
"os"
"path"
"strings"
"sync"
"github.com/flike/kingbus/storage/storagepb"
"github.com/flike/kingbus/log"
"github.com/flike/kingbus/utils"
"github.com/coreos/etcd/raft/raftpb"
)
//SegmentStatus is the status of segment file
type SegmentStatus int8
const (
//SegmentReadOnly represents segment file read only
SegmentReadOnly SegmentStatus = iota
//SegmentRDWR represents segment file read write
SegmentRDWR
//SegmentClosed represents segment file is closed
SegmentClosed
)
const (
//ReadonlySegmentPattern is the read only segment file name pattern
ReadonlySegmentPattern = "%020d-%020d.log"
//ReadonlyIndexPattern is the read only index file name pattern
ReadonlyIndexPattern = "%020d-%020d.index"
//ReadWriteSegmentPattern is the read write segment file name pattern
ReadWriteSegmentPattern = "%020d-inprogress.log"
//ReadWriteIndexPattern is the read only index file name pattern
ReadWriteIndexPattern = "%020d-inprogress.index"
//RecordLengthSize is the record length size
RecordLengthSize int = 4
)
//Segment is segment file
type Segment struct {
Mu sync.RWMutex
FirstIndex uint64
LastIndex uint64
LogFile *MmapFile
LogIndex *Index
Status SegmentStatus
}
func newSegment(dir string, name string) *Segment {
s := new(Segment)
s.FirstIndex = 0
s.LastIndex = 0
s.LogFile = newMmapFile(dir, name, SegmentSize)
indexFileName := utils.GetIndexName(name)
s.LogIndex = newIndex(dir, indexFileName)
s.Status = SegmentRDWR
return s
}
//todo test the order is right?
func openSegments(dir string, needRecover bool) []*Segment {
//files have been sorted by name
files, err := utils.ReadDir(dir)
if err != nil {
log.Log.Fatalf("ReadDir error,err:%s,dir:%s", err, dir)
}
logFiles := make([]os.FileInfo, 0, 10)
for _, file := range files {
if strings.HasSuffix(file.Name(), utils.LogSuffix) {
logFiles = append(logFiles, file)
}
}
fileCount := len(logFiles)
segments := make([]*Segment, 0, 10)
if fileCount == 0 {
return segments
}
for i := 0; i < fileCount-1; i++ {
s := openSegment(dir, logFiles[i].Name(), SegmentReadOnly, false)
segments = append(segments, s)
}
//the last segment if need rebuild index file,
//depend on the needRecover flag
s := openSegment(dir, logFiles[fileCount-1].Name(), SegmentRDWR, needRecover)
segments = append(segments, s)
return segments
}
func openSegment(dir string, name string, status SegmentStatus, needRecover bool) *Segment {
s := new(Segment)
indexFileName := utils.GetIndexName(name)
s.LogFile = openMmapFile(dir, name)
if status == SegmentReadOnly {
n, err := fmt.Sscanf(name, ReadonlySegmentPattern, &(s.FirstIndex), &(s.LastIndex))
if err != nil || n != 2 {
log.Log.Fatalf("Sscanf error,err:%s,name:%s", err, name)
}
s.LogIndex = openIndex(dir, indexFileName, SegmentReadOnly)
} else {
n, err := fmt.Sscanf(name, ReadWriteSegmentPattern, &(s.FirstIndex))
if err != nil || n != 1 {
log.Log.Fatalf("Sscanf error,err:%s,name:%s", err, name)
}
if needRecover {
s.LogIndex = s.rebuildIndex()
} else {
s.LogIndex = openIndex(dir, indexFileName, SegmentRDWR)
}
lastIndexEntry := s.LogIndex.getLastRaftEntry(SegmentRDWR)
s.LastIndex = lastIndexEntry.RaftIndex
//set write position
lastRecordPosition := int(lastIndexEntry.FilePosition)
lastRecordLen := binary.LittleEndian.Uint32(s.LogFile.mappedData[lastRecordPosition : lastRecordPosition+RecordLengthSize])
nextWritePos := int(lastIndexEntry.FilePosition) + int(lastRecordLen) + RecordLengthSize
s.LogFile.setPosition(nextWritePos)
}
s.Status = status
return s
}
func (s *Segment) rebuildIndex() *Index {
var record storagepb.Record
var raftEntry raftpb.Entry
//1.remove old index file
indexPath := utils.GetIndexPath(s.LogFile.filePath)
err := os.Remove(indexPath)
if err != nil {
log.Log.Fatalf("Remove file error,err:%s,filePath:%s", err, indexPath)
}
//2.new index
logIndex := newIndex(path.Dir(indexPath), path.Base(indexPath))
//3.parse segment and rebuild index
position := 0
segmentMaxSize := int(SegmentSize)
for {
if segmentMaxSize <= position+RecordLengthSize {
break
}
recordLen := binary.LittleEndian.Uint32(s.LogFile.mappedData[position : position+RecordLengthSize])
position += RecordLengthSize
if segmentMaxSize <= position || segmentMaxSize < position+int(recordLen) {
break
}
recordBuf := s.LogFile.mappedData[position : position+int(recordLen)]
if len(recordBuf) == 0 {
break
}
err := record.Unmarshal(recordBuf)
if err != nil {
log.Log.Errorf("record Unmarshal error,err:%s,buf:%v", err, recordBuf)
break
}
crc := crc32.ChecksumIEEE(record.Data)
if crc != record.Crc {
log.Log.Errorf("crc not equal,record.crc:%d,calculatedCrc:%d,buf:%v", record.Crc, crc, recordBuf)
break
}
err = raftEntry.Unmarshal(record.Data)
if err != nil {
log.Log.Errorf("entry Unmarshal error,err:%s,buf:%v", err, record.Data)
break
}
indexEntry := &IndexEntry{
RaftIndex: raftEntry.Index,
FilePosition: uint32(position - RecordLengthSize),
}
err = logIndex.appendIndexEntry(indexEntry)
if err != nil {
log.Log.Fatalf("appendIndexEntry error,err:%s,indexEntry:%v", err, *indexEntry)
}
position += int(recordLen)
//log.Log.Debugf("rebuildIndex:position is %d,segmentMaxSize is %d", position, segmentMaxSize)
}
log.Log.Debugf("rebuildIndex success,indexName:%s,entry size:%d",
logIndex.readWrite.filePath, len(logIndex.readWrite.entries))
return logIndex
}
func (s *Segment) getFirstIndex() uint64 {
s.Mu.RLock()
defer s.Mu.RUnlock()
return s.FirstIndex
}
func (s *Segment) getLastIndex() uint64 {
s.Mu.RLock()
defer s.Mu.RUnlock()
return s.LastIndex
}
//append raft entries into segment
func (s *Segment) appendRecords(records []*storagepb.Record, startIndex uint64) error {
s.Mu.Lock()
defer s.Mu.Unlock()
var err error
if s.Status != SegmentRDWR {
return ErrNotWritable
}
//check the continuity of raft index in segment
if s.LastIndex != 0 && s.LastIndex+1 != startIndex {
return ErrNotContinuous
}
for i := 0; i < len(records); i++ {
err = s.appendRecord(records[i], startIndex+uint64(i))
if err != nil {
return err
}
}
//sync log file
err = s.LogFile.sync()
if err != nil {
return err
}
//update the segment raft index range
if s.FirstIndex == uint64(0) {
s.FirstIndex = startIndex
}
if s.LastIndex < startIndex+uint64(len(records)-1) {
s.LastIndex = startIndex + uint64(len(records)-1)
}
return nil
}
func (s *Segment) appendRecord(record *storagepb.Record, raftIndex uint64) error {
var err error
recordBuf, err := record.Marshal()
if err != nil {
return err
}
recordLengthBuf := make([]byte, RecordLengthSize)
binary.LittleEndian.PutUint32(recordLengthBuf, uint32(record.Size()))
startPos, err := s.LogFile.append(recordLengthBuf)
if err != nil {
log.Log.Errorf("append recordLengthBuf error,err:%s", err)
return err
}
_, err = s.LogFile.append(recordBuf)
if err != nil {
log.Log.Errorf("append recordBuf error,err:%s", err)
return err
}
//append raftIndex entry
indexEntry := &IndexEntry{
RaftIndex: raftIndex,
FilePosition: uint32(startPos),
}
err = s.LogIndex.appendIndexEntry(indexEntry)
if err != nil {
log.Log.Errorf("append raftIndex entry error,err:%s", err)
return err
}
return nil
}
func (s *Segment) readRaftEntries(first uint64, last uint64, maxSize uint64) ([]raftpb.Entry, uint64, error) {
s.Mu.RLock()
defer s.Mu.RUnlock()
var (
entries []raftpb.Entry
readSize uint64
)
if s.Status == SegmentClosed {
return nil, 0, ErrClosed
}
readEntriesCount := int(last - first + 1)
//create entries with a limit size
if readEntriesCount < commonSliceSize {
entries = make([]raftpb.Entry, 0, readEntriesCount)
} else {
entries = make([]raftpb.Entry, 0, commonSliceSize)
}
//get the file position of first
position, err := s.LogIndex.getRaftEntryPosition(first, s.FirstIndex, s.Status)
if err != nil {
return nil, 0, err
}
for {
entry, nextReadPosition := s.LogFile.readRaftEntry(position)
entries = append(entries, entry)
readSize += uint64(entry.Size())
if readSize > maxSize {
break
}
if entry.Index == last {
break
}
if entry.Index == s.LastIndex {
break
}
//update the read position
position = nextReadPosition
}
if readSize > maxSize {
return entries, 0, nil
}
return entries, maxSize - readSize, nil
}
func (s *Segment) needRolling(size int) bool {
s.Mu.RLock()
defer s.Mu.RUnlock()
return s.LogFile.writePosition+size > s.LogFile.maxBytes
}
func (s *Segment) close() {
s.Mu.Lock()
defer s.Mu.Unlock()
s.Status = SegmentClosed
s.FirstIndex = 0
s.LastIndex = 0
err := s.LogFile.close()
if err != nil {
log.Log.Fatalf("close segment file error,err:%s,filePath:%s", err, s.LogFile.filePath)
}
err = s.LogIndex.close()
if err != nil {
log.Log.Fatalf("close index file error,err:%s,segmentPath:%s", err, s.LogFile.filePath)
}
}
func (s *Segment) truncateSuffix(i uint64) error {
s.Mu.Lock()
defer s.Mu.Unlock()
//truncate segment file
segmentPos, err := s.LogIndex.getRaftEntryPosition(i, s.FirstIndex, s.Status)
if err != nil {
return err
}
err = s.LogFile.truncateSuffix(segmentPos)
if err != nil {
return err
}
//truncate index file
if s.Status == SegmentRDWR {
err = s.LogIndex.truncateSuffix(i, s.FirstIndex, SegmentRDWR)
if err != nil {
return err
}
} else {
//rename LogFile
newName := fmt.Sprintf(ReadWriteSegmentPattern, s.FirstIndex)
err := s.LogFile.rename(newName)
if err != nil {
return err
}
err = s.LogIndex.truncateSuffix(i, s.FirstIndex, SegmentReadOnly)
if err != nil {
return err
}
s.Status = SegmentRDWR
}
//update segment range
if i == s.FirstIndex {
s.FirstIndex = 0
s.LastIndex = 0
} else {
s.LastIndex = i - 1
}
return nil
}
func (s *Segment) changeToReadonly() error {
s.Mu.Lock()
defer s.Mu.Unlock()
if s.Status != SegmentRDWR {
return ErrArgsIllegal
}
//change the name from "%020d-inprogress.log" to "%020d-%020d.log"
newName := fmt.Sprintf(ReadonlySegmentPattern, s.FirstIndex, s.LastIndex)
err := s.LogFile.rename(newName)
if err != nil {
return err
}
err = s.LogIndex.changeToReadonly()
if err != nil {
return err
}
s.Status = SegmentReadOnly
return nil
}
func (s *Segment) remove() error {
s.Mu.Lock()
defer s.Mu.Unlock()
s.Status = SegmentClosed
s.FirstIndex = 0
s.LastIndex = 0
err := s.LogFile.remove()
if err != nil {
log.Log.Errorf("remove segment error,err:%s,filePath:%s", err, s.LogFile.filePath)
return err
}
err = s.LogIndex.remove()
if err != nil {
log.Log.Errorf("remove logIndex error,err:%s", err)
return err
}
return nil
}