@powersync/service-core 0.18.1 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/api/RouteAPI.d.ts +1 -1
  3. package/dist/api/diagnostics.js +107 -169
  4. package/dist/api/diagnostics.js.map +1 -1
  5. package/dist/entry/commands/compact-action.js +10 -73
  6. package/dist/entry/commands/compact-action.js.map +1 -1
  7. package/dist/modules/AbstractModule.d.ts +1 -1
  8. package/dist/replication/AbstractReplicator.js +8 -76
  9. package/dist/replication/AbstractReplicator.js.map +1 -1
  10. package/dist/routes/endpoints/checkpointing.js +3 -2
  11. package/dist/routes/endpoints/checkpointing.js.map +1 -1
  12. package/dist/routes/endpoints/socket-route.js +5 -5
  13. package/dist/routes/endpoints/socket-route.js.map +1 -1
  14. package/dist/routes/endpoints/sync-stream.js +5 -5
  15. package/dist/routes/endpoints/sync-stream.js.map +1 -1
  16. package/dist/runner/teardown.js +3 -65
  17. package/dist/runner/teardown.js.map +1 -1
  18. package/dist/storage/BucketStorage.d.ts +8 -441
  19. package/dist/storage/BucketStorage.js +9 -10
  20. package/dist/storage/BucketStorage.js.map +1 -1
  21. package/dist/storage/BucketStorageBatch.d.ts +130 -0
  22. package/dist/storage/BucketStorageBatch.js +10 -0
  23. package/dist/storage/BucketStorageBatch.js.map +1 -0
  24. package/dist/storage/BucketStorageFactory.d.ts +145 -0
  25. package/dist/storage/BucketStorageFactory.js +2 -0
  26. package/dist/storage/BucketStorageFactory.js.map +1 -0
  27. package/dist/storage/ChecksumCache.js.map +1 -1
  28. package/dist/storage/PersistedSyncRulesContent.d.ts +20 -0
  29. package/dist/storage/PersistedSyncRulesContent.js +2 -0
  30. package/dist/storage/PersistedSyncRulesContent.js.map +1 -0
  31. package/dist/storage/ReplicationEventPayload.d.ts +1 -1
  32. package/dist/storage/ReplicationLock.d.ts +4 -0
  33. package/dist/storage/ReplicationLock.js +2 -0
  34. package/dist/storage/ReplicationLock.js.map +1 -0
  35. package/dist/storage/SourceEntity.d.ts +6 -2
  36. package/dist/storage/SourceTable.d.ts +2 -2
  37. package/dist/storage/SourceTable.js.map +1 -1
  38. package/dist/storage/StorageEngine.d.ts +4 -4
  39. package/dist/storage/StorageEngine.js +2 -2
  40. package/dist/storage/StorageEngine.js.map +1 -1
  41. package/dist/storage/StorageProvider.d.ts +4 -1
  42. package/dist/storage/SyncRulesBucketStorage.d.ts +207 -0
  43. package/dist/storage/SyncRulesBucketStorage.js +7 -0
  44. package/dist/storage/SyncRulesBucketStorage.js.map +1 -0
  45. package/dist/storage/bson.d.ts +14 -3
  46. package/dist/storage/bson.js +18 -2
  47. package/dist/storage/bson.js.map +1 -1
  48. package/dist/storage/storage-index.d.ts +5 -0
  49. package/dist/storage/storage-index.js +5 -0
  50. package/dist/storage/storage-index.js.map +1 -1
  51. package/dist/sync/BucketChecksumState.d.ts +91 -0
  52. package/dist/sync/BucketChecksumState.js +313 -0
  53. package/dist/sync/BucketChecksumState.js.map +1 -0
  54. package/dist/sync/sync-index.d.ts +1 -0
  55. package/dist/sync/sync-index.js +1 -0
  56. package/dist/sync/sync-index.js.map +1 -1
  57. package/dist/sync/sync.d.ts +7 -3
  58. package/dist/sync/sync.js +139 -135
  59. package/dist/sync/sync.js.map +1 -1
  60. package/dist/sync/util.d.ts +9 -0
  61. package/dist/sync/util.js +44 -0
  62. package/dist/sync/util.js.map +1 -1
  63. package/dist/util/checkpointing.d.ts +1 -1
  64. package/dist/util/checkpointing.js +15 -78
  65. package/dist/util/checkpointing.js.map +1 -1
  66. package/dist/util/protocol-types.d.ts +13 -4
  67. package/package.json +5 -5
  68. package/src/api/RouteAPI.ts +1 -1
  69. package/src/api/diagnostics.ts +1 -1
  70. package/src/entry/commands/compact-action.ts +2 -3
  71. package/src/modules/AbstractModule.ts +1 -1
  72. package/src/replication/AbstractReplicator.ts +7 -12
  73. package/src/routes/endpoints/checkpointing.ts +3 -3
  74. package/src/routes/endpoints/socket-route.ts +7 -5
  75. package/src/routes/endpoints/sync-stream.ts +8 -5
  76. package/src/runner/teardown.ts +1 -1
  77. package/src/storage/BucketStorage.ts +8 -550
  78. package/src/storage/BucketStorageBatch.ts +158 -0
  79. package/src/storage/BucketStorageFactory.ts +166 -0
  80. package/src/storage/ChecksumCache.ts +1 -0
  81. package/src/storage/PersistedSyncRulesContent.ts +26 -0
  82. package/src/storage/ReplicationEventPayload.ts +1 -1
  83. package/src/storage/ReplicationLock.ts +5 -0
  84. package/src/storage/SourceEntity.ts +6 -2
  85. package/src/storage/SourceTable.ts +1 -1
  86. package/src/storage/StorageEngine.ts +4 -4
  87. package/src/storage/StorageProvider.ts +4 -1
  88. package/src/storage/SyncRulesBucketStorage.ts +265 -0
  89. package/src/storage/bson.ts +22 -4
  90. package/src/storage/storage-index.ts +5 -0
  91. package/src/sync/BucketChecksumState.ts +392 -0
  92. package/src/sync/sync-index.ts +1 -0
  93. package/src/sync/sync.ts +182 -157
  94. package/src/sync/util.ts +54 -0
  95. package/src/util/checkpointing.ts +4 -6
  96. package/src/util/protocol-types.ts +16 -4
  97. package/test/src/auth.test.ts +5 -5
  98. package/test/src/sync/BucketChecksumState.test.ts +565 -0
  99. package/test/src/sync/util.test.ts +34 -0
  100. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,392 @@
