@powersync/service-core 0.18.1 → 1.7.0
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.
- package/CHANGELOG.md +16 -0
- package/dist/api/RouteAPI.d.ts +1 -1
- package/dist/api/diagnostics.js +107 -169
- package/dist/api/diagnostics.js.map +1 -1
- package/dist/entry/commands/compact-action.js +10 -73
- package/dist/entry/commands/compact-action.js.map +1 -1
- package/dist/modules/AbstractModule.d.ts +1 -1
- package/dist/replication/AbstractReplicator.js +2 -65
- package/dist/replication/AbstractReplicator.js.map +1 -1
- package/dist/routes/endpoints/checkpointing.js +3 -2
- package/dist/routes/endpoints/checkpointing.js.map +1 -1
- package/dist/routes/endpoints/socket-route.js +5 -5
- package/dist/routes/endpoints/socket-route.js.map +1 -1
- package/dist/routes/endpoints/sync-stream.js +5 -5
- package/dist/routes/endpoints/sync-stream.js.map +1 -1
- package/dist/runner/teardown.js +3 -65
- package/dist/runner/teardown.js.map +1 -1
- package/dist/storage/BucketStorage.d.ts +1 -442
- package/dist/storage/BucketStorage.js +0 -9
- package/dist/storage/BucketStorage.js.map +1 -1
- package/dist/storage/BucketStorageBatch.d.ts +130 -0
- package/dist/storage/BucketStorageBatch.js +10 -0
- package/dist/storage/BucketStorageBatch.js.map +1 -0
- package/dist/storage/BucketStorageFactory.d.ts +136 -0
- package/dist/storage/BucketStorageFactory.js +2 -0
- package/dist/storage/BucketStorageFactory.js.map +1 -0
- package/dist/storage/ChecksumCache.js.map +1 -1
- package/dist/storage/PersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/PersistedSyncRulesContent.js +2 -0
- package/dist/storage/PersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/ReplicationEventPayload.d.ts +1 -1
- package/dist/storage/ReplicationLock.d.ts +4 -0
- package/dist/storage/ReplicationLock.js +2 -0
- package/dist/storage/ReplicationLock.js.map +1 -0
- package/dist/storage/StorageEngine.d.ts +4 -4
- package/dist/storage/StorageEngine.js +2 -2
- package/dist/storage/StorageEngine.js.map +1 -1
- package/dist/storage/StorageProvider.d.ts +4 -1
- package/dist/storage/SyncRulesBucketStorage.d.ts +201 -0
- package/dist/storage/SyncRulesBucketStorage.js +7 -0
- package/dist/storage/SyncRulesBucketStorage.js.map +1 -0
- package/dist/storage/bson.d.ts +11 -3
- package/dist/storage/bson.js +24 -2
- package/dist/storage/bson.js.map +1 -1
- package/dist/storage/storage-index.d.ts +5 -0
- package/dist/storage/storage-index.js +5 -0
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/sync/BucketChecksumState.d.ts +91 -0
- package/dist/sync/BucketChecksumState.js +313 -0
- package/dist/sync/BucketChecksumState.js.map +1 -0
- package/dist/sync/sync-index.d.ts +1 -0
- package/dist/sync/sync-index.js +1 -0
- package/dist/sync/sync-index.js.map +1 -1
- package/dist/sync/sync.d.ts +7 -3
- package/dist/sync/sync.js +131 -135
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/util.d.ts +9 -0
- package/dist/sync/util.js +44 -0
- package/dist/sync/util.js.map +1 -1
- package/dist/util/checkpointing.d.ts +1 -1
- package/dist/util/checkpointing.js +15 -78
- package/dist/util/checkpointing.js.map +1 -1
- package/dist/util/protocol-types.d.ts +13 -4
- package/package.json +4 -4
- package/src/api/RouteAPI.ts +1 -1
- package/src/api/diagnostics.ts +1 -1
- package/src/entry/commands/compact-action.ts +2 -3
- package/src/modules/AbstractModule.ts +1 -1
- package/src/replication/AbstractReplicator.ts +1 -2
- package/src/routes/endpoints/checkpointing.ts +3 -3
- package/src/routes/endpoints/socket-route.ts +7 -5
- package/src/routes/endpoints/sync-stream.ts +8 -5
- package/src/runner/teardown.ts +1 -1
- package/src/storage/BucketStorage.ts +1 -552
- package/src/storage/BucketStorageBatch.ts +158 -0
- package/src/storage/BucketStorageFactory.ts +156 -0
- package/src/storage/ChecksumCache.ts +1 -0
- package/src/storage/PersistedSyncRulesContent.ts +26 -0
- package/src/storage/ReplicationEventPayload.ts +1 -1
- package/src/storage/ReplicationLock.ts +5 -0
- package/src/storage/StorageEngine.ts +4 -4
- package/src/storage/StorageProvider.ts +4 -1
- package/src/storage/SyncRulesBucketStorage.ts +256 -0
- package/src/storage/bson.ts +28 -4
- package/src/storage/storage-index.ts +5 -0
- package/src/sync/BucketChecksumState.ts +392 -0
- package/src/sync/sync-index.ts +1 -0
- package/src/sync/sync.ts +173 -157
- package/src/sync/util.ts +54 -0
- package/src/util/checkpointing.ts +4 -6
- package/src/util/protocol-types.ts +16 -4
- package/test/src/auth.test.ts +5 -5
- package/test/src/sync/BucketChecksumState.test.ts +565 -0
- package/test/src/sync/util.test.ts +34 -0
- package/tsconfig.tsbuildinfo +1 -1
package/src/storage/bson.ts
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import * as bson from 'bson';
|
|
2
2
|
|
|
3
3
|
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
4
|
-
import { ReplicaId } from './
|
|
4
|
+
import { ReplicaId } from './BucketStorageBatch.js';
|
|
5
5
|
|
|
6
6
|
type NodeBuffer = Buffer<ArrayBuffer>;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Use for internal (bucket storage) data, where we control each field.
|
|
10
|
+
*/
|
|
11
|
+
export const BSON_DESERIALIZE_INTERNAL_OPTIONS: bson.DeserializeOptions = {
|
|
9
12
|
// use bigint instead of Long
|
|
10
13
|
useBigInt64: true
|
|
11
14
|
};
|
|
12
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Use for data from external sources.
|
|
18
|
+
*/
|
|
19
|
+
export const BSON_DESERIALIZE_DATA_OPTIONS: bson.DeserializeOptions = {
|
|
20
|
+
// Temporarily disable due to https://jira.mongodb.org/browse/NODE-6764
|
|
21
|
+
useBigInt64: false
|
|
22
|
+
};
|
|
23
|
+
|
|
13
24
|
/**
|
|
14
25
|
* Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
|
|
15
26
|
* @param lookup
|
|
@@ -29,6 +40,11 @@ export const serializeLookup = (lookup: SqliteJsonValue[]) => {
|
|
|
29
40
|
return new bson.Binary(serializeLookupBuffer(lookup));
|
|
30
41
|
};
|
|
31
42
|
|
|
43
|
+
export const getLookupBucketDefinitionName = (lookup: bson.Binary) => {
|
|
44
|
+
const parsed = bson.deserialize(lookup.buffer, BSON_DESERIALIZE_INTERNAL_OPTIONS).l as SqliteJsonValue[];
|
|
45
|
+
return parsed[0] as string;
|
|
46
|
+
};
|
|
47
|
+
|
|
32
48
|
/**
|
|
33
49
|
* True if this is a bson.UUID.
|
|
34
50
|
*
|
|
@@ -51,8 +67,16 @@ export const deserializeReplicaId = (id: Buffer): ReplicaId => {
|
|
|
51
67
|
return deserialized.id;
|
|
52
68
|
};
|
|
53
69
|
|
|
54
|
-
export const deserializeBson = (buffer:
|
|
55
|
-
|
|
70
|
+
export const deserializeBson = (buffer: Uint8Array): bson.Document => {
|
|
71
|
+
const doc = bson.deserialize(buffer, BSON_DESERIALIZE_DATA_OPTIONS);
|
|
72
|
+
// Temporary workaround due to https://jira.mongodb.org/browse/NODE-6764
|
|
73
|
+
for (let key in doc) {
|
|
74
|
+
const value = doc[key];
|
|
75
|
+
if (value instanceof bson.Long) {
|
|
76
|
+
doc[key] = value.toBigInt();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return doc;
|
|
56
80
|
};
|
|
57
81
|
|
|
58
82
|
export const serializeBson = (document: any): NodeBuffer => {
|
|
@@ -7,3 +7,8 @@ export * from './SourceTable.js';
|
|
|
7
7
|
export * from './StorageEngine.js';
|
|
8
8
|
export * from './StorageProvider.js';
|
|
9
9
|
export * from './WriteCheckpointAPI.js';
|
|
10
|
+
export * from './BucketStorageFactory.js';
|
|
11
|
+
export * from './BucketStorageBatch.js';
|
|
12
|
+
export * from './SyncRulesBucketStorage.js';
|
|
13
|
+
export * from './PersistedSyncRulesContent.js';
|
|
14
|
+
export * from './ReplicationLock.js';
|
|
@@ -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
|
+
}
|
package/src/sync/sync-index.ts
CHANGED