forked from rosedblabs/wal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.go
562 lines (483 loc) · 14.9 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package wal
import (
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"os"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/valyala/bytebufferpool"
)
type ChunkType = byte
type SegmentID = uint32
const (
ChunkTypeFull ChunkType = iota
ChunkTypeFirst
ChunkTypeMiddle
ChunkTypeLast
)
var (
ErrClosed = errors.New("the segment file is closed")
ErrInvalidCRC = errors.New("invalid crc, the data may be corrupted")
)
const (
// 7 Bytes
// Checksum Length Type
// 4 2 1
chunkHeaderSize = 7
// 32 KB
blockSize = 32 * KB
fileModePerm = 0644
// uin32 + uint32 + int64 + uin32
// segmentId + BlockNumber + ChunkOffset + ChunkSize
maxLen = binary.MaxVarintLen32*3 + binary.MaxVarintLen64
)
// Segment represents a single segment file in WAL.
// The segment file is append-only, and the data is written in blocks.
// Each block is 32KB, and the data is written in chunks.
type segment struct {
id SegmentID
fd *os.File
currentBlockNumber uint32
currentBlockSize uint32
closed bool
cache *lru.Cache[uint64, []byte]
header []byte
blockPool sync.Pool
}
// segmentReader is used to iterate all the data from the segment file.
// You can call Next to get the next chunk data,
// and io.EOF will be returned when there is no data.
type segmentReader struct {
segment *segment
blockNumber uint32
chunkOffset int64
}
// block and chunk header, saved in pool.
type blockAndHeader struct {
block []byte
header []byte
}
// ChunkPosition represents the position of a chunk in a segment file.
// Used to read the data from the segment file.
type ChunkPosition struct {
SegmentId SegmentID
// BlockNumber The block number of the chunk in the segment file.
BlockNumber uint32
// ChunkOffset The start offset of the chunk in the segment file.
ChunkOffset int64
// ChunkSize How many bytes the chunk data takes up in the segment file.
ChunkSize uint32
}
// openSegmentFile a new segment file.
func openSegmentFile(dirPath, extName string, id uint32, cache *lru.Cache[uint64, []byte]) (*segment, error) {
fd, err := os.OpenFile(
SegmentFileName(dirPath, extName, id),
os.O_CREATE|os.O_RDWR|os.O_APPEND,
fileModePerm,
)
if err != nil {
return nil, err
}
// set the current block number and block size.
offset, err := fd.Seek(0, io.SeekEnd)
if err != nil {
panic(fmt.Errorf("seek to the end of segment file %d%s failed: %v", id, extName, err))
}
return &segment{
id: id,
fd: fd,
cache: cache,
header: make([]byte, chunkHeaderSize),
blockPool: sync.Pool{New: newBlockAndHeader},
currentBlockNumber: uint32(offset / blockSize),
currentBlockSize: uint32(offset % blockSize),
}, nil
}
func newBlockAndHeader() interface{} {
return &blockAndHeader{
block: make([]byte, blockSize),
header: make([]byte, chunkHeaderSize),
}
}
// NewReader creates a new segment reader.
// You can call Next to get the next chunk data,
// and io.EOF will be returned when there is no data.
func (seg *segment) NewReader() *segmentReader {
return &segmentReader{
segment: seg,
blockNumber: 0,
chunkOffset: 0,
}
}
// Sync flushes the segment file to disk.
func (seg *segment) Sync() error {
if seg.closed {
return nil
}
return seg.fd.Sync()
}
// Remove removes the segment file.
func (seg *segment) Remove() error {
if !seg.closed {
seg.closed = true
_ = seg.fd.Close()
}
return os.Remove(seg.fd.Name())
}
// Close closes the segment file.
func (seg *segment) Close() error {
if seg.closed {
return nil
}
seg.closed = true
return seg.fd.Close()
}
// Size returns the size of the segment file.
func (seg *segment) Size() int64 {
size := int64(seg.currentBlockNumber) * int64(blockSize)
return size + int64(seg.currentBlockSize)
}
// writeToBuffer calculate chunkPosition for data, write data to bytebufferpool, update segment status
// The data will be written in chunks, and the chunk has four types:
// ChunkTypeFull, ChunkTypeFirst, ChunkTypeMiddle, ChunkTypeLast.
//
// Each chunk has a header, and the header contains the length, type and checksum.
// And the payload of the chunk is the real data you want to Write.
func (seg *segment) writeToBuffer(data []byte, chunkBuffer *bytebufferpool.ByteBuffer) (*ChunkPosition, error) {
startBufferLen := chunkBuffer.Len()
padding := uint32(0)
if seg.closed {
return nil, ErrClosed
}
// if the left block size can not hold the chunk header, padding the block
if seg.currentBlockSize+chunkHeaderSize >= blockSize {
// padding if necessary
if seg.currentBlockSize < blockSize {
p := make([]byte, blockSize-seg.currentBlockSize)
chunkBuffer.B = append(chunkBuffer.B, p...)
padding += blockSize - seg.currentBlockSize
// a new block
seg.currentBlockNumber += 1
seg.currentBlockSize = 0
}
}
// return the start position of the chunk, then the user can use it to read the data.
position := &ChunkPosition{
SegmentId: seg.id,
BlockNumber: seg.currentBlockNumber,
ChunkOffset: int64(seg.currentBlockSize),
}
dataSize := uint32(len(data))
// The entire chunk can fit into the block.
if seg.currentBlockSize+dataSize+chunkHeaderSize <= blockSize {
seg.appendChunkBuffer(chunkBuffer, data, ChunkTypeFull)
position.ChunkSize = dataSize + chunkHeaderSize
} else {
// If the size of the data exceeds the size of the block,
// the data should be written to the block in batches.
var (
leftSize = dataSize
blockCount uint32 = 0
currBlockSize = seg.currentBlockSize
)
for leftSize > 0 {
chunkSize := blockSize - currBlockSize - chunkHeaderSize
if chunkSize > leftSize {
chunkSize = leftSize
}
var end = dataSize - leftSize + chunkSize
if end > dataSize {
end = dataSize
}
// append the chunks to the buffer
var chunkType ChunkType
switch leftSize {
case dataSize: // First chunk
chunkType = ChunkTypeFirst
case chunkSize: // Last chunk
chunkType = ChunkTypeLast
default: // Middle chunk
chunkType = ChunkTypeMiddle
}
seg.appendChunkBuffer(chunkBuffer, data[dataSize-leftSize:end], chunkType)
leftSize -= chunkSize
blockCount += 1
currBlockSize = (currBlockSize + chunkSize + chunkHeaderSize) % blockSize
}
position.ChunkSize = blockCount*chunkHeaderSize + dataSize
}
// the buffer length must be equal to chunkSize+padding length
endBufferLen := chunkBuffer.Len()
if position.ChunkSize+padding != uint32(endBufferLen-startBufferLen) {
panic(fmt.Sprintf("wrong!!! the chunk size %d is not equal to the buffer len %d",
position.ChunkSize+padding, endBufferLen-startBufferLen))
}
// update segment status
seg.currentBlockSize += position.ChunkSize
if seg.currentBlockSize >= blockSize {
seg.currentBlockNumber += seg.currentBlockSize / blockSize
seg.currentBlockSize = seg.currentBlockSize % blockSize
}
return position, nil
}
// writeAll write batch data to the segment file.
func (seg *segment) writeAll(data [][]byte) (positions []*ChunkPosition, err error) {
if seg.closed {
return nil, ErrClosed
}
// if any error occurs, restore the segment status
originBlockNumber := seg.currentBlockNumber
originBlockSize := seg.currentBlockSize
// init chunk buffer
chunkBuffer := bytebufferpool.Get()
chunkBuffer.Reset()
defer func() {
if err != nil {
seg.currentBlockNumber = originBlockNumber
seg.currentBlockSize = originBlockSize
}
bytebufferpool.Put(chunkBuffer)
}()
// write all data to the chunk buffer
var pos *ChunkPosition
positions = make([]*ChunkPosition, len(data))
for i := 0; i < len(positions); i++ {
pos, err = seg.writeToBuffer(data[i], chunkBuffer)
if err != nil {
return
}
positions[i] = pos
}
// write the chunk buffer to the segment file
if err = seg.writeChunkBuffer(chunkBuffer); err != nil {
return
}
return
}
// Write writes the data to the segment file.
func (seg *segment) Write(data []byte) (pos *ChunkPosition, err error) {
if seg.closed {
return nil, ErrClosed
}
originBlockNumber := seg.currentBlockNumber
originBlockSize := seg.currentBlockSize
// init chunk buffer
chunkBuffer := bytebufferpool.Get()
chunkBuffer.Reset()
defer func() {
if err != nil {
seg.currentBlockNumber = originBlockNumber
seg.currentBlockSize = originBlockSize
}
bytebufferpool.Put(chunkBuffer)
}()
// write all data to the chunk buffer
pos, err = seg.writeToBuffer(data, chunkBuffer)
if err != nil {
return
}
// write the chunk buffer to the segment file
if err = seg.writeChunkBuffer(chunkBuffer); err != nil {
return
}
return
}
func (seg *segment) appendChunkBuffer(buf *bytebufferpool.ByteBuffer, data []byte, chunkType ChunkType) {
// Length 2 Bytes index:4-5
binary.LittleEndian.PutUint16(seg.header[4:6], uint16(len(data)))
// Type 1 Byte index:6
seg.header[6] = chunkType
// Checksum 4 Bytes index:0-3
sum := crc32.ChecksumIEEE(seg.header[4:])
sum = crc32.Update(sum, crc32.IEEETable, data)
binary.LittleEndian.PutUint32(seg.header[:4], sum)
// append the header and data to segment chunk buffer
buf.B = append(buf.B, seg.header...)
buf.B = append(buf.B, data...)
}
// write the pending chunk buffer to the segment file
func (seg *segment) writeChunkBuffer(buf *bytebufferpool.ByteBuffer) error {
if seg.currentBlockSize > blockSize {
panic("wrong! can not exceed the block size")
}
// write the data into underlying file
if _, err := seg.fd.Write(buf.Bytes()); err != nil {
return err
}
return nil
}
// Read reads the data from the segment file by the block number and chunk offset.
func (seg *segment) Read(blockNumber uint32, chunkOffset int64) ([]byte, error) {
value, _, err := seg.readInternal(blockNumber, chunkOffset)
return value, err
}
func (seg *segment) readInternal(blockNumber uint32, chunkOffset int64) ([]byte, *ChunkPosition, error) {
if seg.closed {
return nil, nil, ErrClosed
}
var (
result []byte
bh = seg.blockPool.Get().(*blockAndHeader)
segSize = seg.Size()
nextChunk = &ChunkPosition{SegmentId: seg.id}
)
defer func() {
seg.blockPool.Put(bh)
}()
for {
size := int64(blockSize)
offset := int64(blockNumber) * blockSize
if size+offset > segSize {
size = segSize - offset
}
if chunkOffset >= size {
return nil, nil, io.EOF
}
var ok bool
var cachedBlock []byte
// try to read from the cache if it is enabled
if seg.cache != nil {
cachedBlock, ok = seg.cache.Get(seg.getCacheKey(blockNumber))
}
// cache hit, get block from the cache
if ok {
copy(bh.block, cachedBlock)
} else {
// cache miss, read block from the segment file
_, err := seg.fd.ReadAt(bh.block[0:size], offset)
if err != nil {
return nil, nil, err
}
// cache the block, so that the next time it can be read from the cache.
// if the block size is smaller than blockSize, it means that the block is not full,
// so we will not cache it.
if seg.cache != nil && size == blockSize && len(cachedBlock) == 0 {
cacheBlock := make([]byte, blockSize)
copy(cacheBlock, bh.block)
seg.cache.Add(seg.getCacheKey(blockNumber), cacheBlock)
}
}
// header
copy(bh.header, bh.block[chunkOffset:chunkOffset+chunkHeaderSize])
// length
length := binary.LittleEndian.Uint16(bh.header[4:6])
// copy data
start := chunkOffset + chunkHeaderSize
result = append(result, bh.block[start:start+int64(length)]...)
// check sum
checksumEnd := chunkOffset + chunkHeaderSize + int64(length)
checksum := crc32.ChecksumIEEE(bh.block[chunkOffset+4 : checksumEnd])
savedSum := binary.LittleEndian.Uint32(bh.header[:4])
if savedSum != checksum {
return nil, nil, ErrInvalidCRC
}
// type
chunkType := bh.header[6]
if chunkType == ChunkTypeFull || chunkType == ChunkTypeLast {
nextChunk.BlockNumber = blockNumber
nextChunk.ChunkOffset = checksumEnd
// If this is the last chunk in the block, and the left block
// space are paddings, the next chunk should be in the next block.
if checksumEnd+chunkHeaderSize >= blockSize {
nextChunk.BlockNumber += 1
nextChunk.ChunkOffset = 0
}
break
}
blockNumber += 1
chunkOffset = 0
}
return result, nextChunk, nil
}
func (seg *segment) getCacheKey(blockNumber uint32) uint64 {
return uint64(seg.id)<<32 | uint64(blockNumber)
}
// Next returns the Next chunk data.
// You can call it repeatedly until io.EOF is returned.
func (segReader *segmentReader) Next() ([]byte, *ChunkPosition, error) {
// The segment file is closed
if segReader.segment.closed {
return nil, nil, ErrClosed
}
// this position describes the current chunk info
chunkPosition := &ChunkPosition{
SegmentId: segReader.segment.id,
BlockNumber: segReader.blockNumber,
ChunkOffset: segReader.chunkOffset,
}
value, nextChunk, err := segReader.segment.readInternal(
segReader.blockNumber,
segReader.chunkOffset,
)
if err != nil {
return nil, nil, err
}
// Calculate the chunk size.
// Remember that the chunk size is just an estimated value,
// not accurate, so don't use it for any important logic.
chunkPosition.ChunkSize =
nextChunk.BlockNumber*blockSize + uint32(nextChunk.ChunkOffset) -
(segReader.blockNumber*blockSize + uint32(segReader.chunkOffset))
// update the position
segReader.blockNumber = nextChunk.BlockNumber
segReader.chunkOffset = nextChunk.ChunkOffset
return value, chunkPosition, nil
}
// Encode encodes the chunk position to a byte slice.
// Return the slice with the actual occupied elements.
// You can decode it by calling wal.DecodeChunkPosition().
func (cp *ChunkPosition) Encode() []byte {
return cp.encode(true)
}
// EncodeFixedSize encodes the chunk position to a byte slice.
// Return a slice of size "maxLen".
// You can decode it by calling wal.DecodeChunkPosition().
func (cp *ChunkPosition) EncodeFixedSize() []byte {
return cp.encode(false)
}
// encode the chunk position to a byte slice.
func (cp *ChunkPosition) encode(shrink bool) []byte {
buf := make([]byte, maxLen)
var index = 0
// SegmentId
index += binary.PutUvarint(buf[index:], uint64(cp.SegmentId))
// BlockNumber
index += binary.PutUvarint(buf[index:], uint64(cp.BlockNumber))
// ChunkOffset
index += binary.PutUvarint(buf[index:], uint64(cp.ChunkOffset))
// ChunkSize
index += binary.PutUvarint(buf[index:], uint64(cp.ChunkSize))
if shrink {
return buf[:index]
}
return buf
}
// DecodeChunkPosition decodes the chunk position from a byte slice.
// You can encode it by calling wal.ChunkPosition.Encode().
func DecodeChunkPosition(buf []byte) *ChunkPosition {
if len(buf) == 0 {
return nil
}
var index = 0
// SegmentId
segmentId, n := binary.Uvarint(buf[index:])
index += n
// BlockNumber
blockNumber, n := binary.Uvarint(buf[index:])
index += n
// ChunkOffset
chunkOffset, n := binary.Uvarint(buf[index:])
index += n
// ChunkSize
chunkSize, n := binary.Uvarint(buf[index:])
index += n
return &ChunkPosition{
SegmentId: uint32(segmentId),
BlockNumber: uint32(blockNumber),
ChunkOffset: int64(chunkOffset),
ChunkSize: uint32(chunkSize),
}
}