@powersync/service-module-mongodb 0.0.0-dev-20250102111825 → 0.0.0-dev-20250108084515

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/CHANGELOG.md +8 -5
  2. package/dist/api/MongoRouteAPIAdapter.d.ts +1 -1
  3. package/dist/api/MongoRouteAPIAdapter.js +4 -3
  4. package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
  5. package/dist/index.d.ts +3 -2
  6. package/dist/index.js +3 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/module/MongoModule.d.ts +0 -1
  9. package/dist/module/MongoModule.js +2 -10
  10. package/dist/module/MongoModule.js.map +1 -1
  11. package/dist/replication/MongoRelation.js +15 -5
  12. package/dist/replication/MongoRelation.js.map +1 -1
  13. package/dist/types/types.d.ts +16 -22
  14. package/dist/types/types.js +4 -24
  15. package/dist/types/types.js.map +1 -1
  16. package/package.json +9 -10
  17. package/src/api/MongoRouteAPIAdapter.ts +4 -4
  18. package/src/index.ts +3 -4
  19. package/src/module/MongoModule.ts +2 -14
  20. package/src/replication/MongoRelation.ts +13 -5
  21. package/src/types/types.ts +8 -34
  22. package/test/src/change_stream.test.ts +2 -4
  23. package/test/src/env.ts +1 -1
  24. package/test/src/mongo_test.test.ts +69 -10
  25. package/test/src/setup.ts +4 -1
  26. package/test/src/slow_tests.test.ts +5 -12
  27. package/test/src/util.ts +5 -38
  28. package/test/tsconfig.json +1 -2
  29. package/tsconfig.json +6 -0
  30. package/tsconfig.tsbuildinfo +1 -1
  31. package/dist/db/db-index.d.ts +0 -1
  32. package/dist/db/db-index.js +0 -2
  33. package/dist/db/db-index.js.map +0 -1
  34. package/dist/db/mongo.d.ts +0 -35
  35. package/dist/db/mongo.js +0 -73
  36. package/dist/db/mongo.js.map +0 -1
  37. package/dist/locks/MongoLockManager.d.ts +0 -25
  38. package/dist/locks/MongoLockManager.js +0 -79
  39. package/dist/locks/MongoLockManager.js.map +0 -1
  40. package/dist/migrations/MongoMigrationAgent.d.ts +0 -12
  41. package/dist/migrations/MongoMigrationAgent.js +0 -25
  42. package/dist/migrations/MongoMigrationAgent.js.map +0 -1
  43. package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
  44. package/dist/migrations/db/migrations/1684951997326-init.js +0 -30
  45. package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
  46. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
  47. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
  48. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
  49. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
  50. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -54
  51. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
  52. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
  53. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -26
  54. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
  55. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
  56. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -28
  57. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
  58. package/dist/migrations/mongo-migration-store.d.ts +0 -7
  59. package/dist/migrations/mongo-migration-store.js +0 -49
  60. package/dist/migrations/mongo-migration-store.js.map +0 -1
  61. package/dist/storage/MongoBucketStorage.d.ts +0 -48
  62. package/dist/storage/MongoBucketStorage.js +0 -426
  63. package/dist/storage/MongoBucketStorage.js.map +0 -1
  64. package/dist/storage/implementation/MongoBucketBatch.d.ts +0 -72
  65. package/dist/storage/implementation/MongoBucketBatch.js +0 -681
  66. package/dist/storage/implementation/MongoBucketBatch.js.map +0 -1
  67. package/dist/storage/implementation/MongoCompactor.d.ts +0 -40
  68. package/dist/storage/implementation/MongoCompactor.js +0 -300
  69. package/dist/storage/implementation/MongoCompactor.js.map +0 -1
  70. package/dist/storage/implementation/MongoIdSequence.d.ts +0 -12
  71. package/dist/storage/implementation/MongoIdSequence.js +0 -21
  72. package/dist/storage/implementation/MongoIdSequence.js.map +0 -1
  73. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -9
  74. package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -9
  75. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
  76. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -20
  77. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -26
  78. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
  79. package/dist/storage/implementation/MongoStorageProvider.d.ts +0 -6
  80. package/dist/storage/implementation/MongoStorageProvider.js +0 -34
  81. package/dist/storage/implementation/MongoStorageProvider.js.map +0 -1
  82. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +0 -36
  83. package/dist/storage/implementation/MongoSyncBucketStorage.js +0 -529
  84. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +0 -1
  85. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +0 -16
  86. package/dist/storage/implementation/MongoSyncRulesLock.js +0 -65
  87. package/dist/storage/implementation/MongoSyncRulesLock.js.map +0 -1
  88. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +0 -20
  89. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +0 -104
  90. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +0 -1
  91. package/dist/storage/implementation/OperationBatch.d.ts +0 -34
  92. package/dist/storage/implementation/OperationBatch.js +0 -119
  93. package/dist/storage/implementation/OperationBatch.js.map +0 -1
  94. package/dist/storage/implementation/PersistedBatch.d.ts +0 -46
  95. package/dist/storage/implementation/PersistedBatch.js +0 -223
  96. package/dist/storage/implementation/PersistedBatch.js.map +0 -1
  97. package/dist/storage/implementation/config.d.ts +0 -19
  98. package/dist/storage/implementation/config.js +0 -26
  99. package/dist/storage/implementation/config.js.map +0 -1
  100. package/dist/storage/implementation/db.d.ts +0 -36
  101. package/dist/storage/implementation/db.js +0 -47
  102. package/dist/storage/implementation/db.js.map +0 -1
  103. package/dist/storage/implementation/models.d.ts +0 -139
  104. package/dist/storage/implementation/models.js +0 -2
  105. package/dist/storage/implementation/models.js.map +0 -1
  106. package/dist/storage/implementation/util.d.ts +0 -41
  107. package/dist/storage/implementation/util.js +0 -141
  108. package/dist/storage/implementation/util.js.map +0 -1
  109. package/dist/storage/storage-index.d.ts +0 -14
  110. package/dist/storage/storage-index.js +0 -15
  111. package/dist/storage/storage-index.js.map +0 -1
  112. package/src/db/db-index.ts +0 -1
  113. package/src/db/mongo.ts +0 -81
  114. package/src/locks/MongoLockManager.ts +0 -123
  115. package/src/migrations/MongoMigrationAgent.ts +0 -39
  116. package/src/migrations/db/migrations/1684951997326-init.ts +0 -39
  117. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
  118. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -105
  119. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -38
  120. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -40
  121. package/src/migrations/mongo-migration-store.ts +0 -62
  122. package/src/storage/MongoBucketStorage.ts +0 -531
  123. package/src/storage/implementation/MongoBucketBatch.ts +0 -896
  124. package/src/storage/implementation/MongoCompactor.ts +0 -381
  125. package/src/storage/implementation/MongoIdSequence.ts +0 -24
  126. package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -16
  127. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -49
  128. package/src/storage/implementation/MongoStorageProvider.ts +0 -42
  129. package/src/storage/implementation/MongoSyncBucketStorage.ts +0 -612
  130. package/src/storage/implementation/MongoSyncRulesLock.ts +0 -88
  131. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +0 -146
  132. package/src/storage/implementation/OperationBatch.ts +0 -129
  133. package/src/storage/implementation/PersistedBatch.ts +0 -283
  134. package/src/storage/implementation/config.ts +0 -40
  135. package/src/storage/implementation/db.ts +0 -87
  136. package/src/storage/implementation/models.ts +0 -161
  137. package/src/storage/implementation/util.ts +0 -154
  138. package/src/storage/storage-index.ts +0 -14
  139. package/test/src/__snapshots__/storage_sync.test.ts.snap +0 -332
  140. package/test/src/storage.test.ts +0 -7
  141. package/test/src/storage_compacting.test.ts +0 -6
  142. package/test/src/storage_sync.test.ts +0 -113
