-
Notifications
You must be signed in to change notification settings - Fork 23
/
wal_rw_segment.go
243 lines (188 loc) · 5.79 KB
/
wal_rw_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
// Copyright 2023 StreamNative, Inc.
//
// 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 wal
import (
"encoding/binary"
"os"
"sync"
"time"
"github.com/edsrzf/mmap-go"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
type ReadWriteSegment interface {
ReadOnlySegment
Append(offset int64, data []byte) error
Truncate(lastSafeOffset int64) error
HasSpace(l int) bool
Flush() error
}
type readWriteSegment struct {
sync.RWMutex
path string
baseOffset int64
lastOffset int64
txnFile *os.File
txnMappedFile mmap.MMap
currentFileOffset uint32
writingIdx []byte
segmentSize uint32
}
func newReadWriteSegment(basePath string, baseOffset int64, segmentSize uint32) (ReadWriteSegment, error) {
var err error
if _, err = os.Stat(basePath); os.IsNotExist(err) {
if err = os.MkdirAll(basePath, 0755); err != nil {
return nil, errors.Wrapf(err, "failed to create wal directory %s", basePath)
}
}
ms := &readWriteSegment{
path: segmentPath(basePath, baseOffset),
baseOffset: baseOffset,
segmentSize: segmentSize,
}
txnPath := ms.path + txnExtension
var segmentExists bool
if _, err = os.Stat(txnPath); os.IsNotExist(err) {
// The segment file does not exist yet, create file and initialize it
segmentExists = false
} else {
segmentExists = true
}
if ms.txnFile, err = os.OpenFile(txnPath, os.O_CREATE|os.O_RDWR, 0644); err != nil {
return nil, errors.Wrapf(err, "failed to open segment file %s", txnPath)
}
if !segmentExists {
if err = initFileWithZeroes(ms.txnFile, segmentSize); err != nil {
return nil, err
}
}
if ms.txnMappedFile, err = mmap.MapRegion(ms.txnFile, int(segmentSize), mmap.RDWR, 0, 0); err != nil {
return nil, errors.Wrapf(err, "failed to map segment file %s", txnPath)
}
if err = ms.rebuildIdx(); err != nil {
return nil, errors.Wrapf(err, "failed to rebuild index for segment file %s", txnPath)
}
return ms, nil
}
func (ms *readWriteSegment) BaseOffset() int64 {
return ms.baseOffset
}
func (ms *readWriteSegment) LastOffset() int64 {
ms.RLock()
defer ms.RUnlock()
return ms.lastOffset
}
func (ms *readWriteSegment) Read(offset int64) ([]byte, error) {
ms.Lock()
defer ms.Unlock()
fileOffset := fileOffset(ms.writingIdx, ms.baseOffset, offset)
entryLen := readInt(ms.txnMappedFile, fileOffset)
entry := make([]byte, entryLen)
copy(entry, ms.txnMappedFile[fileOffset+4:fileOffset+4+entryLen])
return entry, nil
}
func (ms *readWriteSegment) HasSpace(l int) bool {
return ms.currentFileOffset+4+uint32(l) <= ms.segmentSize
}
func (ms *readWriteSegment) Append(offset int64, data []byte) error {
ms.Lock()
defer ms.Unlock()
if offset != ms.lastOffset+1 {
return ErrInvalidNextOffset
}
entryOffset := ms.currentFileOffset
entrySize := uint32(len(data))
binary.BigEndian.PutUint32(ms.txnMappedFile[ms.currentFileOffset:], entrySize)
copy(ms.txnMappedFile[ms.currentFileOffset+4:], data)
ms.currentFileOffset += 4 + entrySize
ms.lastOffset = offset
ms.writingIdx = binary.BigEndian.AppendUint32(ms.writingIdx, entryOffset)
return nil
}
func (ms *readWriteSegment) Flush() error {
return ms.txnMappedFile.Flush()
}
//nolint:unparam
func (ms *readWriteSegment) rebuildIdx() error {
// Scan the mapped file and rebuild the index
entryOffset := ms.baseOffset
for ms.currentFileOffset < ms.segmentSize {
size := readInt(ms.txnMappedFile, ms.currentFileOffset)
if size == 0 || size > (ms.segmentSize-ms.currentFileOffset) {
break
}
ms.writingIdx = binary.BigEndian.AppendUint32(ms.writingIdx, ms.currentFileOffset)
ms.currentFileOffset += 4 + size
entryOffset++
}
ms.lastOffset = entryOffset - 1
return nil
}
func (*readWriteSegment) OpenTimestamp() time.Time {
return time.Now()
}
func (ms *readWriteSegment) Close() error {
ms.Lock()
defer ms.Unlock()
return multierr.Combine(
ms.txnMappedFile.Unmap(),
ms.txnFile.Close(),
// Write index file
ms.writeIndex(),
)
}
func (ms *readWriteSegment) Delete() error {
return multierr.Combine(
ms.Close(),
os.Remove(ms.path+txnExtension),
os.Remove(ms.path+idxExtension),
)
}
func (ms *readWriteSegment) writeIndex() error {
idxPath := ms.path + idxExtension
idxFile, err := os.OpenFile(idxPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return errors.Wrapf(err, "failed to open index file %s", idxPath)
}
if _, err = idxFile.Write(ms.writingIdx); err != nil {
return errors.Wrapf(err, "failed write index file %s", idxPath)
}
return idxFile.Close()
}
func (ms *readWriteSegment) Truncate(lastSafeOffset int64) error {
if lastSafeOffset < ms.baseOffset || lastSafeOffset > ms.lastOffset {
return ErrOffsetOutOfBounds
}
// Write zeroes in the section to clear
fileLastSafeOffset := fileOffset(ms.writingIdx, ms.baseOffset, lastSafeOffset)
entryLen := readInt(ms.txnMappedFile, fileLastSafeOffset)
fileEndOffset := fileLastSafeOffset + 4 + entryLen
for i := fileEndOffset; i < ms.currentFileOffset; i++ {
ms.txnMappedFile[i] = 0
}
// Truncate the index
ms.writingIdx = ms.writingIdx[:4*(lastSafeOffset-ms.baseOffset+1)]
ms.currentFileOffset = fileEndOffset
ms.lastOffset = lastSafeOffset
return ms.Flush()
}
func initFileWithZeroes(f *os.File, size uint32) error {
if _, err := f.Seek(int64(size), 0); err != nil {
return err
}
if _, err := f.Write([]byte{0x00}); err != nil {
return err
}
return f.Sync()
}