-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMongoCompactor.ts
394 lines (359 loc) · 11.7 KB
/
MongoCompactor.ts
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
import { mongo } from '@powersync/lib-service-mongodb';
import { logger, ReplicationAssertionError } from '@powersync/lib-services-framework';
import { InternalOpId, storage, utils } from '@powersync/service-core';
import { PowerSyncMongo } from './db.js';
import { BucketDataDocument, BucketDataKey } from './models.js';
import { cacheKey } from './OperationBatch.js';
interface CurrentBucketState {
/** Bucket name */
bucket: string;
/**
* Rows seen in the bucket, with the last op_id of each.
*/
seen: Map<string, InternalOpId>;
/**
* Estimated memory usage of the seen Map.
*/
trackingSize: number;
/**
* Last (lowest) seen op_id that is not a PUT.
*/
lastNotPut: InternalOpId | null;
/**
* Number of REMOVE/MOVE operations seen since lastNotPut.
*/
opsSincePut: number;
}
/**
* Additional options, primarily for testing.
*/
export interface MongoCompactOptions extends storage.CompactOptions {}
const DEFAULT_CLEAR_BATCH_LIMIT = 5000;
const DEFAULT_MOVE_BATCH_LIMIT = 2000;
const DEFAULT_MOVE_BATCH_QUERY_LIMIT = 10_000;
/** This default is primarily for tests. */
const DEFAULT_MEMORY_LIMIT_MB = 64;
export class MongoCompactor {
private updates: mongo.AnyBulkWriteOperation<BucketDataDocument>[] = [];
private idLimitBytes: number;
private moveBatchLimit: number;
private moveBatchQueryLimit: number;
private clearBatchLimit: number;
private maxOpId: bigint | undefined;
private buckets: string[] | undefined;
constructor(
private db: PowerSyncMongo,
private group_id: number,
options?: MongoCompactOptions
) {
this.idLimitBytes = (options?.memoryLimitMB ?? DEFAULT_MEMORY_LIMIT_MB) * 1024 * 1024;
this.moveBatchLimit = options?.moveBatchLimit ?? DEFAULT_MOVE_BATCH_LIMIT;
this.moveBatchQueryLimit = options?.moveBatchQueryLimit ?? DEFAULT_MOVE_BATCH_QUERY_LIMIT;
this.clearBatchLimit = options?.clearBatchLimit ?? DEFAULT_CLEAR_BATCH_LIMIT;
this.maxOpId = options?.maxOpId;
this.buckets = options?.compactBuckets;
}
/**
* Compact buckets by converting operations into MOVE and/or CLEAR operations.
*
* See /docs/compacting-operations.md for details.
*/
async compact() {
if (this.buckets) {
for (let bucket of this.buckets) {
// We can make this more efficient later on by iterating
// through the buckets in a single query.
// That makes batching more tricky, so we leave for later.
await this.compactInternal(bucket);
}
} else {
await this.compactInternal(undefined);
}
}
async compactInternal(bucket: string | undefined) {
const idLimitBytes = this.idLimitBytes;
let currentState: CurrentBucketState | null = null;
let bucketLower: string | mongo.MinKey;
let bucketUpper: string | mongo.MaxKey;
if (bucket == null) {
bucketLower = new mongo.MinKey();
bucketUpper = new mongo.MaxKey();
} else if (bucket.includes('[')) {
// Exact bucket name
bucketLower = bucket;
bucketUpper = bucket;
} else {
// Bucket definition name
bucketLower = `${bucket}[`;
bucketUpper = `${bucket}[\uFFFF`;
}
// Constant lower bound
const lowerBound: BucketDataKey = {
g: this.group_id,
b: bucketLower as string,
o: new mongo.MinKey() as any
};
// Upper bound is adjusted for each batch
let upperBound: BucketDataKey = {
g: this.group_id,
b: bucketUpper as string,
o: new mongo.MaxKey() as any
};
while (true) {
// Query one batch at a time, to avoid cursor timeouts
const batch = await this.db.bucket_data
.find(
{
_id: {
$gte: lowerBound,
$lt: upperBound
}
},
{
projection: {
_id: 1,
op: 1,
table: 1,
row_id: 1,
source_table: 1,
source_key: 1
},
limit: this.moveBatchQueryLimit,
sort: { _id: -1 },
singleBatch: true
}
)
.toArray();
if (batch.length == 0) {
// We've reached the end
break;
}
// Set upperBound for the next batch
upperBound = batch[batch.length - 1]._id;
for (let doc of batch) {
if (currentState == null || doc._id.b != currentState.bucket) {
if (currentState != null && currentState.lastNotPut != null && currentState.opsSincePut >= 1) {
// Important to flush before clearBucket()
await this.flush();
logger.info(
`Inserting CLEAR at ${this.group_id}:${currentState.bucket}:${currentState.lastNotPut} to remove ${currentState.opsSincePut} operations`
);
const bucket = currentState.bucket;
const clearOp = currentState.lastNotPut;
// Free memory before clearing bucket
currentState = null;
await this.clearBucket(bucket, clearOp);
}
currentState = {
bucket: doc._id.b,
seen: new Map(),
trackingSize: 0,
lastNotPut: null,
opsSincePut: 0
};
}
if (this.maxOpId != null && doc._id.o > this.maxOpId) {
continue;
}
let isPersistentPut = doc.op == 'PUT';
if (doc.op == 'REMOVE' || doc.op == 'PUT') {
const key = `${doc.table}/${doc.row_id}/${cacheKey(doc.source_table!, doc.source_key!)}`;
const targetOp = currentState.seen.get(key);
if (targetOp) {
// Will convert to MOVE, so don't count as PUT
isPersistentPut = false;
this.updates.push({
updateOne: {
filter: {
_id: doc._id
},
update: {
$set: {
op: 'MOVE',
target_op: targetOp
},
$unset: {
source_table: 1,
source_key: 1,
table: 1,
row_id: 1,
data: 1
}
}
}
});
} else {
if (currentState.trackingSize >= idLimitBytes) {
// Reached memory limit.
// Keep the highest seen values in this case.
} else {
// flatstr reduces the memory usage by flattening the string
currentState.seen.set(utils.flatstr(key), doc._id.o);
// length + 16 for the string
// 24 for the bigint
// 50 for map overhead
// 50 for additional overhead
currentState.trackingSize += key.length + 140;
}
}
}
if (isPersistentPut) {
currentState.lastNotPut = null;
currentState.opsSincePut = 0;
} else if (doc.op != 'CLEAR') {
if (currentState.lastNotPut == null) {
currentState.lastNotPut = doc._id.o;
}
currentState.opsSincePut += 1;
}
if (this.updates.length >= this.moveBatchLimit) {
await this.flush();
}
}
}
await this.flush();
currentState?.seen.clear();
if (currentState?.lastNotPut != null && currentState?.opsSincePut > 1) {
logger.info(
`Inserting CLEAR at ${this.group_id}:${currentState.bucket}:${currentState.lastNotPut} to remove ${currentState.opsSincePut} operations`
);
const bucket = currentState.bucket;
const clearOp = currentState.lastNotPut;
// Free memory before clearing bucket
currentState = null;
await this.clearBucket(bucket, clearOp);
}
}
private async flush() {
if (this.updates.length > 0) {
logger.info(`Compacting ${this.updates.length} ops`);
await this.db.bucket_data.bulkWrite(this.updates, {
// Order is not important.
// Since checksums are not affected, these operations can happen in any order,
// and it's fine if the operations are partially applied.
// Each individual operation is atomic.
ordered: false
});
this.updates = [];
}
}
/**
* Perform a CLEAR compact for a bucket.
*
* @param bucket bucket name
* @param op op_id of the last non-PUT operation, which will be converted to CLEAR.
*/
private async clearBucket(bucket: string, op: InternalOpId) {
const opFilter = {
_id: {
$gte: {
g: this.group_id,
b: bucket,
o: new mongo.MinKey() as any
},
$lte: {
g: this.group_id,
b: bucket,
o: op
}
}
};
const session = this.db.client.startSession();
try {
let done = false;
while (!done) {
// Do the CLEAR operation in batches, with each batch a separate transaction.
// The state after each batch is fully consistent.
// We need a transaction per batch to make sure checksums stay consistent.
await session.withTransaction(
async () => {
const query = this.db.bucket_data.find(opFilter, {
session,
sort: { _id: 1 },
projection: {
_id: 1,
op: 1,
checksum: 1,
target_op: 1
},
limit: this.clearBatchLimit
});
let checksum = 0;
let lastOpId: BucketDataKey | null = null;
let targetOp: bigint | null = null;
let gotAnOp = false;
let numberOfOpsToClear = 0;
for await (let op of query.stream()) {
if (op.op == 'MOVE' || op.op == 'REMOVE' || op.op == 'CLEAR') {
checksum = utils.addChecksums(checksum, op.checksum);
lastOpId = op._id;
numberOfOpsToClear += 1;
if (op.op != 'CLEAR') {
gotAnOp = true;
}
if (op.target_op != null) {
if (targetOp == null || op.target_op > targetOp) {
targetOp = op.target_op;
}
}
} else {
throw new ReplicationAssertionError(
`Unexpected ${op.op} operation at ${op._id.g}:${op._id.b}:${op._id.o}`
);
}
}
if (!gotAnOp) {
done = true;
return;
}
logger.info(`Flushing CLEAR for ${numberOfOpsToClear} ops at ${lastOpId?.o}`);
await this.db.bucket_data.deleteMany(
{
_id: {
$gte: {
g: this.group_id,
b: bucket,
o: new mongo.MinKey() as any
},
$lte: lastOpId!
}
},
{ session }
);
await this.db.bucket_data.insertOne(
{
_id: lastOpId!,
op: 'CLEAR',
checksum: checksum,
data: null,
target_op: targetOp
},
{ session }
);
// Note: This does not update anything if there is no existing state
await this.db.bucket_state.updateOne(
{
_id: {
g: this.group_id,
b: bucket
}
},
{
$inc: {
op_count: 1 - numberOfOpsToClear
}
},
{ session }
);
},
{
writeConcern: { w: 'majority' },
readConcern: { level: 'snapshot' }
}
);
}
} finally {
await session.endSession();
}
}
}