@@ -1,896 +0,0 @@
1
- import { SqlEventDescriptor, SqliteRow, SqlSyncRules } from '@powersync/service-sync-rules';
2
- import * as bson from 'bson';
3
- import * as mongo from 'mongodb';
4
-
5
- import { container, DisposableObserver, errors, logger } from '@powersync/lib-services-framework';
6
- import { SaveOperationTag, storage, utils } from '@powersync/service-core';
7
- import * as timers from 'node:timers/promises';
8
- import { PowerSyncMongo } from './db.js';
9
- import { CurrentBucket, CurrentDataDocument, SourceKey, SyncRuleDocument } from './models.js';
10
- import { MongoIdSequence } from './MongoIdSequence.js';
11
- import { batchCreateCustomWriteCheckpoints } from './MongoWriteCheckpointAPI.js';
12
- import { cacheKey, OperationBatch, RecordOperation } from './OperationBatch.js';
13
- import { PersistedBatch } from './PersistedBatch.js';
14
- import { idPrefixFilter } from './util.js';
15
-
16
- /**
17
- * 15MB
18
- */
19
- const MAX_ROW_SIZE = 15 * 1024 * 1024;
20
-
21
- // Currently, we can only have a single flush() at a time, since it locks the op_id sequence.
22
- // While the MongoDB transaction retry mechanism handles this okay, using an in-process Mutex
23
- // makes it more fair and has less overhead.
24
- //
25
- // In the future, we can investigate allowing multiple replication streams operating independently.
26
- const replicationMutex = new utils.Mutex();
27
-
28
- export interface MongoBucketBatchOptions {
29
- db: PowerSyncMongo;
30
- syncRules: SqlSyncRules;
31
- groupId: number;
32
- slotName: string;
33
- lastCheckpointLsn: string | null;
34
- keepaliveOp: string | null;
35
- noCheckpointBeforeLsn: string;
36
- storeCurrentData: boolean;
37
- /**
38
- * Set to true for initial replication.
39
- */
40
- skipExistingRows: boolean;
41
- }
42
-
43
- export class MongoBucketBatch
44
- extends DisposableObserver<storage.BucketBatchStorageListener>
45
- implements storage.BucketStorageBatch
46
- {
47
- private readonly client: mongo.MongoClient;
48
- public readonly db: PowerSyncMongo;
49
- public readonly session: mongo.ClientSession;
50
- private readonly sync_rules: SqlSyncRules;
51
-
52
- private readonly group_id: number;
53
-
54
- private readonly slot_name: string;
55
- private readonly storeCurrentData: boolean;
56
- private readonly skipExistingRows: boolean;
57
-
58
- private batch: OperationBatch | null = null;
59
- private write_checkpoint_batch: storage.CustomWriteCheckpointOptions[] = [];
60
-
61
- /**
62
- * Last LSN received associated with a checkpoint.
63
- *
64
- * This could be either:
65
- * 1. A commit LSN.
66
- * 2. A keepalive message LSN.
67
- */
68
- private last_checkpoint_lsn: string | null = null;
69
-
70
- private no_checkpoint_before_lsn: string;
71
-
72
- private persisted_op: bigint | null = null;
73
-
74
- /**
75
- * For tests only - not for persistence logic.
76
- */
77
- public last_flushed_op: bigint | null = null;
78
-
79
- constructor(options: MongoBucketBatchOptions) {
80
- super();
81
- this.client = options.db.client;
82
- this.db = options.db;
83
- this.group_id = options.groupId;
84
- this.last_checkpoint_lsn = options.lastCheckpointLsn;
85
- this.no_checkpoint_before_lsn = options.noCheckpointBeforeLsn;
86
- this.session = this.client.startSession();
87
- this.slot_name = options.slotName;
88
- this.sync_rules = options.syncRules;
89
- this.storeCurrentData = options.storeCurrentData;
90
- this.skipExistingRows = options.skipExistingRows;
91
- this.batch = new OperationBatch();
92
-
93
- if (options.keepaliveOp) {
94
- this.persisted_op = BigInt(options.keepaliveOp);
95
- }
96
- }
97
-
98
- addCustomWriteCheckpoint(checkpoint: storage.BatchedCustomWriteCheckpointOptions): void {
99
- this.write_checkpoint_batch.push({
100
- ...checkpoint,
101
- sync_rules_id: this.group_id
102
- });
103
- }
104
-
105
- get lastCheckpointLsn() {
106
- return this.last_checkpoint_lsn;
107
- }
108
-
109
- async flush(): Promise<storage.FlushedResult | null> {
110
- let result: storage.FlushedResult | null = null;
111
- // One flush may be split over multiple transactions.
112
- // Each flushInner() is one transaction.
113
- while (this.batch != null) {
114
- let r = await this.flushInner();
115
- if (r) {
116
- result = r;
117
- }
118
- }
119
- await batchCreateCustomWriteCheckpoints(this.db, this.write_checkpoint_batch);
120
- this.write_checkpoint_batch = [];
121
- return result;
122
- }
123
-
124
- private async flushInner(): Promise<storage.FlushedResult | null> {
125
- const batch = this.batch;
126
- if (batch == null) {
127
- return null;
128
- }
129
-
130
- let last_op: bigint | null = null;
131
- let resumeBatch: OperationBatch | null = null;
132
-
133
- await this.withReplicationTransaction(`Flushing ${batch.length} ops`, async (session, opSeq) => {
134
- resumeBatch = await this.replicateBatch(session, batch, opSeq);
135
-
136
- last_op = opSeq.last();
137
- });
138
-
139
- // null if done, set if we need another flush
140
- this.batch = resumeBatch;
141
-
142
- if (last_op == null) {
143
- throw new Error('Unexpected last_op == null');
144
- }
145
-
146
- this.persisted_op = last_op;
147
- this.last_flushed_op = last_op;
148
- return { flushed_op: String(last_op) };
149
- }
150
-
151
- private async replicateBatch(
152
- session: mongo.ClientSession,
153
- batch: OperationBatch,
154
- op_seq: MongoIdSequence
155
- ): Promise<OperationBatch | null> {
156
- let sizes: Map<string, number> | undefined = undefined;
157
- if (this.storeCurrentData && !this.skipExistingRows) {
158
- // We skip this step if we don't store current_data, since the sizes will
159
- // always be small in that case.
160
-
161
- // With skipExistingRows, we don't load the full documents into memory,
162
- // so we can also skip the size lookup step.
163
-
164
- // Find sizes of current_data documents, to assist in intelligent batching without
165
- // exceeding memory limits.
166
- //
167
- // A previous attempt tried to do batching by the results of the current_data query
168
- // (automatically limited to 48MB(?) per batch by MongoDB). The issue is that it changes
169
- // the order of processing, which then becomes really tricky to manage.
170
- // This now takes 2+ queries, but doesn't have any issues with order of operations.
171
- const sizeLookups: SourceKey[] = batch.batch.map((r) => {
172
- return { g: this.group_id, t: r.record.sourceTable.id, k: r.beforeId };
173
- });
174
-
175
- sizes = new Map<string, number>();
176
-
177
- const sizeCursor: mongo.AggregationCursor<{ _id: SourceKey; size: number }> = this.db.current_data.aggregate(
178
- [
179
- {
180
- $match: {
181
- _id: { $in: sizeLookups }
182
- }
183
- },
184
- {
185
- $project: {
186
- _id: 1,
187
- size: { $bsonSize: '$$ROOT' }
188
- }
189
- }
190
- ],
191
- { session }
192
- );
193
- for await (let doc of sizeCursor.stream()) {
194
- const key = cacheKey(doc._id.t, doc._id.k);
195
- sizes.set(key, doc.size);
196
- }
197
- }
198
-
199
- // If set, we need to start a new transaction with this batch.
200
- let resumeBatch: OperationBatch | null = null;
201
- let transactionSize = 0;
202
-
203
- // Now batch according to the sizes
204
- // This is a single batch if storeCurrentData == false
205
- for await (let b of batch.batched(sizes)) {
206
- if (resumeBatch) {
207
- for (let op of b) {
208
- resumeBatch.push(op);
209
- }
210
- continue;
211
- }
212
- const lookups: SourceKey[] = b.map((r) => {
213
- return { g: this.group_id, t: r.record.sourceTable.id, k: r.beforeId };
214
- });
215
- let current_data_lookup = new Map<string, CurrentDataDocument>();
216
- // With skipExistingRows, we only need to know whether or not the row exists.
217
- const projection = this.skipExistingRows ? { _id: 1 } : undefined;
218
- const cursor = this.db.current_data.find(
219
- {
220
- _id: { $in: lookups }
221
- },
222
- { session, projection }
223
- );
224
- for await (let doc of cursor.stream()) {
225
- current_data_lookup.set(cacheKey(doc._id.t, doc._id.k), doc);
226
- }
227
-
228
- let persistedBatch: PersistedBatch | null = new PersistedBatch(this.group_id, transactionSize);
229
-
230
- for (let op of b) {
231
- if (resumeBatch) {
232
- resumeBatch.push(op);
233
- continue;
234
- }
235
- const currentData = current_data_lookup.get(op.internalBeforeKey) ?? null;
236
- if (currentData != null) {
237
- // If it will be used again later, it will be set again using nextData below
238
- current_data_lookup.delete(op.internalBeforeKey);
239
- }
240
- const nextData = this.saveOperation(persistedBatch!, op, currentData, op_seq);
241
- if (nextData != null) {
242
- // Update our current_data and size cache
243
- current_data_lookup.set(op.internalAfterKey!, nextData);
244
- sizes?.set(op.internalAfterKey!, nextData.data.length());
245
- }
246
-
247
- if (persistedBatch!.shouldFlushTransaction()) {
248
- // Transaction is getting big.
249
- // Flush, and resume in a new transaction.
250
- await persistedBatch!.flush(this.db, this.session);
251
- persistedBatch = null;
252
- // Computing our current progress is a little tricky here, since
253
- // we're stopping in the middle of a batch.
254
- // We create a new batch, and push any remaining operations to it.
255
- resumeBatch = new OperationBatch();
256
- }
257
- }
258
-
259
- if (persistedBatch) {
260
- transactionSize = persistedBatch.currentSize;
261
- await persistedBatch.flush(this.db, this.session);
262
- }
263
- }
264
-
265
- return resumeBatch;
266
- }
267
-
268
- private saveOperation(
269
- batch: PersistedBatch,
270
- operation: RecordOperation,
271
- current_data: CurrentDataDocument | null,
272
- opSeq: MongoIdSequence
273
- ) {
274
- const record = operation.record;
275
- const beforeId = operation.beforeId;
276
- const afterId = operation.afterId;
277
- let after = record.after;
278
- const sourceTable = record.sourceTable;
279
-
280
- let existing_buckets: CurrentBucket[] = [];
281
- let new_buckets: CurrentBucket[] = [];
282
- let existing_lookups: bson.Binary[] = [];
283
- let new_lookups: bson.Binary[] = [];
284
-
285
- const before_key: SourceKey = { g: this.group_id, t: record.sourceTable.id, k: beforeId };
286
-
287
- if (this.skipExistingRows) {
288
- if (record.tag == SaveOperationTag.INSERT) {
289
- if (current_data != null) {
290
- // Initial replication, and we already have the record.
291
- // This may be a different version of the record, but streaming replication
292
- // will take care of that.
293
- // Skip the insert here.
294
- return null;
295
- }
296
- } else {
297
- throw new Error(`${record.tag} not supported with skipExistingRows: true`);
298
- }
299
- }
300
-
301
- if (record.tag == SaveOperationTag.UPDATE) {
302
- const result = current_data;
303
- if (result == null) {
304
- // Not an error if we re-apply a transaction
305
- existing_buckets = [];
306
- existing_lookups = [];
307
- // Log to help with debugging if there was a consistency issue
308
- if (this.storeCurrentData) {
309
- logger.warn(
310
- `Cannot find previous record for update on ${record.sourceTable.qualifiedName}: ${beforeId} / ${record.before?.id}`
311
- );
312
- }
313
- } else {
314
- existing_buckets = result.buckets;
315
- existing_lookups = result.lookups;
316
- if (this.storeCurrentData) {
317
- const data = bson.deserialize(
318
- (result.data as mongo.Binary).buffer,
319
- storage.BSON_DESERIALIZE_OPTIONS
320
- ) as SqliteRow;
321
- after = storage.mergeToast(after!, data);
322
- }
323
- }
324
- } else if (record.tag == SaveOperationTag.DELETE) {
325
- const result = current_data;
326
- if (result == null) {
327
- // Not an error if we re-apply a transaction
328
- existing_buckets = [];
329
- existing_lookups = [];
330
- // Log to help with debugging if there was a consistency issue
331
- if (this.storeCurrentData) {
332
- logger.warn(
333
- `Cannot find previous record for delete on ${record.sourceTable.qualifiedName}: ${beforeId} / ${record.before?.id}`
334
- );
335
- }
336
- } else {
337
- existing_buckets = result.buckets;
338
- existing_lookups = result.lookups;
339
- }
340
- }
341
-
342
- let afterData: bson.Binary | undefined;
343
- if (afterId != null && !this.storeCurrentData) {
344
- afterData = new bson.Binary(bson.serialize({}));
345
- } else if (afterId != null) {
346
- try {
347
- // This will fail immediately if the record is > 16MB.
348
- afterData = new bson.Binary(bson.serialize(after!));
349
- // We additionally make sure it's <= 15MB - we need some margin for metadata.
350
- if (afterData.length() > MAX_ROW_SIZE) {
351
- throw new Error(`Row too large: ${afterData.length()}`);
352
- }
353
- } catch (e) {
354
- // Replace with empty values, equivalent to TOAST values
355
- after = Object.fromEntries(
356
- Object.entries(after!).map(([key, value]) => {
357
- return [key, undefined];
358
- })
359
- );
360
- afterData = new bson.Binary(bson.serialize(after!));
361
-
362
- container.reporter.captureMessage(
363
- `Data too big on ${record.sourceTable.qualifiedName}.${record.after?.id}: ${e.message}`,
364
- {
365
- level: errors.ErrorSeverity.WARNING,
366
- metadata: {
367
- replication_slot: this.slot_name,
368
- table: record.sourceTable.qualifiedName
369
- }
370
- }
371
- );
372
- }
373
- }
374
-
375
- // 2. Save bucket data
376
- if (beforeId != null && (afterId == null || !storage.replicaIdEquals(beforeId, afterId))) {
377
- // Source ID updated
378
- if (sourceTable.syncData) {
379
- // Delete old record
380
- batch.saveBucketData({
381
- op_seq: opSeq,
382
- sourceKey: beforeId,
383
- table: sourceTable,
384
- before_buckets: existing_buckets,
385
- evaluated: []
386
- });
387
- // Clear this, so we don't also try to REMOVE for the new id
388
- existing_buckets = [];
389
- }
390
-
391
- if (sourceTable.syncParameters) {
392
- // Delete old parameters
393
- batch.saveParameterData({
394
- op_seq: opSeq,
395
- sourceKey: beforeId,
396
- sourceTable,
397
- evaluated: [],
398
- existing_lookups
399
- });
400
- existing_lookups = [];
401
- }
402
- }
403
-
404
- // If we re-apply a transaction, we can end up with a partial row.
405
- //
406
- // We may end up with toasted values, which means the record is not quite valid.
407
- // However, it will be valid by the end of the transaction.
408
- //
409
- // In this case, we don't save the op, but we do save the current data.
410
- if (afterId && after && utils.isCompleteRow(after)) {
411
- // Insert or update
412
- if (sourceTable.syncData) {
413
- const { results: evaluated, errors: syncErrors } = this.sync_rules.evaluateRowWithErrors({
414
- record: after,
415
- sourceTable
416
- });
417
-
418
- for (let error of syncErrors) {
419
- container.reporter.captureMessage(
420
- `Failed to evaluate data query on ${record.sourceTable.qualifiedName}.${record.after?.id}: ${error.error}`,
421
- {
422
- level: errors.ErrorSeverity.WARNING,
423
- metadata: {
424
- replication_slot: this.slot_name,
425
- table: record.sourceTable.qualifiedName
426
- }
427
- }
428
- );
429
- logger.error(
430
- `Failed to evaluate data query on ${record.sourceTable.qualifiedName}.${record.after?.id}: ${error.error}`
431
- );
432
- }
433
-
434
- // Save new one
435
- batch.saveBucketData({
436
- op_seq: opSeq,
437
- sourceKey: afterId,
438
- evaluated,
439
- table: sourceTable,
440
- before_buckets: existing_buckets
441
- });
442
- new_buckets = evaluated.map((e) => {
443
- return {
444
- bucket: e.bucket,
445
- table: e.table,
446
- id: e.id
447
- };
448
- });
449
- }
450
-
451
- if (sourceTable.syncParameters) {
452
- // Parameters
453
- const { results: paramEvaluated, errors: paramErrors } = this.sync_rules.evaluateParameterRowWithErrors(
454
- sourceTable,
455
- after
456
- );
457
-
458
- for (let error of paramErrors) {
459
- container.reporter.captureMessage(
460
- `Failed to evaluate parameter query on ${record.sourceTable.qualifiedName}.${record.after?.id}: ${error.error}`,
461
- {
462
- level: errors.ErrorSeverity.WARNING,
463
- metadata: {
464
- replication_slot: this.slot_name,
465
- table: record.sourceTable.qualifiedName
466
- }
467
- }
468
- );
469
- logger.error(
470
- `Failed to evaluate parameter query on ${record.sourceTable.qualifiedName}.${after.id}: ${error.error}`
471
- );
472
- }
473
-
474
- batch.saveParameterData({
475
- op_seq: opSeq,
476
- sourceKey: afterId,
477
- sourceTable,
478
- evaluated: paramEvaluated,
479
- existing_lookups
480
- });
481
- new_lookups = paramEvaluated.map((p) => {
482
- return storage.serializeLookup(p.lookup);
483
- });
484
- }
485
- }
486
-
487
- let result: CurrentDataDocument | null = null;
488
-
489
- // 5. TOAST: Update current data and bucket list.
490
- if (afterId) {
491
- // Insert or update
492
- const after_key: SourceKey = { g: this.group_id, t: sourceTable.id, k: afterId };
493
- batch.upsertCurrentData(after_key, {
494
- data: afterData,
495
- buckets: new_buckets,
496
- lookups: new_lookups
497
- });
498
- result = {
499
- _id: after_key,
500
- data: afterData!,
501
- buckets: new_buckets,
502
- lookups: new_lookups
503
- };
504
- }
505
-
506
- if (afterId == null || !storage.replicaIdEquals(beforeId, afterId)) {
507
- // Either a delete (afterId == null), or replaced the old replication id
508
- batch.deleteCurrentData(before_key);
509
- }
510
- return result;
511
- }
512
-
513
- private async withTransaction(cb: () => Promise<void>) {
514
- await replicationMutex.exclusiveLock(async () => {
515
- await this.session.withTransaction(
516
- async () => {
517
- try {
518
- await cb();
519
- } catch (e: unknown) {
520
- if (e instanceof mongo.MongoError && e.hasErrorLabel('TransientTransactionError')) {
521
- // Likely write conflict caused by concurrent write stream replicating
522
- } else {
523
- logger.warn('Transaction error', e as Error);
524
- }
525
- await timers.setTimeout(Math.random() * 50);
526
- throw e;
527
- }
528
- },
529
- { maxCommitTimeMS: 10000 }
530
- );
531
- });
532
- }
533
-
534
- private async withReplicationTransaction(
535
- description: string,
536
- callback: (session: mongo.ClientSession, opSeq: MongoIdSequence) => Promise<void>
537
- ): Promise<void> {
538
- let flushTry = 0;
539
-
540
- const start = Date.now();
541
- const lastTry = start + 90000;
542
-
543
- const session = this.session;
544
-
545
- await this.withTransaction(async () => {
546
- flushTry += 1;
547
- if (flushTry % 10 == 0) {
548
- logger.info(`${this.slot_name} ${description} - try ${flushTry}`);
549
- }
550
- if (flushTry > 20 && Date.now() > lastTry) {
551
- throw new Error('Max transaction tries exceeded');
552
- }
553
-
554
- const next_op_id_doc = await this.db.op_id_sequence.findOneAndUpdate(
555
- {
556
- _id: 'main'
557
- },
558
- {
559
- $setOnInsert: { op_id: 0n },
560
- $set: {
561
- // Force update to ensure we get a mongo lock
562
- ts: Date.now()
563
- }
564
- },
565
- {
566
- upsert: true,
567
- returnDocument: 'after',
568
- session
569
- }
570
- );
571
- const opSeq = new MongoIdSequence(next_op_id_doc?.op_id ?? 0n);
572
-
573
- await callback(session, opSeq);
574
-
575
- await this.db.op_id_sequence.updateOne(
576
- {
577
- _id: 'main'
578
- },
579
- {
580
- $set: {
581
- op_id: opSeq.last()
582
- }
583
- },
584
- {
585
- session
586
- }
587
- );
588
-
589
- await this.db.sync_rules.updateOne(
590
- {
591
- _id: this.group_id
592
- },
593
- {
594
- $set: {
595
- last_keepalive_ts: new Date()
596
- }
597
- },
598
- { session }
599
- );
600
- });
601
- }
602
-
603
- async [Symbol.asyncDispose]() {
604
- await this.session.endSession();
605
- super[Symbol.dispose]();
606
- }
607
-
608
- private lastWaitingLogThottled = 0;
609
-
610
- async commit(lsn: string): Promise<boolean> {
611
- await this.flush();
612
-
613
- if (this.last_checkpoint_lsn != null && lsn < this.last_checkpoint_lsn) {
614
- // When re-applying transactions, don't create a new checkpoint until
615
- // we are past the last transaction.
616
- logger.info(`Re-applied transaction ${lsn} - skipping checkpoint`);
617
- return false;
618
- }
619
- if (lsn < this.no_checkpoint_before_lsn) {
620
- if (Date.now() - this.lastWaitingLogThottled > 5_000) {
621
- logger.info(
622
- `Waiting until ${this.no_checkpoint_before_lsn} before creating checkpoint, currently at ${lsn}. Persisted op: ${this.persisted_op}`
623
- );
624
- this.lastWaitingLogThottled = Date.now();
625
- }
626
-
627
- // Edge case: During initial replication, we have a no_checkpoint_before_lsn set,
628
- // and don't actually commit the snapshot.
629
- // The first commit can happen from an implicit keepalive message.
630
- // That needs the persisted_op to get an accurate checkpoint, so
631
- // we persist that in keepalive_op.
632
-
633
- await this.db.sync_rules.updateOne(
634
- {
635
- _id: this.group_id
636
- },
637
- {
638
- $set: {
639
- keepalive_op: this.persisted_op == null ? null : String(this.persisted_op)
640
- }
641
- },
642
- { session: this.session }
643
- );
644
-
645
- return false;
646
- }
647
-
648
- const now = new Date();
649
- const update: Partial<SyncRuleDocument> = {
650
- last_checkpoint_lsn: lsn,
651
- last_checkpoint_ts: now,
652
- last_keepalive_ts: now,
653
- snapshot_done: true,
654
- last_fatal_error: null,
655
- keepalive_op: null
656
- };
657
-
658
- if (this.persisted_op != null) {
659
- update.last_checkpoint = this.persisted_op;
660
- }
661
-
662
- await this.db.sync_rules.updateOne(
663
- {
664
- _id: this.group_id
665
- },
666
- {
667
- $set: update
668
- },
669
- { session: this.session }
670
- );
671
- this.persisted_op = null;
672
- this.last_checkpoint_lsn = lsn;
673
- return true;
674
- }
675
-
676
- async keepalive(lsn: string): Promise<boolean> {
677
- if (this.last_checkpoint_lsn != null && lsn <= this.last_checkpoint_lsn) {
678
- // No-op
679
- return false;
680
- }
681
-
682
- if (lsn < this.no_checkpoint_before_lsn) {
683
- return false;
684
- }
685
-
686
- if (this.persisted_op != null) {
687
- // The commit may have been skipped due to "no_checkpoint_before_lsn".
688
- // Apply it now if relevant
689
- logger.info(`Commit due to keepalive at ${lsn} / ${this.persisted_op}`);
690
- return await this.commit(lsn);
691
- }
692
-
693
- await this.db.sync_rules.updateOne(
694
- {
695
- _id: this.group_id
696
- },
697
- {
698
- $set: {
699
- last_checkpoint_lsn: lsn,
700
- snapshot_done: true,
701
- last_fatal_error: null,
702
- last_keepalive_ts: new Date()
703
- }
704
- },
705
- { session: this.session }
706
- );
707
- this.last_checkpoint_lsn = lsn;
708
-
709
- return true;
710
- }
711
-
712
- async save(record: storage.SaveOptions): Promise<storage.FlushedResult | null> {
713
- const { after, before, sourceTable, tag } = record;
714
- for (const event of this.getTableEvents(sourceTable)) {
715
- this.iterateListeners((cb) =>
716
- cb.replicationEvent?.({
717
- batch: this,
718
- table: sourceTable,
719
- data: {
720
- op: tag,
721
- after: after && utils.isCompleteRow(after) ? after : undefined,
722
- before: before && utils.isCompleteRow(before) ? before : undefined
723
- },
724
- event
725
- })
726
- );
727
- }
728
-
729
- /**
730
- * Return if the table is just an event table
731
- */
732
- if (!sourceTable.syncData && !sourceTable.syncParameters) {
733
- return null;
734
- }
735
-
736
- logger.debug(`Saving ${record.tag}:${record.before?.id}/${record.after?.id}`);
737
-
738
- this.batch ??= new OperationBatch();
739
- this.batch.push(new RecordOperation(record));
740
-
741
- if (this.batch.shouldFlush()) {
742
- const r = await this.flush();
743
- // HACK: Give other streams a chance to also flush
744
- await timers.setTimeout(5);
745
- return r;
746
- }
747
- return null;
748
- }
749
-
750
- /**
751
- * Drop is equivalent to TRUNCATE, plus removing our record of the table.
752
- */
753
- async drop(sourceTables: storage.SourceTable[]): Promise<storage.FlushedResult | null> {
754
- await this.truncate(sourceTables);
755
- const result = await this.flush();
756
-
757
- await this.withTransaction(async () => {
758
- for (let table of sourceTables) {
759
- await this.db.source_tables.deleteOne({ _id: table.id });
760
- }
761
- });
762
- return result;
763
- }
764
-
765
- async truncate(sourceTables: storage.SourceTable[]): Promise<storage.FlushedResult | null> {
766
- await this.flush();
767
-
768
- let last_op: bigint | null = null;
769
- for (let table of sourceTables) {
770
- last_op = await this.truncateSingle(table);
771
- }
772
-
773
- if (last_op) {
774
- this.persisted_op = last_op;
775
- }
776
-
777
- return {
778
- flushed_op: String(last_op!)
779
- };
780
- }
781
-
782
- async truncateSingle(sourceTable: storage.SourceTable): Promise<bigint> {
783
- let last_op: bigint | null = null;
784
-
785
- // To avoid too large transactions, we limit the amount of data we delete per transaction.
786
- // Since we don't use the record data here, we don't have explicit size limits per batch.
787
- const BATCH_LIMIT = 2000;
788
-
789
- let lastBatchCount = BATCH_LIMIT;
790
- while (lastBatchCount == BATCH_LIMIT) {
791
- await this.withReplicationTransaction(`Truncate ${sourceTable.qualifiedName}`, async (session, opSeq) => {
792
- const current_data_filter: mongo.Filter<CurrentDataDocument> = {
793
- _id: idPrefixFilter<SourceKey>({ g: this.group_id, t: sourceTable.id }, ['k'])
794
- };
795
-
796
- const cursor = this.db.current_data.find(current_data_filter, {
797
- projection: {
798
- _id: 1,
799
- buckets: 1,
800
- lookups: 1
801
- },
802
- limit: BATCH_LIMIT,
803
- session: session
804
- });
805
- const batch = await cursor.toArray();
806
- const persistedBatch = new PersistedBatch(this.group_id, 0);
807
-
808
- for (let value of batch) {
809
- persistedBatch.saveBucketData({
810
- op_seq: opSeq,
811
- before_buckets: value.buckets,
812
- evaluated: [],
813
- table: sourceTable,
814
- sourceKey: value._id.k
815
- });
816
- persistedBatch.saveParameterData({
817
- op_seq: opSeq,
818
- existing_lookups: value.lookups,
819
- evaluated: [],
820
- sourceTable: sourceTable,
821
- sourceKey: value._id.k
822
- });
823
-
824
- persistedBatch.deleteCurrentData(value._id);
825
- }
826
- await persistedBatch.flush(this.db, session);
827
- lastBatchCount = batch.length;
828
-
829
- last_op = opSeq.last();
830
- });
831
- }
832
-
833
- return last_op!;
834
- }
835
-
836
- async markSnapshotDone(tables: storage.SourceTable[], no_checkpoint_before_lsn: string) {
837
- const session = this.session;
838
- const ids = tables.map((table) => table.id);
839
-
840
- await this.withTransaction(async () => {
841
- await this.db.source_tables.updateMany(
842
- { _id: { $in: ids } },
843
- {
844
- $set: {
845
- snapshot_done: true
846
- }
847
- },
848
- { session }
849
- );
850
-
851
- if (no_checkpoint_before_lsn > this.no_checkpoint_before_lsn) {
852
- this.no_checkpoint_before_lsn = no_checkpoint_before_lsn;
853
-
854
- await this.db.sync_rules.updateOne(
855
- {
856
- _id: this.group_id
857
- },
858
- {
859
- $set: {
860
- no_checkpoint_before: no_checkpoint_before_lsn,
861
- last_keepalive_ts: new Date()
862
- }
863
- },
864
- { session: this.session }
865
- );
866
- }
867
- });
868
- return tables.map((table) => {
869
- const copy = new storage.SourceTable(
870
- table.id,
871
- table.connectionTag,
872
- table.objectId,
873
- table.schema,
874
- table.table,
875
- table.replicaIdColumns,
876
- table.snapshotComplete
877
- );
878
- copy.syncData = table.syncData;
879
- copy.syncParameters = table.syncParameters;
880
- return copy;
881
- });
882
- }
883
-
884
- /**
885
- * Gets relevant {@link SqlEventDescriptor}s for the given {@link SourceTable}
886
- */
887
- protected getTableEvents(table: storage.SourceTable): SqlEventDescriptor[] {
888
- return this.sync_rules.event_descriptors.filter((evt) =>
889
- [...evt.getSourceTables()].some((sourceTable) => sourceTable.matches(table))
890
- );
891
- }
892
- }
893
-
894
- export function currentBucketKey(b: CurrentBucket) {
895
- return `${b.bucket}/${b.table}/${b.id}`;
896
- }