Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit ea3c6e4

Browse files
committed
use sizeDiff=1 to represent holes filled
1 parent e3b19e1 commit ea3c6e4

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

compaction.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function Compaction(log, onDone) {
134134
const progress = Obv() // for the unshifted offset
135135
let startOffset = 0
136136
let version = 0
137+
let holesFound = true // assume true
137138

138139
let compactedBlockIndex = -1
139140
let compactedBlockBuf = null
@@ -170,13 +171,11 @@ function Compaction(log, onDone) {
170171
if (state.initial) {
171172
findStateFromLog(function foundStateFromLog(err, state) {
172173
if (err) return cb(err)
173-
startOffset = state.compactedBlockIndex * log.blockSize
174174
compactedBlockIndex = state.compactedBlockIndex
175+
startOffset = compactedBlockIndex * log.blockSize
175176
unshiftedOffset = state.unshiftedOffset
176177
unshiftedBlockBuf = state.unshiftedBlockBuf
177-
unshiftedBlockIndex = Math.floor(
178-
state.unshiftedOffset / log.blockSize
179-
)
178+
unshiftedBlockIndex = Math.floor(unshiftedOffset / log.blockSize)
180179
savePersistentState(cb)
181180
})
182181
} else {
@@ -212,6 +211,7 @@ function Compaction(log, onDone) {
212211
if (err) return cb(err)
213212
if (holeOffset === -1) {
214213
compactedBlockIndex = Math.floor(log.since.value / log.blockSize)
214+
holesFound = false
215215
stop()
216216
return
217217
}
@@ -414,7 +414,13 @@ function Compaction(log, onDone) {
414414
if (err) return onDone(err)
415415
persistentState.destroy(function onStateDestroyed(err) {
416416
if (err) return onDone(err)
417-
onDone(null, sizeDiff)
417+
if (sizeDiff === 0 && holesFound) {
418+
// Truncation did not make the log smaller but it did rewrite the log.
419+
// So report 1 byte as a way of saying that compaction filled holes.
420+
onDone(null, { sizeDiff: 1 })
421+
} else {
422+
onDone(null, { sizeDiff })
423+
}
418424
})
419425
})
420426
}

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,10 @@ module.exports = function AsyncAppendOnlyLog(filename, opts) {
485485
}
486486
onDrain(function startCompactAfterDrain() {
487487
onDeletesFlushed(function startCompactAfterDeletes() {
488-
compaction = new Compaction(self, (err, sizeDiff) => {
488+
compaction = new Compaction(self, (err, stats) => {
489489
compaction = null
490490
if (err) return cb(err)
491-
compactionProgress.set({ sizeDiff, percent: 1, done: true })
491+
compactionProgress.set({ ...stats, percent: 1, done: true })
492492
for (const callback of waitingCompaction) callback()
493493
waitingCompaction.length = 0
494494
cb()

test/compaction.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,32 @@ tape('compact a log that does not have holes', async (t) => {
2626
await run(log.onDrain)()
2727
t.pass('append two records')
2828

29+
const progressArr = []
30+
log.compactionProgress((stats) => {
31+
progressArr.push(stats)
32+
})
33+
2934
const [err] = await run(log.compact)()
3035
await run(log.onDrain)()
3136
t.error(err, 'no error when compacting')
3237

38+
t.deepEquals(
39+
progressArr,
40+
[
41+
{
42+
sizeDiff: 0,
43+
percent: 1,
44+
done: true,
45+
},
46+
{
47+
sizeDiff: 0,
48+
percent: 1,
49+
done: true,
50+
},
51+
],
52+
'progress events'
53+
)
54+
3355
await new Promise((resolve) => {
3456
log.stream({ offsets: false }).pipe(
3557
push.collect((err, ary) => {
@@ -519,7 +541,7 @@ tape('startOffset is correct', async (t) => {
519541
done: false,
520542
},
521543
{
522-
sizeDiff: 0,
544+
sizeDiff: 1,
523545
percent: 1,
524546
done: true,
525547
},
@@ -616,7 +638,7 @@ tape('recovers from crash just after persisting state', async (t) => {
616638
done: false,
617639
},
618640
{
619-
sizeDiff: 0,
641+
sizeDiff: 1,
620642
percent: 1,
621643
done: true,
622644
},
@@ -668,8 +690,8 @@ tape('recovers from crash just after persisting block', async (t) => {
668690
t.pass('suppose compaction was in progress: [0x22, 0x33] and [0x33, 0x44]')
669691

670692
const version = [1, 0, 0, 0] // uint32LE
671-
const startOffset = [0,0,0,0] // uint32LE
672-
const truncateBlockIndex = [255, 255, 255, 255] //uint32LE
693+
const startOffset = [0, 0, 0, 0] // uint32LE
694+
const truncateBlockIndex = [255, 255, 255, 255] // uint32LE
673695
const compactingBlockIndex = [0, 0, 0, 0] // uint32LE
674696
const unshiftedOffset = [0, 0, 0, 0] // uint32LE
675697
const unshiftedBlock = [
@@ -739,7 +761,7 @@ tape('restarts from crash just before truncating log', async (t) => {
739761
t.pass('suppose compaction ready: [0x22, 0x44], [0x55, 0x66], [0x55, 0x66]')
740762

741763
const version = [1, 0, 0, 0] // uint32LE
742-
const startOffset = [0,0,0,0] // uint32LE
764+
const startOffset = [0, 0, 0, 0] // uint32LE
743765
const truncateBlockIndex = [1, 0, 0, 0] //uint32LE
744766
const compactingBlockIndex = [0, 0, 0, 0] // uint32LE
745767
const unshiftedOffset = [0, 0, 0, 0] // uint32LE

0 commit comments

Comments
 (0)