-
Notifications
You must be signed in to change notification settings - Fork 0
/
opslog.go
326 lines (271 loc) · 6.63 KB
/
opslog.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
package fifodb
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"sync"
"github.com/golang-collections/go-datastructures/bitarray"
"github.com/pkg/errors"
)
const (
opWr = iota + 1
opDel
)
// opsLog represents log of operations of operations like
// - write message (push)
// - remove message (ack/reject)
// - requeue message (nack)
//
// We need that opsLog to handle messages that we should remove or requeue after storage (server) restart
// Each segmentsController segment has it's own WAL segment.
type opsLog struct {
sync.RWMutex
path string
logSegments map[uint32]*logSegment
}
type logSegment struct {
sync.Mutex
segID uint32
fd *os.File
wr *bufio.Writer
// At first view that 8 bytes looks like overhead, cause we really need just 5 bytes for operation
// 1 to opId and 4 to offset. With 8 byte length we try to avoid data corruption on separate writes inside
// buffered writer
entryData [8]byte
}
func (seg *logSegment) sync() error {
if err := seg.wr.Flush(); err != nil {
return err
}
return seg.fd.Sync()
}
func (seg *logSegment) close() error {
if err := seg.sync(); err != nil {
return err
}
return seg.fd.Close()
}
func newOperationsLog(path string) *opsLog {
log := &opsLog{
path: path,
logSegments: make(map[uint32]*logSegment),
}
return log
}
// Write writes write operation into WAL.
func (log *opsLog) Write(segID uint32, offset uint32) error {
return log.writeOp(opWr, segID, offset)
}
// Delete writes remove operation into WAL.
func (log *opsLog) Delete(segID uint32, offset uint32) error {
return log.writeOp(opDel, segID, offset)
}
func (log *opsLog) writeOp(op byte, segID uint32, offset uint32) error {
var (
seg *logSegment
err error
)
if seg, err = log.getSegment(segID); err != nil {
return err
}
seg.Lock()
seg.entryData[3] = op
binary.BigEndian.PutUint32(seg.entryData[4:], offset)
_, err = seg.wr.Write(seg.entryData[:])
seg.Unlock()
return err
}
func (log *opsLog) getSegment(segID uint32) (seg *logSegment, err error) {
log.RLock()
if seg, ok := log.logSegments[segID]; ok && seg != nil {
log.RUnlock()
return seg, nil
}
log.RUnlock()
log.Lock()
if seg, err = log.openWalSegment(segID); err != nil {
log.Unlock()
return nil, err
}
log.logSegments[segID] = seg
log.Unlock()
return seg, nil
}
func (log *opsLog) openWalSegment(segID uint32) (seg *logSegment, err error) {
seg = &logSegment{}
if seg.fd, err = os.OpenFile(log.fileNameBySegID(segID), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600); err != nil {
return nil, err
}
// @todo 64<<10 can that bufferSize be configurable?
seg.wr = bufio.NewWriterSize(seg.fd, 64<<10)
seg.segID = segID
return seg, nil
}
func (log *opsLog) fileNameBySegID(segID uint32) string {
return path.Join(log.path, fmt.Sprintf(opsLogSegmentsPattern, segID))
}
func (log *opsLog) getSegIDByFileName(name string) (segID uint32, err error) {
if _, err = fmt.Sscanf(name, opsLogSegmentsPattern, &segID); err != nil {
return 0, errors.Wrapf(err, "get segId by file name '%s'", name)
}
return
}
func (log *opsLog) isCorrectFileName(name string) bool {
return strings.HasPrefix(name, opsLogSegmentsName)
}
func (log *opsLog) release(segID uint32) error {
return os.Remove(log.fileNameBySegID(segID))
}
func (log *opsLog) createAfterCompaction(offsets []uint32, segID uint32) (err error) {
for _, offset := range offsets {
if err = log.writeOp(opWr, segID, offset); err != nil {
return err
}
}
logSeg, err := log.getSegment(segID)
if err != nil {
return err
}
return logSeg.sync()
}
func (log *opsLog) getDataForCompaction(segID uint32) (meta *compactionMeta, err error) {
// we accumulate only requeue offset, cause if we have empty reqOffsets at the end
// we can remove log file
var (
segFd *os.File
data [8]byte
op byte
offset uint64
n int
)
segFileName := log.fileNameBySegID(segID)
if _, err = os.Stat(segFileName); os.IsNotExist(err) {
return
}
if segFd, err = os.OpenFile(segFileName, os.O_RDONLY, 0600); err != nil {
return
}
fileStat, err := os.Stat(path.Join(log.path, fmt.Sprintf(dataSegmentsPattern, segID)))
if err != nil {
return nil, err
}
fileSize := uint64(fileStat.Size())
bitArraySize := roundUp(fileSize)
meta = &compactionMeta{
delOffsets: bitarray.NewBitArray(bitArraySize),
}
defer func() {
if closeErr := segFd.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
reader := bufio.NewReaderSize(segFd, 4<<20)
_, err = io.ReadFull(reader, data[:])
for err != io.EOF || err == nil {
op = data[3]
offset = uint64(binary.BigEndian.Uint32(data[4:]))
switch op {
case opWr:
meta.wrCnt++
case opDel:
if ok, _ := meta.delOffsets.GetBit(offset); !ok {
if err = meta.delOffsets.SetBit(offset); err != nil {
break
}
meta.delCnt++
}
}
if n, err = io.ReadFull(reader, data[:]); n != 8 {
break
}
}
if err != nil && err != io.EOF {
if err != io.ErrUnexpectedEOF {
return
}
}
return meta, nil
}
func (log *opsLog) sync() error {
log.Lock()
defer log.Unlock()
for _, seg := range log.logSegments {
if seg != nil {
if err := seg.sync(); err != nil {
return err
}
}
}
return nil
}
func (log *opsLog) syncSeg(segID uint32) error {
var (
seg *logSegment
ok bool
)
log.RLock()
defer log.RUnlock()
if seg, ok = log.logSegments[segID]; !ok || seg == nil {
return fmt.Errorf("segment [%d] has not been opened yet", segID)
}
return seg.sync()
}
func (log *opsLog) Close() error {
log.Lock()
defer log.Unlock()
for idx, seg := range log.logSegments {
if seg != nil {
if err := seg.close(); err != nil {
return err
}
delete(log.logSegments, idx)
}
}
return nil
}
// Clean
func (log *opsLog) Clean() error {
log.Lock()
defer log.Unlock()
for idx, seg := range log.logSegments {
if seg != nil {
if err := seg.close(); err != nil {
return err
}
delete(log.logSegments, idx)
}
}
files, err := ioutil.ReadDir(log.path)
if err != nil {
return errors.Wrapf(err, "failed to read opslog's directory: %s", log.path)
}
var segID uint32
for _, f := range files {
if segID, err = log.getSegIDByFileName(f.Name()); err != nil {
continue
}
if err := os.Remove(log.fileNameBySegID(segID)); err != nil && !os.IsNotExist(err) {
return err
}
}
return nil
}
type compactionMeta struct {
wrCnt uint64
reqCnt uint64
delCnt uint64
delOffsets bitarray.BitArray
}
// Write writes write operation into WAL
func (m *compactionMeta) full() bool {
return m.wrCnt == m.delCnt
}
// Write writes write operation into WAL
func (m *compactionMeta) needCompactLog() bool {
return !m.full() && m.reqCnt != 0 || m.delCnt != 0
}