1
+ import { BucketDescription, RequestParameters, SqlSyncRules } from '@powersync/service-sync-rules';
2
+
3
+ import * as storage from '../storage/storage-index.js';
4
+ import * as util from '../util/util-index.js';
5
+
6
+ import { ErrorCode, logger, ServiceAssertionError, ServiceError } from '@powersync/lib-services-framework';
7
+ import { BucketParameterQuerier } from '@powersync/service-sync-rules/src/BucketParameterQuerier.js';
8
+ import { BucketSyncState } from './sync.js';
9
+
10
+ export interface BucketChecksumStateOptions {
11
+ bucketStorage: BucketChecksumStateStorage;
12
+ syncRules: SqlSyncRules;
13
+ syncParams: RequestParameters;
14
+ initialBucketPositions?: { name: string; after: string }[];
15
+ }
16
+
17
+ /**
18
+ * Represents the state of the checksums and data for a specific connection.
19
+ *
20
+ * Handles incrementally re-computing checkpoints.
21
+ */
22
+ export class BucketChecksumState {
23
+ private readonly bucketStorage: BucketChecksumStateStorage;
24
+
25
+ /**
26
+ * Bucket state of bucket id -> op_id.
27
+ * This starts with the state from the client. May contain buckets that the user do not have access to (anymore).
28
+ */
29
+ public bucketDataPositions = new Map<string, BucketSyncState>();
30
+
31
+ /**
32
+ * Last checksums sent to the client. We keep this to calculate checkpoint diffs.
33
+ */
34
+ private lastChecksums: util.ChecksumMap | null = null;
35
+ private lastWriteCheckpoint: bigint | null = null;
36
+
37
+ private readonly parameterState: BucketParameterState;
38
+
39
+ /**
40
+ * Keep track of buckets that need to be downloaded. This is specifically relevant when
41
+ * partial checkpoints are sent.
42
+ */
43
+ private pendingBucketDownloads = new Set<string>();
44
+
45
+ constructor(options: BucketChecksumStateOptions) {
46
+ this.bucketStorage = options.bucketStorage;
47
+ this.parameterState = new BucketParameterState(options.bucketStorage, options.syncRules, options.syncParams);
48
+ this.bucketDataPositions = new Map();
49
+
50
+ for (let { name, after: start } of options.initialBucketPositions ?? []) {
51
+ this.bucketDataPositions.set(name, { start_op_id: start });
52
+ }
53
+ }
54
+
55
+ async buildNextCheckpointLine(next: storage.StorageCheckpointUpdate): Promise<CheckpointLine | null> {
56
+ const { writeCheckpoint, base } = next;
57
+ const user_id = this.parameterState.syncParams.user_id;
58
+
59
+ const storage = this.bucketStorage;
60
+
61
+ const update = await this.parameterState.getCheckpointUpdate(next);
62
+ if (update == null) {
63
+ return null;
64
+ }
65
+
66
+ const { buckets: allBuckets, updatedBuckets } = update;
67
+
68
+ let dataBucketsNew = new Map<string, BucketSyncState>();
69
+ for (let bucket of allBuckets) {
70
+ dataBucketsNew.set(bucket.bucket, {
71
+ description: bucket,
72
+ start_op_id: this.bucketDataPositions.get(bucket.bucket)?.start_op_id ?? '0'
73
+ });
74
+ }
75
+ this.bucketDataPositions = dataBucketsNew;
76
+
77
+ let checksumMap: util.ChecksumMap;
78
+ if (updatedBuckets != null) {
79
+ if (this.lastChecksums == null) {
80
+ throw new ServiceAssertionError(`Bucket diff received without existing checksums`);
81
+ }
82
+
83
+ // Re-check updated buckets only
84
+ let checksumLookups: string[] = [];
85
+
86
+ let newChecksums = new Map<string, util.BucketChecksum>();
87
+ for (let bucket of dataBucketsNew.keys()) {
88
+ if (!updatedBuckets.has(bucket)) {
89
+ const existing = this.lastChecksums.get(bucket);
90
+ if (existing == null) {
91
+ // If this happens, it means updatedBuckets did not correctly include all new buckets
92
+ throw new ServiceAssertionError(`Existing checksum not found for bucket ${bucket}`);
93
+ }
94
+ // Bucket is not specifically updated, and we have a previous checksum
95
+ newChecksums.set(bucket, existing);
96
+ } else {
97
+ checksumLookups.push(bucket);
98
+ }
99
+ }
100
+
101
+ let updatedChecksums = await storage.getChecksums(base.checkpoint, checksumLookups);
102
+ for (let [bucket, value] of updatedChecksums.entries()) {
103
+ newChecksums.set(bucket, value);
104
+ }
105
+ checksumMap = newChecksums;
106
+ } else {
107
+ // Re-check all buckets
108
+ const bucketList = [...dataBucketsNew.keys()];
109
+ checksumMap = await storage.getChecksums(base.checkpoint, bucketList);
110
+ }
111
+ // Subset of buckets for which there may be new data in this batch.
112
+ let bucketsToFetch: BucketDescription[];
113
+
114
+ let checkpointLine: util.StreamingSyncCheckpointDiff | util.StreamingSyncCheckpoint;
115
+
116
+ if (this.lastChecksums) {
117
+ // TODO: If updatedBuckets is present, we can use that to more efficiently calculate a diff,
118
+ // and avoid any unnecessary loops through the entire list of buckets.
119
+ const diff = util.checksumsDiff(this.lastChecksums, checksumMap);
120
+
121
+ if (
122
+ this.lastWriteCheckpoint == writeCheckpoint &&
123
+ diff.removedBuckets.length == 0 &&
124
+ diff.updatedBuckets.length == 0
125
+ ) {
126
+ // No changes - don't send anything to the client
127
+ return null;
128
+ }
129
+
130
+ let generateBucketsToFetch = new Set<string>();
131
+ for (let bucket of diff.updatedBuckets) {
132
+ generateBucketsToFetch.add(bucket.bucket);
133
+ }
134
+ for (let bucket of this.pendingBucketDownloads) {
135
+ // Bucket from a previous checkpoint that hasn't been downloaded yet.
136
+ // If we still have this bucket, include it in the list of buckets to fetch.
137
+ if (checksumMap.has(bucket)) {
138
+ generateBucketsToFetch.add(bucket);
139
+ }
140
+ }
141
+
142
+ const updatedBucketDescriptions = diff.updatedBuckets.map((e) => ({
143
+ ...e,
144
+ priority: this.bucketDataPositions.get(e.bucket)!.description!.priority
145
+ }));
146
+ bucketsToFetch = [...generateBucketsToFetch].map((b) => {
147
+ return {
148
+ bucket: b,
149
+ priority: this.bucketDataPositions.get(b)!.description!.priority
150
+ };
151
+ });
152
+
153
+ let message = `Updated checkpoint: ${base.checkpoint} | `;
154
+ message += `write: ${writeCheckpoint} | `;
155
+ message += `buckets: ${allBuckets.length} | `;
156
+ message += `updated: ${limitedBuckets(diff.updatedBuckets, 20)} | `;
157
+ message += `removed: ${limitedBuckets(diff.removedBuckets, 20)}`;
158
+ logger.info(message, {
159
+ checkpoint: base.checkpoint,
160
+ user_id: user_id,
161
+ buckets: allBuckets.length,
162
+ updated: diff.updatedBuckets.length,
163
+ removed: diff.removedBuckets.length
164
+ });
165
+
166
+ checkpointLine = {
167
+ checkpoint_diff: {
168
+ last_op_id: base.checkpoint,
169
+ write_checkpoint: writeCheckpoint ? String(writeCheckpoint) : undefined,
170
+ removed_buckets: diff.removedBuckets,
171
+ updated_buckets: updatedBucketDescriptions
172
+ }
173
+ } satisfies util.StreamingSyncCheckpointDiff;
174
+ } else {
175
+ let message = `New checkpoint: ${base.checkpoint} | write: ${writeCheckpoint} | `;
176
+ message += `buckets: ${allBuckets.length} ${limitedBuckets(allBuckets, 20)}`;
177
+ logger.info(message, { checkpoint: base.checkpoint, user_id: user_id, buckets: allBuckets.length });
178
+ bucketsToFetch = allBuckets;
179
+ checkpointLine = {
180
+ checkpoint: {
181
+ last_op_id: base.checkpoint,
182
+ write_checkpoint: writeCheckpoint ? String(writeCheckpoint) : undefined,
183
+ buckets: [...checksumMap.values()].map((e) => ({
184
+ ...e,
185
+ priority: this.bucketDataPositions.get(e.bucket)!.description!.priority
186
+ }))
187
+ }
188
+ } satisfies util.StreamingSyncCheckpoint;
189
+ }
190
+
191
+ this.lastChecksums = checksumMap;
192
+ this.lastWriteCheckpoint = writeCheckpoint;
193
+ this.pendingBucketDownloads = new Set(bucketsToFetch.map((b) => b.bucket));
194
+
195
+ return {
196
+ checkpointLine,
197
+ bucketsToFetch
198
+ };
199
+ }
200
+
201
+ /**
202
+ * Get bucket positions to sync, given the list of buckets.
203
+ *
204
+ * @param bucketsToFetch List of buckets to fetch, typically from buildNextCheckpointLine, or a subset of that
205
+ * @returns
206
+ */
207
+ getFilteredBucketPositions(bucketsToFetch: BucketDescription[]): Map<string, string> {
208
+ const filtered = new Map<string, string>();
209
+ for (let bucket of bucketsToFetch) {
210
+ const state = this.bucketDataPositions.get(bucket.bucket);
211
+ if (state) {
212
+ filtered.set(bucket.bucket, state.start_op_id);
213
+ }
214
+ }
215
+ return filtered;
216
+ }
217
+
218
+ /**
219
+ * Update the position of bucket data the client has.
220
+ *
221
+ * @param bucket the bucket name
222
+ * @param nextAfter sync operations >= this value in the next batch
223
+ */
224
+ updateBucketPosition(options: { bucket: string; nextAfter: string; hasMore: boolean }) {
225
+ const state = this.bucketDataPositions.get(options.bucket);
226
+ if (state) {
227
+ state.start_op_id = options.nextAfter;
228
+ }
229
+ if (!options.hasMore) {
230
+ this.pendingBucketDownloads.delete(options.bucket);
231
+ }
232
+ }
233
+ }
234
+
235
+ export interface CheckpointUpdate {
236
+ /**
237
+ * All buckets forming part of the checkpoint.
238
+ */
239
+ buckets: BucketDescription[];
240
+
241
+ /**
242
+ * If present, a set of buckets that have been updated since the last checkpoint.
243
+ *
244
+ * If null, assume that any bucket in `buckets` may have been updated.
245
+ */
246
+ updatedBuckets: Set<string> | null;
247
+ }
248
+
249
+ export class BucketParameterState {
250
+ public readonly bucketStorage: BucketChecksumStateStorage;
251
+ public readonly syncRules: SqlSyncRules;
252
+ public readonly syncParams: RequestParameters;
253
+ private readonly querier: BucketParameterQuerier;
254
+ private readonly staticBuckets: Map<string, BucketDescription>;
255
+
256
+ constructor(bucketStorage: BucketChecksumStateStorage, syncRules: SqlSyncRules, syncParams: RequestParameters) {
257
+ this.bucketStorage = bucketStorage;
258
+ this.syncRules = syncRules;
259
+ this.syncParams = syncParams;
260
+
261
+ this.querier = syncRules.getBucketParameterQuerier(this.syncParams);
262
+ this.staticBuckets = new Map<string, BucketDescription>(this.querier.staticBuckets.map((b) => [b.bucket, b]));
263
+ }
264
+
265
+ async getCheckpointUpdate(checkpoint: storage.StorageCheckpointUpdate): Promise<CheckpointUpdate | null> {
266
+ const querier = this.querier;
267
+ let update: CheckpointUpdate | null;
268
+ if (querier.hasDynamicBuckets) {
269
+ update = await this.getCheckpointUpdateDynamic(checkpoint);
270
+ } else {
271
+ update = await this.getCheckpointUpdateStatic(checkpoint);
272
+ }
273
+
274
+ if (update == null) {
275
+ return null;
276
+ }
277
+
278
+ if (update.buckets.length > 1000) {
279
+ // TODO: Limit number of buckets even before we get to this point
280
+ const error = new ServiceError(ErrorCode.PSYNC_S2305, `Too many buckets: ${update.buckets.length}`);
281
+ logger.error(error.message, {
282
+ checkpoint: checkpoint,
283
+ user_id: this.syncParams.user_id,
284
+ buckets: update.buckets.length
285
+ });
286
+
287
+ throw error;
288
+ }
289
+ return update;
290
+ }
291
+
292
+ /**
293
+ * For static buckets, we can keep track of which buckets have been updated.
294
+ */
295
+ private async getCheckpointUpdateStatic(
296
+ checkpoint: storage.StorageCheckpointUpdate
297
+ ): Promise<CheckpointUpdate | null> {
298
+ const querier = this.querier;
299
+ const update = checkpoint.update;
300
+
301
+ if (update.invalidateDataBuckets) {
302
+ return {
303
+ buckets: querier.staticBuckets,
304
+ updatedBuckets: null
305
+ };
306
+ }
307
+
308
+ let updatedBuckets = new Set<string>();
309
+
310
+ for (let bucket of update.updatedDataBuckets ?? []) {
311
+ if (this.staticBuckets.has(bucket)) {
312
+ updatedBuckets.add(bucket);
313
+ }
314
+ }
315
+
316
+ if (updatedBuckets.size == 0) {
317
+ // No change - skip this checkpoint
318
+ return null;
319
+ }
320
+
321
+ return {
322
+ buckets: querier.staticBuckets,
323
+ updatedBuckets
324
+ };
325
+ }
326
+
327
+ /**
328
+ * For dynamic buckets, we need to re-query the list of buckets every time.
329
+ */
330
+ private async getCheckpointUpdateDynamic(
331
+ checkpoint: storage.StorageCheckpointUpdate
332
+ ): Promise<CheckpointUpdate | null> {
333
+ const querier = this.querier;
334
+ const storage = this.bucketStorage;
335
+ const staticBuckets = querier.staticBuckets;
336
+ const update = checkpoint.update;
337
+
338
+ let hasChange = false;
339
+ if (update.invalidateDataBuckets || update.updatedDataBuckets?.length > 0) {
340
+ hasChange = true;
341
+ } else if (update.invalidateParameterBuckets) {
342
+ hasChange = true;
343
+ } else {
344
+ for (let bucket of update.updatedParameterBucketDefinitions ?? []) {
345
+ if (querier.dynamicBucketDefinitions.has(bucket)) {
346
+ hasChange = true;
347
+ break;
348
+ }
349
+ }
350
+ }
351
+
352
+ if (!hasChange) {
353
+ return null;
354
+ }
355
+
356
+ const dynamicBuckets = await querier.queryDynamicBucketDescriptions({
357
+ getParameterSets(lookups) {
358
+ return storage.getParameterSets(checkpoint.base.checkpoint, lookups);
359
+ }
360
+ });
361
+ const allBuckets = [...staticBuckets, ...dynamicBuckets];
362
+
363
+ return {
364
+ buckets: allBuckets,
365
+ // We cannot track individual bucket updates for dynamic lookups yet
366
+ updatedBuckets: null
367
+ };
368
+ }
369
+ }
370
+
371
+ export interface CheckpointLine {
372
+ checkpointLine: util.StreamingSyncCheckpointDiff | util.StreamingSyncCheckpoint;
373
+ bucketsToFetch: BucketDescription[];
374
+ }
375
+
376
+ // Use a more specific type to simplify testing
377
+ export type BucketChecksumStateStorage = Pick<storage.SyncRulesBucketStorage, 'getChecksums' | 'getParameterSets'>;
378
+
379
+ function limitedBuckets(buckets: string[] | { bucket: string }[], limit: number) {
380
+ buckets = buckets.map((b) => {
381
+ if (typeof b != 'string') {
382
+ return b.bucket;
383
+ } else {
384
+ return b;
385
+ }
386
+ });
387
+ if (buckets.length <= limit) {
388
+ return JSON.stringify(buckets);
389
+ }
390
+ const limited = buckets.slice(0, limit);
391
+ return `${JSON.stringify(limited)}...`;
392
+ }
@@ -5,3 +5,4 @@ export * from './RequestTracker.js';
5
5
  export * from './safeRace.js';
6
6
  export * from './sync.js';
7
7
  export * from './util.js';
8
+ export * from './BucketChecksumState.js';