@powersync/service-module-mongodb-storage 0.23.2 → 0.24.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 +36 -0
- package/LICENSE +201 -67
- package/dist/storage/MongoBucketStorage.d.ts +1 -0
- package/dist/storage/MongoBucketStorage.js +3 -0
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoBucketBatch.d.ts +60 -39
- package/dist/storage/implementation/MongoBucketBatch.js +123 -296
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoOpIdAllocator.d.ts +6 -1
- package/dist/storage/implementation/MongoOpIdAllocator.js +12 -5
- package/dist/storage/implementation/MongoOpIdAllocator.js.map +1 -1
- package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +7 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js +28 -2
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -1
- package/dist/storage/implementation/MongoReplicationOptions.d.ts +27 -0
- package/dist/storage/implementation/MongoReplicationOptions.js +26 -0
- package/dist/storage/implementation/MongoReplicationOptions.js.map +1 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js +2 -3
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +15 -2
- package/dist/storage/implementation/MongoSyncRulesLock.js +36 -14
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/common/BatchOperationBuffer.d.ts +79 -0
- package/dist/storage/implementation/common/BatchOperationBuffer.js +286 -0
- package/dist/storage/implementation/common/BatchOperationBuffer.js.map +1 -0
- package/dist/storage/implementation/common/PersistedBatch.d.ts +10 -103
- package/dist/storage/implementation/common/PersistedBatch.js +1 -291
- package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
- package/dist/storage/implementation/common/PreparedPublication.d.ts +43 -0
- package/dist/storage/implementation/common/PreparedPublication.js +2 -0
- package/dist/storage/implementation/common/PreparedPublication.js.map +1 -0
- package/dist/storage/implementation/common/SourceRecordStore.d.ts +0 -2
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +17 -13
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +259 -18
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +21 -15
- package/dist/storage/implementation/v1/PersistedBatchV1.js +58 -24
- package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/SourceRecordStoreV1.d.ts +0 -2
- package/dist/storage/implementation/v1/SourceRecordStoreV1.js +0 -3
- package/dist/storage/implementation/v1/SourceRecordStoreV1.js.map +1 -1
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +25 -14
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +350 -23
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +1 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.js +10 -4
- package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +39 -28
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +46 -24
- package/dist/storage/implementation/v3/PersistedBatchV3.js +159 -83
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +12 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +41 -13
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -1
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +4 -0
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +4 -0
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -1
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +27 -2
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +61 -5
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -1
- package/package.json +9 -10
- package/src/storage/MongoBucketStorage.ts +4 -0
- package/src/storage/implementation/MongoBucketBatch.ts +185 -392
- package/src/storage/implementation/MongoOpIdAllocator.ts +12 -5
- package/src/storage/implementation/MongoParsedSyncConfigSet.ts +40 -1
- package/src/storage/implementation/MongoReplicationOptions.ts +28 -0
- package/src/storage/implementation/MongoSyncBucketStorage.ts +2 -3
- package/src/storage/implementation/MongoSyncRulesLock.ts +39 -17
- package/src/storage/implementation/common/BatchOperationBuffer.ts +353 -0
- package/src/storage/implementation/common/PersistedBatch.ts +12 -390
- package/src/storage/implementation/common/PreparedPublication.ts +49 -0
- package/src/storage/implementation/common/SourceRecordStore.ts +0 -2
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +339 -27
- package/src/storage/implementation/v1/PersistedBatchV1.ts +74 -32
- package/src/storage/implementation/v1/SourceRecordStoreV1.ts +0 -5
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +430 -32
- package/src/storage/implementation/v3/MongoCompactorV3.ts +13 -4
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +43 -29
- package/src/storage/implementation/v3/PersistedBatchV3.ts +209 -108
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +45 -15
- package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +9 -0
- package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +79 -5
- package/test/src/checkpoint_cleanup.test.ts +75 -0
- package/test/src/concurrent_writers.test.ts +127 -53
- package/test/src/helpers/PublicationCommandMonitor.ts +119 -0
- package/test/src/helpers/concurrentWriter.mjs +2 -2
- package/test/src/invalid_sync_config.test.ts +9 -0
- package/test/src/object_storage_lifecycle.test.ts +72 -2
- package/test/src/prepared_publication.test.ts +388 -0
- package/test/src/replication_preparation.test.ts +829 -0
- package/test/src/replication_publication_baseline.test.ts +203 -0
- package/test/src/replication_publication_batching.test.ts +212 -0
- package/test/src/replication_uploads.test.ts +393 -0
- package/test/src/source_table_config.test.ts +4 -4
- package/test/src/storage_object_storage_inline_threshold.test.ts +1 -1
- package/test/src/storage_s3_compaction_lifecycle.test.ts +93 -1
- package/test/src/storage_sync.test.ts +83 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,43 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Logger } from '@powersync/lib-services-framework';
|
|
2
|
+
import { InternalOpId, storage } from '@powersync/service-core';
|
|
3
|
+
import { BucketDefinitionId, EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
|
|
3
4
|
import * as bson from 'bson';
|
|
4
|
-
|
|
5
|
-
import { logger as defaultLogger, Logger } from '@powersync/lib-services-framework';
|
|
6
|
-
import { BucketDefinitionMapping, InternalOpId, storage, utils } from '@powersync/service-core';
|
|
7
|
-
import { JSONBig } from '@powersync/service-jsonbig';
|
|
8
|
-
import { mongoTableId, replicaIdToSubkey } from '../../../utils/util.js';
|
|
9
|
-
import { currentBucketKey, MAX_ROW_SIZE } from '../MongoBucketBatchShared.js';
|
|
10
5
|
import { MongoIdSequence } from '../MongoIdSequence.js';
|
|
11
|
-
import { MongoWriteBatch } from '../MongoWriteBatch.js';
|
|
12
|
-
import type { VersionedPowerSyncMongo } from '../db.js';
|
|
13
|
-
import { TaggedBucketParameterDocument } from '../models.js';
|
|
14
|
-
import { ObjectStorage } from '../v3/object-storage/ObjectStorage.js';
|
|
15
|
-
import { BucketDataDoc, BucketKey } from './BucketDataDoc.js';
|
|
16
6
|
import { SourceRecordBucketState, SourceRecordLookupState } from './SourceRecordStore.js';
|
|
17
7
|
|
|
18
|
-
/**
|
|
19
|
-
* Maximum size of operations we write in a single transaction.
|
|
20
|
-
*
|
|
21
|
-
* It's tricky to find the exact limit, but from experience, over 100MB
|
|
22
|
-
* can cause an error:
|
|
23
|
-
* > transaction is too large and will not fit in the storage engine cache
|
|
24
|
-
*
|
|
25
|
-
* Additionally, unbounded size here can balloon our memory usage in some edge
|
|
26
|
-
* cases.
|
|
27
|
-
*
|
|
28
|
-
* When we reach this threshold, we commit the transaction and start a new one.
|
|
29
|
-
*/
|
|
30
|
-
const MAX_TRANSACTION_BATCH_SIZE = 30_000_000;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Limit number of documents to write in a single transaction.
|
|
34
|
-
*
|
|
35
|
-
* This has an effect on error message size in some cases.
|
|
36
|
-
*/
|
|
37
|
-
const MAX_TRANSACTION_DOC_COUNT = 2_000;
|
|
38
|
-
|
|
39
|
-
export const DEFAULT_INLINE_THRESHOLD_BYTES = 16 * 1024;
|
|
40
|
-
|
|
41
8
|
export interface SaveBucketDataOptions {
|
|
42
9
|
op_seq: MongoIdSequence;
|
|
43
10
|
sourceKey: storage.ReplicaId;
|
|
@@ -64,366 +31,21 @@ export interface UpsertCurrentDataOptions {
|
|
|
64
31
|
|
|
65
32
|
export interface PersistedBatchOptions {
|
|
66
33
|
logger?: Logger;
|
|
67
|
-
objectStorage?: ObjectStorage;
|
|
68
|
-
inlineThresholdBytes?: number;
|
|
69
|
-
/**
|
|
70
|
-
* Aborts in-flight object storage uploads when replication stops.
|
|
71
|
-
*/
|
|
72
|
-
signal?: AbortSignal;
|
|
73
|
-
objectStorageUsageWriterId?: string;
|
|
74
34
|
}
|
|
75
35
|
|
|
76
|
-
/**
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
logger: Logger;
|
|
84
|
-
bucketData: BucketDataDoc[] = [];
|
|
85
|
-
bucketParameters: TaggedBucketParameterDocument[] = [];
|
|
86
|
-
bucketStates: Map<string, BucketStateUpdate> = new Map();
|
|
87
|
-
|
|
88
|
-
protected readonly objectStorage?: ObjectStorage;
|
|
89
|
-
protected readonly inlineThresholdBytes: number = DEFAULT_INLINE_THRESHOLD_BYTES;
|
|
90
|
-
protected readonly signal?: AbortSignal;
|
|
91
|
-
protected readonly objectStorageUsageWriterId?: string;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* For debug logging only.
|
|
95
|
-
*/
|
|
96
|
-
debugLastOpId: InternalOpId | null = null;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Very rough estimate of transaction size.
|
|
100
|
-
*/
|
|
101
|
-
currentSize = 0;
|
|
102
|
-
|
|
103
|
-
constructor(
|
|
104
|
-
protected readonly db: VersionedPowerSyncMongo,
|
|
105
|
-
protected readonly group_id: number,
|
|
106
|
-
protected readonly mapping: BucketDefinitionMapping,
|
|
107
|
-
writtenSize: number,
|
|
108
|
-
options?: PersistedBatchOptions
|
|
109
|
-
) {
|
|
110
|
-
this.currentSize = writtenSize;
|
|
111
|
-
this.logger = options?.logger ?? defaultLogger;
|
|
112
|
-
this.objectStorage = options?.objectStorage;
|
|
113
|
-
this.signal = options?.signal;
|
|
114
|
-
this.objectStorageUsageWriterId = options?.objectStorageUsageWriterId;
|
|
115
|
-
if (options?.inlineThresholdBytes != null) {
|
|
116
|
-
this.inlineThresholdBytes = options.inlineThresholdBytes;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
saveBucketData(options: SaveBucketDataOptions) {
|
|
121
|
-
const remaining_buckets = new Map<string, SourceRecordBucketState>();
|
|
122
|
-
for (let bucket of options.before_buckets) {
|
|
123
|
-
const mapped: SourceRecordBucketState = {
|
|
124
|
-
bucket: bucket.bucket,
|
|
125
|
-
definitionId: this.checkDefinitionId(bucket.definitionId),
|
|
126
|
-
id: bucket.id,
|
|
127
|
-
table: bucket.table
|
|
128
|
-
};
|
|
129
|
-
if (options.table.bucketDataSourceIds != null && !options.table.bucketDataSourceIds.has(mapped.definitionId!)) {
|
|
130
|
-
// The bucket definition is not active anymore.
|
|
131
|
-
// We don't cleanup these references upfront, but do need to ignore them after the definition is removed.
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
remaining_buckets.set(currentBucketKey(mapped), mapped);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const subkey = this.persistedSubkey(options.table.id, options.sourceKey);
|
|
138
|
-
const dchecksum = BigInt(utils.hashDelete(subkey));
|
|
139
|
-
|
|
140
|
-
for (const evaluated of options.evaluated) {
|
|
141
|
-
const definitionId = this.getBucketDefinitionId(evaluated.source);
|
|
142
|
-
const key = currentBucketKey({
|
|
143
|
-
definitionId: definitionId,
|
|
144
|
-
bucket: evaluated.bucket,
|
|
145
|
-
table: evaluated.table,
|
|
146
|
-
id: evaluated.id
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
const recordData = JSONBig.stringify(evaluated.data);
|
|
150
|
-
const checksum = utils.hashData(evaluated.table, evaluated.id, recordData);
|
|
151
|
-
if (recordData.length > MAX_ROW_SIZE) {
|
|
152
|
-
this.logger.error(`Row ${key} too large: ${recordData.length} bytes. Removing.`);
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
remaining_buckets.delete(key);
|
|
157
|
-
const byteEstimate = recordData.length + 200;
|
|
158
|
-
this.currentSize += byteEstimate;
|
|
159
|
-
|
|
160
|
-
const op_id = options.op_seq.next();
|
|
161
|
-
this.debugLastOpId = op_id;
|
|
162
|
-
|
|
163
|
-
this.addBucketDataPut({
|
|
164
|
-
bucketKey: {
|
|
165
|
-
bucket: evaluated.bucket,
|
|
166
|
-
definitionId: definitionId,
|
|
167
|
-
replicationStreamId: this.group_id
|
|
168
|
-
},
|
|
169
|
-
op_id,
|
|
170
|
-
bucket: evaluated.bucket,
|
|
171
|
-
sourceTableId: options.table.id,
|
|
172
|
-
sourceKey: options.sourceKey,
|
|
173
|
-
subkey,
|
|
174
|
-
table: evaluated.table,
|
|
175
|
-
rowId: evaluated.id,
|
|
176
|
-
checksum: BigInt(checksum),
|
|
177
|
-
data: recordData
|
|
178
|
-
});
|
|
179
|
-
this.incrementBucket(definitionId, evaluated.bucket, op_id, byteEstimate);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
for (let bucket of remaining_buckets.values()) {
|
|
183
|
-
const definitionId = bucket.definitionId!;
|
|
184
|
-
const op_id = options.op_seq.next();
|
|
185
|
-
this.debugLastOpId = op_id;
|
|
186
|
-
|
|
187
|
-
this.addBucketDataRemove({
|
|
188
|
-
bucketKey: {
|
|
189
|
-
replicationStreamId: this.group_id,
|
|
190
|
-
definitionId,
|
|
191
|
-
bucket: bucket.bucket
|
|
192
|
-
},
|
|
193
|
-
op_id,
|
|
194
|
-
sourceTableId: options.table.id,
|
|
195
|
-
sourceKey: options.sourceKey,
|
|
196
|
-
subkey,
|
|
197
|
-
table: bucket.table,
|
|
198
|
-
rowId: bucket.id,
|
|
199
|
-
checksum: dchecksum
|
|
200
|
-
});
|
|
201
|
-
this.currentSize += 200;
|
|
202
|
-
this.incrementBucket(definitionId, bucket.bucket, op_id, 200);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
abstract saveParameterData(data: SaveParameterDataOptions): void;
|
|
207
|
-
|
|
208
|
-
abstract hardDeleteCurrentData(sourceTableId: bson.ObjectId, replicaId: storage.ReplicaId): void;
|
|
209
|
-
|
|
210
|
-
abstract softDeleteCurrentData(
|
|
36
|
+
/** Operations used by the shared writer; each storage version owns its lifecycle. */
|
|
37
|
+
export interface PersistedBatch {
|
|
38
|
+
readonly currentSize: number;
|
|
39
|
+
saveBucketData(options: SaveBucketDataOptions): void;
|
|
40
|
+
saveParameterData(options: SaveParameterDataOptions): void;
|
|
41
|
+
hardDeleteCurrentData(sourceTableId: bson.ObjectId, replicaId: storage.ReplicaId): void;
|
|
42
|
+
softDeleteCurrentData(
|
|
211
43
|
sourceTableId: bson.ObjectId,
|
|
212
44
|
replicaId: storage.ReplicaId,
|
|
213
45
|
checkpointGreaterThan: bigint
|
|
214
46
|
): void;
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
protected abstract get currentDataCount(): number;
|
|
219
|
-
|
|
220
|
-
protected abstract queueBucketData(writes: MongoWriteBatch): Promise<void>;
|
|
221
|
-
|
|
222
|
-
protected abstract queueBucketParameters(writes: MongoWriteBatch): void;
|
|
223
|
-
|
|
224
|
-
protected abstract queueCurrentData(writes: MongoWriteBatch): void;
|
|
225
|
-
|
|
226
|
-
protected abstract queueBucketStates(writes: MongoWriteBatch): void;
|
|
227
|
-
|
|
228
|
-
protected abstract resetCurrentData(): void;
|
|
229
|
-
|
|
230
|
-
protected abstract checkDefinitionId(definitionId: BucketDefinitionId | null): BucketDefinitionId;
|
|
231
|
-
protected abstract getBucketDefinitionId(bucketSource: BucketDataSource): BucketDefinitionId;
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* New bucket operations persist protocol subkeys during replication. Older
|
|
235
|
-
* V1 documents may not have one, so sync reads retain a fallback.
|
|
236
|
-
*/
|
|
237
|
-
protected persistedSubkey(table: storage.SourceTableId, key: storage.ReplicaId): string {
|
|
238
|
-
return replicaIdToSubkey(table, key);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
protected get bucketDataCount(): number {
|
|
242
|
-
return this.bucketData.length;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
protected incrementBucket(definitionId: BucketDefinitionId, bucket: string, op_id: InternalOpId, bytes: number) {
|
|
246
|
-
const key = `${definitionId ?? ''}:${bucket}`;
|
|
247
|
-
let existingState = this.bucketStates.get(key);
|
|
248
|
-
if (existingState) {
|
|
249
|
-
existingState.lastOp = op_id;
|
|
250
|
-
existingState.incrementCount += 1;
|
|
251
|
-
existingState.incrementBytes += bytes;
|
|
252
|
-
} else {
|
|
253
|
-
this.bucketStates.set(key, {
|
|
254
|
-
definitionId,
|
|
255
|
-
bucket,
|
|
256
|
-
lastOp: op_id,
|
|
257
|
-
incrementCount: 1,
|
|
258
|
-
incrementBytes: bytes,
|
|
259
|
-
incrementChunks: 0
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* V3 persists operations in chunks. Keep this separate from incrementBucket:
|
|
266
|
-
* operation counts are known while evaluating rows, while chunk counts and
|
|
267
|
-
* exact persisted bytes are only known after the writer has chunked a flush.
|
|
268
|
-
*/
|
|
269
|
-
protected incrementBucketPersistedChunk(definitionId: BucketDefinitionId, bucket: string, bytes: number) {
|
|
270
|
-
const key = `${definitionId ?? ''}:${bucket}`;
|
|
271
|
-
const existingState = this.bucketStates.get(key);
|
|
272
|
-
if (existingState != null) {
|
|
273
|
-
existingState.incrementChunks += 1;
|
|
274
|
-
existingState.incrementBytes += bytes;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* V3's compactor calculates byte deltas from persisted chunk metadata.
|
|
280
|
-
* Replace the writer's per-operation estimate with those exact sizes before
|
|
281
|
-
* flushing the corresponding bucket state.
|
|
282
|
-
*/
|
|
283
|
-
protected resetBucketPersistedBytes(definitionId: BucketDefinitionId, bucket: string) {
|
|
284
|
-
const state = this.bucketStates.get(`${definitionId ?? ''}:${bucket}`);
|
|
285
|
-
if (state != null) {
|
|
286
|
-
state.incrementBytes = 0;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
protected addBucketDataPut(options: {
|
|
291
|
-
op_id: InternalOpId;
|
|
292
|
-
bucketKey: BucketKey;
|
|
293
|
-
bucket: string;
|
|
294
|
-
sourceTableId: storage.SourceTable['id'];
|
|
295
|
-
sourceKey: storage.ReplicaId;
|
|
296
|
-
subkey: string;
|
|
297
|
-
table: string;
|
|
298
|
-
rowId: string;
|
|
299
|
-
checksum: bigint;
|
|
300
|
-
data: string;
|
|
301
|
-
}) {
|
|
302
|
-
this.bucketData.push({
|
|
303
|
-
bucketKey: options.bucketKey,
|
|
304
|
-
o: options.op_id,
|
|
305
|
-
op: 'PUT',
|
|
306
|
-
source_table: mongoTableId(options.sourceTableId),
|
|
307
|
-
source_key: options.sourceKey,
|
|
308
|
-
subkey: options.subkey,
|
|
309
|
-
table: options.table,
|
|
310
|
-
row_id: options.rowId,
|
|
311
|
-
checksum: options.checksum,
|
|
312
|
-
data: options.data
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
protected addBucketDataRemove(options: {
|
|
317
|
-
op_id: InternalOpId;
|
|
318
|
-
bucketKey: BucketKey;
|
|
319
|
-
sourceTableId: storage.SourceTable['id'];
|
|
320
|
-
sourceKey: storage.ReplicaId;
|
|
321
|
-
subkey: string;
|
|
322
|
-
table: string;
|
|
323
|
-
rowId: string;
|
|
324
|
-
checksum: bigint;
|
|
325
|
-
}) {
|
|
326
|
-
this.bucketData.push({
|
|
327
|
-
bucketKey: options.bucketKey,
|
|
328
|
-
o: options.op_id,
|
|
329
|
-
op: 'REMOVE',
|
|
330
|
-
source_table: mongoTableId(options.sourceTableId),
|
|
331
|
-
source_key: options.sourceKey,
|
|
332
|
-
subkey: options.subkey,
|
|
333
|
-
table: options.table,
|
|
334
|
-
row_id: options.rowId,
|
|
335
|
-
checksum: options.checksum,
|
|
336
|
-
data: null
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
shouldFlushTransaction() {
|
|
341
|
-
return (
|
|
342
|
-
this.currentSize >= MAX_TRANSACTION_BATCH_SIZE ||
|
|
343
|
-
this.bucketDataCount >= MAX_TRANSACTION_DOC_COUNT ||
|
|
344
|
-
this.currentDataCount >= MAX_TRANSACTION_DOC_COUNT ||
|
|
345
|
-
this.bucketParameters.length >= MAX_TRANSACTION_DOC_COUNT
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
async flush(session: mongo.ClientSession, options?: storage.BucketBatchCommitOptions) {
|
|
350
|
-
const startAt = performance.now();
|
|
351
|
-
let flushedSomething = false;
|
|
352
|
-
const writes = this.db.createWriteBatch(session, { ordered: false });
|
|
353
|
-
if (this.bucketDataCount > 0) {
|
|
354
|
-
flushedSomething = true;
|
|
355
|
-
await this.queueBucketData(writes);
|
|
356
|
-
}
|
|
357
|
-
if (this.bucketParameters.length > 0) {
|
|
358
|
-
flushedSomething = true;
|
|
359
|
-
this.queueBucketParameters(writes);
|
|
360
|
-
}
|
|
361
|
-
if (this.currentDataCount > 0) {
|
|
362
|
-
flushedSomething = true;
|
|
363
|
-
this.queueCurrentData(writes);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
if (this.bucketStates.size > 0) {
|
|
367
|
-
flushedSomething = true;
|
|
368
|
-
this.queueBucketStates(writes);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
await writes.execute();
|
|
372
|
-
|
|
373
|
-
if (flushedSomething) {
|
|
374
|
-
const duration = Math.round(performance.now() - startAt);
|
|
375
|
-
if (options?.oldestUncommittedChange != null) {
|
|
376
|
-
const replicationLag = Math.round((Date.now() - options.oldestUncommittedChange.getTime()) / 1000);
|
|
377
|
-
|
|
378
|
-
this.logger.info(
|
|
379
|
-
`Flushed: ${this.bucketDataCount} ops, ${this.bucketParameters.length} index entries, ${
|
|
380
|
-
this.currentDataCount
|
|
381
|
-
} records. ${Math.round(this.currentSize / 1024)}kb in ${duration}ms. Last op_id: ${this.debugLastOpId}. Replication lag: ${replicationLag}s`,
|
|
382
|
-
{
|
|
383
|
-
flushed: {
|
|
384
|
-
duration: duration,
|
|
385
|
-
size: this.currentSize,
|
|
386
|
-
bucket_ops_count: this.bucketDataCount,
|
|
387
|
-
parameter_indexes_count: this.bucketParameters.length,
|
|
388
|
-
source_records_count: this.currentDataCount,
|
|
389
|
-
replication_lag_seconds: replicationLag
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
);
|
|
393
|
-
} else {
|
|
394
|
-
this.logger.info(
|
|
395
|
-
`Flushed: ${this.bucketDataCount} ops, ${this.bucketParameters.length} index entries, ${
|
|
396
|
-
this.currentDataCount
|
|
397
|
-
} records. ${Math.round(this.currentSize / 1024)}kb in ${duration}ms. Last op_id: ${this.debugLastOpId}`,
|
|
398
|
-
{
|
|
399
|
-
flushed: {
|
|
400
|
-
duration: duration,
|
|
401
|
-
size: this.currentSize,
|
|
402
|
-
bucket_ops_count: this.bucketDataCount,
|
|
403
|
-
parameter_indexes_count: this.bucketParameters.length,
|
|
404
|
-
source_records_count: this.currentDataCount
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
const stats = {
|
|
412
|
-
bucketDataCount: this.bucketDataCount,
|
|
413
|
-
parameterDataCount: this.bucketParameters.length,
|
|
414
|
-
currentDataCount: this.currentDataCount,
|
|
415
|
-
flushedAny: flushedSomething
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
this.bucketData = [];
|
|
419
|
-
this.bucketParameters = [];
|
|
420
|
-
this.resetCurrentData();
|
|
421
|
-
this.bucketStates.clear();
|
|
422
|
-
this.currentSize = 0;
|
|
423
|
-
this.debugLastOpId = null;
|
|
424
|
-
|
|
425
|
-
return stats;
|
|
426
|
-
}
|
|
47
|
+
upsertCurrentData(values: UpsertCurrentDataOptions): void;
|
|
48
|
+
shouldFlushTransaction(): boolean;
|
|
427
49
|
}
|
|
428
50
|
|
|
429
51
|
export interface BucketStateUpdate {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { storage } from '@powersync/service-core';
|
|
3
|
+
import { MongoWriteBatch } from '../MongoWriteBatch.js';
|
|
4
|
+
|
|
5
|
+
export interface PublicationStats {
|
|
6
|
+
bucketDataCount: number;
|
|
7
|
+
parameterDataCount: number;
|
|
8
|
+
currentDataCount: number;
|
|
9
|
+
flushedAny: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Sealed view of a v3/v4 batch. Preparation runs once, including on failure;
|
|
14
|
+
* publication creates fresh MongoDB writes on every attempt without clearing it.
|
|
15
|
+
*
|
|
16
|
+
* Scheduling metadata belongs to the caller: lease, selected config/mappings,
|
|
17
|
+
* input suffix, checkpoint intent and durable progress. Replay is valid only
|
|
18
|
+
* while that context and upload protection remain valid. A changed owner or
|
|
19
|
+
* config, or expired payload, must abandon this generation, never mutate it to
|
|
20
|
+
* fit a different context or overwrite its object paths. Publication also
|
|
21
|
+
* asserts that assigned IDs advance the freshly fenced persisted head.
|
|
22
|
+
*
|
|
23
|
+
* No application repair callbacks run here. The coordinator collects
|
|
24
|
+
* markRecordUnavailable records during evaluation and delivers them only after
|
|
25
|
+
* confirmed commit. Diagnostics run during evaluation.
|
|
26
|
+
* Before/after-flush hooks, source events and custom checkpoint consumption also
|
|
27
|
+
* belong to that coordinator, never to replayable writes. Only a confirmed
|
|
28
|
+
* commit may consume input, advance durable progress or notify readers.
|
|
29
|
+
*/
|
|
30
|
+
export interface PreparedPublication {
|
|
31
|
+
/**
|
|
32
|
+
* Explicitly start preparation after consuming assigned IDs. Sealing itself
|
|
33
|
+
* starts no I/O, so the coordinator can establish ownership before yielding.
|
|
34
|
+
* A rejected preparation is terminal for this generation; all started uploads
|
|
35
|
+
* must have settled before this promise rejects.
|
|
36
|
+
*/
|
|
37
|
+
preparePayloads(): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Requires completed readiness before starting the publication transaction.
|
|
40
|
+
* An optional batch supplies independent head/checkpoint writes to execute
|
|
41
|
+
* together with this group's data. This method owns execution of that batch;
|
|
42
|
+
* callers must not execute it again or include another group's dependent writes.
|
|
43
|
+
*/
|
|
44
|
+
publish(
|
|
45
|
+
session: mongo.ClientSession,
|
|
46
|
+
options?: storage.BucketBatchCommitOptions,
|
|
47
|
+
additionalWrites?: MongoWriteBatch
|
|
48
|
+
): Promise<PublicationStats>;
|
|
49
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
-
import { Logger } from '@powersync/lib-services-framework';
|
|
3
2
|
import { storage } from '@powersync/service-core';
|
|
4
3
|
import { BucketDefinitionId, EvaluatedParameters, EvaluatedRow, ParameterIndexId } from '@powersync/service-sync-rules';
|
|
5
4
|
import * as bson from 'bson';
|
|
@@ -44,5 +43,4 @@ export interface SourceRecordStore {
|
|
|
44
43
|
sourceTableId: bson.ObjectId,
|
|
45
44
|
limit: number
|
|
46
45
|
): Promise<LoadedSourceRecord[]>;
|
|
47
|
-
postCommitCleanup(lastCheckpoint: bigint, logger: Logger): Promise<void>;
|
|
48
46
|
}
|