/
index.js
270 lines (249 loc) · 7.32 KB
/
index.js
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
// @ts-ignore
const multicb = require('multicb')
const makeDebug = require('debug')
/**
* @typedef {ReturnType<import('ppppp-db').init>} PPPPPDB
* @typedef {ReturnType<import('ppppp-goals').init>} PPPPPGoal
* @typedef {{
* gc: {
* maxLogBytes: number
* compactionInterval?: number
* }
* }} ExpectedConfig
* @typedef {{gc?: Partial<ExpectedConfig['gc']>}} Config
*/
/**
* @template T
* @typedef {T extends void ?
* (...args: [Error] | []) => void :
* (...args: [Error] | [null, T]) => void
* } CB
*/
/**
* @param {{ db: PPPPPDB, goals: PPPPPGoal }} peer
* @param {Config} config
*/
function initGC(peer, config) {
// State
const debug = makeDebug('ppppp:gc')
let stopMonitoringLogSize = /** @type {CallableFunction | null} */ (null)
let hasCleanupScheduled = false
/**
* @param {Error} err
*/
function flattenCauseChain(err) {
let str = ''
while (err?.message ?? err) {
str += (err.message ?? err) + ': '
err = /**@type {Error}*/ (err.cause)
}
return str
}
/**
* Deletes messages that don't correspond to any goal.
* @private
* @param {CB<void>} cb
*/
function cleanup(cb) {
debug('Cleanup started')
const startTime = Date.now()
const done = multicb({ pluck: 1 })
/**
* @param {string} errExplanation
*/
function makeRecCB(errExplanation) {
const cb = done()
return (/**@type {Error=}*/ err) => {
if (err) debug('%s: %s', errExplanation, flattenCauseChain(err))
cb()
}
}
let waiting = false
for (const rec of peer.db.records()) {
if (!rec.msg) continue
const { id: msgID, msg } = rec
const [purpose, details] = peer.goals.getMsgPurpose(msgID, msg)
switch (purpose) {
case 'goal': {
continue // don't cleanup
}
case 'none': {
const recCB = makeRecCB('Failed to delete msg when cleaning up')
debug('Deleting msg %s with purpose=none', msgID)
peer.db.del(msgID, recCB)
waiting = true
continue
}
case 'ghost': {
const { tangleID, span } = details
const recCB = makeRecCB('Failed to delete ghost msg when cleaning up')
// TODO: Could one msg be a ghostable in MANY tangles? Or just one?
debug('Deleting and ghosting msg %s with purpose=ghost', msgID)
peer.db.ghosts.add({ tangleID, msgID, span }, (err) => {
if (err) return recCB(err)
peer.db.del(msgID, recCB)
})
waiting = true
continue
}
case 'trail': {
if (!msg.data) continue // it's already erased
const recCB = makeRecCB('Failed to erase trail msg when cleaning up')
debug('Erasing msg %s with purpose=trail', msgID)
peer.db.erase(msgID, recCB)
waiting = true
continue
}
default: {
cb(new Error('Unreachable'))
return
}
}
}
if (waiting) done(whenEnded)
else whenEnded()
/** @param {Error=} err */
function whenEnded(err) {
const duration = Date.now() - startTime
if (err) debug('Cleanup ended with an error %s', err.message ?? err)
else debug('Cleanup completed in %sms', duration)
cb()
}
}
/**
* Recreates the log so it has no empty space.
* @private
* @param {CB<void>} cb
*/
function compact(cb) {
debug('Compaction started')
const startTime = Date.now()
peer.db.log.compact((err) => {
const duration = Date.now() - startTime
if (err) debug('Compaction ended with an error %s', err.message ?? err)
else debug('Compaction completed in %sms', duration)
cb()
})
}
/**
* @param {number} percentUsed
* @param {number} maxLogBytes
* @param {{ totalBytes: number; }} stats
*/
function reportCleanupNeed(percentUsed, maxLogBytes, stats) {
const bytesRemaining = maxLogBytes - stats.totalBytes
const kbRemaining = bytesRemaining >> 10
const mbRemaining = bytesRemaining >> 20
const remaining =
mbRemaining > 0
? `${mbRemaining}MB`
: kbRemaining > 0
? `${kbRemaining}KB`
: `${bytesRemaining}B`
if (bytesRemaining > 0) {
// prettier-ignore
debug('Log is %s% full (only %s of free space), needs cleanup', percentUsed.toFixed(0), remaining)
} else if (bytesRemaining === 0) {
debug('Log is full, needs cleanup')
} else {
// prettier-ignore
debug('Log is %s% full (%s beyond limit), needs cleanup', percentUsed.toFixed(0), remaining.slice(1))
}
}
/**
* @param {number} percentDeleted
* @param {{ deletedBytes: any; }} stats
*/
function reportCompactionNeed(percentDeleted, stats) {
const unusedBytes = stats.deletedBytes
const kbUnused = unusedBytes >> 10
const mbUnused = unusedBytes >> 20
const unused =
mbUnused > 0
? `${mbUnused}MB`
: kbUnused > 0
? `${kbUnused}KB`
: `${unusedBytes}B`
// prettier-ignore
debug('Log is %s% deleted (%s of unused space), needs compaction', percentDeleted.toFixed(0), unused)
}
/**
* Monitor the log size and schedule compaction and/or cleanup.
*
* @param {number} maxLogBytes
*/
function monitorLogSize(maxLogBytes) {
/** Number of records that match roughly 1% of the max log size */
const CHECKPOINT = Math.floor((maxLogBytes * 0.01) / 500) // assuming 1 record = 500 bytes
function checkLogSize() {
peer.db.log.stats((err, stats) => {
if (err) return
const percentUsed = (stats.totalBytes / maxLogBytes) * 100
const percentDeleted = (stats.deletedBytes / stats.totalBytes) * 100
const needsCleanup = percentUsed > 80
const needsCompaction = percentDeleted > 30
// Schedule clean up
if ((needsCleanup || needsCompaction) && !hasCleanupScheduled) {
if (needsCleanup) reportCleanupNeed(percentUsed, maxLogBytes, stats)
if (needsCompaction) reportCompactionNeed(percentDeleted, stats)
hasCleanupScheduled = true
if (needsCleanup) {
cleanup(() => {
compact(() => {
hasCleanupScheduled = false
})
})
} else {
compact(() => {
hasCleanupScheduled = false
})
}
}
})
}
let count = 0
stopMonitoringLogSize = peer.db.onRecordAdded(() => {
count += 1
if (count >= CHECKPOINT) {
count = 0
checkLogSize()
}
})
checkLogSize()
}
/**
* @param {number?} maxLogBytes
*/
function start(maxLogBytes) {
stop()
const actualMaxLogBytes = maxLogBytes ?? config.gc?.maxLogBytes ?? null
// prettier-ignore
if (!actualMaxLogBytes) throw new Error('gc plugin requires maxLogBytes via start() argument or config.gc.maxLogBytes')
if (!stopMonitoringLogSize) {
monitorLogSize(actualMaxLogBytes)
}
}
function stop() {
if (stopMonitoringLogSize) {
stopMonitoringLogSize()
stopMonitoringLogSize = null
}
}
/**
* @param {CB<void>} cb
*/
function forceImmediately(cb) {
debug('Force clean and compact immediately')
cleanup(() => {
compact(cb)
})
}
return {
start,
stop,
forceImmediately,
}
}
exports.name = 'gc'
exports.needs = ['db', 'goals']
exports.init = initGC