-
Notifications
You must be signed in to change notification settings - Fork 1
/
compaction.go
222 lines (187 loc) · 4.79 KB
/
compaction.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
package eagle
import (
"log"
"sync"
"github.com/ostafen/eagle/util"
)
const compactFilename = "COMPACT"
type compactRegistry struct {
currWriteFileId int
lastCompactedFileId int
compacting bool
}
func readCompactFile(path string) (*compactRegistry, error) {
registry := &compactRegistry{
currWriteFileId: -1,
lastCompactedFileId: -1,
}
err := readFile(path+"/"+compactFilename, registry)
return registry, err
}
func (r *compactRegistry) Save(path string) (*compactRegistry, error) {
err := SaveToFile(path+"/"+compactFilename, r)
return r, err
}
type fileCompactor struct {
wg sync.WaitGroup
db *DB
quit chan struct{}
fileChan chan *logFile
currFileId util.AtomicUint32
currWriteFile *logFile
lock sync.Mutex
compactionMap map[uint32]struct{}
}
func newFileCompactor(db *DB) *fileCompactor {
return &fileCompactor{
db: db,
quit: make(chan struct{}, 1),
fileChan: make(chan *logFile, 100),
compactionMap: make(map[uint32]struct{}),
currWriteFile: nil,
}
}
func (fc *fileCompactor) rotateCurrentFile() error {
if fc.currWriteFile != nil {
if err := fc.currWriteFile.Sync(); err != nil {
return err
}
}
newFile, err := fc.db.createNewLogFile()
if err != nil {
return err
}
fc.currFileId.Set(newFile.FileId)
fc.currWriteFile = newFile
fc.setCompacting(fc.currWriteFile)
return nil
}
func (fc *fileCompactor) setCompacting(file *logFile) error {
if fc.currWriteFile != nil {
compactRegistry := &compactRegistry{
currWriteFileId: int(fc.currWriteFile.FileId),
compacting: true,
lastCompactedFileId: -1,
}
return SaveToFile(fc.db.rootDir+"/"+compactFilename, compactRegistry)
}
return nil
}
func (fc *fileCompactor) setCompacted(file *logFile) error {
if fc.currWriteFile != nil {
compactRegistry := &compactRegistry{
currWriteFileId: int(fc.currWriteFile.FileId), // unuseful in this case
compacting: false,
lastCompactedFileId: int(file.FileId),
}
return SaveToFile(fc.db.rootDir+"/"+compactFilename, compactRegistry)
}
return nil
}
func (fc *fileCompactor) ensureRoomForWrite(size uint32) error {
if fc.currWriteFile == nil || fc.currWriteFile.Size()+size > fc.db.opts.MaxFileSize {
if err := fc.rotateCurrentFile(); err != nil {
return err
}
}
return nil
}
func (fc *fileCompactor) compactFile(file *logFile) error {
if err := fc.setCompacting(file); err != nil {
return err
}
nRecordCopied := 0
it := file.keyFile.Iterator()
for it.HasNext() {
entry, _, err := it.Next()
if err != nil {
return err
}
rInfo := fc.db.table.Get(entry.Key)
if rInfo != nil && entry.SeqNumber == rInfo.seqNumber { // record is not stale
if err := fc.ensureRoomForWrite(recordSize(len(entry.Key), int(entry.ValueSize))); err != nil {
return err
}
newInfo := &recordInfo{seqNumber: entry.SeqNumber, ptr: nil}
var value []byte = nil
if entry.ValueSize > 0 {
if value, err = file.ReadPointer(rInfo.ptr); err != nil {
return err
}
}
writeOffset := fc.currWriteFile.valueFile.size
r := &Record{Key: entry.Key, Value: value, SeqNumber: entry.SeqNumber}
if _, err = fc.currWriteFile.AppendRecord(r); err != nil {
return err
}
if entry.ValueSize > 0 {
newInfo.ptr = &ValuePointer{
FileId: fc.currWriteFile.FileId,
valueOffset: writeOffset,
valueSize: entry.ValueSize,
}
}
if _, updated := fc.db.table.Update(entry.Key, newInfo); updated {
nRecordCopied++
} else {
fc.db.markPreviousAsStale(fc.currWriteFile.FileId, entry.ValueSize)
}
}
}
if nRecordCopied > 0 {
if err := fc.currWriteFile.Sync(); err != nil {
return err
}
}
if err := fc.db.removeFile(file.FileId); err != nil {
return err
}
log.Printf("completed compaction of file with id %d: copied %d records.\n", file.FileId, nRecordCopied)
return fc.setCompacted(file)
}
func (fc *fileCompactor) start() {
fc.wg.Add(1)
go func() {
defer fc.wg.Done()
for {
select {
case file := <-fc.fileChan:
err := fc.compactFile(file)
if err != nil {
log.Printf("an error occurred while compacting file %d: %s\n", file.FileId, err.Error())
}
fc.markAsCompacted(file.FileId)
case <-fc.quit:
return
}
}
}()
}
func (fc *fileCompactor) stop() error {
fc.quit <- struct{}{}
fc.wg.Wait()
return nil
}
func (fc *fileCompactor) markAsCompacted(fileId uint32) {
fc.lock.Lock()
delete(fc.compactionMap, fileId)
fc.lock.Unlock()
}
func (fc *fileCompactor) sendFile(file *logFile) bool {
fc.lock.Lock()
defer fc.lock.Unlock()
if fc.currFileId.Get() == file.FileId {
return false
}
_, ok := fc.compactionMap[file.FileId]
if !ok {
select {
case fc.fileChan <- file:
fc.compactionMap[file.FileId] = struct{}{}
return true
default:
return false
}
}
return false
}