-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaof.go
161 lines (125 loc) · 3.22 KB
/
aof.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
package aof
import (
"bytes"
"context"
"os"
"sync"
"time"
)
type AOFOptions struct {
Path string // absolute path to the aof log directory
Fsync string // file synchronization options
// todo: blockCache uint32
// todo: bytesPerSync uint64
}
const (
aofQueueSize = 1 << 20 // 1 MB size of the channel used to communicate
aofMaxSize = 20 << 20 // 20 MB, max size of an aof file
FsyncAlways = "always" // if you want to be sure that every single operation is written to disk (it's really so slow but guarantees data integrity)
FsyncNo = "no" // if you don't care about data loss in case of a crash
FsyncEverySecond = "everysec" // (default)
)
var (
ErrEOF = os.ErrClosed
DefaultOptions = &AOFOptions{
Path: "/tmp/letly_aof",
Fsync: FsyncAlways,
}
)
type AOF struct {
sync sync.RWMutex
ctx context.Context
cancel context.CancelFunc
aofChan chan *AofCmd
Options *AOFOptions
listeners map[Listener]struct{}
buffer [][][]byte
segment *os.File
segments []*segment
reader *Reader
}
type AofCmd struct {
CmdLine [][]byte
DbIndex int
Wg *sync.WaitGroup
}
func NewAOF(aofOptions *AOFOptions) (*AOF, error) {
var err error
if aofOptions == nil {
aofOptions = DefaultOptions
}
aof := &AOF{
Options: aofOptions,
aofChan: make(chan *AofCmd, aofQueueSize),
listeners: make(map[Listener]struct{}),
}
aof.aofChan = make(chan *AofCmd, aofQueueSize)
aof.listeners = make(map[Listener]struct{})
aof.ctx, aof.cancel = context.WithCancel(context.Background())
if err = os.MkdirAll(aof.Options.Path, 0755); err != nil {
return nil, err
}
if err = aof.loadFiles(); err != nil {
return nil, err
}
go func() {
for {
select {
case <-aof.ctx.Done():
return
default:
if FsyncEverySecond == aof.Options.Fsync {
aof.fsync() // fsync every second
}
aof.Retention()
time.Sleep(time.Second * 1)
}
}
}()
return aof, nil
}
// WriteAof writes a new entry to the Append-Only Log.
func (aof *AOF) WriteAof(p *AofCmd) error {
aof.sync.Lock()
defer aof.sync.Unlock()
aof.buffer = aof.buffer[:0] // clear the buffer
aof.buffer = append(aof.buffer, p.CmdLine)
// convert buffer to bytes
bufferInBytes := aof.bufferToBytes()
// write to aof file in byte format
if _, err := aof.segment.Write(bufferInBytes); err != nil {
return err
}
if FsyncAlways == aof.Options.Fsync {
aof.segment.Sync()
}
// notify listeners
for listener := range aof.listeners {
listener.Callback(aof.buffer)
}
return nil
}
// bufferToBytes converts the buffer to bytes and adds a file header.
func (aof *AOF) bufferToBytes() []byte {
var buffer []byte
for _, cmdLine := range aof.buffer {
buffer = append(buffer, bytes.Join(cmdLine, []byte(" "))...)
}
var fileHeader FileHeader
fileHeader.Create(buffer)
fileHeaderInBytes := fileHeader.Encode()[:] // Encode to bytes
fileHeaderInBytes = append(fileHeaderInBytes, ' ')
buffer = append(fileHeaderInBytes, buffer...)
buffer = append(buffer, '\n')
return buffer
}
func (aof *AOF) GetReader(buffer []byte) (*Reader, error) {
aof.sync.RLock()
defer aof.sync.RUnlock()
var err error
aof.reader, err = NewReader(aof.Options.Path)
if err != nil {
return nil, err
}
return aof.reader, nil
}