@powersync/service-core 0.13.0 → 0.14.0

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