@powersync/service-module-mongodb-storage 0.0.0-dev-20260602073133 → 0.0.0-dev-20260803144812
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 +101 -5
- package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.js +41 -0
- package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.js.map +1 -0
- package/dist/storage/MongoBucketStorage.d.ts +41 -16
- package/dist/storage/MongoBucketStorage.js +226 -71
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoBucketBatch.d.ts +56 -25
- package/dist/storage/implementation/MongoBucketBatch.js +110 -62
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoChecksums.d.ts +41 -13
- package/dist/storage/implementation/MongoChecksums.js +36 -121
- package/dist/storage/implementation/MongoChecksums.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +17 -19
- package/dist/storage/implementation/MongoCompactor.js +80 -263
- package/dist/storage/implementation/MongoCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoHydrationState.d.ts +11 -0
- package/dist/storage/implementation/MongoHydrationState.js +49 -0
- package/dist/storage/implementation/MongoHydrationState.js.map +1 -0
- package/dist/storage/implementation/MongoParameterCompactor.d.ts +7 -6
- package/dist/storage/implementation/MongoParameterCompactor.js +25 -2
- package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +13 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js +64 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.d.ts +36 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.js +90 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.d.ts +25 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.js +123 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -0
- package/dist/storage/implementation/MongoStorageProvider.js +39 -4
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +57 -29
- package/dist/storage/implementation/MongoSyncBucketStorage.js +87 -61
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.js +3 -3
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +2 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +171 -24
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +8 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js +2 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js.map +1 -0
- package/dist/storage/implementation/common/PersistedBatch.d.ts +7 -2
- package/dist/storage/implementation/common/PersistedBatch.js +12 -0
- package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +1 -1
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -1
- package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +2 -2
- package/dist/storage/implementation/createMongoSyncBucketStorage.js +4 -4
- package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/db.js +17 -15
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/models.d.ts +20 -13
- package/dist/storage/implementation/models.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +9 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +61 -29
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +4 -4
- package/dist/storage/implementation/v1/MongoChecksumsV1.js +119 -7
- package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +11 -2
- package/dist/storage/implementation/v1/MongoCompactorV1.js +251 -2
- package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +18 -19
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +82 -58
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
- package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +9 -10
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js +2 -8
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +2 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +3 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -1
- package/dist/storage/implementation/v1/models.d.ts +10 -1
- package/dist/storage/implementation/v3/BucketDataContextV3.d.ts +13 -0
- package/dist/storage/implementation/v3/BucketDataContextV3.js +23 -0
- package/dist/storage/implementation/v3/BucketDataContextV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +8 -4
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +309 -259
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +30 -9
- package/dist/storage/implementation/v3/MongoChecksumsV3.js +161 -20
- package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +40 -2
- package/dist/storage/implementation/v3/MongoCompactorV3.js +647 -23
- package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +67 -0
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +351 -0
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +45 -30
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +272 -140
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +6 -3
- package/dist/storage/implementation/v3/PersistedBatchV3.js +84 -21
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +3 -3
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +13 -11
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -1
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +15 -13
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +32 -27
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
- package/dist/storage/implementation/v3/bucket-format.d.ts +13 -0
- package/dist/storage/implementation/v3/bucket-format.js +77 -0
- package/dist/storage/implementation/v3/bucket-format.js.map +1 -0
- package/dist/storage/implementation/v3/chunking.d.ts +10 -0
- package/dist/storage/implementation/v3/chunking.js +30 -0
- package/dist/storage/implementation/v3/chunking.js.map +1 -0
- package/dist/storage/implementation/v3/models.d.ts +101 -21
- package/dist/storage/implementation/v3/models.js +11 -28
- package/dist/storage/implementation/v3/models.js.map +1 -1
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +21 -0
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +44 -0
- package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +30 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorage.js +12 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorage.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +70 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +146 -0
- package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -0
- package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +42 -0
- package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +285 -0
- package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -0
- package/dist/storage/implementation/v3/source-table-utils.d.ts +55 -0
- package/dist/storage/implementation/v3/source-table-utils.js +214 -0
- package/dist/storage/implementation/v3/source-table-utils.js.map +1 -0
- package/dist/storage/storage-index.d.ts +4 -4
- package/dist/storage/storage-index.js +4 -4
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/types/types.d.ts +49 -3
- package/dist/types/types.js +48 -1
- package/dist/types/types.js.map +1 -1
- package/dist/utils/test-utils.d.ts +1 -2
- package/dist/utils/test-utils.js +7 -1
- package/dist/utils/test-utils.js.map +1 -1
- package/dist/utils/util.d.ts +9 -2
- package/dist/utils/util.js +128 -4
- package/dist/utils/util.js.map +1 -1
- package/package.json +12 -19
- package/src/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.ts +55 -0
- package/src/storage/MongoBucketStorage.ts +347 -88
- package/src/storage/implementation/MongoBucketBatch.ts +147 -82
- package/src/storage/implementation/MongoChecksums.ts +93 -153
- package/src/storage/implementation/MongoCompactor.ts +102 -316
- package/src/storage/implementation/MongoHydrationState.ts +61 -0
- package/src/storage/implementation/MongoParameterCompactor.ts +25 -6
- package/src/storage/implementation/MongoParsedSyncConfigSet.ts +88 -0
- package/src/storage/implementation/MongoPersistedReplicationStream.ts +118 -0
- package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +154 -0
- package/src/storage/implementation/MongoStorageProvider.ts +46 -4
- package/src/storage/implementation/MongoSyncBucketStorage.ts +121 -87
- package/src/storage/implementation/MongoSyncRulesLock.ts +7 -4
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +202 -28
- package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +9 -0
- package/src/storage/implementation/common/PersistedBatch.ts +19 -3
- package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +1 -1
- package/src/storage/implementation/createMongoSyncBucketStorage.ts +21 -7
- package/src/storage/implementation/db.ts +19 -20
- package/src/storage/implementation/models.ts +20 -14
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +75 -37
- package/src/storage/implementation/v1/MongoChecksumsV1.ts +154 -12
- package/src/storage/implementation/v1/MongoCompactorV1.ts +294 -9
- package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +142 -105
- package/src/storage/implementation/v1/SingleBucketStoreV1.ts +18 -27
- package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +11 -1
- package/src/storage/implementation/v1/models.ts +11 -1
- package/src/storage/implementation/v3/BucketDataContextV3.ts +32 -0
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +382 -321
- package/src/storage/implementation/v3/MongoChecksumsV3.ts +250 -32
- package/src/storage/implementation/v3/MongoCompactorV3.ts +847 -32
- package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +482 -0
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +359 -187
- package/src/storage/implementation/v3/PersistedBatchV3.ts +118 -37
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +12 -12
- package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +46 -36
- package/src/storage/implementation/v3/bucket-format.ts +97 -0
- package/src/storage/implementation/v3/chunking.ts +40 -0
- package/src/storage/implementation/v3/models.ts +132 -55
- package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +54 -0
- package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +29 -0
- package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +171 -0
- package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +343 -0
- package/src/storage/implementation/v3/source-table-utils.ts +392 -0
- package/src/storage/storage-index.ts +12 -4
- package/src/types/types.ts +58 -1
- package/src/utils/test-utils.ts +8 -3
- package/src/utils/util.ts +185 -6
- package/test/src/__snapshots__/storage.test.ts.snap +0 -75
- package/test/src/__snapshots__/storage_sync.test.ts.snap +35 -617
- package/test/src/cleanup-stopped-sync-configs.test.ts +671 -0
- package/test/src/helpers/MemoryObjectStorage.ts +53 -0
- package/test/src/helpers/s3TestFactory.ts +69 -0
- package/test/src/object_storage_lifecycle.test.ts +59 -0
- package/test/src/setup.ts +6 -1
- package/test/src/storage.test.ts +227 -13
- package/test/src/storage_compacting.test.ts +1951 -12
- package/test/src/storage_object_storage_inline_threshold.test.ts +116 -0
- package/test/src/storage_s3_checksums.test.ts +191 -0
- package/test/src/storage_s3_compaction_lifecycle.test.ts +417 -0
- package/test/src/storage_s3_reading.test.ts +526 -0
- package/test/src/storage_s3_writing.test.ts +135 -0
- package/test/src/storage_sync.test.ts +1180 -73
- package/test/src/storeCurrentData.test.ts +227 -0
- package/test/src/util.ts +2 -1
- package/tsconfig.json +1 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +0 -15
- package/dist/storage/implementation/BucketDefinitionMapping.js +0 -58
- package/dist/storage/implementation/BucketDefinitionMapping.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -14
- package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -67
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -29
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -76
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +0 -13
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +0 -2
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +0 -1
- package/dist/storage/implementation/common/SingleBucketStore.d.ts +0 -54
- package/dist/storage/implementation/common/SingleBucketStore.js +0 -3
- package/dist/storage/implementation/common/SingleBucketStore.js.map +0 -1
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +0 -9
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +0 -18
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +0 -1
- package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +0 -4
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js +0 -9
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +0 -1
- package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +0 -18
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js +0 -48
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +0 -1
- package/src/storage/implementation/BucketDefinitionMapping.ts +0 -75
- package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -82
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -101
- package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +0 -15
- package/src/storage/implementation/common/SingleBucketStore.ts +0 -63
- package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +0 -24
- package/src/storage/implementation/v3/MongoParameterLookupV3.ts +0 -11
- package/src/storage/implementation/v3/SingleBucketStoreV3.ts +0 -68
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { bson, BucketChecksum, BucketChecksumRequest, ChecksumMap, FetchPartialBucketChecksum, InternalOpId, PartialChecksumMap, PartialOrFullChecksum } from '@powersync/service-core';
|
|
2
3
|
import type { VersionedPowerSyncMongo } from './db.js';
|
|
3
|
-
import * as lib_mongo from '@powersync/lib-service-mongodb';
|
|
4
4
|
import { BucketDefinitionId } from '@powersync/service-sync-rules';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export interface FetchPartialBucketChecksumV3 {
|
|
5
|
+
import { StorageConfig } from './models.js';
|
|
6
|
+
export interface FetchPartialBucketChecksumByDefinition {
|
|
8
7
|
bucket: string;
|
|
9
8
|
definitionId: BucketDefinitionId;
|
|
10
9
|
start?: InternalOpId;
|
|
@@ -27,15 +26,39 @@ export interface MongoChecksumOptions {
|
|
|
27
26
|
* Limit on the number of documents to calculate a checksum on at a time.
|
|
28
27
|
*/
|
|
29
28
|
operationBatchLimit?: number;
|
|
29
|
+
checksumCacheTtlMs?: number;
|
|
30
30
|
storageConfig: StorageConfig;
|
|
31
|
-
mapping?: BucketDefinitionMapping;
|
|
32
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Read options used in requests to the cache, used to construct a session.
|
|
34
|
+
*
|
|
35
|
+
* readConcern is always 'snapshot'.
|
|
36
|
+
*/
|
|
37
|
+
interface MongoChecksumCacheReadContext {
|
|
38
|
+
/**
|
|
39
|
+
* (Optional) Specify a specific Timestamp for the snapshot read.
|
|
40
|
+
* Must include clusterTime as well.
|
|
41
|
+
*/
|
|
42
|
+
snapshotTime?: mongo.Timestamp;
|
|
43
|
+
clusterTime?: mongo.ClusterTime;
|
|
44
|
+
/**
|
|
45
|
+
* Read preference. Use undefined for the default,
|
|
46
|
+
*/
|
|
47
|
+
readPreference: mongo.ReadPreference | undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Read options passed into individual methods.
|
|
51
|
+
*/
|
|
52
|
+
export interface MongoChecksumSessionContext {
|
|
53
|
+
readOptions: Pick<mongo.CommandOperationOptions, 'session' | 'readPreference' | 'readConcern'>;
|
|
54
|
+
}
|
|
55
|
+
export declare const DEFAULT_OPERATION_BATCH_LIMIT = 50000;
|
|
33
56
|
export declare abstract class MongoChecksums {
|
|
34
57
|
protected readonly db: VersionedPowerSyncMongo;
|
|
35
58
|
protected readonly group_id: number;
|
|
36
59
|
protected readonly options: MongoChecksumOptions;
|
|
37
60
|
private _cache;
|
|
38
|
-
|
|
61
|
+
protected readonly storageConfig: StorageConfig;
|
|
39
62
|
constructor(db: VersionedPowerSyncMongo, group_id: number, options: MongoChecksumOptions);
|
|
40
63
|
/**
|
|
41
64
|
* Lazy-instantiated cache.
|
|
@@ -47,7 +70,7 @@ export declare abstract class MongoChecksums {
|
|
|
47
70
|
* Calculate checksums, utilizing the cache for partial checkums, and querying the remainder from
|
|
48
71
|
* the database (bucket_state + bucket_data).
|
|
49
72
|
*/
|
|
50
|
-
getChecksums(checkpoint: InternalOpId, buckets: BucketChecksumRequest[]): Promise<ChecksumMap>;
|
|
73
|
+
getChecksums(checkpoint: InternalOpId, buckets: BucketChecksumRequest[], options: MongoChecksumCacheReadContext): Promise<ChecksumMap>;
|
|
51
74
|
clearCache(): void;
|
|
52
75
|
/**
|
|
53
76
|
* Calculate (partial) checksums from bucket_state (pre-aggregated) and bucket_data (individual operations).
|
|
@@ -59,6 +82,7 @@ export declare abstract class MongoChecksums {
|
|
|
59
82
|
* can be slow.
|
|
60
83
|
*/
|
|
61
84
|
private computePartialChecksums;
|
|
85
|
+
private computePartialChecksumsWithSession;
|
|
62
86
|
/**
|
|
63
87
|
* Calculate (partial) checksums from the data collection directly, bypassing the cache and bucket_state.
|
|
64
88
|
*
|
|
@@ -69,7 +93,7 @@ export declare abstract class MongoChecksums {
|
|
|
69
93
|
* For large buckets, this can be slow, but should not time out as the underlying queries are performed in
|
|
70
94
|
* smaller batches.
|
|
71
95
|
*/
|
|
72
|
-
computePartialChecksumsDirect
|
|
96
|
+
private computePartialChecksumsDirect;
|
|
73
97
|
/**
|
|
74
98
|
* Query a batch of checksums.
|
|
75
99
|
*
|
|
@@ -77,11 +101,15 @@ export declare abstract class MongoChecksums {
|
|
|
77
101
|
*
|
|
78
102
|
* `batch` must be limited to DEFAULT_BUCKET_BATCH_LIMIT buckets before calling this.
|
|
79
103
|
*/
|
|
80
|
-
protected abstract computePartialChecksumsInternal(batch: FetchPartialBucketChecksum[]): Promise<PartialChecksumMap>;
|
|
81
|
-
protected abstract fetchPreStates(batch: FetchPartialBucketChecksum[]): Promise<Map<string, {
|
|
104
|
+
protected abstract computePartialChecksumsInternal(batch: FetchPartialBucketChecksum[], context: MongoChecksumSessionContext): Promise<PartialChecksumMap>;
|
|
105
|
+
protected abstract fetchPreStates(batch: FetchPartialBucketChecksum[], context: MongoChecksumSessionContext): Promise<Map<string, {
|
|
82
106
|
opId: InternalOpId;
|
|
83
107
|
checksum: BucketChecksum;
|
|
84
108
|
}>>;
|
|
85
|
-
protected computePartialChecksumsForCollection<TRequest extends FetchPartialBucketChecksumByBucket, TBucketDataDocument extends BucketDataDocumentBase>(batch: TRequest[], collection: lib_mongo.mongo.Collection<TBucketDataDocument>, createFilter: (request: TRequest) => any): Promise<PartialChecksumMap>;
|
|
86
109
|
}
|
|
87
|
-
export declare function emptyChecksumForRequest(request: Pick<FetchPartialBucketChecksum |
|
|
110
|
+
export declare function emptyChecksumForRequest(request: Pick<FetchPartialBucketChecksum | FetchPartialBucketChecksumByDefinition, 'bucket' | 'start'>): PartialOrFullChecksum;
|
|
111
|
+
/**
|
|
112
|
+
* Convert output of the $group stage into a checksum.
|
|
113
|
+
*/
|
|
114
|
+
export declare function checksumFromAggregate(doc: bson.Document): PartialOrFullChecksum;
|
|
115
|
+
export {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { addPartialChecksums, ChecksumCache
|
|
2
|
-
import
|
|
1
|
+
import { addPartialChecksums, ChecksumCache } from '@powersync/service-core';
|
|
2
|
+
import { ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
3
|
+
import { setSessionSnapshotTime } from '../../utils/util.js';
|
|
3
4
|
const DEFAULT_BUCKET_BATCH_LIMIT = 200;
|
|
4
|
-
const DEFAULT_OPERATION_BATCH_LIMIT = 50_000;
|
|
5
|
+
export const DEFAULT_OPERATION_BATCH_LIMIT = 50_000;
|
|
5
6
|
export class MongoChecksums {
|
|
6
7
|
db;
|
|
7
8
|
group_id;
|
|
@@ -21,8 +22,9 @@ export class MongoChecksums {
|
|
|
21
22
|
*/
|
|
22
23
|
get cache() {
|
|
23
24
|
this._cache ??= new ChecksumCache({
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
ttlMs: this.options.checksumCacheTtlMs,
|
|
26
|
+
fetchChecksums: (batch, context) => {
|
|
27
|
+
return this.computePartialChecksums(batch, context);
|
|
26
28
|
}
|
|
27
29
|
});
|
|
28
30
|
return this._cache;
|
|
@@ -31,8 +33,8 @@ export class MongoChecksums {
|
|
|
31
33
|
* Calculate checksums, utilizing the cache for partial checkums, and querying the remainder from
|
|
32
34
|
* the database (bucket_state + bucket_data).
|
|
33
35
|
*/
|
|
34
|
-
async getChecksums(checkpoint, buckets) {
|
|
35
|
-
return this.cache.getChecksumMap(checkpoint, buckets);
|
|
36
|
+
async getChecksums(checkpoint, buckets, options) {
|
|
37
|
+
return this.cache.getChecksumMap(checkpoint, buckets, options);
|
|
36
38
|
}
|
|
37
39
|
clearCache() {
|
|
38
40
|
this.cache.clear();
|
|
@@ -46,11 +48,31 @@ export class MongoChecksums {
|
|
|
46
48
|
* As long as data is compacted regularly, this should be fast. Large buckets without pre-compacted bucket_state
|
|
47
49
|
* can be slow.
|
|
48
50
|
*/
|
|
49
|
-
async computePartialChecksums(batch) {
|
|
51
|
+
async computePartialChecksums(batch, context) {
|
|
50
52
|
if (batch.length == 0) {
|
|
51
53
|
return new Map();
|
|
52
54
|
}
|
|
53
|
-
|
|
55
|
+
if (context == null) {
|
|
56
|
+
throw new ServiceAssertionError(`context is required`);
|
|
57
|
+
}
|
|
58
|
+
const { snapshotTime, clusterTime, readPreference } = context;
|
|
59
|
+
return this.db.client.withSession({ snapshot: true }, async (session) => {
|
|
60
|
+
if (snapshotTime != null && clusterTime != null) {
|
|
61
|
+
setSessionSnapshotTime(session, snapshotTime);
|
|
62
|
+
session.advanceClusterTime(clusterTime);
|
|
63
|
+
}
|
|
64
|
+
return this.computePartialChecksumsWithSession(batch, {
|
|
65
|
+
readOptions: {
|
|
66
|
+
// This is set on the session, but we set it on each operation as well just in case
|
|
67
|
+
readConcern: 'snapshot',
|
|
68
|
+
readPreference,
|
|
69
|
+
session
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async computePartialChecksumsWithSession(batch, context) {
|
|
75
|
+
const preStates = await this.fetchPreStates(batch, context);
|
|
54
76
|
const mappedRequests = batch.map((request) => {
|
|
55
77
|
let start = request.start;
|
|
56
78
|
if (start == null) {
|
|
@@ -64,7 +86,7 @@ export class MongoChecksums {
|
|
|
64
86
|
start
|
|
65
87
|
};
|
|
66
88
|
});
|
|
67
|
-
const queriedChecksums = await this.computePartialChecksumsDirect(mappedRequests);
|
|
89
|
+
const queriedChecksums = await this.computePartialChecksumsDirect(mappedRequests, context);
|
|
68
90
|
return new Map(batch.map((request) => {
|
|
69
91
|
const bucket = request.bucket;
|
|
70
92
|
// Could be null if this is either (1) a partial request, or (2) no compacted checksum was available
|
|
@@ -85,131 +107,24 @@ export class MongoChecksums {
|
|
|
85
107
|
* For large buckets, this can be slow, but should not time out as the underlying queries are performed in
|
|
86
108
|
* smaller batches.
|
|
87
109
|
*/
|
|
88
|
-
async computePartialChecksumsDirect(batch) {
|
|
110
|
+
async computePartialChecksumsDirect(batch, context) {
|
|
89
111
|
// Limit the number of buckets we query for at a time.
|
|
90
112
|
const bucketBatchLimit = this.options?.bucketBatchLimit ?? DEFAULT_BUCKET_BATCH_LIMIT;
|
|
91
113
|
if (batch.length <= bucketBatchLimit) {
|
|
92
114
|
// Single batch - no need for splitting the batch and merging results
|
|
93
|
-
return await this.computePartialChecksumsInternal(batch);
|
|
115
|
+
return await this.computePartialChecksumsInternal(batch, context);
|
|
94
116
|
}
|
|
95
117
|
// Split the batch and merge results
|
|
96
118
|
let results = new Map();
|
|
97
119
|
for (let i = 0; i < batch.length; i += bucketBatchLimit) {
|
|
98
120
|
const bucketBatch = batch.slice(i, i + bucketBatchLimit);
|
|
99
|
-
const batchResults = await this.computePartialChecksumsInternal(bucketBatch);
|
|
121
|
+
const batchResults = await this.computePartialChecksumsInternal(bucketBatch, context);
|
|
100
122
|
for (let r of batchResults.values()) {
|
|
101
123
|
results.set(r.bucket, r);
|
|
102
124
|
}
|
|
103
125
|
}
|
|
104
126
|
return results;
|
|
105
127
|
}
|
|
106
|
-
async computePartialChecksumsForCollection(batch, collection, createFilter) {
|
|
107
|
-
const batchLimit = this.options?.operationBatchLimit ?? DEFAULT_OPERATION_BATCH_LIMIT;
|
|
108
|
-
// Map requests by bucket. We adjust this as we get partial results.
|
|
109
|
-
let requests = new Map();
|
|
110
|
-
for (let request of batch) {
|
|
111
|
-
requests.set(request.bucket, request);
|
|
112
|
-
}
|
|
113
|
-
const partialChecksums = new Map();
|
|
114
|
-
while (requests.size > 0) {
|
|
115
|
-
const filters = Array.from(requests.values(), createFilter);
|
|
116
|
-
// Historically, checksum may be stored as 'int' or 'double'.
|
|
117
|
-
// More recently, this should be a 'long'.
|
|
118
|
-
// $toLong ensures that we always sum it as a long, avoiding inaccuracies in the calculations.
|
|
119
|
-
const checksumLong = this.storageConfig.longChecksums ? '$checksum' : { $toLong: '$checksum' };
|
|
120
|
-
// Aggregate over a max of `batchLimit` operations at a time.
|
|
121
|
-
// Let's say we have 3 buckets (A, B, C), each with 10 operations, and our batch limit is 12.
|
|
122
|
-
// Then we'll do three batches:
|
|
123
|
-
// 1. Query: A[1-end], B[1-end], C[1-end]
|
|
124
|
-
// Returns: A[1-10], B[1-2]
|
|
125
|
-
// 2. Query: B[3-end], C[1-end]
|
|
126
|
-
// Returns: B[3-10], C[1-4]
|
|
127
|
-
// 3. Query: C[5-end]
|
|
128
|
-
// Returns: C[5-10]
|
|
129
|
-
const aggregate = await collection
|
|
130
|
-
.aggregate([
|
|
131
|
-
{
|
|
132
|
-
$match: {
|
|
133
|
-
$or: filters
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
// sort and limit _before_ grouping
|
|
137
|
-
{ $sort: { _id: 1 } },
|
|
138
|
-
{ $limit: batchLimit },
|
|
139
|
-
{
|
|
140
|
-
$group: {
|
|
141
|
-
_id: '$_id.b',
|
|
142
|
-
checksum_total: { $sum: checksumLong },
|
|
143
|
-
count: { $sum: 1 },
|
|
144
|
-
has_clear_op: {
|
|
145
|
-
$max: {
|
|
146
|
-
$cond: [{ $eq: ['$op', 'CLEAR'] }, 1, 0]
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
last_op: { $max: '$_id.o' }
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
// Sort the aggregated results (100 max, so should be fast).
|
|
153
|
-
// This is important to identify which buckets we have partial data for.
|
|
154
|
-
{ $sort: { _id: 1 } }
|
|
155
|
-
], { session: undefined, readConcern: 'snapshot', maxTimeMS: lib_mongo.MONGO_CHECKSUM_TIMEOUT_MS })
|
|
156
|
-
.toArray()
|
|
157
|
-
.catch((e) => {
|
|
158
|
-
throw lib_mongo.mapQueryError(e, 'while reading checksums');
|
|
159
|
-
});
|
|
160
|
-
let batchCount = 0;
|
|
161
|
-
let limitReached = false;
|
|
162
|
-
for (let doc of aggregate) {
|
|
163
|
-
const bucket = doc._id;
|
|
164
|
-
const checksum = checksumFromAggregate(doc);
|
|
165
|
-
const existing = partialChecksums.get(bucket);
|
|
166
|
-
if (existing != null) {
|
|
167
|
-
partialChecksums.set(bucket, addPartialChecksums(bucket, existing, checksum));
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
partialChecksums.set(bucket, checksum);
|
|
171
|
-
}
|
|
172
|
-
batchCount += doc.count;
|
|
173
|
-
if (batchCount == batchLimit) {
|
|
174
|
-
// Limit reached. Request more in the next batch.
|
|
175
|
-
// Note that this only affects the _last_ bucket in a batch.
|
|
176
|
-
limitReached = true;
|
|
177
|
-
const req = requests.get(bucket);
|
|
178
|
-
requests.set(bucket, {
|
|
179
|
-
...req,
|
|
180
|
-
start: doc.last_op
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
// All done for this bucket
|
|
185
|
-
requests.delete(bucket);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (!limitReached) {
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return new Map(batch.map((request) => {
|
|
193
|
-
const bucket = request.bucket;
|
|
194
|
-
// Could be null if we got no data
|
|
195
|
-
let partialChecksum = partialChecksums.get(bucket);
|
|
196
|
-
if (partialChecksum == null) {
|
|
197
|
-
partialChecksum = {
|
|
198
|
-
bucket,
|
|
199
|
-
partialCount: 0,
|
|
200
|
-
partialChecksum: 0
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
if (request.start == null && isPartialChecksum(partialChecksum)) {
|
|
204
|
-
partialChecksum = {
|
|
205
|
-
bucket,
|
|
206
|
-
count: partialChecksum.partialCount,
|
|
207
|
-
checksum: partialChecksum.partialChecksum
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
return [bucket, partialChecksum];
|
|
211
|
-
}));
|
|
212
|
-
}
|
|
213
128
|
}
|
|
214
129
|
export function emptyChecksumForRequest(request) {
|
|
215
130
|
if (request.start == null) {
|
|
@@ -220,7 +135,7 @@ export function emptyChecksumForRequest(request) {
|
|
|
220
135
|
/**
|
|
221
136
|
* Convert output of the $group stage into a checksum.
|
|
222
137
|
*/
|
|
223
|
-
function checksumFromAggregate(doc) {
|
|
138
|
+
export function checksumFromAggregate(doc) {
|
|
224
139
|
const partialChecksum = Number(BigInt(doc.checksum_total) & 0xffffffffn) & 0xffffffff;
|
|
225
140
|
const bucket = doc._id;
|
|
226
141
|
if (doc.has_clear_op == 1) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MongoChecksums.js","sourceRoot":"","sources":["../../../src/storage/implementation/MongoChecksums.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MongoChecksums.js","sourceRoot":"","sources":["../../../src/storage/implementation/MongoChecksums.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EAInB,aAAa,EAOd,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAE1E,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AA6D7D,MAAM,0BAA0B,GAAG,GAAG,CAAC;AACvC,MAAM,CAAC,MAAM,6BAA6B,GAAG,MAAM,CAAC;AAEpD,MAAM,OAAgB,cAAc;IAKb;IACA;IACA;IANb,MAAM,CAA2D;IACtD,aAAa,CAAgB;IAEhD,YACqB,EAA2B,EAC3B,QAAgB,EAChB,OAA6B;QAF7B,OAAE,GAAF,EAAE,CAAyB;QAC3B,aAAQ,GAAR,QAAQ,CAAQ;QAChB,YAAO,GAAP,OAAO,CAAsB;QAEhD,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAC7C,CAAC;IAED;;;;OAIG;IACH,IAAY,KAAK;QACf,IAAI,CAAC,MAAM,KAAK,IAAI,aAAa,CAAgC;YAC/D,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,kBAAkB;YACtC,cAAc,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;gBACjC,OAAO,IAAI,CAAC,uBAAuB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACtD,CAAC;SACF,CAAC,CAAC;QACH,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,YAAY,CAChB,UAAwB,EACxB,OAAgC,EAChC,OAAsC;QAEtC,OAAO,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACjE,CAAC;IAED,UAAU;QACR,IAA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,OAAO;YACL,gDAAgD;YAChD,MAAM;YACN,YAAY,EAAE,GAAG,CAAC,KAAK;YACvB,eAAe;SACU,CAAC;IAC9B,CAAC;AACH,CAAC"}
|
|
@@ -2,12 +2,11 @@ import { mongo } from '@powersync/lib-service-mongodb';
|
|
|
2
2
|
import { Logger } from '@powersync/lib-services-framework';
|
|
3
3
|
import { InternalOpId, PopulateChecksumCacheResults, storage } from '@powersync/service-core';
|
|
4
4
|
import { BucketDefinitionId } from '@powersync/service-sync-rules';
|
|
5
|
-
import {
|
|
6
|
-
import { BucketDataDocumentGeneric, SingleBucketStore } from './common/SingleBucketStore.js';
|
|
5
|
+
import { BucketKey } from './common/BucketDataDoc.js';
|
|
7
6
|
import type { VersionedPowerSyncMongo } from './db.js';
|
|
8
7
|
import { BucketStateDocumentBase } from './models.js';
|
|
9
8
|
import type { MongoSyncBucketStorage } from './MongoSyncBucketStorage.js';
|
|
10
|
-
interface CurrentBucketState {
|
|
9
|
+
export interface CurrentBucketState {
|
|
11
10
|
/** Bucket name */
|
|
12
11
|
bucket: string;
|
|
13
12
|
definitionId: BucketDefinitionId;
|
|
@@ -42,6 +41,9 @@ interface CurrentBucketState {
|
|
|
42
41
|
}
|
|
43
42
|
export interface MongoCompactOptions extends storage.CompactOptions {
|
|
44
43
|
}
|
|
44
|
+
export declare class ConcurrentCompactionError extends Error {
|
|
45
|
+
constructor(message: string);
|
|
46
|
+
}
|
|
45
47
|
export interface DirtyBucket {
|
|
46
48
|
bucket: string;
|
|
47
49
|
definitionId: BucketDefinitionId | null;
|
|
@@ -51,16 +53,17 @@ export interface DirtyBucket {
|
|
|
51
53
|
export declare abstract class MongoCompactor {
|
|
52
54
|
protected readonly storage: MongoSyncBucketStorage;
|
|
53
55
|
protected readonly db: VersionedPowerSyncMongo;
|
|
54
|
-
protected updates: mongo.AnyBulkWriteOperation<BucketDataDocumentGeneric>[];
|
|
55
56
|
protected bucketStateUpdates: mongo.AnyBulkWriteOperation<BucketStateDocumentBase>[];
|
|
56
57
|
protected readonly idLimitBytes: number;
|
|
57
58
|
protected readonly moveBatchLimit: number;
|
|
58
59
|
protected readonly moveBatchQueryLimit: number;
|
|
60
|
+
protected readonly moveBatchByteLimit: number;
|
|
59
61
|
protected readonly clearBatchLimit: number;
|
|
60
62
|
protected readonly minBucketChanges: number;
|
|
61
63
|
protected readonly minChangeRatio: number;
|
|
62
64
|
protected readonly maxOpId: bigint;
|
|
63
65
|
protected readonly buckets: string[] | undefined;
|
|
66
|
+
protected readonly deleteCheckpointRequestsBefore: Date | undefined;
|
|
64
67
|
protected readonly signal?: AbortSignal;
|
|
65
68
|
protected readonly group_id: number;
|
|
66
69
|
protected readonly logger: Logger;
|
|
@@ -68,9 +71,10 @@ export declare abstract class MongoCompactor {
|
|
|
68
71
|
/**
|
|
69
72
|
* Compact buckets by converting operations into MOVE and/or CLEAR operations.
|
|
70
73
|
*
|
|
71
|
-
* See /docs/compacting-operations.md for details.
|
|
74
|
+
* See /docs/storage/compacting-operations.md for details.
|
|
72
75
|
*/
|
|
73
76
|
compact(): Promise<void>;
|
|
77
|
+
private deleteOldCheckpointRequests;
|
|
74
78
|
/**
|
|
75
79
|
* Subset of compact, only populating checksums where relevant.
|
|
76
80
|
*/
|
|
@@ -93,28 +97,22 @@ export declare abstract class MongoCompactor {
|
|
|
93
97
|
/**
|
|
94
98
|
* Compaction for a single bucket, with retries on failure.
|
|
95
99
|
*
|
|
96
|
-
*
|
|
100
|
+
* A compaction can race another compactor after its initial scan. Restarting
|
|
101
|
+
* the bucket is safe because each replacement is transactional. Object
|
|
102
|
+
* storage paths are also prepared with lifecycle markers, so a retry can
|
|
103
|
+
* safely overwrite or eventually clean up uploads from the failed attempt.
|
|
97
104
|
*/
|
|
98
105
|
protected compactSingleBucketRetried(bucket: string, definitionId?: BucketDefinitionId | null): Promise<void>;
|
|
99
|
-
protected compactSingleBucket(bucket: string, definitionId?: BucketDefinitionId | null): Promise<void>;
|
|
100
|
-
protected
|
|
101
|
-
protected
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Perform a CLEAR compact for a bucket.
|
|
105
|
-
*
|
|
106
|
-
* @param currentState tracks the last non-PUT op, which will be converted to CLEAR.
|
|
107
|
-
*/
|
|
108
|
-
protected clearBucket(currentState: CurrentBucketState, col: SingleBucketStore): Promise<void>;
|
|
106
|
+
protected abstract compactSingleBucket(bucket: string, definitionId?: BucketDefinitionId | null): Promise<void>;
|
|
107
|
+
protected collectBucketStateUpdates(state: CurrentBucketState, compactedOpId: InternalOpId): mongo.AnyBulkWriteOperation<BucketStateDocumentBase>;
|
|
108
|
+
protected updateBucketChecksums(state: CurrentBucketState, compactedOpId: InternalOpId): void;
|
|
109
|
+
protected flushBucketStateUpdates(): Promise<void>;
|
|
109
110
|
protected updateChecksumsBatch(buckets: Pick<DirtyBucket, 'bucket' | 'definitionId'>[]): Promise<void>;
|
|
110
|
-
protected formatBucketDataKey(doc: Pick<BucketDataDoc, 'bucketKey' | 'o'>): string;
|
|
111
111
|
protected abstract writeBucketStateUpdates(): Promise<void>;
|
|
112
112
|
protected abstract computeChecksumsForBuckets(buckets: Pick<DirtyBucket, 'bucket' | 'definitionId'>[]): Promise<storage.PartialChecksumMap>;
|
|
113
113
|
protected abstract bucketStateFilter(bucket: string, definitionId: BucketDefinitionId | null): mongo.Document;
|
|
114
|
-
protected abstract getBucketDataContext(bucket: string, definitionId: BucketDefinitionId | null): Promise<SingleBucketStore | null>;
|
|
115
114
|
}
|
|
116
115
|
export interface BucketDataCollectionContext<TBucketData extends mongo.Document> {
|
|
117
116
|
bucketKey: BucketKey;
|
|
118
117
|
collection: mongo.Collection<TBucketData>;
|
|
119
118
|
}
|
|
120
|
-
export {};
|