forked from gholt/store
/
compaction.go
504 lines (481 loc) · 14.8 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
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
package valuestore
import (
"bytes"
"encoding/binary"
"errors"
"io"
"math/rand"
"os"
"path"
"sort"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/spaolacci/murmur3"
)
type compactionState struct {
interval int
workerCount int
ageThreshold int64
abort uint32
threshold float64
notifyChan chan *backgroundNotification
}
func (vs *DefaultValueStore) compactionConfig(cfg *Config) {
vs.compactionState.interval = cfg.CompactionInterval
vs.compactionState.threshold = cfg.CompactionThreshold
vs.compactionState.ageThreshold = int64(cfg.CompactionAgeThreshold * 1000000000)
vs.compactionState.notifyChan = make(chan *backgroundNotification, 1)
vs.compactionState.workerCount = cfg.CompactionWorkers
}
func (vs *DefaultValueStore) compactionLaunch() {
go vs.compactionLauncher()
}
// DisableCompaction will stop any compaction passes until
// EnableCompaction is called. A compaction pass searches for files
// with a percentage of XX deleted entries.
func (vs *DefaultValueStore) DisableCompaction() {
c := make(chan struct{}, 1)
vs.compactionState.notifyChan <- &backgroundNotification{
disable: true,
doneChan: c,
}
<-c
}
// EnableCompaction will resume compaction passes.
// A compaction pass searches for files with a percentage of XX deleted
// entries.
func (vs *DefaultValueStore) EnableCompaction() {
c := make(chan struct{}, 1)
vs.compactionState.notifyChan <- &backgroundNotification{
enable: true,
doneChan: c,
}
<-c
}
// CompactionPass will immediately execute a compaction pass to compact stale files.
func (vs *DefaultValueStore) CompactionPass() {
atomic.StoreUint32(&vs.compactionState.abort, 1)
c := make(chan struct{}, 1)
vs.compactionState.notifyChan <- &backgroundNotification{doneChan: c}
<-c
}
func (vs *DefaultValueStore) compactionLauncher() {
var enabled bool
interval := float64(vs.compactionState.interval) * float64(time.Second)
vs.randMutex.Lock()
nextRun := time.Now().Add(time.Duration(interval + interval*vs.rand.NormFloat64()*0.1))
vs.randMutex.Unlock()
for {
var notification *backgroundNotification
sleep := nextRun.Sub(time.Now())
if sleep > 0 {
select {
case notification = <-vs.compactionState.notifyChan:
case <-time.After(sleep):
}
} else {
select {
case notification = <-vs.compactionState.notifyChan:
default:
}
}
vs.randMutex.Lock()
nextRun = time.Now().Add(time.Duration(interval + interval*vs.rand.NormFloat64()*0.1))
vs.randMutex.Unlock()
if notification != nil {
if notification.enable {
enabled = true
notification.doneChan <- struct{}{}
continue
}
if notification.disable {
atomic.StoreUint32(&vs.compactionState.abort, 1)
enabled = false
notification.doneChan <- struct{}{}
continue
}
atomic.StoreUint32(&vs.compactionState.abort, 0)
vs.compactionPass()
notification.doneChan <- struct{}{}
} else if enabled {
atomic.StoreUint32(&vs.compactionState.abort, 0)
vs.compactionPass()
}
}
}
type compactionJob struct {
name string
candidateBlockID uint32
}
func (vs *DefaultValueStore) compactionPass() {
if vs.logDebug != nil {
begin := time.Now()
defer func() {
vs.logDebug.Printf("compaction pass took %s", time.Now().Sub(begin))
}()
}
fp, err := os.Open(vs.pathtoc)
if err != nil {
panic(err)
}
names, err := fp.Readdirnames(-1)
fp.Close()
if err != nil {
panic(err)
}
sort.Strings(names)
compactionJobs := make(chan compactionJob, len(names))
compactionResults := make(chan string, len(names))
//Spin up new workers on each pass rather than at startup so that
//the number of workers can change between passes.
for i := 1; i <= vs.compactionState.workerCount; i++ {
go vs.compactionWorker(i, compactionJobs, compactionResults)
}
submitted := 0
for i := 0; i < len(names); i++ {
namets, valid := vs.compactionCandidate(path.Join(vs.pathtoc, names[i]))
if valid {
compactionJobs <- compactionJob{path.Join(vs.pathtoc, names[i]), vs.valueLocBlockIDFromTimestampnano(namets)}
submitted++
}
}
close(compactionJobs)
if vs.logDebug != nil {
vs.logDebug.Println("compaction candidates submitted:", submitted)
}
for i := 1; i <= submitted; i++ {
<-compactionResults
}
close(compactionResults)
}
// compactionCandidate verifies that the given toc is a valid candidate for
// compaction and also returns the extracted namets.
// TODO: This doesn't need to be its own func anymore
func (vs *DefaultValueStore) compactionCandidate(name string) (int64, bool) {
if !strings.HasSuffix(name, ".valuestoc") {
return 0, false
}
var namets int64
_, n := path.Split(name)
namets, err := strconv.ParseInt(n[:len(n)-len(".valuestoc")], 10, 64)
if err != nil {
vs.logError.Printf("bad timestamp in name: %#v\n", name)
return 0, false
}
if namets == 0 {
vs.logError.Printf("bad timestamp in name: %#v\n", name)
return namets, false
}
if namets == int64(atomic.LoadUint64(&vs.activeTOCA)) || namets == int64(atomic.LoadUint64(&vs.activeTOCB)) {
return namets, false
}
if namets >= time.Now().UnixNano()-vs.compactionState.ageThreshold {
return namets, false
}
return namets, true
}
func (vs *DefaultValueStore) compactionWorker(id int, tocfiles <-chan compactionJob, result chan<- string) {
for c := range tocfiles {
f, err := os.Open(c.name)
if err != nil {
vs.logError.Println("Unable to open for stat:", c.name, err)
continue
}
fstat, err := f.Stat()
if err != nil {
vs.logError.Println("Unable to stat:", c.name)
continue
}
total := int(fstat.Size()) / 34
if total < 100 {
vs.logInfo.Println("Triggering compaction for", c.name, "due to size")
result, err := vs.compactFile(c.name, c.candidateBlockID)
if err != nil {
vs.logCritical.Println(err)
}
if (result.rewrote + result.stale) == result.count {
err = os.Remove(c.name)
if err != nil {
vs.logCritical.Println("Unable to remove", c.name, err)
continue
}
err = os.Remove(c.name[:len(c.name)-len("toc")])
if err != nil {
vs.logCritical.Println("Unable to remove", c.name, "values", err)
continue
}
vs.logInfo.Printf("Compacted %s (total %d, rewrote %d, stale %d)", c.name, result.count, result.rewrote, result.stale)
}
} else {
rand.Seed(time.Now().UnixNano())
skipOffset := rand.Intn(int(float64(total) * 0.01)) //randomly skip up to the first 1% of entries
skipTotal := total - skipOffset
staleTarget := int(float64(skipTotal) * vs.compactionState.threshold)
skip := skipTotal/staleTarget - 1
count, stale, err := vs.sampleTOC(c.name, c.candidateBlockID, skipOffset, skip)
if err != nil {
continue
}
if vs.logDebug != nil {
vs.logDebug.Println(c.name, "sample result:", count, stale, staleTarget)
}
if stale >= staleTarget {
if vs.logDebug != nil {
vs.logDebug.Println("Triggering compaction for", c.name, "with", count, "entries.")
}
result, err := vs.compactFile(c.name, c.candidateBlockID)
if err != nil {
vs.logCritical.Println(err)
}
if (result.rewrote + result.stale) == result.count {
err = os.Remove(c.name)
if err != nil {
vs.logCritical.Println("Unable to remove", c.name, err)
continue
}
err = os.Remove(c.name[:len(c.name)-len("toc")])
if err != nil {
vs.logCritical.Println("Unable to remove", c.name, "values", err)
continue
}
vs.logInfo.Printf("Compacted %s: (total %d, rewrote %d, stale %d)", c.name, result.count, result.rewrote, result.stale)
}
}
}
result <- c.name
}
}
func (vs *DefaultValueStore) sampleTOC(name string, candidateBlockID uint32, skipOffset, skipCount int) (int, int, error) {
count := 0
stale := 0
fromDiskBuf := make([]byte, vs.checksumInterval+4)
fromDiskOverflow := make([]byte, 0, 32)
fp, err := os.Open(name)
if err != nil {
vs.logError.Printf("error opening %s: %s\n", name, err)
return 0, 0, err
}
checksumFailures := 0
first := true
terminated := false
fromDiskOverflow = fromDiskOverflow[:0]
skipCounter := 0 - skipOffset
for {
n, err := io.ReadFull(fp, fromDiskBuf)
if n < 4 {
if err != io.EOF && err != io.ErrUnexpectedEOF {
vs.logError.Printf("error reading %s: %s\n", name, err)
}
break
}
n -= 4
if murmur3.Sum32(fromDiskBuf[:n]) != binary.BigEndian.Uint32(fromDiskBuf[n:]) {
checksumFailures++
} else {
j := 0
if first {
if !bytes.Equal(fromDiskBuf[:28], []byte("VALUESTORETOC v0 ")) {
vs.logError.Printf("bad header: %s\n", name)
break
}
if binary.BigEndian.Uint32(fromDiskBuf[28:]) != vs.checksumInterval {
vs.logError.Printf("bad header checksum interval: %s\n", name)
break
}
j += 32
first = false
}
if n < int(vs.checksumInterval) {
if binary.BigEndian.Uint32(fromDiskBuf[n-16:]) != 0 {
vs.logError.Printf("bad terminator size marker: %s\n", name)
break
}
if !bytes.Equal(fromDiskBuf[n-4:n], []byte("TERM")) {
vs.logError.Printf("bad terminator: %s\n", name)
break
}
n -= 16
terminated = true
}
if len(fromDiskOverflow) > 0 {
j += 32 - len(fromDiskOverflow)
fromDiskOverflow = append(fromDiskOverflow, fromDiskBuf[j-32+len(fromDiskOverflow):j]...)
keyB := binary.BigEndian.Uint64(fromDiskOverflow[8:])
keyA := binary.BigEndian.Uint64(fromDiskOverflow)
timestampbits := binary.BigEndian.Uint64(fromDiskOverflow[16:])
fromDiskOverflow = fromDiskOverflow[:0]
count++
if skipCounter == skipCount {
tsm, blockid, _, _ := vs.lookup(keyA, keyB)
if tsm>>_TSB_UTIL_BITS != timestampbits>>_TSB_UTIL_BITS && blockid != candidateBlockID || tsm&_TSB_DELETION != 0 {
stale++
}
skipCounter = 0
} else {
skipCounter++
}
}
for ; j+32 <= n; j += 32 {
keyB := binary.BigEndian.Uint64(fromDiskBuf[j+8:])
keyA := binary.BigEndian.Uint64(fromDiskBuf[j:])
timestampbits := binary.BigEndian.Uint64(fromDiskBuf[j+16:])
tsm, blockid, _, _ := vs.lookup(keyA, keyB)
count++
if skipCounter == skipCount {
if tsm>>_TSB_UTIL_BITS != timestampbits>>_TSB_UTIL_BITS && blockid != candidateBlockID || tsm&_TSB_DELETION != 0 {
stale++
}
skipCounter = 0
} else {
skipCounter++
}
}
if j != n {
fromDiskOverflow = fromDiskOverflow[:n-j]
copy(fromDiskOverflow, fromDiskBuf[j:])
}
}
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
vs.logError.Printf("error reading %s: %s\n", name, err)
break
}
}
fp.Close()
if !terminated {
vs.logError.Printf("early end of file: %s\n", name)
}
if checksumFailures > 0 {
vs.logWarning.Printf("%d checksum failures for %s\n", checksumFailures, name)
}
return count, stale, nil
}
type compactionResult struct {
checksumFailures int
count int
rewrote int
stale int
}
func (vs *DefaultValueStore) compactFile(name string, candidateBlockID uint32) (compactionResult, error) {
var cr compactionResult
fromDiskBuf := make([]byte, vs.checksumInterval+4)
fromDiskOverflow := make([]byte, 0, 32)
fp, err := os.Open(name)
if err != nil {
vs.logError.Printf("error opening %s: %s\n", name, err)
return cr, errors.New("Error opening toc")
}
first := true
terminated := false
fromDiskOverflow = fromDiskOverflow[:0]
for {
n, err := io.ReadFull(fp, fromDiskBuf)
if n < 4 {
if err != io.EOF && err != io.ErrUnexpectedEOF {
vs.logError.Printf("error reading %s: %s\n", name, err)
return cr, errors.New("Error attempting to read toc")
}
break
}
n -= 4
if murmur3.Sum32(fromDiskBuf[:n]) != binary.BigEndian.Uint32(fromDiskBuf[n:]) {
cr.checksumFailures++
} else {
j := 0
if first {
if !bytes.Equal(fromDiskBuf[:28], []byte("VALUESTORETOC v0 ")) {
vs.logError.Printf("bad header: %s\n", name)
return cr, errors.New("Bad header")
}
if binary.BigEndian.Uint32(fromDiskBuf[28:]) != vs.checksumInterval {
vs.logError.Printf("bad header checksum interval: %s\n", name)
return cr, errors.New("Bad header checksum interval")
}
j += 32
first = false
}
if n < int(vs.checksumInterval) {
if binary.BigEndian.Uint32(fromDiskBuf[n-16:]) != 0 {
vs.logError.Printf("bad terminator size marker: %s\n", name)
return cr, errors.New("Error on toc term size marker")
}
if !bytes.Equal(fromDiskBuf[n-4:n], []byte("TERM")) {
vs.logError.Printf("bad terminator: %s\n", name)
return cr, errors.New("Error on toc term marker")
}
n -= 16
terminated = true
}
if len(fromDiskOverflow) > 0 {
j += 32 - len(fromDiskOverflow)
fromDiskOverflow = append(fromDiskOverflow, fromDiskBuf[j-32+len(fromDiskOverflow):j]...)
keyB := binary.BigEndian.Uint64(fromDiskOverflow[8:])
keyA := binary.BigEndian.Uint64(fromDiskOverflow)
timestampbits := binary.BigEndian.Uint64(fromDiskOverflow[16:])
fromDiskOverflow = fromDiskOverflow[:0]
tsm, blockid, _, _ := vs.lookup(keyA, keyB)
if tsm>>_TSB_UTIL_BITS != timestampbits>>_TSB_UTIL_BITS && blockid != candidateBlockID || tsm&_TSB_DELETION != 0 {
cr.count++
cr.stale++
} else {
var value []byte
_, value, err := vs.read(keyA, keyB, value)
if err != nil {
vs.logCritical.Println("Error on rewrite read", err)
return cr, errors.New("Error on read for compaction rewrite.")
}
_, err = vs.write(keyA, keyB, timestampbits|_TSB_COMPACTION_REWRITE, value)
if err != nil {
vs.logCritical.Println("Error on rewrite", err)
return cr, errors.New("Write error on compaction rewrite.")
}
cr.count++
cr.rewrote++
}
}
for ; j+32 <= n; j += 32 {
keyB := binary.BigEndian.Uint64(fromDiskBuf[j+8:])
keyA := binary.BigEndian.Uint64(fromDiskBuf[j:])
timestampbits := binary.BigEndian.Uint64(fromDiskBuf[j+16:])
tsm, blockid, _, _ := vs.lookup(keyA, keyB)
if tsm>>_TSB_UTIL_BITS != timestampbits>>_TSB_UTIL_BITS && blockid != candidateBlockID || tsm&_TSB_DELETION != 0 {
cr.count++
cr.stale++
} else {
var value []byte
_, value, err := vs.read(keyA, keyB, value)
if err != nil {
vs.logCritical.Println("Error on rewrite read", err)
return cr, errors.New("Error on rewrite read")
}
_, err = vs.write(keyA, keyB, timestampbits|_TSB_COMPACTION_REWRITE, value)
if err != nil {
vs.logCritical.Println("Error on rewrite", err)
return cr, errors.New("Error on rewrite")
}
cr.count++
cr.rewrote++
}
}
if j != n {
fromDiskOverflow = fromDiskOverflow[:n-j]
copy(fromDiskOverflow, fromDiskBuf[j:])
}
}
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
vs.logError.Printf("error reading %s: %s\n", name, err)
return cr, errors.New("EOF while reading toc during compaction")
}
}
fp.Close()
if !terminated {
vs.logError.Printf("early end of file: %s\n", name)
return cr, nil
}
if cr.checksumFailures > 0 {
vs.logWarning.Printf("%d checksum failures for %s\n", cr.checksumFailures, name)
return cr, nil
}
return cr, nil
}