-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPersistedBatch.ts
345 lines (311 loc) · 9.97 KB
/
PersistedBatch.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
import { mongo } from '@powersync/lib-service-mongodb';
import { JSONBig } from '@powersync/service-jsonbig';
import { EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
import * as bson from 'bson';
import { logger } from '@powersync/lib-services-framework';
import { InternalOpId, storage, utils } from '@powersync/service-core';
import { currentBucketKey, MAX_ROW_SIZE } from './MongoBucketBatch.js';
import { MongoIdSequence } from './MongoIdSequence.js';
import { PowerSyncMongo } from './db.js';
import {
BucketDataDocument,
BucketParameterDocument,
BucketStateDocument,
CurrentBucket,
CurrentDataDocument,
SourceKey
} from './models.js';
import { replicaIdToSubkey } from './util.js';
/**
* Maximum size of operations we write in a single transaction.
*
* It's tricky to find the exact limit, but from experience, over 100MB
* can cause an error:
* > transaction is too large and will not fit in the storage engine cache
*
* Additionally, unbounded size here can balloon our memory usage in some edge
* cases.
*
* When we reach this threshold, we commit the transaction and start a new one.
*/
const MAX_TRANSACTION_BATCH_SIZE = 30_000_000;
/**
* Limit number of documents to write in a single transaction.
*
* This has an effect on error message size in some cases.
*/
const MAX_TRANSACTION_DOC_COUNT = 2_000;
/**
* Keeps track of bulkwrite operations within a transaction.
*
* There may be multiple of these batches per transaction, but it may not span
* multiple transactions.
*/
export class PersistedBatch {
bucketData: mongo.AnyBulkWriteOperation<BucketDataDocument>[] = [];
bucketParameters: mongo.AnyBulkWriteOperation<BucketParameterDocument>[] = [];
currentData: mongo.AnyBulkWriteOperation<CurrentDataDocument>[] = [];
bucketStates: Map<string, BucketStateUpdate> = new Map();
/**
* For debug logging only.
*/
debugLastOpId: InternalOpId | null = null;
/**
* Very rough estimate of transaction size.
*/
currentSize = 0;
constructor(
private group_id: number,
writtenSize: number
) {
this.currentSize = writtenSize;
}
private incrementBucket(bucket: string, op_id: InternalOpId) {
let existingState = this.bucketStates.get(bucket);
if (existingState) {
existingState.lastOp = op_id;
existingState.incrementCount += 1;
} else {
this.bucketStates.set(bucket, {
lastOp: op_id,
incrementCount: 1
});
}
}
saveBucketData(options: {
op_seq: MongoIdSequence;
sourceKey: storage.ReplicaId;
table: storage.SourceTable;
evaluated: EvaluatedRow[];
before_buckets: CurrentBucket[];
}) {
const remaining_buckets = new Map<string, CurrentBucket>();
for (let b of options.before_buckets) {
const key = currentBucketKey(b);
remaining_buckets.set(key, b);
}
const dchecksum = utils.hashDelete(replicaIdToSubkey(options.table.id, options.sourceKey));
for (const k of options.evaluated) {
const key = currentBucketKey(k);
// INSERT
const recordData = JSONBig.stringify(k.data);
const checksum = utils.hashData(k.table, k.id, recordData);
if (recordData.length > MAX_ROW_SIZE) {
// In many cases, the raw data size would have been too large already. But there are cases where
// the BSON size is small enough, but the JSON size is too large.
// In these cases, we can't store the data, so we skip it, or generate a REMOVE operation if the row
// was synced previously.
logger.error(`powersync_${this.group_id} Row ${key} too large: ${recordData.length} bytes. Removing.`);
continue;
}
remaining_buckets.delete(key);
this.currentSize += recordData.length + 200;
const op_id = options.op_seq.next();
this.debugLastOpId = op_id;
this.bucketData.push({
insertOne: {
document: {
_id: {
g: this.group_id,
b: k.bucket,
o: op_id
},
op: 'PUT',
source_table: options.table.id,
source_key: options.sourceKey,
table: k.table,
row_id: k.id,
checksum: checksum,
data: recordData
}
}
});
this.incrementBucket(k.bucket, op_id);
}
for (let bd of remaining_buckets.values()) {
// REMOVE
const op_id = options.op_seq.next();
this.debugLastOpId = op_id;
this.bucketData.push({
insertOne: {
document: {
_id: {
g: this.group_id,
b: bd.bucket,
o: op_id
},
op: 'REMOVE',
source_table: options.table.id,
source_key: options.sourceKey,
table: bd.table,
row_id: bd.id,
checksum: dchecksum,
data: null
}
}
});
this.currentSize += 200;
this.incrementBucket(bd.bucket, op_id);
}
}
saveParameterData(data: {
op_seq: MongoIdSequence;
sourceKey: storage.ReplicaId;
sourceTable: storage.SourceTable;
evaluated: EvaluatedParameters[];
existing_lookups: bson.Binary[];
}) {
// This is similar to saving bucket data.
// A key difference is that we don't need to keep the history intact.
// We do need to keep track of recent history though - enough that we can get consistent data for any specific checkpoint.
// Instead of storing per bucket id, we store per "lookup".
// A key difference is that we don't need to store or keep track of anything per-bucket - the entire record is
// either persisted or removed.
// We also don't need to keep history intact.
const { sourceTable, sourceKey, evaluated } = data;
const remaining_lookups = new Map<string, bson.Binary>();
for (let l of data.existing_lookups) {
remaining_lookups.set(l.toString('base64'), l);
}
// 1. Insert new entries
for (let result of evaluated) {
const binLookup = storage.serializeLookup(result.lookup);
const hex = binLookup.toString('base64');
remaining_lookups.delete(hex);
const op_id = data.op_seq.next();
this.debugLastOpId = op_id;
this.bucketParameters.push({
insertOne: {
document: {
_id: op_id,
key: {
g: this.group_id,
t: sourceTable.id,
k: sourceKey
},
lookup: binLookup,
bucket_parameters: result.bucket_parameters
}
}
});
this.currentSize += 200;
}
// 2. "REMOVE" entries for any lookup not touched.
for (let lookup of remaining_lookups.values()) {
const op_id = data.op_seq.next();
this.debugLastOpId = op_id;
this.bucketParameters.push({
insertOne: {
document: {
_id: op_id,
key: {
g: this.group_id,
t: sourceTable.id,
k: sourceKey
},
lookup: lookup,
bucket_parameters: []
}
}
});
this.currentSize += 200;
}
}
deleteCurrentData(id: SourceKey) {
const op: mongo.AnyBulkWriteOperation<CurrentDataDocument> = {
deleteOne: {
filter: { _id: id }
}
};
this.currentData.push(op);
this.currentSize += 50;
}
upsertCurrentData(id: SourceKey, values: Partial<CurrentDataDocument>) {
const op: mongo.AnyBulkWriteOperation<CurrentDataDocument> = {
updateOne: {
filter: { _id: id },
update: {
$set: values
},
upsert: true
}
};
this.currentData.push(op);
this.currentSize += (values.data?.length() ?? 0) + 100;
}
shouldFlushTransaction() {
return (
this.currentSize >= MAX_TRANSACTION_BATCH_SIZE ||
this.bucketData.length >= MAX_TRANSACTION_DOC_COUNT ||
this.currentData.length >= MAX_TRANSACTION_DOC_COUNT ||
this.bucketParameters.length >= MAX_TRANSACTION_DOC_COUNT
);
}
async flush(db: PowerSyncMongo, session: mongo.ClientSession) {
const startAt = performance.now();
if (this.bucketData.length > 0) {
await db.bucket_data.bulkWrite(this.bucketData, {
session,
// inserts only - order doesn't matter
ordered: false
});
}
if (this.bucketParameters.length > 0) {
await db.bucket_parameters.bulkWrite(this.bucketParameters, {
session,
// inserts only - order doesn't matter
ordered: false
});
}
if (this.currentData.length > 0) {
await db.current_data.bulkWrite(this.currentData, {
session,
// may update and delete data within the same batch - order matters
ordered: true
});
}
if (this.bucketStates.size > 0) {
await db.bucket_state.bulkWrite(this.getBucketStateUpdates(), {
session,
// Per-bucket operation - order doesn't matter
ordered: false
});
}
const duration = performance.now() - startAt;
logger.info(
`powersync_${this.group_id} Flushed ${this.bucketData.length} + ${this.bucketParameters.length} + ${
this.currentData.length
} updates, ${Math.round(this.currentSize / 1024)}kb in ${duration.toFixed(0)}ms. Last op_id: ${this.debugLastOpId}`
);
this.bucketData = [];
this.bucketParameters = [];
this.currentData = [];
this.bucketStates.clear();
this.currentSize = 0;
this.debugLastOpId = null;
}
private getBucketStateUpdates(): mongo.AnyBulkWriteOperation<BucketStateDocument>[] {
return Array.from(this.bucketStates.entries()).map(([bucket, state]) => {
return {
updateOne: {
filter: {
_id: {
g: this.group_id,
b: bucket
}
},
update: {
$set: {
last_op: state.lastOp
}
},
upsert: true
}
} satisfies mongo.AnyBulkWriteOperation<BucketStateDocument>;
});
}
}
interface BucketStateUpdate {
lastOp: InternalOpId;
incrementCount: number;
}