@powersync/service-module-mongodb 0.0.0-dev-20250102111825 → 0.0.0-dev-20250108073049
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 +8 -5
- package/dist/api/MongoRouteAPIAdapter.d.ts +1 -1
- package/dist/api/MongoRouteAPIAdapter.js +4 -3
- package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/module/MongoModule.d.ts +0 -1
- package/dist/module/MongoModule.js +2 -10
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/MongoRelation.js +15 -5
- package/dist/replication/MongoRelation.js.map +1 -1
- package/dist/types/types.d.ts +16 -22
- package/dist/types/types.js +4 -24
- package/dist/types/types.js.map +1 -1
- package/package.json +9 -10
- package/src/api/MongoRouteAPIAdapter.ts +4 -4
- package/src/index.ts +3 -4
- package/src/module/MongoModule.ts +2 -14
- package/src/replication/MongoRelation.ts +13 -5
- package/src/types/types.ts +8 -34
- package/test/src/change_stream.test.ts +2 -4
- package/test/src/env.ts +1 -1
- package/test/src/mongo_test.test.ts +69 -10
- package/test/src/setup.ts +4 -1
- package/test/src/slow_tests.test.ts +5 -12
- package/test/src/util.ts +5 -38
- package/test/tsconfig.json +1 -2
- package/tsconfig.json +6 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/db/db-index.d.ts +0 -1
- package/dist/db/db-index.js +0 -2
- package/dist/db/db-index.js.map +0 -1
- package/dist/db/mongo.d.ts +0 -35
- package/dist/db/mongo.js +0 -73
- package/dist/db/mongo.js.map +0 -1
- package/dist/locks/MongoLockManager.d.ts +0 -25
- package/dist/locks/MongoLockManager.js +0 -79
- package/dist/locks/MongoLockManager.js.map +0 -1
- package/dist/migrations/MongoMigrationAgent.d.ts +0 -12
- package/dist/migrations/MongoMigrationAgent.js +0 -25
- package/dist/migrations/MongoMigrationAgent.js.map +0 -1
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
- package/dist/migrations/db/migrations/1684951997326-init.js +0 -30
- package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -54
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -26
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -28
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
- package/dist/migrations/mongo-migration-store.d.ts +0 -7
- package/dist/migrations/mongo-migration-store.js +0 -49
- package/dist/migrations/mongo-migration-store.js.map +0 -1
- package/dist/storage/MongoBucketStorage.d.ts +0 -48
- package/dist/storage/MongoBucketStorage.js +0 -426
- package/dist/storage/MongoBucketStorage.js.map +0 -1
- package/dist/storage/implementation/MongoBucketBatch.d.ts +0 -72
- package/dist/storage/implementation/MongoBucketBatch.js +0 -681
- package/dist/storage/implementation/MongoBucketBatch.js.map +0 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +0 -40
- package/dist/storage/implementation/MongoCompactor.js +0 -300
- package/dist/storage/implementation/MongoCompactor.js.map +0 -1
- package/dist/storage/implementation/MongoIdSequence.d.ts +0 -12
- package/dist/storage/implementation/MongoIdSequence.js +0 -21
- package/dist/storage/implementation/MongoIdSequence.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -9
- package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -9
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -20
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -26
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
- package/dist/storage/implementation/MongoStorageProvider.d.ts +0 -6
- package/dist/storage/implementation/MongoStorageProvider.js +0 -34
- package/dist/storage/implementation/MongoStorageProvider.js.map +0 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +0 -36
- package/dist/storage/implementation/MongoSyncBucketStorage.js +0 -529
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +0 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +0 -16
- package/dist/storage/implementation/MongoSyncRulesLock.js +0 -65
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +0 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +0 -20
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +0 -104
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +0 -1
- package/dist/storage/implementation/OperationBatch.d.ts +0 -34
- package/dist/storage/implementation/OperationBatch.js +0 -119
- package/dist/storage/implementation/OperationBatch.js.map +0 -1
- package/dist/storage/implementation/PersistedBatch.d.ts +0 -46
- package/dist/storage/implementation/PersistedBatch.js +0 -223
- package/dist/storage/implementation/PersistedBatch.js.map +0 -1
- package/dist/storage/implementation/config.d.ts +0 -19
- package/dist/storage/implementation/config.js +0 -26
- package/dist/storage/implementation/config.js.map +0 -1
- package/dist/storage/implementation/db.d.ts +0 -36
- package/dist/storage/implementation/db.js +0 -47
- package/dist/storage/implementation/db.js.map +0 -1
- package/dist/storage/implementation/models.d.ts +0 -139
- package/dist/storage/implementation/models.js +0 -2
- package/dist/storage/implementation/models.js.map +0 -1
- package/dist/storage/implementation/util.d.ts +0 -41
- package/dist/storage/implementation/util.js +0 -141
- package/dist/storage/implementation/util.js.map +0 -1
- package/dist/storage/storage-index.d.ts +0 -14
- package/dist/storage/storage-index.js +0 -15
- package/dist/storage/storage-index.js.map +0 -1
- package/src/db/db-index.ts +0 -1
- package/src/db/mongo.ts +0 -81
- package/src/locks/MongoLockManager.ts +0 -123
- package/src/migrations/MongoMigrationAgent.ts +0 -39
- package/src/migrations/db/migrations/1684951997326-init.ts +0 -39
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -105
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -38
- package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -40
- package/src/migrations/mongo-migration-store.ts +0 -62
- package/src/storage/MongoBucketStorage.ts +0 -531
- package/src/storage/implementation/MongoBucketBatch.ts +0 -896
- package/src/storage/implementation/MongoCompactor.ts +0 -381
- package/src/storage/implementation/MongoIdSequence.ts +0 -24
- package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -16
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -49
- package/src/storage/implementation/MongoStorageProvider.ts +0 -42
- package/src/storage/implementation/MongoSyncBucketStorage.ts +0 -612
- package/src/storage/implementation/MongoSyncRulesLock.ts +0 -88
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +0 -146
- package/src/storage/implementation/OperationBatch.ts +0 -129
- package/src/storage/implementation/PersistedBatch.ts +0 -283
- package/src/storage/implementation/config.ts +0 -40
- package/src/storage/implementation/db.ts +0 -87
- package/src/storage/implementation/models.ts +0 -161
- package/src/storage/implementation/util.ts +0 -154
- package/src/storage/storage-index.ts +0 -14
- package/test/src/__snapshots__/storage_sync.test.ts.snap +0 -332
- package/test/src/storage.test.ts +0 -7
- package/test/src/storage_compacting.test.ts +0 -6
- package/test/src/storage_sync.test.ts +0 -113
|
@@ -1,531 +0,0 @@
|
|
|
1
|
-
import { SqlSyncRules } from '@powersync/service-sync-rules';
|
|
2
|
-
import { wrapWithAbort } from 'ix/asynciterable/operators/withabort.js';
|
|
3
|
-
import { LRUCache } from 'lru-cache/min';
|
|
4
|
-
import * as mongo from 'mongodb';
|
|
5
|
-
import * as timers from 'timers/promises';
|
|
6
|
-
|
|
7
|
-
import { storage, sync, utils } from '@powersync/service-core';
|
|
8
|
-
|
|
9
|
-
import { DisposableObserver, logger } from '@powersync/lib-services-framework';
|
|
10
|
-
import { v4 as uuid } from 'uuid';
|
|
11
|
-
|
|
12
|
-
import { MongoLockManager } from '../locks/MongoLockManager.js';
|
|
13
|
-
import { PowerSyncMongo } from './implementation/db.js';
|
|
14
|
-
import { SyncRuleDocument } from './implementation/models.js';
|
|
15
|
-
import { MongoPersistedSyncRulesContent } from './implementation/MongoPersistedSyncRulesContent.js';
|
|
16
|
-
import { MongoSyncBucketStorage } from './implementation/MongoSyncBucketStorage.js';
|
|
17
|
-
import { generateSlotName } from './implementation/util.js';
|
|
18
|
-
|
|
19
|
-
export class MongoBucketStorage
|
|
20
|
-
extends DisposableObserver<storage.BucketStorageFactoryListener>
|
|
21
|
-
implements storage.BucketStorageFactory
|
|
22
|
-
{
|
|
23
|
-
private readonly client: mongo.MongoClient;
|
|
24
|
-
private readonly session: mongo.ClientSession;
|
|
25
|
-
// TODO: This is still Postgres specific and needs to be reworked
|
|
26
|
-
public readonly slot_name_prefix: string;
|
|
27
|
-
|
|
28
|
-
private readonly storageCache = new LRUCache<number, MongoSyncBucketStorage>({
|
|
29
|
-
max: 3,
|
|
30
|
-
fetchMethod: async (id) => {
|
|
31
|
-
const doc2 = await this.db.sync_rules.findOne(
|
|
32
|
-
{
|
|
33
|
-
_id: id
|
|
34
|
-
},
|
|
35
|
-
{ limit: 1 }
|
|
36
|
-
);
|
|
37
|
-
if (doc2 == null) {
|
|
38
|
-
// Deleted in the meantime?
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
const rules = new MongoPersistedSyncRulesContent(this.db, doc2);
|
|
42
|
-
return this.getInstance(rules);
|
|
43
|
-
},
|
|
44
|
-
dispose: (storage) => {
|
|
45
|
-
storage[Symbol.dispose]();
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
public readonly db: PowerSyncMongo;
|
|
50
|
-
|
|
51
|
-
constructor(
|
|
52
|
-
db: PowerSyncMongo,
|
|
53
|
-
options: {
|
|
54
|
-
slot_name_prefix: string;
|
|
55
|
-
}
|
|
56
|
-
) {
|
|
57
|
-
super();
|
|
58
|
-
this.client = db.client;
|
|
59
|
-
this.db = db;
|
|
60
|
-
this.session = this.client.startSession();
|
|
61
|
-
this.slot_name_prefix = options.slot_name_prefix;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
getInstance(options: storage.PersistedSyncRulesContent): MongoSyncBucketStorage {
|
|
65
|
-
let { id, slot_name } = options;
|
|
66
|
-
if ((typeof id as any) == 'bigint') {
|
|
67
|
-
id = Number(id);
|
|
68
|
-
}
|
|
69
|
-
const storage = new MongoSyncBucketStorage(this, id, options, slot_name);
|
|
70
|
-
this.iterateListeners((cb) => cb.syncStorageCreated?.(storage));
|
|
71
|
-
storage.registerListener({
|
|
72
|
-
batchStarted: (batch) => {
|
|
73
|
-
// This nested listener will be automatically disposed when the storage is disposed
|
|
74
|
-
batch.registerManagedListener(storage, {
|
|
75
|
-
replicationEvent: (payload) => this.iterateListeners((cb) => cb.replicationEvent?.(payload))
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
return storage;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async configureSyncRules(sync_rules: string, options?: { lock?: boolean }) {
|
|
83
|
-
const next = await this.getNextSyncRulesContent();
|
|
84
|
-
const active = await this.getActiveSyncRulesContent();
|
|
85
|
-
|
|
86
|
-
if (next?.sync_rules_content == sync_rules) {
|
|
87
|
-
logger.info('Sync rules from configuration unchanged');
|
|
88
|
-
return { updated: false };
|
|
89
|
-
} else if (next == null && active?.sync_rules_content == sync_rules) {
|
|
90
|
-
logger.info('Sync rules from configuration unchanged');
|
|
91
|
-
return { updated: false };
|
|
92
|
-
} else {
|
|
93
|
-
logger.info('Sync rules updated from configuration');
|
|
94
|
-
const persisted_sync_rules = await this.updateSyncRules({
|
|
95
|
-
content: sync_rules,
|
|
96
|
-
lock: options?.lock
|
|
97
|
-
});
|
|
98
|
-
return { updated: true, persisted_sync_rules, lock: persisted_sync_rules.current_lock ?? undefined };
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async slotRemoved(slot_name: string) {
|
|
103
|
-
const next = await this.getNextSyncRulesContent();
|
|
104
|
-
const active = await this.getActiveSyncRulesContent();
|
|
105
|
-
|
|
106
|
-
// In both the below cases, we create a new sync rules instance.
|
|
107
|
-
// The current one will continue erroring until the next one has finished processing.
|
|
108
|
-
// TODO: Update
|
|
109
|
-
if (next != null && next.slot_name == slot_name) {
|
|
110
|
-
// We need to redo the "next" sync rules
|
|
111
|
-
await this.updateSyncRules({
|
|
112
|
-
content: next.sync_rules_content
|
|
113
|
-
});
|
|
114
|
-
// Pro-actively stop replicating
|
|
115
|
-
await this.db.sync_rules.updateOne(
|
|
116
|
-
{
|
|
117
|
-
_id: next.id,
|
|
118
|
-
state: storage.SyncRuleState.PROCESSING
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
$set: {
|
|
122
|
-
state: storage.SyncRuleState.STOP
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
);
|
|
126
|
-
} else if (next == null && active?.slot_name == slot_name) {
|
|
127
|
-
// Slot removed for "active" sync rules, while there is no "next" one.
|
|
128
|
-
await this.updateSyncRules({
|
|
129
|
-
content: active.sync_rules_content
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// Pro-actively stop replicating
|
|
133
|
-
await this.db.sync_rules.updateOne(
|
|
134
|
-
{
|
|
135
|
-
_id: active.id,
|
|
136
|
-
state: storage.SyncRuleState.ACTIVE
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
$set: {
|
|
140
|
-
state: storage.SyncRuleState.STOP
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async updateSyncRules(options: storage.UpdateSyncRulesOptions): Promise<MongoPersistedSyncRulesContent> {
|
|
148
|
-
// Parse and validate before applying any changes
|
|
149
|
-
const parsed = SqlSyncRules.fromYaml(options.content, {
|
|
150
|
-
// No schema-based validation at this point
|
|
151
|
-
schema: undefined,
|
|
152
|
-
defaultSchema: 'not_applicable', // Not needed for validation
|
|
153
|
-
throwOnError: true
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
let rules: MongoPersistedSyncRulesContent | undefined = undefined;
|
|
157
|
-
|
|
158
|
-
await this.session.withTransaction(async () => {
|
|
159
|
-
// Only have a single set of sync rules with PROCESSING.
|
|
160
|
-
await this.db.sync_rules.updateMany(
|
|
161
|
-
{
|
|
162
|
-
state: storage.SyncRuleState.PROCESSING
|
|
163
|
-
},
|
|
164
|
-
{ $set: { state: storage.SyncRuleState.STOP } }
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const id_doc = await this.db.op_id_sequence.findOneAndUpdate(
|
|
168
|
-
{
|
|
169
|
-
_id: 'sync_rules'
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
$inc: {
|
|
173
|
-
op_id: 1n
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
upsert: true,
|
|
178
|
-
returnDocument: 'after'
|
|
179
|
-
}
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
const id = Number(id_doc!.op_id);
|
|
183
|
-
const slot_name = generateSlotName(this.slot_name_prefix, id);
|
|
184
|
-
|
|
185
|
-
const doc: SyncRuleDocument = {
|
|
186
|
-
_id: id,
|
|
187
|
-
content: options.content,
|
|
188
|
-
last_checkpoint: null,
|
|
189
|
-
last_checkpoint_lsn: null,
|
|
190
|
-
no_checkpoint_before: null,
|
|
191
|
-
keepalive_op: null,
|
|
192
|
-
snapshot_done: false,
|
|
193
|
-
state: storage.SyncRuleState.PROCESSING,
|
|
194
|
-
slot_name: slot_name,
|
|
195
|
-
last_checkpoint_ts: null,
|
|
196
|
-
last_fatal_error: null,
|
|
197
|
-
last_keepalive_ts: null
|
|
198
|
-
};
|
|
199
|
-
await this.db.sync_rules.insertOne(doc);
|
|
200
|
-
rules = new MongoPersistedSyncRulesContent(this.db, doc);
|
|
201
|
-
if (options.lock) {
|
|
202
|
-
const lock = await rules.lock();
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
return rules!;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async getActiveSyncRulesContent(): Promise<MongoPersistedSyncRulesContent | null> {
|
|
210
|
-
const doc = await this.db.sync_rules.findOne(
|
|
211
|
-
{
|
|
212
|
-
state: storage.SyncRuleState.ACTIVE
|
|
213
|
-
},
|
|
214
|
-
{ sort: { _id: -1 }, limit: 1 }
|
|
215
|
-
);
|
|
216
|
-
if (doc == null) {
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
return new MongoPersistedSyncRulesContent(this.db, doc);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async getActiveSyncRules(options: storage.ParseSyncRulesOptions): Promise<storage.PersistedSyncRules | null> {
|
|
224
|
-
const content = await this.getActiveSyncRulesContent();
|
|
225
|
-
return content?.parsed(options) ?? null;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async getNextSyncRulesContent(): Promise<MongoPersistedSyncRulesContent | null> {
|
|
229
|
-
const doc = await this.db.sync_rules.findOne(
|
|
230
|
-
{
|
|
231
|
-
state: storage.SyncRuleState.PROCESSING
|
|
232
|
-
},
|
|
233
|
-
{ sort: { _id: -1 }, limit: 1 }
|
|
234
|
-
);
|
|
235
|
-
if (doc == null) {
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return new MongoPersistedSyncRulesContent(this.db, doc);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async getNextSyncRules(options: storage.ParseSyncRulesOptions): Promise<storage.PersistedSyncRules | null> {
|
|
243
|
-
const content = await this.getNextSyncRulesContent();
|
|
244
|
-
return content?.parsed(options) ?? null;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
async getReplicatingSyncRules(): Promise<storage.PersistedSyncRulesContent[]> {
|
|
248
|
-
const docs = await this.db.sync_rules
|
|
249
|
-
.find({
|
|
250
|
-
$or: [{ state: storage.SyncRuleState.ACTIVE }, { state: storage.SyncRuleState.PROCESSING }]
|
|
251
|
-
})
|
|
252
|
-
.toArray();
|
|
253
|
-
|
|
254
|
-
return docs.map((doc) => {
|
|
255
|
-
return new MongoPersistedSyncRulesContent(this.db, doc);
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
async getStoppedSyncRules(): Promise<storage.PersistedSyncRulesContent[]> {
|
|
260
|
-
const docs = await this.db.sync_rules
|
|
261
|
-
.find({
|
|
262
|
-
state: storage.SyncRuleState.STOP
|
|
263
|
-
})
|
|
264
|
-
.toArray();
|
|
265
|
-
|
|
266
|
-
return docs.map((doc) => {
|
|
267
|
-
return new MongoPersistedSyncRulesContent(this.db, doc);
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
async getActiveCheckpoint(): Promise<storage.ActiveCheckpoint> {
|
|
272
|
-
const doc = await this.db.sync_rules.findOne(
|
|
273
|
-
{
|
|
274
|
-
state: storage.SyncRuleState.ACTIVE
|
|
275
|
-
},
|
|
276
|
-
{
|
|
277
|
-
sort: { _id: -1 },
|
|
278
|
-
limit: 1,
|
|
279
|
-
projection: { _id: 1, last_checkpoint: 1, last_checkpoint_lsn: 1 }
|
|
280
|
-
}
|
|
281
|
-
);
|
|
282
|
-
|
|
283
|
-
return this.makeActiveCheckpoint(doc);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
async getStorageMetrics(): Promise<storage.StorageMetrics> {
|
|
287
|
-
const ignoreNotExiting = (e: unknown) => {
|
|
288
|
-
if (e instanceof mongo.MongoServerError && e.codeName == 'NamespaceNotFound') {
|
|
289
|
-
// Collection doesn't exist - return 0
|
|
290
|
-
return [{ storageStats: { size: 0 } }];
|
|
291
|
-
} else {
|
|
292
|
-
return Promise.reject(e);
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
const active_sync_rules = await this.getActiveSyncRules({ defaultSchema: 'public' });
|
|
297
|
-
if (active_sync_rules == null) {
|
|
298
|
-
return {
|
|
299
|
-
operations_size_bytes: 0,
|
|
300
|
-
parameters_size_bytes: 0,
|
|
301
|
-
replication_size_bytes: 0
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
const operations_aggregate = await this.db.bucket_data
|
|
305
|
-
|
|
306
|
-
.aggregate([
|
|
307
|
-
{
|
|
308
|
-
$collStats: {
|
|
309
|
-
storageStats: {}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
])
|
|
313
|
-
.toArray()
|
|
314
|
-
.catch(ignoreNotExiting);
|
|
315
|
-
|
|
316
|
-
const parameters_aggregate = await this.db.bucket_parameters
|
|
317
|
-
.aggregate([
|
|
318
|
-
{
|
|
319
|
-
$collStats: {
|
|
320
|
-
storageStats: {}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
])
|
|
324
|
-
.toArray()
|
|
325
|
-
.catch(ignoreNotExiting);
|
|
326
|
-
|
|
327
|
-
const replication_aggregate = await this.db.current_data
|
|
328
|
-
.aggregate([
|
|
329
|
-
{
|
|
330
|
-
$collStats: {
|
|
331
|
-
storageStats: {}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
])
|
|
335
|
-
.toArray()
|
|
336
|
-
.catch(ignoreNotExiting);
|
|
337
|
-
|
|
338
|
-
return {
|
|
339
|
-
operations_size_bytes: Number(operations_aggregate[0].storageStats.size),
|
|
340
|
-
parameters_size_bytes: Number(parameters_aggregate[0].storageStats.size),
|
|
341
|
-
replication_size_bytes: Number(replication_aggregate[0].storageStats.size)
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
async getPowerSyncInstanceId(): Promise<string> {
|
|
346
|
-
let instance = await this.db.instance.findOne({
|
|
347
|
-
_id: { $exists: true }
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
if (!instance) {
|
|
351
|
-
const manager = new MongoLockManager({
|
|
352
|
-
collection: this.db.locks,
|
|
353
|
-
name: `instance-id-insertion-lock`
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
await manager.lock(async () => {
|
|
357
|
-
await this.db.instance.insertOne({
|
|
358
|
-
_id: uuid()
|
|
359
|
-
});
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
instance = await this.db.instance.findOne({
|
|
363
|
-
_id: { $exists: true }
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
return instance!._id;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
private makeActiveCheckpoint(doc: SyncRuleDocument | null) {
|
|
371
|
-
return {
|
|
372
|
-
checkpoint: utils.timestampToOpId(doc?.last_checkpoint ?? 0n),
|
|
373
|
-
lsn: doc?.last_checkpoint_lsn ?? null,
|
|
374
|
-
hasSyncRules() {
|
|
375
|
-
return doc != null;
|
|
376
|
-
},
|
|
377
|
-
getBucketStorage: async () => {
|
|
378
|
-
if (doc == null) {
|
|
379
|
-
return null;
|
|
380
|
-
}
|
|
381
|
-
return (await this.storageCache.fetch(doc._id)) ?? null;
|
|
382
|
-
}
|
|
383
|
-
} satisfies storage.ActiveCheckpoint;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Instance-wide watch on the latest available checkpoint (op_id + lsn).
|
|
388
|
-
*/
|
|
389
|
-
private async *watchActiveCheckpoint(signal: AbortSignal): AsyncIterable<storage.ActiveCheckpoint> {
|
|
390
|
-
const pipeline: mongo.Document[] = [
|
|
391
|
-
{
|
|
392
|
-
$match: {
|
|
393
|
-
'fullDocument.state': 'ACTIVE',
|
|
394
|
-
operationType: { $in: ['insert', 'update'] }
|
|
395
|
-
}
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
$project: {
|
|
399
|
-
operationType: 1,
|
|
400
|
-
'fullDocument._id': 1,
|
|
401
|
-
'fullDocument.last_checkpoint': 1,
|
|
402
|
-
'fullDocument.last_checkpoint_lsn': 1
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
];
|
|
406
|
-
|
|
407
|
-
// Use this form instead of (doc: SyncRuleDocument | null = null),
|
|
408
|
-
// otherwise we get weird "doc: never" issues.
|
|
409
|
-
let doc = null as SyncRuleDocument | null;
|
|
410
|
-
let clusterTime = null as mongo.Timestamp | null;
|
|
411
|
-
|
|
412
|
-
await this.client.withSession(async (session) => {
|
|
413
|
-
doc = await this.db.sync_rules.findOne(
|
|
414
|
-
{
|
|
415
|
-
state: storage.SyncRuleState.ACTIVE
|
|
416
|
-
},
|
|
417
|
-
{
|
|
418
|
-
session,
|
|
419
|
-
sort: { _id: -1 },
|
|
420
|
-
limit: 1,
|
|
421
|
-
projection: {
|
|
422
|
-
_id: 1,
|
|
423
|
-
last_checkpoint: 1,
|
|
424
|
-
last_checkpoint_lsn: 1
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
);
|
|
428
|
-
const time = session.clusterTime?.clusterTime ?? null;
|
|
429
|
-
clusterTime = time;
|
|
430
|
-
});
|
|
431
|
-
if (clusterTime == null) {
|
|
432
|
-
throw new Error('Could not get clusterTime');
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
if (signal.aborted) {
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
if (doc) {
|
|
440
|
-
yield this.makeActiveCheckpoint(doc);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
const stream = this.db.sync_rules.watch(pipeline, {
|
|
444
|
-
fullDocument: 'updateLookup',
|
|
445
|
-
// Start at the cluster time where we got the initial doc, to make sure
|
|
446
|
-
// we don't skip any updates.
|
|
447
|
-
// This may result in the first operation being a duplicate, but we filter
|
|
448
|
-
// it out anyway.
|
|
449
|
-
startAtOperationTime: clusterTime
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
signal.addEventListener(
|
|
453
|
-
'abort',
|
|
454
|
-
() => {
|
|
455
|
-
stream.close();
|
|
456
|
-
},
|
|
457
|
-
{ once: true }
|
|
458
|
-
);
|
|
459
|
-
|
|
460
|
-
let lastOp: storage.ActiveCheckpoint | null = null;
|
|
461
|
-
|
|
462
|
-
for await (const update of stream.stream()) {
|
|
463
|
-
if (signal.aborted) {
|
|
464
|
-
break;
|
|
465
|
-
}
|
|
466
|
-
if (update.operationType != 'insert' && update.operationType != 'update') {
|
|
467
|
-
continue;
|
|
468
|
-
}
|
|
469
|
-
const doc = update.fullDocument!;
|
|
470
|
-
if (doc == null) {
|
|
471
|
-
continue;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
const op = this.makeActiveCheckpoint(doc);
|
|
475
|
-
// Check for LSN / checkpoint changes - ignore other metadata changes
|
|
476
|
-
if (lastOp == null || op.lsn != lastOp.lsn || op.checkpoint != lastOp.checkpoint) {
|
|
477
|
-
lastOp = op;
|
|
478
|
-
yield op;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// Nothing is done here until a subscriber starts to iterate
|
|
484
|
-
private readonly sharedIter = new sync.BroadcastIterable((signal) => {
|
|
485
|
-
return this.watchActiveCheckpoint(signal);
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
/**
|
|
489
|
-
* User-specific watch on the latest checkpoint and/or write checkpoint.
|
|
490
|
-
*/
|
|
491
|
-
async *watchWriteCheckpoint(user_id: string, signal: AbortSignal): AsyncIterable<storage.WriteCheckpoint> {
|
|
492
|
-
let lastCheckpoint: utils.OpId | null = null;
|
|
493
|
-
let lastWriteCheckpoint: bigint | null = null;
|
|
494
|
-
|
|
495
|
-
const iter = wrapWithAbort(this.sharedIter, signal);
|
|
496
|
-
for await (const cp of iter) {
|
|
497
|
-
const { checkpoint, lsn } = cp;
|
|
498
|
-
|
|
499
|
-
// lsn changes are not important by itself.
|
|
500
|
-
// What is important is:
|
|
501
|
-
// 1. checkpoint (op_id) changes.
|
|
502
|
-
// 2. write checkpoint changes for the specific user
|
|
503
|
-
const bucketStorage = await cp.getBucketStorage();
|
|
504
|
-
if (!bucketStorage) {
|
|
505
|
-
continue;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
const lsnFilters: Record<string, string> = lsn ? { 1: lsn } : {};
|
|
509
|
-
|
|
510
|
-
const currentWriteCheckpoint = await bucketStorage.lastWriteCheckpoint({
|
|
511
|
-
user_id,
|
|
512
|
-
heads: {
|
|
513
|
-
...lsnFilters
|
|
514
|
-
}
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
if (currentWriteCheckpoint == lastWriteCheckpoint && checkpoint == lastCheckpoint) {
|
|
518
|
-
// No change - wait for next one
|
|
519
|
-
// In some cases, many LSNs may be produced in a short time.
|
|
520
|
-
// Add a delay to throttle the write checkpoint lookup a bit.
|
|
521
|
-
await timers.setTimeout(20 + 10 * Math.random());
|
|
522
|
-
continue;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
lastWriteCheckpoint = currentWriteCheckpoint;
|
|
526
|
-
lastCheckpoint = checkpoint;
|
|
527
|
-
|
|
528
|
-
yield { base: cp, writeCheckpoint: currentWriteCheckpoint };
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
}
|