@powersync/service-module-mongodb-storage 0.24.2 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/dist/migrations/db/migrations/1684951997326-init.js +3 -29
- package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -1
- package/dist/migrations/db/migrations/1741697235857-bucket-state-index.js +3 -24
- package/dist/migrations/db/migrations/1741697235857-bucket-state-index.js.map +1 -1
- package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.js +3 -24
- package/dist/migrations/db/migrations/1760433882550-bucket-state-index2.js.map +1 -1
- package/dist/migrations/db/migrations/1794268800000-v1-storage-collections.d.ts +3 -0
- package/dist/migrations/db/migrations/1794268800000-v1-storage-collections.js +21 -0
- package/dist/migrations/db/migrations/1794268800000-v1-storage-collections.js.map +1 -0
- package/dist/storage/MongoBucketStorage.d.ts +9 -0
- package/dist/storage/MongoBucketStorage.js +60 -30
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoBucketBatch.d.ts +21 -16
- package/dist/storage/implementation/MongoBucketBatch.js +22 -125
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoStorageProvider.js +1 -0
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.js +12 -5
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +9 -2
- package/dist/storage/implementation/MongoSyncRulesLock.js +12 -3
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/OperationBatch.d.ts +10 -3
- package/dist/storage/implementation/OperationBatch.js +22 -13
- package/dist/storage/implementation/OperationBatch.js.map +1 -1
- package/dist/storage/implementation/ReplicationBatchLimits.d.ts +24 -0
- package/dist/storage/implementation/ReplicationBatchLimits.js +25 -0
- package/dist/storage/implementation/ReplicationBatchLimits.js.map +1 -0
- package/dist/storage/implementation/common/BatchOperationBuffer.d.ts +17 -1
- package/dist/storage/implementation/common/BatchOperationBuffer.js +22 -24
- package/dist/storage/implementation/common/BatchOperationBuffer.js.map +1 -1
- package/dist/storage/implementation/common/PersistedBatch.d.ts +2 -0
- package/dist/storage/implementation/common/PreparedPublication.d.ts +13 -9
- package/dist/storage/implementation/db.d.ts +2 -0
- package/dist/storage/implementation/db.js +14 -0
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +18 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +99 -14
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/PersistedBatchV1.js +1 -1
- package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +3 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -1
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +39 -3
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +356 -90
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +14 -5
- package/dist/storage/implementation/v3/PersistedBatchV3.js +26 -13
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/ReplicationPipelineV3.d.ts +84 -0
- package/dist/storage/implementation/v3/ReplicationPipelineV3.js +187 -0
- package/dist/storage/implementation/v3/ReplicationPipelineV3.js.map +1 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +4 -4
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +4 -4
- package/dist/storage/implementation/v3/chunking.js +7 -3
- package/dist/storage/implementation/v3/chunking.js.map +1 -1
- package/dist/types/types.d.ts +5 -0
- package/dist/types/types.js +4 -0
- package/dist/types/types.js.map +1 -1
- package/dist/utils/collection-sizes.d.ts +9 -0
- package/dist/utils/collection-sizes.js +95 -0
- package/dist/utils/collection-sizes.js.map +1 -0
- package/dist/utils/test-utils.js +1 -4
- package/dist/utils/test-utils.js.map +1 -1
- package/package.json +7 -7
- package/src/migrations/db/migrations/1684951997326-init.ts +3 -36
- package/src/migrations/db/migrations/1741697235857-bucket-state-index.ts +3 -31
- package/src/migrations/db/migrations/1760433882550-bucket-state-index2.ts +3 -31
- package/src/migrations/db/migrations/1794268800000-v1-storage-collections.ts +22 -0
- package/src/storage/MongoBucketStorage.ts +80 -31
- package/src/storage/implementation/MongoBucketBatch.ts +47 -137
- package/src/storage/implementation/MongoStorageProvider.ts +1 -0
- package/src/storage/implementation/MongoSyncBucketStorage.ts +13 -5
- package/src/storage/implementation/MongoSyncRulesLock.ts +13 -3
- package/src/storage/implementation/OperationBatch.ts +23 -15
- package/src/storage/implementation/ReplicationBatchLimits.ts +45 -0
- package/src/storage/implementation/common/BatchOperationBuffer.ts +21 -26
- package/src/storage/implementation/common/PersistedBatch.ts +2 -0
- package/src/storage/implementation/common/PreparedPublication.ts +13 -9
- package/src/storage/implementation/db.ts +15 -0
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +135 -17
- package/src/storage/implementation/v1/PersistedBatchV1.ts +2 -1
- package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +3 -1
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +433 -101
- package/src/storage/implementation/v3/PersistedBatchV3.ts +26 -13
- package/src/storage/implementation/v3/ReplicationPipelineV3.ts +220 -0
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +4 -4
- package/src/storage/implementation/v3/chunking.ts +8 -3
- package/src/types/types.ts +7 -0
- package/src/utils/collection-sizes.ts +114 -0
- package/src/utils/test-utils.ts +1 -5
- package/test/src/concurrent_writers.test.ts +9 -15
- package/test/src/operation_batch.test.ts +207 -0
- package/test/src/prepared_publication.test.ts +187 -6
- package/test/src/replication_batch_config.test.ts +117 -0
- package/test/src/replication_pipeline.test.ts +738 -0
- package/test/src/replication_pipeline_benchmark.test.ts +133 -0
- package/test/src/replication_preparation.test.ts +4 -2
- package/test/src/replication_uploads.test.ts +28 -3
- package/test/src/storage.test.ts +24 -5
- package/test/src/storage_compacting.test.ts +16 -0
- package/test/src/storage_metrics.test.ts +88 -0
- package/test/src/v1_collections.test.ts +233 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
import { mongoTestStorageFactoryGenerator } from '@module/utils/test-utils.js';
|
|
2
|
+
import { storage, updateSyncRulesFromYaml } from '@powersync/service-core';
|
|
3
|
+
import { getTestStorage, test_utils } from '@powersync/service-core-tests';
|
|
4
|
+
import * as bson from 'bson';
|
|
5
|
+
import { describe, expect, test, vi } from 'vitest';
|
|
6
|
+
import { MongoPersistedReplicationStream } from '../../src/storage/implementation/MongoPersistedReplicationStream.js';
|
|
7
|
+
import { OperationBatch } from '../../src/storage/implementation/OperationBatch.js';
|
|
8
|
+
import { DEFAULT_REPLICATION_BATCH_LIMITS } from '../../src/storage/implementation/ReplicationBatchLimits.js';
|
|
9
|
+
import { MongoSyncBucketStorageV3 } from '../../src/storage/implementation/v3/MongoSyncBucketStorageV3.js';
|
|
10
|
+
import { PersistedBatchV3 } from '../../src/storage/implementation/v3/PersistedBatchV3.js';
|
|
11
|
+
import { SourceRecordStoreV3 } from '../../src/storage/implementation/v3/SourceRecordStoreV3.js';
|
|
12
|
+
import { env } from './env.js';
|
|
13
|
+
import { MemoryObjectStorage } from './helpers/MemoryObjectStorage.js';
|
|
14
|
+
|
|
15
|
+
const { maxInputBatchCount: MAX_INPUT_BATCH_COUNT, maxPipelineBytes: MAX_PIPELINE_BYTES } =
|
|
16
|
+
DEFAULT_REPLICATION_BATCH_LIMITS;
|
|
17
|
+
|
|
18
|
+
const CONFIG = `
|
|
19
|
+
config:
|
|
20
|
+
edition: 3
|
|
21
|
+
streams:
|
|
22
|
+
items:
|
|
23
|
+
query: SELECT * FROM items
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
async function open(version: number, mode: string) {
|
|
27
|
+
const objects = new MemoryObjectStorage();
|
|
28
|
+
const gen = mongoTestStorageFactoryGenerator({
|
|
29
|
+
url: env.MONGO_TEST_URL,
|
|
30
|
+
isCI: env.CI,
|
|
31
|
+
objectStorage: mode === 'disabled' ? undefined : objects,
|
|
32
|
+
inlineThresholdBytes: mode === 'inline' ? 16 * 1024 : 0
|
|
33
|
+
});
|
|
34
|
+
const factory = await gen.factory();
|
|
35
|
+
const stream = await factory.updateSyncRules(updateSyncRulesFromYaml(CONFIG, { storageVersion: version }));
|
|
36
|
+
const buckets = (await getTestStorage(factory, stream)) as MongoSyncBucketStorageV3;
|
|
37
|
+
const writer = await buckets.createWriter(test_utils.BATCH_OPTIONS);
|
|
38
|
+
const table = await test_utils.resolveTestTable(writer, 'items', ['id'], gen, 1);
|
|
39
|
+
await writer.markAllSnapshotDone('1/1');
|
|
40
|
+
const lock = (stream as MongoPersistedReplicationStream).current_lock!;
|
|
41
|
+
return { factory, stream, buckets, writer, table, objects, lock, pipeline: lock.replicationPipeline! };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function save(writer: storage.BucketStorageBatch, table: storage.SourceTable, id: string, description?: string) {
|
|
45
|
+
return writer.save({
|
|
46
|
+
sourceTable: table,
|
|
47
|
+
tag: storage.SaveOperationTag.UPDATE,
|
|
48
|
+
afterReplicaId: test_utils.rid(id),
|
|
49
|
+
after: { id, description }
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe.each([3, 4])('replication pipeline v%s', (version) => {
|
|
54
|
+
test.each(['disabled', 'inline', 'offloaded'])('prepares ahead of a blocked publisher (%s)', async (mode) => {
|
|
55
|
+
const c = await open(version, mode);
|
|
56
|
+
await using _factory = c.factory;
|
|
57
|
+
await using writer = c.writer;
|
|
58
|
+
// Keep integration fixtures small while exercising the real automatic path.
|
|
59
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
60
|
+
const entered = Promise.withResolvers<void>();
|
|
61
|
+
const release = Promise.withResolvers<void>();
|
|
62
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
63
|
+
const published: PersistedBatchV3[] = [];
|
|
64
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementation(async function (
|
|
65
|
+
this: PersistedBatchV3,
|
|
66
|
+
...args
|
|
67
|
+
) {
|
|
68
|
+
published.push(this);
|
|
69
|
+
if (published.length === 1) {
|
|
70
|
+
entered.resolve();
|
|
71
|
+
await release.promise;
|
|
72
|
+
}
|
|
73
|
+
return original.apply(this, args);
|
|
74
|
+
});
|
|
75
|
+
using reads = vi.spyOn(SourceRecordStoreV3.prototype, 'loadDocuments');
|
|
76
|
+
const secondUpload = Promise.withResolvers<void>();
|
|
77
|
+
const put = c.objects.put.bind(c.objects);
|
|
78
|
+
using uploads = vi.spyOn(c.objects, 'put').mockImplementation(async (...args) => {
|
|
79
|
+
const result = await put(...args);
|
|
80
|
+
if (uploads.mock.calls.length === 2) {
|
|
81
|
+
secondUpload.resolve();
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
});
|
|
85
|
+
let third: Promise<storage.FlushedResult | null> | undefined;
|
|
86
|
+
try {
|
|
87
|
+
expect(await save(writer, c.table, 'A', 'first')).toBeNull();
|
|
88
|
+
await entered.promise;
|
|
89
|
+
expect(await save(writer, c.table, 'A', 'second')).toBeNull();
|
|
90
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
91
|
+
expect(reads).toHaveBeenCalledTimes(1); // The second row uses pending state.
|
|
92
|
+
if (mode === 'offloaded') {
|
|
93
|
+
await secondUpload.promise;
|
|
94
|
+
}
|
|
95
|
+
expect(uploads).toHaveBeenCalledTimes(mode === 'offloaded' ? 2 : 0);
|
|
96
|
+
expect(writer.last_flushed_op).toBeNull();
|
|
97
|
+
expect(c.pipeline.pendingCount).toBe(2);
|
|
98
|
+
const waiting = Promise.withResolvers<void>();
|
|
99
|
+
const capacity = c.pipeline.capacity.bind(c.pipeline);
|
|
100
|
+
using _capacity = vi.spyOn(c.pipeline, 'capacity').mockImplementation(async (...args) => {
|
|
101
|
+
waiting.resolve();
|
|
102
|
+
return capacity(...args);
|
|
103
|
+
});
|
|
104
|
+
third = save(writer, c.table, 'B', 'third');
|
|
105
|
+
await waiting.promise;
|
|
106
|
+
expect(reads).toHaveBeenCalledTimes(1);
|
|
107
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
108
|
+
} finally {
|
|
109
|
+
release.resolve();
|
|
110
|
+
await third;
|
|
111
|
+
}
|
|
112
|
+
await writer.commit('1/2');
|
|
113
|
+
expect(await c.buckets.getCheckpoint()).toMatchObject({ checkpoint: 3n, lsn: '1/2' });
|
|
114
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
115
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
116
|
+
expect(c.pipeline.overlayEntryCount).toBe(0);
|
|
117
|
+
const records = await c.buckets.db
|
|
118
|
+
.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId)
|
|
119
|
+
.find({})
|
|
120
|
+
.toArray();
|
|
121
|
+
expect(records).toHaveLength(2);
|
|
122
|
+
expect(records.map((r) => bson.deserialize(r.data!.buffer)).sort((a, b) => a.id.localeCompare(b.id))).toEqual([
|
|
123
|
+
{ id: 'A', description: 'second' },
|
|
124
|
+
{ id: 'B', description: 'third' }
|
|
125
|
+
]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('uploads the successor while its predecessor is still uploading', async () => {
|
|
129
|
+
const c = await open(version, 'offloaded');
|
|
130
|
+
await using _factory = c.factory;
|
|
131
|
+
await using writer = c.writer;
|
|
132
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
133
|
+
const first = Promise.withResolvers<void>();
|
|
134
|
+
const second = Promise.withResolvers<void>();
|
|
135
|
+
const release = Promise.withResolvers<void>();
|
|
136
|
+
const put = c.objects.put.bind(c.objects);
|
|
137
|
+
let count = 0;
|
|
138
|
+
using _put = vi.spyOn(c.objects, 'put').mockImplementation(async (...args) => {
|
|
139
|
+
if (++count === 1) {
|
|
140
|
+
first.resolve();
|
|
141
|
+
await release.promise;
|
|
142
|
+
}
|
|
143
|
+
if (count === 2) {
|
|
144
|
+
second.resolve();
|
|
145
|
+
}
|
|
146
|
+
return put(...args);
|
|
147
|
+
});
|
|
148
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish');
|
|
149
|
+
await save(writer, c.table, 'A', 'first');
|
|
150
|
+
await first.promise;
|
|
151
|
+
try {
|
|
152
|
+
await save(writer, c.table, 'B', 'second');
|
|
153
|
+
await second.promise;
|
|
154
|
+
expect(_put).toHaveBeenCalledTimes(2);
|
|
155
|
+
expect(publish).not.toHaveBeenCalled();
|
|
156
|
+
expect(c.pipeline.pendingCount).toBe(2);
|
|
157
|
+
} finally {
|
|
158
|
+
release.resolve();
|
|
159
|
+
await writer.flush();
|
|
160
|
+
}
|
|
161
|
+
expect(publish).toHaveBeenCalledTimes(2);
|
|
162
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test('interleaved snapshot and streaming saves retain normal input blocks', async () => {
|
|
166
|
+
const c = await open(version, 'offloaded');
|
|
167
|
+
await using _factory = c.factory;
|
|
168
|
+
await using writer = c.writer;
|
|
169
|
+
await using snapshot = await c.buckets.createWriter({ ...test_utils.BATCH_OPTIONS, skipExistingRows: true });
|
|
170
|
+
using reads = vi.spyOn(SourceRecordStoreV3.prototype, 'loadDocuments');
|
|
171
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish');
|
|
172
|
+
using uploads = vi.spyOn(c.objects, 'put');
|
|
173
|
+
// Deliberately use the production input threshold, with alternating
|
|
174
|
+
// writers. Switching after every save must not create tiny transactions/objects.
|
|
175
|
+
for (let i = 0; i < MAX_INPUT_BATCH_COUNT; i++) {
|
|
176
|
+
await save(writer, c.table, `stream-${i}`, 'streaming');
|
|
177
|
+
await snapshot.save({
|
|
178
|
+
sourceTable: c.table,
|
|
179
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
180
|
+
afterReplicaId: test_utils.rid(`snapshot-${i}`),
|
|
181
|
+
after: { id: `snapshot-${i}`, description: 'snapshot' }
|
|
182
|
+
});
|
|
183
|
+
if (i === MAX_INPUT_BATCH_COUNT - 2) {
|
|
184
|
+
expect(reads).not.toHaveBeenCalled();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
await snapshot.flush();
|
|
188
|
+
await writer.commit('1/2');
|
|
189
|
+
expect(reads).toHaveBeenCalledTimes(2);
|
|
190
|
+
expect(publish).toHaveBeenCalledTimes(2);
|
|
191
|
+
expect(uploads.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
192
|
+
expect(uploads.mock.calls.length).toBeLessThan(16);
|
|
193
|
+
expect(
|
|
194
|
+
await c.buckets.db.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId).countDocuments()
|
|
195
|
+
).toBe(2 * MAX_INPUT_BATCH_COUNT);
|
|
196
|
+
expect((await c.buckets.getCheckpoint()).lsn).toBe('1/2');
|
|
197
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
198
|
+
}, 30_000);
|
|
199
|
+
|
|
200
|
+
test('metadata flushes every writer before truncating rows buffered below the input limit', async () => {
|
|
201
|
+
const c = await open(version, 'offloaded');
|
|
202
|
+
await using _factory = c.factory;
|
|
203
|
+
await using writer = c.writer;
|
|
204
|
+
await using snapshot = await c.buckets.createWriter({ ...test_utils.BATCH_OPTIONS, skipExistingRows: true });
|
|
205
|
+
using reads = vi.spyOn(SourceRecordStoreV3.prototype, 'loadDocuments');
|
|
206
|
+
await save(writer, c.table, 'A', 'streaming');
|
|
207
|
+
await snapshot.save({
|
|
208
|
+
sourceTable: c.table,
|
|
209
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
210
|
+
afterReplicaId: test_utils.rid('B'),
|
|
211
|
+
after: { id: 'B', description: 'snapshot' }
|
|
212
|
+
});
|
|
213
|
+
// These are writer-local buffers, not admitted publication batches.
|
|
214
|
+
expect(reads).not.toHaveBeenCalled();
|
|
215
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
216
|
+
|
|
217
|
+
await writer.truncate([c.table]);
|
|
218
|
+
// Later flushes must not resurrect input that was buffered before truncation.
|
|
219
|
+
await snapshot.flush();
|
|
220
|
+
await writer.commit('1/2');
|
|
221
|
+
expect(
|
|
222
|
+
await c.buckets.db.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId).countDocuments()
|
|
223
|
+
).toBe(0);
|
|
224
|
+
expect((await c.buckets.getCheckpoint()).lsn).toBe('1/2');
|
|
225
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
226
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('buffers without locking while metadata publishes only its captured prefix', async () => {
|
|
230
|
+
const c = await open(version, 'offloaded');
|
|
231
|
+
await using _factory = c.factory;
|
|
232
|
+
await using writer = c.writer;
|
|
233
|
+
await using other = await c.buckets.createWriter(test_utils.BATCH_OPTIONS);
|
|
234
|
+
await save(writer, c.table, 'A', 'before truncate');
|
|
235
|
+
await save(other, c.table, 'B', 'before truncate');
|
|
236
|
+
const entered = Promise.withResolvers<void>();
|
|
237
|
+
const release = Promise.withResolvers<void>();
|
|
238
|
+
const load = SourceRecordStoreV3.prototype.loadDocuments;
|
|
239
|
+
using _read = vi.spyOn(SourceRecordStoreV3.prototype, 'loadDocuments').mockImplementationOnce(async function (
|
|
240
|
+
this: SourceRecordStoreV3,
|
|
241
|
+
...args
|
|
242
|
+
) {
|
|
243
|
+
entered.resolve();
|
|
244
|
+
await release.promise;
|
|
245
|
+
return load.apply(this, args);
|
|
246
|
+
});
|
|
247
|
+
const truncating = writer.truncate([c.table]);
|
|
248
|
+
let saving: Promise<unknown> | undefined;
|
|
249
|
+
try {
|
|
250
|
+
await entered.promise;
|
|
251
|
+
using acquisitions = vi.spyOn(c.lock.writerMutex, 'exclusiveLock');
|
|
252
|
+
let buffered = false;
|
|
253
|
+
saving = Promise.all([
|
|
254
|
+
save(writer, c.table, 'C', 'after truncate'),
|
|
255
|
+
save(other, c.table, 'D', 'after truncate')
|
|
256
|
+
]).then(() => {
|
|
257
|
+
buffered = true;
|
|
258
|
+
});
|
|
259
|
+
// Buffering is synchronous, even while preparation owns the writer mutex.
|
|
260
|
+
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
261
|
+
expect(buffered).toBe(true);
|
|
262
|
+
expect(acquisitions).not.toHaveBeenCalled();
|
|
263
|
+
} finally {
|
|
264
|
+
release.resolve();
|
|
265
|
+
await Promise.all([truncating, saving]);
|
|
266
|
+
}
|
|
267
|
+
const records = c.buckets.db.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId);
|
|
268
|
+
expect(await records.countDocuments()).toBe(0);
|
|
269
|
+
// Neither the active writer's input nor the other writer's queued input may
|
|
270
|
+
// absorb rows arriving after the metadata barrier was requested.
|
|
271
|
+
await other.flush();
|
|
272
|
+
await writer.commit('1/2');
|
|
273
|
+
expect((await records.find({}).toArray()).map((row) => bson.deserialize(row.data!.buffer).id).sort()).toEqual([
|
|
274
|
+
'C',
|
|
275
|
+
'D'
|
|
276
|
+
]);
|
|
277
|
+
expect((await c.buckets.getCheckpoint()).lsn).toBe('1/2');
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test('a full input buffered after a metadata request waits and publishes after it', async () => {
|
|
281
|
+
const c = await open(version, 'disabled');
|
|
282
|
+
await using _factory = c.factory;
|
|
283
|
+
await using writer = c.writer;
|
|
284
|
+
await using other = await c.buckets.createWriter(test_utils.BATCH_OPTIONS);
|
|
285
|
+
await save(writer, c.table, 'A', 'before truncate');
|
|
286
|
+
const entered = Promise.withResolvers<void>();
|
|
287
|
+
const release = Promise.withResolvers<void>();
|
|
288
|
+
const holding = c.lock.writerMutex.exclusiveLock(async () => {
|
|
289
|
+
entered.resolve();
|
|
290
|
+
await release.promise;
|
|
291
|
+
});
|
|
292
|
+
await entered.promise;
|
|
293
|
+
const truncating = writer.truncate([c.table]);
|
|
294
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
295
|
+
let submitted = false;
|
|
296
|
+
const saving = save(other, c.table, 'B', 'after truncate').then(() => {
|
|
297
|
+
submitted = true;
|
|
298
|
+
});
|
|
299
|
+
try {
|
|
300
|
+
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
301
|
+
expect(submitted).toBe(false); // A full buffer still applies backpressure.
|
|
302
|
+
} finally {
|
|
303
|
+
release.resolve();
|
|
304
|
+
await Promise.all([holding, truncating, saving]);
|
|
305
|
+
}
|
|
306
|
+
await other.commit('1/2');
|
|
307
|
+
const records = await c.buckets.db
|
|
308
|
+
.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId)
|
|
309
|
+
.find({})
|
|
310
|
+
.toArray();
|
|
311
|
+
expect(records.map((row) => bson.deserialize(row.data!.buffer).id)).toEqual(['B']);
|
|
312
|
+
expect((await c.buckets.getCheckpoint()).lsn).toBe('1/2');
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test('split flush remainders do not overwrite rows buffered during preparation', async () => {
|
|
316
|
+
const c = await open(version, 'disabled');
|
|
317
|
+
await using _factory = c.factory;
|
|
318
|
+
await using writer = c.writer;
|
|
319
|
+
await save(writer, c.table, 'A', 'first');
|
|
320
|
+
await save(writer, c.table, 'B', 'second');
|
|
321
|
+
using _split = vi.spyOn(PersistedBatchV3.prototype, 'shouldFlushTransaction').mockReturnValue(true);
|
|
322
|
+
const entered = Promise.withResolvers<void>();
|
|
323
|
+
const release = Promise.withResolvers<void>();
|
|
324
|
+
const load = SourceRecordStoreV3.prototype.loadDocuments;
|
|
325
|
+
using _read = vi.spyOn(SourceRecordStoreV3.prototype, 'loadDocuments').mockImplementationOnce(async function (
|
|
326
|
+
this: SourceRecordStoreV3,
|
|
327
|
+
...args
|
|
328
|
+
) {
|
|
329
|
+
entered.resolve();
|
|
330
|
+
await release.promise;
|
|
331
|
+
return load.apply(this, args);
|
|
332
|
+
});
|
|
333
|
+
const flushing = writer.flush();
|
|
334
|
+
try {
|
|
335
|
+
await entered.promise;
|
|
336
|
+
await save(writer, c.table, 'C', 'next input');
|
|
337
|
+
} finally {
|
|
338
|
+
release.resolve();
|
|
339
|
+
await flushing;
|
|
340
|
+
}
|
|
341
|
+
const records = c.buckets.db.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId);
|
|
342
|
+
const ids = async () =>
|
|
343
|
+
(await records.find({}).toArray()).map((row) => bson.deserialize(row.data!.buffer).id).sort();
|
|
344
|
+
expect(await ids()).toEqual(['A', 'B']);
|
|
345
|
+
await writer.commit('1/2');
|
|
346
|
+
expect(await ids()).toEqual(['A', 'B', 'C']);
|
|
347
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test('disposing a writer with detached input waiting for the mutex invalidates the lease', async () => {
|
|
351
|
+
const c = await open(version, 'disabled');
|
|
352
|
+
await using _factory = c.factory;
|
|
353
|
+
await using writer = c.writer;
|
|
354
|
+
await save(writer, c.table, 'A', 'queued');
|
|
355
|
+
const entered = Promise.withResolvers<void>();
|
|
356
|
+
const release = Promise.withResolvers<void>();
|
|
357
|
+
const holding = c.lock.writerMutex.exclusiveLock(async () => {
|
|
358
|
+
entered.resolve();
|
|
359
|
+
await release.promise;
|
|
360
|
+
});
|
|
361
|
+
await entered.promise;
|
|
362
|
+
const flushing = expect(writer.flush()).rejects.toThrow('restart');
|
|
363
|
+
const disposing = writer.dispose();
|
|
364
|
+
try {
|
|
365
|
+
expect(c.lock.signal.aborted).toBe(true);
|
|
366
|
+
} finally {
|
|
367
|
+
release.resolve();
|
|
368
|
+
await Promise.all([holding, flushing, disposing]);
|
|
369
|
+
}
|
|
370
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test('an oversized prefix stops further admission until publication completes', async () => {
|
|
374
|
+
const c = await open(version, 'disabled');
|
|
375
|
+
await using _factory = c.factory;
|
|
376
|
+
await using writer = c.writer;
|
|
377
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
378
|
+
// Exercise byte backpressure with a small real fixture, independent of the
|
|
379
|
+
// ordinary two-prefix count limit.
|
|
380
|
+
using _size = vi
|
|
381
|
+
.spyOn(PersistedBatchV3.prototype, 'estimatedRetainedBytes', 'get')
|
|
382
|
+
.mockReturnValue(MAX_PIPELINE_BYTES + 1);
|
|
383
|
+
const entered = Promise.withResolvers<void>();
|
|
384
|
+
const release = Promise.withResolvers<void>();
|
|
385
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
386
|
+
using _publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async function (
|
|
387
|
+
this: PersistedBatchV3,
|
|
388
|
+
...args
|
|
389
|
+
) {
|
|
390
|
+
entered.resolve();
|
|
391
|
+
await release.promise;
|
|
392
|
+
return original.apply(this, args);
|
|
393
|
+
});
|
|
394
|
+
using reads = vi.spyOn(SourceRecordStoreV3.prototype, 'loadDocuments');
|
|
395
|
+
await save(writer, c.table, 'A', 'first');
|
|
396
|
+
await entered.promise;
|
|
397
|
+
const attempted = Promise.withResolvers<void>();
|
|
398
|
+
const capacity = c.pipeline.capacity.bind(c.pipeline);
|
|
399
|
+
using _capacity = vi.spyOn(c.pipeline, 'capacity').mockImplementation(async (...args) => {
|
|
400
|
+
attempted.resolve();
|
|
401
|
+
return capacity(...args);
|
|
402
|
+
});
|
|
403
|
+
const next = save(writer, c.table, 'B', 'second');
|
|
404
|
+
try {
|
|
405
|
+
await attempted.promise;
|
|
406
|
+
expect(c.pipeline.pendingCount).toBe(1);
|
|
407
|
+
expect(reads).toHaveBeenCalledTimes(1);
|
|
408
|
+
} finally {
|
|
409
|
+
release.resolve();
|
|
410
|
+
await next;
|
|
411
|
+
await writer.flush();
|
|
412
|
+
}
|
|
413
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test('shares pending state across writers and orders snapshot progress behind it', async () => {
|
|
417
|
+
const c = await open(version, 'offloaded');
|
|
418
|
+
await using _factory = c.factory;
|
|
419
|
+
await using writer = c.writer;
|
|
420
|
+
await using other = await c.buckets.createWriter(test_utils.BATCH_OPTIONS);
|
|
421
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
422
|
+
const entered = Promise.withResolvers<void>();
|
|
423
|
+
const release = Promise.withResolvers<void>();
|
|
424
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
425
|
+
using _publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async function (
|
|
426
|
+
this: PersistedBatchV3,
|
|
427
|
+
...args
|
|
428
|
+
) {
|
|
429
|
+
entered.resolve();
|
|
430
|
+
await release.promise;
|
|
431
|
+
return original.apply(this, args);
|
|
432
|
+
});
|
|
433
|
+
await save(writer, c.table, 'A', 'first');
|
|
434
|
+
await entered.promise;
|
|
435
|
+
await other.save({
|
|
436
|
+
sourceTable: c.table,
|
|
437
|
+
tag: storage.SaveOperationTag.UPDATE,
|
|
438
|
+
afterReplicaId: test_utils.rid('A'),
|
|
439
|
+
after: { id: undefined, description: 'second' }
|
|
440
|
+
});
|
|
441
|
+
let persisted = false;
|
|
442
|
+
const progress = other.updateTableProgress(c.table, { replicatedCount: 1 }).then((table) => {
|
|
443
|
+
persisted = true;
|
|
444
|
+
return table;
|
|
445
|
+
});
|
|
446
|
+
try {
|
|
447
|
+
expect(c.pipeline.pendingCount).toBe(2);
|
|
448
|
+
expect(persisted).toBe(false);
|
|
449
|
+
} finally {
|
|
450
|
+
release.resolve();
|
|
451
|
+
await progress;
|
|
452
|
+
}
|
|
453
|
+
expect(persisted).toBe(true);
|
|
454
|
+
const record = await c.buckets.db
|
|
455
|
+
.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId)
|
|
456
|
+
.findOne({});
|
|
457
|
+
expect(bson.deserialize(record!.data!.buffer)).toEqual({ id: 'A', description: 'second' });
|
|
458
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
test('snapshot skip-existing sees a pending streaming tombstone', async () => {
|
|
462
|
+
const c = await open(version, 'offloaded');
|
|
463
|
+
await using _factory = c.factory;
|
|
464
|
+
await using writer = c.writer;
|
|
465
|
+
await save(writer, c.table, 'A', 'initial');
|
|
466
|
+
await writer.commit('1/1');
|
|
467
|
+
await using snapshot = await c.buckets.createWriter({ ...test_utils.BATCH_OPTIONS, skipExistingRows: true });
|
|
468
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
469
|
+
const entered = Promise.withResolvers<void>();
|
|
470
|
+
const release = Promise.withResolvers<void>();
|
|
471
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
472
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async function (
|
|
473
|
+
this: PersistedBatchV3,
|
|
474
|
+
...args
|
|
475
|
+
) {
|
|
476
|
+
entered.resolve();
|
|
477
|
+
await release.promise;
|
|
478
|
+
return original.apply(this, args);
|
|
479
|
+
});
|
|
480
|
+
using uploads = vi.spyOn(c.objects, 'put');
|
|
481
|
+
await writer.save({
|
|
482
|
+
sourceTable: c.table,
|
|
483
|
+
tag: storage.SaveOperationTag.DELETE,
|
|
484
|
+
beforeReplicaId: test_utils.rid('A')
|
|
485
|
+
});
|
|
486
|
+
await entered.promise;
|
|
487
|
+
try {
|
|
488
|
+
await snapshot.save({
|
|
489
|
+
sourceTable: c.table,
|
|
490
|
+
tag: storage.SaveOperationTag.INSERT,
|
|
491
|
+
afterReplicaId: test_utils.rid('A'),
|
|
492
|
+
after: { id: 'A', description: 'stale snapshot' }
|
|
493
|
+
});
|
|
494
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
495
|
+
} finally {
|
|
496
|
+
release.resolve();
|
|
497
|
+
await snapshot.flush();
|
|
498
|
+
}
|
|
499
|
+
// Only the streaming REMOVE uploads; the stale snapshot generates no PUT.
|
|
500
|
+
expect(uploads).toHaveBeenCalledTimes(1);
|
|
501
|
+
await writer.commit('1/2');
|
|
502
|
+
expect(
|
|
503
|
+
await c.buckets.db.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId).countDocuments()
|
|
504
|
+
).toBe(0);
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
test('a drained writer can dispose while another publisher is stalled', async () => {
|
|
508
|
+
const c = await open(version, 'disabled');
|
|
509
|
+
await using _factory = c.factory;
|
|
510
|
+
await using writer = c.writer;
|
|
511
|
+
const other = await c.buckets.createWriter(test_utils.BATCH_OPTIONS);
|
|
512
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
513
|
+
const entered = Promise.withResolvers<void>();
|
|
514
|
+
const release = Promise.withResolvers<void>();
|
|
515
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
516
|
+
using _publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async function (
|
|
517
|
+
this: PersistedBatchV3,
|
|
518
|
+
...args
|
|
519
|
+
) {
|
|
520
|
+
entered.resolve();
|
|
521
|
+
await release.promise;
|
|
522
|
+
return original.apply(this, args);
|
|
523
|
+
});
|
|
524
|
+
await save(writer, c.table, 'A', 'first');
|
|
525
|
+
await entered.promise;
|
|
526
|
+
try {
|
|
527
|
+
await other.dispose();
|
|
528
|
+
expect(c.lock.signal.aborted).toBe(false);
|
|
529
|
+
} finally {
|
|
530
|
+
release.resolve();
|
|
531
|
+
await writer.flush();
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test('pins pending row versions through a stale read finishing after commit', async () => {
|
|
536
|
+
const c = await open(version, 'disabled');
|
|
537
|
+
await using _factory = c.factory;
|
|
538
|
+
await using writer = c.writer;
|
|
539
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
540
|
+
const publishing = Promise.withResolvers<void>();
|
|
541
|
+
const releasePublication = Promise.withResolvers<void>();
|
|
542
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
543
|
+
using _publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async function (
|
|
544
|
+
this: PersistedBatchV3,
|
|
545
|
+
...args
|
|
546
|
+
) {
|
|
547
|
+
publishing.resolve();
|
|
548
|
+
await releasePublication.promise;
|
|
549
|
+
return original.apply(this, args);
|
|
550
|
+
});
|
|
551
|
+
await save(writer, c.table, 'A', 'first');
|
|
552
|
+
await publishing.promise;
|
|
553
|
+
const reading = Promise.withResolvers<void>();
|
|
554
|
+
const releaseRead = Promise.withResolvers<void>();
|
|
555
|
+
const sizes = SourceRecordStoreV3.prototype.loadSizes;
|
|
556
|
+
using _sizes = vi.spyOn(SourceRecordStoreV3.prototype, 'loadSizes').mockImplementationOnce(async function (
|
|
557
|
+
this: SourceRecordStoreV3,
|
|
558
|
+
...args
|
|
559
|
+
) {
|
|
560
|
+
const result = await sizes.apply(this, args);
|
|
561
|
+
reading.resolve();
|
|
562
|
+
await releaseRead.promise;
|
|
563
|
+
return result;
|
|
564
|
+
});
|
|
565
|
+
const next = writer.save({
|
|
566
|
+
sourceTable: c.table,
|
|
567
|
+
tag: storage.SaveOperationTag.UPDATE,
|
|
568
|
+
afterReplicaId: test_utils.rid('A'),
|
|
569
|
+
after: { id: undefined, description: 'second' }
|
|
570
|
+
});
|
|
571
|
+
try {
|
|
572
|
+
await reading.promise;
|
|
573
|
+
releasePublication.resolve();
|
|
574
|
+
await c.pipeline.join();
|
|
575
|
+
expect(c.pipeline.overlayEntryCount).toBe(1);
|
|
576
|
+
expect(c.pipeline.accountedBytes).toBeGreaterThan(0);
|
|
577
|
+
} finally {
|
|
578
|
+
releaseRead.resolve();
|
|
579
|
+
releasePublication.resolve();
|
|
580
|
+
await next;
|
|
581
|
+
}
|
|
582
|
+
await writer.commit('1/2');
|
|
583
|
+
const record = await c.buckets.db
|
|
584
|
+
.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId)
|
|
585
|
+
.findOne({});
|
|
586
|
+
expect(bson.deserialize(record!.data!.buffer)).toEqual({ id: 'A', description: 'second' });
|
|
587
|
+
expect(c.pipeline.overlayEntryCount).toBe(0);
|
|
588
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
test('automatic flush returns a durable result when a partial row needs repair', async () => {
|
|
592
|
+
const c = await open(version, 'disabled');
|
|
593
|
+
await using _factory = c.factory;
|
|
594
|
+
await using _writer = c.writer;
|
|
595
|
+
const repairs = vi.fn();
|
|
596
|
+
await using writer = await c.buckets.createWriter({ ...test_utils.BATCH_OPTIONS, markRecordUnavailable: repairs });
|
|
597
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
598
|
+
const result = await writer.save({
|
|
599
|
+
sourceTable: c.table,
|
|
600
|
+
tag: storage.SaveOperationTag.UPDATE,
|
|
601
|
+
afterReplicaId: 'missing',
|
|
602
|
+
after: { id: 'missing', description: undefined }
|
|
603
|
+
});
|
|
604
|
+
expect(result).not.toBeNull();
|
|
605
|
+
expect(result!.flushed_op).toBe(writer.last_flushed_op);
|
|
606
|
+
expect(repairs).toHaveBeenCalledTimes(1);
|
|
607
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
608
|
+
expect((await c.buckets.getCheckpoint()).lsn).toBeNull();
|
|
609
|
+
// Simulate the connector's targeted repair before its source commit.
|
|
610
|
+
expect(await save(writer, c.table, 'missing', 'repaired')).toBeNull();
|
|
611
|
+
await writer.flush();
|
|
612
|
+
await writer.commit('1/2');
|
|
613
|
+
expect((await c.buckets.getCheckpoint()).lsn).toBe('1/2');
|
|
614
|
+
expect(repairs).toHaveBeenCalledTimes(1);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
test('joins an active successor upload when predecessor publication fails', async () => {
|
|
618
|
+
const c = await open(version, 'offloaded');
|
|
619
|
+
await using _factory = c.factory;
|
|
620
|
+
await using writer = c.writer;
|
|
621
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
622
|
+
const publishing = Promise.withResolvers<void>();
|
|
623
|
+
const failPublication = Promise.withResolvers<void>();
|
|
624
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async () => {
|
|
625
|
+
publishing.resolve();
|
|
626
|
+
await failPublication.promise;
|
|
627
|
+
throw new Error('publication failed');
|
|
628
|
+
});
|
|
629
|
+
const uploading = Promise.withResolvers<void>();
|
|
630
|
+
const finishUpload = Promise.withResolvers<void>();
|
|
631
|
+
const put = c.objects.put.bind(c.objects);
|
|
632
|
+
let uploads = 0;
|
|
633
|
+
using _put = vi.spyOn(c.objects, 'put').mockImplementation(async (...args) => {
|
|
634
|
+
if (++uploads === 2) {
|
|
635
|
+
uploading.resolve();
|
|
636
|
+
await finishUpload.promise;
|
|
637
|
+
}
|
|
638
|
+
return put(...args);
|
|
639
|
+
});
|
|
640
|
+
let joined = false;
|
|
641
|
+
let joining: Promise<void> | undefined;
|
|
642
|
+
try {
|
|
643
|
+
await save(writer, c.table, 'A', 'first');
|
|
644
|
+
await publishing.promise;
|
|
645
|
+
await save(writer, c.table, 'B', 'second');
|
|
646
|
+
await uploading.promise;
|
|
647
|
+
joining = c.pipeline.join().then(() => {
|
|
648
|
+
joined = true;
|
|
649
|
+
});
|
|
650
|
+
failPublication.resolve();
|
|
651
|
+
await expect(save(writer, c.table, 'C', 'third')).rejects.toThrow('restart');
|
|
652
|
+
expect(joined).toBe(false);
|
|
653
|
+
expect(c.pipeline.accountedBytes).toBeGreaterThan(0);
|
|
654
|
+
} finally {
|
|
655
|
+
failPublication.resolve();
|
|
656
|
+
finishUpload.resolve();
|
|
657
|
+
await joining;
|
|
658
|
+
}
|
|
659
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
660
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
661
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
662
|
+
expect(
|
|
663
|
+
await c.buckets.db.sourceRecords(c.stream.replicationStreamId, c.table.id as bson.ObjectId).countDocuments()
|
|
664
|
+
).toBe(0);
|
|
665
|
+
expect((await c.buckets.getCheckpoint()).checkpoint).toBe(0n);
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
test('successor upload failure stops admission before predecessor publication finishes', async () => {
|
|
669
|
+
const c = await open(version, 'offloaded');
|
|
670
|
+
await using _factory = c.factory;
|
|
671
|
+
await using writer = c.writer;
|
|
672
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
673
|
+
const publishing = Promise.withResolvers<void>();
|
|
674
|
+
const release = Promise.withResolvers<void>();
|
|
675
|
+
const original = PersistedBatchV3.prototype.publish;
|
|
676
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async function (
|
|
677
|
+
this: PersistedBatchV3,
|
|
678
|
+
...args
|
|
679
|
+
) {
|
|
680
|
+
publishing.resolve();
|
|
681
|
+
await release.promise;
|
|
682
|
+
return original.apply(this, args);
|
|
683
|
+
});
|
|
684
|
+
const put = c.objects.put.bind(c.objects);
|
|
685
|
+
let uploads = 0;
|
|
686
|
+
using _put = vi.spyOn(c.objects, 'put').mockImplementation(async (...args) => {
|
|
687
|
+
if (++uploads === 2) {
|
|
688
|
+
throw new Error('successor upload failed');
|
|
689
|
+
}
|
|
690
|
+
return put(...args);
|
|
691
|
+
});
|
|
692
|
+
const aborted = Promise.withResolvers<void>();
|
|
693
|
+
c.lock.signal.addEventListener('abort', () => aborted.resolve(), { once: true });
|
|
694
|
+
try {
|
|
695
|
+
await save(writer, c.table, 'A', 'first');
|
|
696
|
+
await publishing.promise;
|
|
697
|
+
await save(writer, c.table, 'B', 'second');
|
|
698
|
+
await aborted.promise;
|
|
699
|
+
await expect(save(writer, c.table, 'C', 'third')).rejects.toThrow('restart');
|
|
700
|
+
// The failed successor must stay behind its predecessor through teardown.
|
|
701
|
+
expect(c.pipeline.pendingCount).toBe(2);
|
|
702
|
+
expect(c.pipeline.accountedBytes).toBeGreaterThan(0);
|
|
703
|
+
} finally {
|
|
704
|
+
release.resolve();
|
|
705
|
+
await c.pipeline.join();
|
|
706
|
+
}
|
|
707
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
708
|
+
expect(c.pipeline.pendingCount).toBe(0);
|
|
709
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
710
|
+
expect((await c.buckets.getCheckpoint()).checkpoint).toBe(0n);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
test('publication failure rejects capacity waiters and stops the suffix', async () => {
|
|
714
|
+
const c = await open(version, 'offloaded');
|
|
715
|
+
await using _factory = c.factory;
|
|
716
|
+
await using writer = c.writer;
|
|
717
|
+
using _limit = vi.spyOn(OperationBatch.prototype, 'shouldFlush').mockReturnValue(true);
|
|
718
|
+
const entered = Promise.withResolvers<void>();
|
|
719
|
+
const release = Promise.withResolvers<void>();
|
|
720
|
+
using publish = vi.spyOn(PersistedBatchV3.prototype, 'publish').mockImplementationOnce(async () => {
|
|
721
|
+
entered.resolve();
|
|
722
|
+
await release.promise;
|
|
723
|
+
throw new Error('publication failed');
|
|
724
|
+
});
|
|
725
|
+
await save(writer, c.table, 'A', 'first');
|
|
726
|
+
await entered.promise;
|
|
727
|
+
await save(writer, c.table, 'B', 'second');
|
|
728
|
+
const rejected = expect(save(writer, c.table, 'C', 'third')).rejects.toThrow('restart');
|
|
729
|
+
release.resolve();
|
|
730
|
+
await rejected;
|
|
731
|
+
await expect(writer.flush()).rejects.toThrow('restart');
|
|
732
|
+
await c.pipeline.join();
|
|
733
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
734
|
+
expect(c.pipeline.accountedBytes).toBe(0);
|
|
735
|
+
expect(writer.last_flushed_op).toBeNull();
|
|
736
|
+
expect((await c.buckets.getCheckpoint()).checkpoint).toBe(0n);
|
|
737
|
+
});
|
|
738
|
+
});
|