@powersync/service-core 0.0.0-dev-20260601140105 → 0.0.0-dev-20260803144812
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +83 -4
- package/dist/api/RouteAPI.d.ts +42 -14
- package/dist/api/diagnostics.d.ts +12 -1
- package/dist/api/diagnostics.js +34 -22
- package/dist/api/diagnostics.js.map +1 -1
- package/dist/entry/commands/compact-action.js +5 -1
- package/dist/entry/commands/compact-action.js.map +1 -1
- package/dist/modules/AbstractModule.d.ts +6 -2
- package/dist/modules/AbstractModule.js.map +1 -1
- package/dist/replication/AbstractReplicator.d.ts +26 -7
- package/dist/replication/AbstractReplicator.js +148 -68
- package/dist/replication/AbstractReplicator.js.map +1 -1
- package/dist/replication/ReplicationModule.d.ts +3 -3
- package/dist/replication/ReplicationModule.js.map +1 -1
- package/dist/routes/RouterEngine.d.ts +1 -0
- package/dist/routes/RouterEngine.js +6 -1
- package/dist/routes/RouterEngine.js.map +1 -1
- package/dist/routes/configure-fastify.d.ts +95 -56
- package/dist/routes/configure-fastify.js +15 -2
- package/dist/routes/configure-fastify.js.map +1 -1
- package/dist/routes/endpoints/admin.d.ts +24 -24
- package/dist/routes/endpoints/admin.js +46 -35
- package/dist/routes/endpoints/admin.js.map +1 -1
- package/dist/routes/endpoints/checkpointing.d.ts +62 -0
- package/dist/routes/endpoints/checkpointing.js +65 -7
- package/dist/routes/endpoints/checkpointing.js.map +1 -1
- package/dist/routes/endpoints/socket-route.js +3 -2
- package/dist/routes/endpoints/socket-route.js.map +1 -1
- package/dist/routes/endpoints/sync-rules.js +22 -13
- package/dist/routes/endpoints/sync-rules.js.map +1 -1
- package/dist/routes/endpoints/sync-stream.js +1 -1
- package/dist/routes/endpoints/sync-stream.js.map +1 -1
- package/dist/runner/teardown.js +8 -8
- package/dist/runner/teardown.js.map +1 -1
- package/dist/storage/BucketStorageBatch.d.ts +63 -17
- package/dist/storage/BucketStorageBatch.js.map +1 -1
- package/dist/storage/BucketStorageFactory.d.ts +22 -27
- package/dist/storage/BucketStorageFactory.js +4 -18
- package/dist/storage/BucketStorageFactory.js.map +1 -1
- package/dist/storage/CheckpointChecksumInvalidatedError.d.ts +12 -0
- package/dist/storage/CheckpointChecksumInvalidatedError.js +17 -0
- package/dist/storage/CheckpointChecksumInvalidatedError.js.map +1 -0
- package/dist/storage/ChecksumCache.d.ts +11 -6
- package/dist/storage/ChecksumCache.js +5 -4
- package/dist/storage/ChecksumCache.js.map +1 -1
- package/dist/storage/ParsedSyncConfigSet.d.ts +23 -0
- package/dist/storage/ParsedSyncConfigSet.js +2 -0
- package/dist/storage/ParsedSyncConfigSet.js.map +1 -0
- package/dist/storage/PersistedReplicationStream.d.ts +32 -0
- package/dist/storage/PersistedReplicationStream.js +26 -0
- package/dist/storage/PersistedReplicationStream.js.map +1 -0
- package/dist/storage/PersistedSyncConfigContent.d.ts +63 -0
- package/dist/storage/PersistedSyncConfigContent.js +125 -0
- package/dist/storage/PersistedSyncConfigContent.js.map +1 -0
- package/dist/storage/PersistedSyncConfigStatus.d.ts +13 -0
- package/dist/storage/PersistedSyncConfigStatus.js +2 -0
- package/dist/storage/PersistedSyncConfigStatus.js.map +1 -0
- package/dist/storage/SourceEntity.d.ts +6 -0
- package/dist/storage/SourceTable.d.ts +21 -3
- package/dist/storage/SourceTable.js +26 -3
- package/dist/storage/SourceTable.js.map +1 -1
- package/dist/storage/SyncRulesBucketStorage.d.ts +119 -21
- package/dist/storage/SyncRulesBucketStorage.js +3 -0
- package/dist/storage/SyncRulesBucketStorage.js.map +1 -1
- package/dist/storage/WriteCheckpointAPI.d.ts +60 -3
- package/dist/storage/WriteCheckpointAPI.js +33 -0
- package/dist/storage/WriteCheckpointAPI.js.map +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +117 -0
- package/dist/storage/implementation/BucketDefinitionMapping.js +277 -0
- package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
- package/dist/storage/implementation/IncrementalReprocessingCompatibility.d.ts +6 -0
- package/dist/storage/implementation/IncrementalReprocessingCompatibility.js +31 -0
- package/dist/storage/implementation/IncrementalReprocessingCompatibility.js.map +1 -0
- package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.d.ts +17 -0
- package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js +62 -0
- package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js.map +1 -0
- package/dist/storage/implementation/ReplicationStreamStorageIds.d.ts +15 -0
- package/dist/storage/implementation/ReplicationStreamStorageIds.js +20 -0
- package/dist/storage/implementation/ReplicationStreamStorageIds.js.map +1 -0
- package/dist/storage/storage-index.d.ts +9 -1
- package/dist/storage/storage-index.js +9 -1
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/sync/BucketChecksumState.d.ts +12 -2
- package/dist/sync/BucketChecksumState.js +69 -21
- package/dist/sync/BucketChecksumState.js.map +1 -1
- package/dist/sync/RequestTracker.d.ts +7 -0
- package/dist/sync/RequestTracker.js +12 -0
- package/dist/sync/RequestTracker.js.map +1 -1
- package/dist/sync/sync.js +154 -41
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/util.d.ts +5 -0
- package/dist/sync/util.js +9 -0
- package/dist/sync/util.js.map +1 -1
- package/dist/system/ServiceContext.d.ts +2 -0
- package/dist/system/ServiceContext.js +2 -0
- package/dist/system/ServiceContext.js.map +1 -1
- package/dist/tracing/PerformanceTracer.d.ts +23 -1
- package/dist/tracing/PerformanceTracer.js +11 -0
- package/dist/tracing/PerformanceTracer.js.map +1 -1
- package/dist/util/checkpointing.d.ts +4 -8
- package/dist/util/checkpointing.js +1 -16
- package/dist/util/checkpointing.js.map +1 -1
- package/dist/util/config/compound-config-collector.js +18 -2
- package/dist/util/config/compound-config-collector.js.map +1 -1
- package/dist/util/config/defaults.d.ts +2 -0
- package/dist/util/config/defaults.js +2 -0
- package/dist/util/config/defaults.js.map +1 -1
- package/dist/util/config/types.d.ts +3 -1
- package/dist/util/lsn.d.ts +4 -0
- package/dist/util/lsn.js +10 -0
- package/dist/util/lsn.js.map +1 -1
- package/dist/util/util-index.d.ts +1 -0
- package/dist/util/util-index.js +1 -0
- package/dist/util/util-index.js.map +1 -1
- package/dist/util/utils.d.ts +1 -1
- package/dist/util/utils.js +1 -1
- package/dist/util/write-checkpoint-batcher.d.ts +19 -0
- package/dist/util/write-checkpoint-batcher.js +100 -0
- package/dist/util/write-checkpoint-batcher.js.map +1 -0
- package/package.json +6 -7
- package/src/api/RouteAPI.ts +43 -14
- package/src/api/diagnostics.ts +43 -26
- package/src/entry/commands/compact-action.ts +7 -1
- package/src/modules/AbstractModule.ts +6 -2
- package/src/replication/AbstractReplicator.ts +181 -72
- package/src/replication/ReplicationModule.ts +2 -3
- package/src/routes/RouterEngine.ts +7 -1
- package/src/routes/configure-fastify.ts +25 -2
- package/src/routes/endpoints/admin.ts +72 -50
- package/src/routes/endpoints/checkpointing.ts +79 -8
- package/src/routes/endpoints/socket-route.ts +3 -2
- package/src/routes/endpoints/sync-rules.ts +23 -13
- package/src/routes/endpoints/sync-stream.ts +1 -1
- package/src/runner/teardown.ts +8 -8
- package/src/storage/BucketStorageBatch.ts +64 -18
- package/src/storage/BucketStorageFactory.ts +30 -39
- package/src/storage/CheckpointChecksumInvalidatedError.ts +17 -0
- package/src/storage/ChecksumCache.ts +27 -14
- package/src/storage/ParsedSyncConfigSet.ts +25 -0
- package/src/storage/PersistedReplicationStream.ts +55 -0
- package/src/storage/PersistedSyncConfigContent.ts +192 -0
- package/src/storage/PersistedSyncConfigStatus.ts +14 -0
- package/src/storage/SourceEntity.ts +6 -0
- package/src/storage/SourceTable.ts +34 -3
- package/src/storage/SyncRulesBucketStorage.ts +138 -22
- package/src/storage/WriteCheckpointAPI.ts +108 -3
- package/src/storage/implementation/BucketDefinitionMapping.ts +436 -0
- package/src/storage/implementation/IncrementalReprocessingCompatibility.ts +41 -0
- package/src/storage/implementation/IncrementalReprocessingSyncConfigLog.ts +109 -0
- package/src/storage/implementation/ReplicationStreamStorageIds.ts +21 -0
- package/src/storage/storage-index.ts +10 -1
- package/src/sync/BucketChecksumState.ts +103 -21
- package/src/sync/RequestTracker.ts +20 -0
- package/src/sync/sync.ts +183 -48
- package/src/sync/util.ts +12 -0
- package/src/system/ServiceContext.ts +7 -0
- package/src/tracing/PerformanceTracer.ts +41 -1
- package/src/util/checkpointing.ts +4 -22
- package/src/util/config/compound-config-collector.ts +25 -1
- package/src/util/config/defaults.ts +2 -0
- package/src/util/config/types.ts +3 -1
- package/src/util/lsn.ts +9 -0
- package/src/util/util-index.ts +1 -0
- package/src/util/utils.ts +1 -1
- package/src/util/write-checkpoint-batcher.ts +142 -0
- package/test/src/AbstractReplicator.test.ts +115 -0
- package/test/src/checksum_cache.test.ts +22 -0
- package/test/src/config.test.ts +102 -0
- package/test/src/diagnostics.test.ts +62 -34
- package/test/src/module-loader.test.ts +33 -1
- package/test/src/routes/admin.test.ts +182 -18
- package/test/src/routes/checkpointing.test.ts +54 -0
- package/test/src/routes/mocks.ts +6 -2
- package/test/src/storage/SourceTable.test.ts +89 -0
- package/test/src/sync/BucketChecksumState.test.ts +98 -12
- package/test/src/sync/util.test.ts +13 -1
- package/test/src/util/checkpointing.test.ts +249 -0
- package/tsconfig.json +1 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/PersistedSyncRulesContent.d.ts +0 -59
- package/dist/storage/PersistedSyncRulesContent.js +0 -100
- package/dist/storage/PersistedSyncRulesContent.js.map +0 -1
- package/src/storage/PersistedSyncRulesContent.ts +0 -174
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import * as sqlite from 'node:sqlite';
|
|
2
2
|
|
|
3
3
|
import { ErrorCode, errors, router, schema } from '@powersync/lib-services-framework';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
HydratedSyncConfig,
|
|
6
|
+
nodeSqlite,
|
|
7
|
+
SourceSchema,
|
|
8
|
+
SqlSyncRules,
|
|
9
|
+
StaticSchema
|
|
10
|
+
} from '@powersync/service-sync-rules';
|
|
5
11
|
import { internal_routes } from '@powersync/service-types';
|
|
6
12
|
|
|
7
13
|
import { DEFAULT_HYDRATION_STATE } from '@powersync/service-sync-rules';
|
|
@@ -65,20 +71,35 @@ export const diagnostics = routeDefinition({
|
|
|
65
71
|
const {
|
|
66
72
|
storageEngine: { activeBucketStorage }
|
|
67
73
|
} = service_context;
|
|
68
|
-
const active = await activeBucketStorage.
|
|
69
|
-
const
|
|
74
|
+
const active = await activeBucketStorage.getActiveSyncConfig();
|
|
75
|
+
const deploying = await activeBucketStorage.getDeployingSyncConfig();
|
|
70
76
|
|
|
71
|
-
const active_status = await api.getSyncRulesStatus(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
const active_status = await api.getSyncRulesStatus(
|
|
78
|
+
apiHandler,
|
|
79
|
+
active?.content ?? null,
|
|
80
|
+
{
|
|
81
|
+
include_content,
|
|
82
|
+
check_connection: status.connected,
|
|
83
|
+
live_status: true,
|
|
84
|
+
active: true
|
|
85
|
+
},
|
|
86
|
+
active?.storage
|
|
87
|
+
);
|
|
76
88
|
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
const deploying_status =
|
|
90
|
+
deploying == null
|
|
91
|
+
? undefined
|
|
92
|
+
: await api.getSyncRulesStatus(
|
|
93
|
+
apiHandler,
|
|
94
|
+
deploying.content,
|
|
95
|
+
{
|
|
96
|
+
include_content,
|
|
97
|
+
check_connection: status.connected,
|
|
98
|
+
live_status: true,
|
|
99
|
+
active: false
|
|
100
|
+
},
|
|
101
|
+
deploying.storage
|
|
102
|
+
);
|
|
82
103
|
|
|
83
104
|
return internal_routes.DiagnosticsResponse.encode({
|
|
84
105
|
connections: [
|
|
@@ -89,7 +110,7 @@ export const diagnostics = routeDefinition({
|
|
|
89
110
|
}
|
|
90
111
|
],
|
|
91
112
|
active_sync_rules: active_status,
|
|
92
|
-
deploying_sync_rules:
|
|
113
|
+
deploying_sync_rules: deploying_status
|
|
93
114
|
});
|
|
94
115
|
}
|
|
95
116
|
});
|
|
@@ -119,12 +140,16 @@ export const reprocess = routeDefinition({
|
|
|
119
140
|
storageEngine: { activeBucketStorage }
|
|
120
141
|
} = service_context;
|
|
121
142
|
const apiHandler = service_context.routerEngine.getAPI();
|
|
122
|
-
const next = await activeBucketStorage.
|
|
143
|
+
const next = await activeBucketStorage.getDeployingSyncConfig();
|
|
123
144
|
if (next != null) {
|
|
124
|
-
throw new
|
|
145
|
+
throw new errors.ServiceError({
|
|
146
|
+
status: 409,
|
|
147
|
+
code: ErrorCode.PSYNC_S4106,
|
|
148
|
+
description: 'Busy processing sync config - cannot reprocess'
|
|
149
|
+
});
|
|
125
150
|
}
|
|
126
151
|
|
|
127
|
-
const active = await activeBucketStorage.
|
|
152
|
+
const active = await activeBucketStorage.getActiveSyncConfig();
|
|
128
153
|
if (active == null) {
|
|
129
154
|
throw new errors.ServiceError({
|
|
130
155
|
status: 422,
|
|
@@ -132,13 +157,12 @@ export const reprocess = routeDefinition({
|
|
|
132
157
|
description: 'No active sync config'
|
|
133
158
|
});
|
|
134
159
|
}
|
|
135
|
-
|
|
136
160
|
// There are some differences between this and using asUpdateOptions():
|
|
137
161
|
// 1. This always re-parses the source YAML. If there are changes to the sync stream compiler, that can affect the sync plan.
|
|
138
162
|
// 2. If the source does not set the storage version, this will update it do the current version.
|
|
139
163
|
// We can consider tweaking this behavior in the future.
|
|
140
164
|
const new_rules = await activeBucketStorage.updateSyncRules(
|
|
141
|
-
storage.updateSyncRulesFromYaml(active.
|
|
165
|
+
storage.updateSyncRulesFromYaml(active.content.sync_rules_content, {
|
|
142
166
|
// This sync config already passed validation. But if the config is not valid anymore due
|
|
143
167
|
// to a service change, we do want to report the error here.
|
|
144
168
|
validate: true
|
|
@@ -153,44 +177,47 @@ export const reprocess = routeDefinition({
|
|
|
153
177
|
// Previously the connection was asserted with `!`
|
|
154
178
|
tag: baseConfig.tag,
|
|
155
179
|
id: baseConfig.id,
|
|
156
|
-
slot_name: new_rules.
|
|
180
|
+
slot_name: new_rules.replicationStreamName
|
|
157
181
|
}
|
|
158
182
|
]
|
|
159
183
|
});
|
|
160
184
|
}
|
|
161
185
|
});
|
|
162
186
|
|
|
163
|
-
class FakeSyncRulesContentForValidation extends storage.
|
|
187
|
+
class FakeSyncRulesContentForValidation extends storage.PersistedSyncConfigContent {
|
|
164
188
|
constructor(
|
|
165
189
|
private readonly apiHandler: api.RouteAPI,
|
|
166
190
|
private readonly schema: SourceSchema,
|
|
167
|
-
data: storage.
|
|
191
|
+
data: storage.PersistedSyncConfigContentData
|
|
168
192
|
) {
|
|
169
193
|
super(data);
|
|
170
194
|
}
|
|
171
195
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
196
|
+
parsed(options: storage.ParseSyncConfigOptions): storage.ParsedSyncConfigSet {
|
|
197
|
+
const syncConfig = SqlSyncRules.fromYaml(this.sync_rules_content, {
|
|
198
|
+
...this.apiHandler.getParseSyncRulesOptions(),
|
|
199
|
+
schema: this.schema
|
|
200
|
+
});
|
|
177
201
|
|
|
178
|
-
|
|
202
|
+
let hydrated: HydratedSyncConfig | undefined;
|
|
179
203
|
return {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
schema: this.schema
|
|
184
|
-
}),
|
|
204
|
+
replicationStreamId: this.replicationStreamId,
|
|
205
|
+
replicationStreamName: this.replicationStreamName,
|
|
206
|
+
syncConfigs: [syncConfig],
|
|
185
207
|
hydrationState: DEFAULT_HYDRATION_STATE,
|
|
186
|
-
hydratedSyncConfig() {
|
|
187
|
-
|
|
208
|
+
get hydratedSyncConfig(): HydratedSyncConfig {
|
|
209
|
+
hydrated ??= syncConfig.config.hydrate({
|
|
188
210
|
hydrationState: DEFAULT_HYDRATION_STATE,
|
|
189
211
|
sqlite: nodeSqlite(sqlite)
|
|
190
212
|
});
|
|
213
|
+
return hydrated;
|
|
191
214
|
}
|
|
192
215
|
};
|
|
193
216
|
}
|
|
217
|
+
|
|
218
|
+
async getSyncConfigStatus(): Promise<storage.PersistedSyncConfigStatus | null> {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
194
221
|
}
|
|
195
222
|
|
|
196
223
|
export const validate = routeDefinition({
|
|
@@ -210,13 +237,12 @@ export const validate = routeDefinition({
|
|
|
210
237
|
|
|
211
238
|
const sync_rules = new FakeSyncRulesContentForValidation(apiHandler, schema, {
|
|
212
239
|
// Dummy values
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
active: false,
|
|
216
|
-
last_checkpoint_lsn: '',
|
|
240
|
+
replicationStreamId: 0,
|
|
241
|
+
replicationStreamName: '',
|
|
217
242
|
storageVersion: storage.LEGACY_STORAGE_VERSION,
|
|
218
243
|
sync_rules_content: content,
|
|
219
|
-
compiled_plan: null
|
|
244
|
+
compiled_plan: null,
|
|
245
|
+
syncConfigState: storage.SyncRuleState.PROCESSING
|
|
220
246
|
});
|
|
221
247
|
|
|
222
248
|
const connectionStatus = await apiHandler.getConnectionStatus();
|
|
@@ -227,16 +253,12 @@ export const validate = routeDefinition({
|
|
|
227
253
|
});
|
|
228
254
|
}
|
|
229
255
|
|
|
230
|
-
const status = (await api.getSyncRulesStatus(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
check_connection: connectionStatus.connected,
|
|
237
|
-
live_status: false
|
|
238
|
-
}
|
|
239
|
-
))!;
|
|
256
|
+
const status = (await api.getSyncRulesStatus(apiHandler, sync_rules, {
|
|
257
|
+
include_content: false,
|
|
258
|
+
check_connection: connectionStatus.connected,
|
|
259
|
+
live_status: false,
|
|
260
|
+
active: false
|
|
261
|
+
}))!;
|
|
240
262
|
|
|
241
263
|
if (connectionStatus == null) {
|
|
242
264
|
status.errors.push({ level: 'fatal', message: 'No connection configured', ts: new Date().toISOString() });
|
|
@@ -1,10 +1,49 @@
|
|
|
1
|
-
import { logger, router, schema } from '@powersync/lib-services-framework';
|
|
1
|
+
import { codecs, logger, router, schema } from '@powersync/lib-services-framework';
|
|
2
2
|
import * as t from 'ts-codec';
|
|
3
3
|
|
|
4
4
|
import * as util from '../../util/util-index.js';
|
|
5
5
|
import { authUser } from '../auth.js';
|
|
6
6
|
import { routeDefinition } from '../router.js';
|
|
7
7
|
|
|
8
|
+
const CHECKPOINT_REQUEST_ID_MAX = 9_223_372_036_854_775_807n;
|
|
9
|
+
|
|
10
|
+
function isValidCheckpointRequestId(value: bigint) {
|
|
11
|
+
return value > 0n && value <= CHECKPOINT_REQUEST_ID_MAX;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const CheckpointRequestPayload = t.object({
|
|
15
|
+
client_id: t.string,
|
|
16
|
+
// Positive int64, matching the managed write checkpoint id. Clients should
|
|
17
|
+
// send values larger than Number.MAX_SAFE_INTEGER as strings, since JSON
|
|
18
|
+
// numbers are only validated up to the safe-integer range.
|
|
19
|
+
checkpoint_request_id: codecs.bigint
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const CheckpointRequestPayloadShapeValidator = schema.createTsCodecValidator(CheckpointRequestPayload, {
|
|
23
|
+
allowAdditional: true
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const CheckpointRequestPayloadValidator = {
|
|
27
|
+
validate(params: t.Encoded<typeof CheckpointRequestPayload>) {
|
|
28
|
+
const shapeValidation = CheckpointRequestPayloadShapeValidator.validate(params);
|
|
29
|
+
if (!shapeValidation.valid) {
|
|
30
|
+
return shapeValidation;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const decodedParams = CheckpointRequestPayload.decode(params);
|
|
34
|
+
if (!isValidCheckpointRequestId(decodedParams.checkpoint_request_id)) {
|
|
35
|
+
return {
|
|
36
|
+
valid: false as const,
|
|
37
|
+
errors: [`Expected checkpoint_request_id between 1 and ${CHECKPOINT_REQUEST_ID_MAX}`]
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
valid: true as const
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
8
47
|
const WriteCheckpointRequest = t.object({
|
|
9
48
|
client_id: t.string.optional()
|
|
10
49
|
});
|
|
@@ -24,13 +63,13 @@ export const writeCheckpoint = routeDefinition({
|
|
|
24
63
|
// Since we don't use LSNs anymore, the only way to get that is to wait.
|
|
25
64
|
const start = Date.now();
|
|
26
65
|
|
|
27
|
-
const head = await apiHandler.createReplicationHead(async (head) => head);
|
|
66
|
+
const head = await apiHandler.createReplicationHead(async (head) => ({ response: head, shouldAdvance: true }));
|
|
28
67
|
|
|
29
68
|
const timeout = 50_000;
|
|
30
69
|
|
|
31
70
|
logger.info(`Waiting for LSN checkpoint: ${head}`);
|
|
32
71
|
while (Date.now() - start < timeout) {
|
|
33
|
-
const bucketStorage = await service_context.storageEngine.activeBucketStorage.
|
|
72
|
+
const bucketStorage = (await service_context.storageEngine.activeBucketStorage.getActiveSyncConfig())?.storage;
|
|
34
73
|
const cp = await bucketStorage?.getCheckpoint();
|
|
35
74
|
if (cp == null) {
|
|
36
75
|
throw new Error('No sync config available');
|
|
@@ -54,13 +93,10 @@ export const writeCheckpoint2 = routeDefinition({
|
|
|
54
93
|
handler: async (payload) => {
|
|
55
94
|
const { token_payload, service_context } = payload.context;
|
|
56
95
|
|
|
57
|
-
const apiHandler = service_context.routerEngine.getAPI();
|
|
58
|
-
|
|
59
96
|
const { replicationHead, writeCheckpoint } = await util.createWriteCheckpoint({
|
|
60
97
|
userId: token_payload!.userIdString,
|
|
61
98
|
clientId: payload.params.client_id,
|
|
62
|
-
|
|
63
|
-
storage: service_context.storageEngine.activeBucketStorage
|
|
99
|
+
batcher: service_context.writeCheckpointBatcher
|
|
64
100
|
});
|
|
65
101
|
|
|
66
102
|
logger.info(
|
|
@@ -73,4 +109,39 @@ export const writeCheckpoint2 = routeDefinition({
|
|
|
73
109
|
}
|
|
74
110
|
});
|
|
75
111
|
|
|
76
|
-
export const
|
|
112
|
+
export const checkpointRequest = routeDefinition({
|
|
113
|
+
path: '/sync/checkpoint-request',
|
|
114
|
+
method: router.HTTPMethod.POST,
|
|
115
|
+
authorize: authUser,
|
|
116
|
+
validator: CheckpointRequestPayloadValidator,
|
|
117
|
+
handler: async (request) => {
|
|
118
|
+
const { token_payload, service_context } = request.context;
|
|
119
|
+
const { params } = request;
|
|
120
|
+
|
|
121
|
+
const decodedParams = CheckpointRequestPayload.decode(params);
|
|
122
|
+
|
|
123
|
+
// Storage only applies supplied request ids that advance the stored managed checkpoint.
|
|
124
|
+
// Stale or duplicate ids return the stored checkpoint; storage decides whether
|
|
125
|
+
// the matched checkpoint still needs a source marker.
|
|
126
|
+
const { replicationHead, writeCheckpoint } = await util.createWriteCheckpoint({
|
|
127
|
+
userId: token_payload!.userIdString,
|
|
128
|
+
clientId: decodedParams.client_id,
|
|
129
|
+
batcher: service_context.writeCheckpointBatcher,
|
|
130
|
+
checkpointRequestId: decodedParams.checkpoint_request_id
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
logger.info(
|
|
134
|
+
`Requested checkpoint for ${token_payload!.userIdString}/${decodedParams.client_id}: ${writeCheckpoint} | ${replicationHead}`
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
// Return the checkpoint request id storage is actually at after this request.
|
|
138
|
+
// When an earlier request has already advanced the stored value beyond the
|
|
139
|
+
// supplied checkpoint_request_id, this returns that larger previous value so
|
|
140
|
+
// the client can treat the response as a stale-request acknowledgement.
|
|
141
|
+
return {
|
|
142
|
+
checkpoint_request_id: String(writeCheckpoint)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
export const CHECKPOINT_ROUTES = [checkpointRequest, writeCheckpoint, writeCheckpoint2];
|
|
@@ -71,7 +71,7 @@ export const syncStreamReactive: SocketRouteGenerator = (router) =>
|
|
|
71
71
|
storageEngine: { activeBucketStorage }
|
|
72
72
|
} = service_context;
|
|
73
73
|
|
|
74
|
-
const bucketStorage = await activeBucketStorage.
|
|
74
|
+
const bucketStorage = (await activeBucketStorage.getActiveSyncConfig())?.storage;
|
|
75
75
|
if (bucketStorage == null) {
|
|
76
76
|
responder.onError(
|
|
77
77
|
new errors.ServiceError({
|
|
@@ -163,8 +163,9 @@ export const syncStreamReactive: SocketRouteGenerator = (router) =>
|
|
|
163
163
|
} catch (ex) {
|
|
164
164
|
// Convert to our standard form before responding.
|
|
165
165
|
// This ensures the error can be serialized.
|
|
166
|
+
// However, use the original error for the logs, so that we have the stack trace.
|
|
166
167
|
const error = new errors.InternalServerError(ex);
|
|
167
|
-
logger.error('Sync stream error',
|
|
168
|
+
logger.error('Sync stream error', ex);
|
|
168
169
|
closeReason ??= 'stream error';
|
|
169
170
|
responder.onError(error);
|
|
170
171
|
} finally {
|
|
@@ -73,7 +73,7 @@ export const deploySyncRules = routeDefinition({
|
|
|
73
73
|
const sync_rules = await storageEngine.activeBucketStorage.updateSyncRules(updateSyncRulesFromConfig(syncConfig));
|
|
74
74
|
|
|
75
75
|
return {
|
|
76
|
-
slot_name: sync_rules.
|
|
76
|
+
slot_name: sync_rules.replicationStreamName
|
|
77
77
|
};
|
|
78
78
|
}
|
|
79
79
|
});
|
|
@@ -110,8 +110,8 @@ export const currentSyncRules = routeDefinition({
|
|
|
110
110
|
storageEngine: { activeBucketStorage }
|
|
111
111
|
} = service_context;
|
|
112
112
|
|
|
113
|
-
const
|
|
114
|
-
if (!
|
|
113
|
+
const active = await activeBucketStorage.getActiveSyncConfig();
|
|
114
|
+
if (!active) {
|
|
115
115
|
throw new errors.ServiceError({
|
|
116
116
|
status: 422,
|
|
117
117
|
code: ErrorCode.PSYNC_S4104,
|
|
@@ -119,15 +119,16 @@ export const currentSyncRules = routeDefinition({
|
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
const sync_rules = active.content;
|
|
122
123
|
const apiHandler = service_context.routerEngine.getAPI();
|
|
123
124
|
const info = await debugSyncRules(apiHandler, sync_rules.sync_rules_content);
|
|
124
|
-
const next = await activeBucketStorage.
|
|
125
|
+
const next = await activeBucketStorage.getDeployingSyncConfig();
|
|
125
126
|
|
|
126
|
-
const next_info = next ? await debugSyncRules(apiHandler, next.sync_rules_content) : null;
|
|
127
|
+
const next_info = next ? await debugSyncRules(apiHandler, next.content.sync_rules_content) : null;
|
|
127
128
|
|
|
128
129
|
const response = {
|
|
129
130
|
current: {
|
|
130
|
-
slot_name: sync_rules.
|
|
131
|
+
slot_name: sync_rules.replicationStreamName,
|
|
131
132
|
content: sync_rules.sync_rules_content,
|
|
132
133
|
...info
|
|
133
134
|
},
|
|
@@ -135,8 +136,8 @@ export const currentSyncRules = routeDefinition({
|
|
|
135
136
|
next == null
|
|
136
137
|
? null
|
|
137
138
|
: {
|
|
138
|
-
slot_name: next.
|
|
139
|
-
content: next.sync_rules_content,
|
|
139
|
+
slot_name: next.content.replicationStreamName,
|
|
140
|
+
content: next.content.sync_rules_content,
|
|
140
141
|
...next_info
|
|
141
142
|
}
|
|
142
143
|
};
|
|
@@ -156,9 +157,17 @@ export const reprocessSyncRules = routeDefinition({
|
|
|
156
157
|
const {
|
|
157
158
|
storageEngine: { activeBucketStorage }
|
|
158
159
|
} = payload.context.service_context;
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
160
|
+
const next = await activeBucketStorage.getDeployingSyncConfig();
|
|
161
|
+
if (next != null) {
|
|
162
|
+
throw new errors.ServiceError({
|
|
163
|
+
status: 409,
|
|
164
|
+
code: ErrorCode.PSYNC_S4106,
|
|
165
|
+
description: 'Busy processing sync config - cannot reprocess'
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const active = await activeBucketStorage.getActiveSyncConfig();
|
|
170
|
+
if (active == null) {
|
|
162
171
|
throw new errors.ServiceError({
|
|
163
172
|
status: 422,
|
|
164
173
|
code: ErrorCode.PSYNC_S4104,
|
|
@@ -166,15 +175,16 @@ export const reprocessSyncRules = routeDefinition({
|
|
|
166
175
|
});
|
|
167
176
|
}
|
|
168
177
|
|
|
178
|
+
const sync_rules = active.content;
|
|
169
179
|
const new_rules = await activeBucketStorage.updateSyncRules(
|
|
170
|
-
updateSyncRulesFromYaml(sync_rules.
|
|
180
|
+
updateSyncRulesFromYaml(sync_rules.sync_rules_content, {
|
|
171
181
|
// This sync config already passed validation. But if the rules are not valid anymore due
|
|
172
182
|
// to a service change, we do want to report the error here.
|
|
173
183
|
validate: true
|
|
174
184
|
})
|
|
175
185
|
);
|
|
176
186
|
return {
|
|
177
|
-
slot_name: new_rules.
|
|
187
|
+
slot_name: new_rules.replicationStreamName
|
|
178
188
|
};
|
|
179
189
|
}
|
|
180
190
|
});
|
|
@@ -62,7 +62,7 @@ export const syncStreamed = routeDefinition({
|
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
const bucketStorage = await storageEngine.activeBucketStorage.
|
|
65
|
+
const bucketStorage = (await storageEngine.activeBucketStorage.getActiveSyncConfig())?.storage;
|
|
66
66
|
|
|
67
67
|
if (bucketStorage == null) {
|
|
68
68
|
throw new errors.ServiceError({
|
package/src/runner/teardown.ts
CHANGED
|
@@ -40,22 +40,22 @@ async function terminateSyncRules(storageFactory: storage.BucketStorageFactory,
|
|
|
40
40
|
const locks: storage.ReplicationLock[] = [];
|
|
41
41
|
while (Date.now() - start < 120_000) {
|
|
42
42
|
let retry = false;
|
|
43
|
-
const
|
|
43
|
+
const replicatingStreams = await storageFactory.getReplicatingReplicationStreams();
|
|
44
44
|
// Lock all the replicating replication streams
|
|
45
|
-
for (const
|
|
46
|
-
const lock = await
|
|
45
|
+
for (const replicatingStream of replicatingStreams) {
|
|
46
|
+
const lock = await replicatingStream.lock();
|
|
47
47
|
locks.push(lock);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
const
|
|
51
|
-
const
|
|
50
|
+
const stoppedStreams = await storageFactory.getStoppedReplicationStreams();
|
|
51
|
+
const combinedStreams = [...replicatingStreams, ...stoppedStreams];
|
|
52
52
|
try {
|
|
53
53
|
// Clean up any module specific configuration for the replication stream
|
|
54
|
-
await moduleManager.tearDown({ syncRules:
|
|
54
|
+
await moduleManager.tearDown({ replicationStreams: combinedStreams, syncRules: combinedStreams });
|
|
55
55
|
|
|
56
56
|
// Mark the replication stream as terminated
|
|
57
|
-
for (let
|
|
58
|
-
const syncRulesStorage = storageFactory.getInstance(
|
|
57
|
+
for (let replicationStream of combinedStreams) {
|
|
58
|
+
const syncRulesStorage = storageFactory.getInstance(replicationStream);
|
|
59
59
|
// The storage will be dropped at the end of the teardown, so we don't need to clear it here
|
|
60
60
|
await syncRulesStorage.terminate({ clearStorage: false });
|
|
61
61
|
}
|
|
@@ -12,6 +12,15 @@ export const DEFAULT_BUCKET_BATCH_COMMIT_OPTIONS: ResolvedBucketBatchCommitOptio
|
|
|
12
12
|
oldestUncommittedChange: null
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Storage writer for a source-specific unit of replication work.
|
|
17
|
+
*
|
|
18
|
+
* A batch evaluates source row changes against the hydrated sync config,
|
|
19
|
+
* persists bucket operations and parameter lookup rows, tracks source table and
|
|
20
|
+
* snapshot state, records the committed source position for visible
|
|
21
|
+
* checkpoints, and stores restart cursors for sources with PowerSync-managed
|
|
22
|
+
* resume state.
|
|
23
|
+
*/
|
|
15
24
|
export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageListener>, AsyncDisposable {
|
|
16
25
|
/**
|
|
17
26
|
* Alias for [Symbol.asyncDispose]
|
|
@@ -29,21 +38,28 @@ export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageLis
|
|
|
29
38
|
readonly skipExistingRows: boolean;
|
|
30
39
|
|
|
31
40
|
/**
|
|
32
|
-
*
|
|
41
|
+
* Queue one normalized source row operation and potentially flush.
|
|
33
42
|
*
|
|
34
|
-
*
|
|
43
|
+
* When flushed, the operation is evaluated against data and parameter
|
|
44
|
+
* queries. That may produce bucket operations, parameter lookup rows, and
|
|
45
|
+
* current-data updates. `save()` may trigger that flush automatically when
|
|
46
|
+
* the batch reaches its size limits, but a flush does not create a checkpoint
|
|
47
|
+
* by itself.
|
|
35
48
|
*/
|
|
36
49
|
save(record: SaveOptions): Promise<FlushedResult | null>;
|
|
37
50
|
|
|
38
51
|
/**
|
|
39
|
-
* Replicate a truncate
|
|
52
|
+
* Replicate a truncate operation by removing all currently replicated rows
|
|
53
|
+
* for the specified source tables.
|
|
40
54
|
*/
|
|
41
55
|
truncate(sourceTables: SourceTable[]): Promise<FlushedResult | null>;
|
|
42
56
|
|
|
43
57
|
/**
|
|
44
|
-
* Drop one or more
|
|
58
|
+
* Drop one or more source table mappings.
|
|
45
59
|
*
|
|
46
|
-
* This
|
|
60
|
+
* This performs the same logical row removal as `truncate()`, then removes
|
|
61
|
+
* the stored `SourceTable` record. It is used for table drops, renames,
|
|
62
|
+
* replica identity changes, and superseded mappings.
|
|
47
63
|
*/
|
|
48
64
|
drop(sourceTables: SourceTable[]): Promise<FlushedResult | null>;
|
|
49
65
|
|
|
@@ -58,21 +74,26 @@ export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageLis
|
|
|
58
74
|
flush(options?: BatchBucketFlushOptions): Promise<FlushedResult | null>;
|
|
59
75
|
|
|
60
76
|
/**
|
|
61
|
-
* Flush
|
|
77
|
+
* Flush saved ops and advance the committed source checkpoint position.
|
|
62
78
|
*
|
|
63
|
-
* Only call this
|
|
79
|
+
* Only call this at a source transaction, page, snapshot, or other boundary
|
|
80
|
+
* where it is valid for clients to observe all flushed changes together. A
|
|
81
|
+
* single commit may include multiple complete source transactions, but must
|
|
82
|
+
* not split one source transaction across checkpoint boundaries.
|
|
64
83
|
*/
|
|
65
84
|
commit(lsn: string, options?: BucketBatchCommitOptions): Promise<CheckpointResult>;
|
|
66
85
|
|
|
67
86
|
/**
|
|
68
|
-
* Advance the checkpoint
|
|
87
|
+
* Advance the committed source checkpoint position without associated row
|
|
88
|
+
* operations.
|
|
69
89
|
*
|
|
70
|
-
*
|
|
90
|
+
* Use this for source heartbeats, marker-only events, and idle-source write
|
|
91
|
+
* checkpoint progress. This must only be called outside a source transaction.
|
|
71
92
|
*/
|
|
72
93
|
keepalive(lsn: string): Promise<CheckpointResult>;
|
|
73
94
|
|
|
74
95
|
/**
|
|
75
|
-
* Set the
|
|
96
|
+
* Set the source position that replication should resume from after restart.
|
|
76
97
|
*
|
|
77
98
|
* This can be used for:
|
|
78
99
|
* 1. Setting the LSN for a snapshot, before starting replication.
|
|
@@ -80,14 +101,13 @@ export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageLis
|
|
|
80
101
|
*
|
|
81
102
|
* Not required if the source database keeps track of this, for example with
|
|
82
103
|
* PostgreSQL logical replication slots.
|
|
104
|
+
*
|
|
105
|
+
* This is for restart safety only. If the same source position should become
|
|
106
|
+
* visible to clients, also call `commit()` or `keepalive()` at a valid source
|
|
107
|
+
* boundary.
|
|
83
108
|
*/
|
|
84
109
|
setResumeLsn(lsn: string): Promise<void>;
|
|
85
110
|
|
|
86
|
-
/**
|
|
87
|
-
* Get the last checkpoint LSN, from either commit or keepalive.
|
|
88
|
-
*/
|
|
89
|
-
lastCheckpointLsn: string | null;
|
|
90
|
-
|
|
91
111
|
/**
|
|
92
112
|
* LSN to resume from.
|
|
93
113
|
*
|
|
@@ -95,7 +115,21 @@ export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageLis
|
|
|
95
115
|
*/
|
|
96
116
|
resumeFromLsn: string | null;
|
|
97
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Mark table snapshots complete and record the source position that
|
|
120
|
+
* checkpoints must not precede for those tables.
|
|
121
|
+
*/
|
|
98
122
|
markTableSnapshotDone(tables: SourceTable[], no_checkpoint_before_lsn?: string): Promise<SourceTable[]>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Mark a source table as requiring snapshot work again.
|
|
126
|
+
*
|
|
127
|
+
* Use this when a table that was previously considered snapshot-complete
|
|
128
|
+
* must be copied again before it can safely contribute to checkpoints. Common
|
|
129
|
+
* cases include detected schema or replica identity changes, interrupted
|
|
130
|
+
* re-snapshot work, or other source metadata changes that make the existing
|
|
131
|
+
* replicated rows no longer trustworthy.
|
|
132
|
+
*/
|
|
99
133
|
markTableSnapshotRequired(table: SourceTable): Promise<void>;
|
|
100
134
|
|
|
101
135
|
/**
|
|
@@ -118,6 +152,10 @@ export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageLis
|
|
|
118
152
|
*/
|
|
119
153
|
markSnapshotDone(no_checkpoint_before_lsn: string, options?: { throwOnConflict?: boolean }): Promise<void>;
|
|
120
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Persist resumable table snapshot progress such as row counts or source
|
|
157
|
+
* cursors.
|
|
158
|
+
*/
|
|
121
159
|
updateTableProgress(table: SourceTable, progress: Partial<TableSnapshotStatus>): Promise<SourceTable>;
|
|
122
160
|
|
|
123
161
|
/**
|
|
@@ -125,10 +163,20 @@ export interface BucketStorageBatch extends ObserverClient<BucketBatchStorageLis
|
|
|
125
163
|
*/
|
|
126
164
|
getSourceTableStatus(table: SourceTable): Promise<SourceTable | null>;
|
|
127
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Resolve discovered source metadata against the active sync config and
|
|
168
|
+
* persisted source table state.
|
|
169
|
+
*
|
|
170
|
+
* @returns Current mappings that should receive replicated rows, plus
|
|
171
|
+
* outdated mappings that the source connector should drop.
|
|
172
|
+
*/
|
|
128
173
|
resolveTables(options: ResolveTablesOptions): Promise<ResolveTablesResult>;
|
|
129
174
|
|
|
130
175
|
/**
|
|
131
|
-
*
|
|
176
|
+
* Queue a custom checkpoint request to be persisted after operations are
|
|
177
|
+
* flushed. Set `checkpoint_requested_at` when the custom checkpoint came from
|
|
178
|
+
* a client checkpoint request and should be cleaned up by request-retention
|
|
179
|
+
* compaction; omit it for persistent source-owned checkpoints.
|
|
132
180
|
*/
|
|
133
181
|
addCustomWriteCheckpoint(checkpoint: BatchedCustomWriteCheckpointOptions): void;
|
|
134
182
|
}
|
|
@@ -216,8 +264,6 @@ export interface CheckpointResult {
|
|
|
216
264
|
/**
|
|
217
265
|
* True if a checkpoint was actually created by this operation. This can be false even if checkpointBlocked is false,
|
|
218
266
|
* if the checkpoint was empty.
|
|
219
|
-
*
|
|
220
|
-
* This is primarily used for testing.
|
|
221
267
|
*/
|
|
222
268
|
checkpointCreated: boolean;
|
|
223
269
|
}
|