-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBucketChecksumState.ts
450 lines (394 loc) · 15.2 KB
/
BucketChecksumState.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
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
import { BucketDescription, RequestParameters, SqlSyncRules } from '@powersync/service-sync-rules';
import * as storage from '../storage/storage-index.js';
import * as util from '../util/util-index.js';
import { ErrorCode, logger, ServiceAssertionError, ServiceError } from '@powersync/lib-services-framework';
import { BucketParameterQuerier } from '@powersync/service-sync-rules/src/BucketParameterQuerier.js';
import { BucketSyncState } from './sync.js';
import { SyncContext } from './SyncContext.js';
import { JSONBig } from '@powersync/service-jsonbig';
import { hasIntersection } from './util.js';
export interface BucketChecksumStateOptions {
syncContext: SyncContext;
bucketStorage: BucketChecksumStateStorage;
syncRules: SqlSyncRules;
syncParams: RequestParameters;
initialBucketPositions?: { name: string; after: util.InternalOpId }[];
}
/**
* Represents the state of the checksums and data for a specific connection.
*
* Handles incrementally re-computing checkpoints.
*/
export class BucketChecksumState {
private readonly context: SyncContext;
private readonly bucketStorage: BucketChecksumStateStorage;
/**
* Bucket state of bucket id -> op_id.
* This starts with the state from the client. May contain buckets that the user do not have access to (anymore).
*/
public bucketDataPositions = new Map<string, BucketSyncState>();
/**
* Last checksums sent to the client. We keep this to calculate checkpoint diffs.
*/
private lastChecksums: util.ChecksumMap | null = null;
private lastWriteCheckpoint: bigint | null = null;
private readonly parameterState: BucketParameterState;
/**
* Keep track of buckets that need to be downloaded. This is specifically relevant when
* partial checkpoints are sent.
*/
private pendingBucketDownloads = new Set<string>();
constructor(options: BucketChecksumStateOptions) {
this.context = options.syncContext;
this.bucketStorage = options.bucketStorage;
this.parameterState = new BucketParameterState(
options.syncContext,
options.bucketStorage,
options.syncRules,
options.syncParams
);
this.bucketDataPositions = new Map();
for (let { name, after: start } of options.initialBucketPositions ?? []) {
this.bucketDataPositions.set(name, { start_op_id: start });
}
}
async buildNextCheckpointLine(next: storage.StorageCheckpointUpdate): Promise<CheckpointLine | null> {
const { writeCheckpoint, base } = next;
const user_id = this.parameterState.syncParams.user_id;
const storage = this.bucketStorage;
const update = await this.parameterState.getCheckpointUpdate(next);
if (update == null) {
return null;
}
const { buckets: allBuckets, updatedBuckets } = update;
let dataBucketsNew = new Map<string, BucketSyncState>();
for (let bucket of allBuckets) {
dataBucketsNew.set(bucket.bucket, {
description: bucket,
start_op_id: this.bucketDataPositions.get(bucket.bucket)?.start_op_id ?? 0n
});
}
this.bucketDataPositions = dataBucketsNew;
if (dataBucketsNew.size > this.context.maxBuckets) {
throw new ServiceError(
ErrorCode.PSYNC_S2305,
`Too many buckets: ${dataBucketsNew.size} (limit of ${this.context.maxBuckets})`
);
}
let checksumMap: util.ChecksumMap;
if (updatedBuckets != null) {
if (this.lastChecksums == null) {
throw new ServiceAssertionError(`Bucket diff received without existing checksums`);
}
// Re-check updated buckets only
let checksumLookups: string[] = [];
let newChecksums = new Map<string, util.BucketChecksum>();
for (let bucket of dataBucketsNew.keys()) {
if (!updatedBuckets.has(bucket)) {
const existing = this.lastChecksums.get(bucket);
if (existing == null) {
// If this happens, it means updatedBuckets did not correctly include all new buckets
throw new ServiceAssertionError(`Existing checksum not found for bucket ${bucket}`);
}
// Bucket is not specifically updated, and we have a previous checksum
newChecksums.set(bucket, existing);
} else {
checksumLookups.push(bucket);
}
}
let updatedChecksums = await storage.getChecksums(base.checkpoint, checksumLookups);
for (let [bucket, value] of updatedChecksums.entries()) {
newChecksums.set(bucket, value);
}
checksumMap = newChecksums;
} else {
// Re-check all buckets
const bucketList = [...dataBucketsNew.keys()];
checksumMap = await storage.getChecksums(base.checkpoint, bucketList);
}
// Subset of buckets for which there may be new data in this batch.
let bucketsToFetch: BucketDescription[];
let checkpointLine: util.StreamingSyncCheckpointDiff | util.StreamingSyncCheckpoint;
if (this.lastChecksums) {
// TODO: If updatedBuckets is present, we can use that to more efficiently calculate a diff,
// and avoid any unnecessary loops through the entire list of buckets.
const diff = util.checksumsDiff(this.lastChecksums, checksumMap);
if (
this.lastWriteCheckpoint == writeCheckpoint &&
diff.removedBuckets.length == 0 &&
diff.updatedBuckets.length == 0
) {
// No changes - don't send anything to the client
return null;
}
let generateBucketsToFetch = new Set<string>();
for (let bucket of diff.updatedBuckets) {
generateBucketsToFetch.add(bucket.bucket);
}
for (let bucket of this.pendingBucketDownloads) {
// Bucket from a previous checkpoint that hasn't been downloaded yet.
// If we still have this bucket, include it in the list of buckets to fetch.
if (checksumMap.has(bucket)) {
generateBucketsToFetch.add(bucket);
}
}
const updatedBucketDescriptions = diff.updatedBuckets.map((e) => ({
...e,
priority: this.bucketDataPositions.get(e.bucket)!.description!.priority
}));
bucketsToFetch = [...generateBucketsToFetch].map((b) => {
return {
bucket: b,
priority: this.bucketDataPositions.get(b)!.description!.priority
};
});
let message = `Updated checkpoint: ${base.checkpoint} | `;
message += `write: ${writeCheckpoint} | `;
message += `buckets: ${allBuckets.length} | `;
message += `updated: ${limitedBuckets(diff.updatedBuckets, 20)} | `;
message += `removed: ${limitedBuckets(diff.removedBuckets, 20)}`;
logger.info(message, {
checkpoint: base.checkpoint,
user_id: user_id,
buckets: allBuckets.length,
updated: diff.updatedBuckets.length,
removed: diff.removedBuckets.length
});
checkpointLine = {
checkpoint_diff: {
last_op_id: util.internalToExternalOpId(base.checkpoint),
write_checkpoint: writeCheckpoint ? String(writeCheckpoint) : undefined,
removed_buckets: diff.removedBuckets,
updated_buckets: updatedBucketDescriptions
}
} satisfies util.StreamingSyncCheckpointDiff;
} else {
let message = `New checkpoint: ${base.checkpoint} | write: ${writeCheckpoint} | `;
message += `buckets: ${allBuckets.length} ${limitedBuckets(allBuckets, 20)}`;
logger.info(message, { checkpoint: base.checkpoint, user_id: user_id, buckets: allBuckets.length });
bucketsToFetch = allBuckets;
checkpointLine = {
checkpoint: {
last_op_id: util.internalToExternalOpId(base.checkpoint),
write_checkpoint: writeCheckpoint ? String(writeCheckpoint) : undefined,
buckets: [...checksumMap.values()].map((e) => ({
...e,
priority: this.bucketDataPositions.get(e.bucket)!.description!.priority
}))
}
} satisfies util.StreamingSyncCheckpoint;
}
this.lastChecksums = checksumMap;
this.lastWriteCheckpoint = writeCheckpoint;
this.pendingBucketDownloads = new Set(bucketsToFetch.map((b) => b.bucket));
return {
checkpointLine,
bucketsToFetch
};
}
/**
* Get bucket positions to sync, given the list of buckets.
*
* @param bucketsToFetch List of buckets to fetch, typically from buildNextCheckpointLine, or a subset of that
* @returns
*/
getFilteredBucketPositions(bucketsToFetch: BucketDescription[]): Map<string, util.InternalOpId> {
const filtered = new Map<string, util.InternalOpId>();
for (let bucket of bucketsToFetch) {
const state = this.bucketDataPositions.get(bucket.bucket);
if (state) {
filtered.set(bucket.bucket, state.start_op_id);
}
}
return filtered;
}
/**
* Update the position of bucket data the client has.
*
* @param bucket the bucket name
* @param nextAfter sync operations >= this value in the next batch
*/
updateBucketPosition(options: { bucket: string; nextAfter: util.InternalOpId; hasMore: boolean }) {
const state = this.bucketDataPositions.get(options.bucket);
if (state) {
state.start_op_id = options.nextAfter;
}
if (!options.hasMore) {
this.pendingBucketDownloads.delete(options.bucket);
}
}
}
export interface CheckpointUpdate {
/**
* All buckets forming part of the checkpoint.
*/
buckets: BucketDescription[];
/**
* If present, a set of buckets that have been updated since the last checkpoint.
*
* If null, assume that any bucket in `buckets` may have been updated.
*/
updatedBuckets: Set<string> | null;
}
export class BucketParameterState {
private readonly context: SyncContext;
public readonly bucketStorage: BucketChecksumStateStorage;
public readonly syncRules: SqlSyncRules;
public readonly syncParams: RequestParameters;
private readonly querier: BucketParameterQuerier;
private readonly staticBuckets: Map<string, BucketDescription>;
private cachedDynamicBuckets: BucketDescription[] | null = null;
private cachedDynamicBucketSet: Set<string> | null = null;
private readonly lookups: Set<string>;
constructor(
context: SyncContext,
bucketStorage: BucketChecksumStateStorage,
syncRules: SqlSyncRules,
syncParams: RequestParameters
) {
this.context = context;
this.bucketStorage = bucketStorage;
this.syncRules = syncRules;
this.syncParams = syncParams;
this.querier = syncRules.getBucketParameterQuerier(this.syncParams);
this.staticBuckets = new Map<string, BucketDescription>(this.querier.staticBuckets.map((b) => [b.bucket, b]));
this.lookups = new Set<string>(this.querier.parameterQueryLookups.map((l) => JSONBig.stringify(l.values)));
}
async getCheckpointUpdate(checkpoint: storage.StorageCheckpointUpdate): Promise<CheckpointUpdate | null> {
const querier = this.querier;
let update: CheckpointUpdate | null;
if (querier.hasDynamicBuckets) {
update = await this.getCheckpointUpdateDynamic(checkpoint);
} else {
update = await this.getCheckpointUpdateStatic(checkpoint);
}
if (update == null) {
return null;
}
if (update.buckets.length > this.context.maxParameterQueryResults) {
// TODO: Limit number of results even before we get to this point
// This limit applies _before_ we get the unique set
const error = new ServiceError(
ErrorCode.PSYNC_S2305,
`Too many parameter query results: ${update.buckets.length} (limit of ${this.context.maxParameterQueryResults})`
);
logger.error(error.message, {
checkpoint: checkpoint,
user_id: this.syncParams.user_id,
buckets: update.buckets.length
});
throw error;
}
return update;
}
/**
* For static buckets, we can keep track of which buckets have been updated.
*/
private async getCheckpointUpdateStatic(
checkpoint: storage.StorageCheckpointUpdate
): Promise<CheckpointUpdate | null> {
const querier = this.querier;
const update = checkpoint.update;
if (update.invalidateDataBuckets) {
return {
buckets: querier.staticBuckets,
updatedBuckets: null
};
}
let updatedBuckets = new Set<string>();
for (let bucket of update.updatedDataBuckets ?? []) {
if (this.staticBuckets.has(bucket)) {
updatedBuckets.add(bucket);
}
}
if (updatedBuckets.size == 0) {
// No change - skip this checkpoint
return null;
}
return {
buckets: querier.staticBuckets,
updatedBuckets
};
}
/**
* For dynamic buckets, we need to re-query the list of buckets every time.
*/
private async getCheckpointUpdateDynamic(
checkpoint: storage.StorageCheckpointUpdate
): Promise<CheckpointUpdate | null> {
const querier = this.querier;
const storage = this.bucketStorage;
const staticBuckets = querier.staticBuckets;
const update = checkpoint.update;
let hasParameterChange = false;
let invalidateDataBuckets = false;
// If hasParameterChange == true, then invalidateDataBuckets = true
// If invalidateDataBuckets == true, we ignore updatedBuckets
let updatedBuckets = new Set<string>();
if (update.invalidateDataBuckets) {
invalidateDataBuckets = true;
}
if (update.invalidateParameterBuckets) {
hasParameterChange = true;
} else {
if (hasIntersection(this.lookups, update.updatedParameterLookups)) {
// This is a very coarse re-check of all queries
hasParameterChange = true;
}
}
let dynamicBuckets: BucketDescription[];
if (hasParameterChange || this.cachedDynamicBuckets == null || this.cachedDynamicBucketSet == null) {
dynamicBuckets = await querier.queryDynamicBucketDescriptions({
getParameterSets(lookups) {
return storage.getParameterSets(checkpoint.base.checkpoint, lookups);
}
});
this.cachedDynamicBuckets = dynamicBuckets;
this.cachedDynamicBucketSet = new Set<string>(dynamicBuckets.map((b) => b.bucket));
invalidateDataBuckets = true;
} else {
dynamicBuckets = this.cachedDynamicBuckets;
if (!invalidateDataBuckets) {
// TODO: Do set intersection instead
for (let bucket of update.updatedDataBuckets ?? []) {
if (this.staticBuckets.has(bucket) || this.cachedDynamicBucketSet.has(bucket)) {
updatedBuckets.add(bucket);
}
}
}
}
const allBuckets = [...staticBuckets, ...dynamicBuckets];
if (invalidateDataBuckets) {
return {
buckets: allBuckets,
// We cannot track individual bucket updates for dynamic lookups yet
updatedBuckets: null
};
} else {
return {
buckets: allBuckets,
updatedBuckets: updatedBuckets
};
}
}
}
export interface CheckpointLine {
checkpointLine: util.StreamingSyncCheckpointDiff | util.StreamingSyncCheckpoint;
bucketsToFetch: BucketDescription[];
}
// Use a more specific type to simplify testing
export type BucketChecksumStateStorage = Pick<storage.SyncRulesBucketStorage, 'getChecksums' | 'getParameterSets'>;
function limitedBuckets(buckets: string[] | { bucket: string }[], limit: number) {
buckets = buckets.map((b) => {
if (typeof b != 'string') {
return b.bucket;
} else {
return b;
}
});
if (buckets.length <= limit) {
return JSON.stringify(buckets);
}
const limited = buckets.slice(0, limit);
return `${JSON.stringify(limited)}...`;
}