@sixb/sync-worker 0.1.0 → 0.1.2
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/README.md +4 -6
- package/dist/checkpoints.d.ts +24 -0
- package/dist/checkpoints.d.ts.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +683 -207
- package/dist/index.js.map +8 -6
- package/dist/normalize.d.ts +3 -1
- package/dist/normalize.d.ts.map +1 -1
- package/dist/run-sync-job.d.ts +4 -5
- package/dist/run-sync-job.d.ts.map +1 -1
- package/dist/source-read.d.ts +21 -0
- package/dist/source-read.d.ts.map +1 -0
- package/dist/types.d.ts +20 -19
- package/dist/types.d.ts.map +1 -1
- package/dist/worker.d.ts +11 -15
- package/dist/worker.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/checkpoints.ts +200 -0
- package/src/index.ts +1 -1
- package/src/normalize.ts +44 -2
- package/src/run-sync-job.ts +409 -152
- package/src/source-read.ts +207 -0
- package/src/types.ts +27 -31
- package/src/worker.ts +128 -111
package/src/run-sync-job.ts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
|
-
assertJsonValue,
|
|
3
2
|
type BlobStorage,
|
|
4
3
|
cloneJsonValue,
|
|
4
|
+
type DatasetDefinition,
|
|
5
|
+
type DatasetRow,
|
|
5
6
|
type FileRef,
|
|
6
7
|
getDatasetRowValidationError,
|
|
7
8
|
isFileRef,
|
|
8
9
|
type JsonValue,
|
|
10
|
+
type MergeChange,
|
|
9
11
|
} from "@sixb/core"
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
import {
|
|
13
|
+
captureSixbFailure,
|
|
14
|
+
createSixbError,
|
|
15
|
+
isSixbError,
|
|
16
|
+
summarizeErrorMessage,
|
|
17
|
+
} from "@sixb/core/internal/errors"
|
|
18
|
+
import { resolveLoggingService } from "@sixb/core/internal/logging"
|
|
19
|
+
import { type DatasetVersion, getDatasetMergeChangeValidationError } from "@sixb/core/lake-storage"
|
|
20
|
+
import { SYNC_RUN_FAILURE_CODES, type SyncRunRecord } from "@sixb/core/storage"
|
|
21
|
+
import { assertDatasetRow, throwIfAborted } from "./normalize"
|
|
22
|
+
import { readSyncValues, type SyncSourceValue } from "./source-read"
|
|
23
|
+
import type { RunSyncJobInput, SyncRunFinishedHandler, SyncRunResult } from "./types"
|
|
15
24
|
|
|
16
25
|
export class SyncRunAlreadyStartedError extends Error {
|
|
17
26
|
override readonly name = "SyncRunAlreadyStartedError"
|
|
@@ -21,19 +30,6 @@ export class SyncRunAlreadyStartedError extends Error {
|
|
|
21
30
|
}
|
|
22
31
|
}
|
|
23
32
|
|
|
24
|
-
function toSyncRunFailure(error: unknown): SyncRunFailure {
|
|
25
|
-
if (error instanceof Error) {
|
|
26
|
-
return {
|
|
27
|
-
name: error.name,
|
|
28
|
-
message: error.message,
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
message: String(error),
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
33
|
function createBookkeepingError(options: {
|
|
38
34
|
syncId: string
|
|
39
35
|
runId: string
|
|
@@ -41,18 +37,64 @@ function createBookkeepingError(options: {
|
|
|
41
37
|
cause: unknown
|
|
42
38
|
}): Error {
|
|
43
39
|
// The lake commit is already durable here, so we surface an explicit repair-needed error.
|
|
44
|
-
return
|
|
40
|
+
return createSixbError(
|
|
41
|
+
"internal.unexpected",
|
|
45
42
|
`[SixbSyncWorker] Sync '${options.syncId}' committed dataset version '${options.version.versionId}', but failed to finalize sync run '${options.runId}'. The dataset commit may already have succeeded and the sync run record may need repair.`,
|
|
46
|
-
{
|
|
43
|
+
{
|
|
44
|
+
cause: options.cause,
|
|
45
|
+
details: {
|
|
46
|
+
syncId: options.syncId,
|
|
47
|
+
runId: options.runId,
|
|
48
|
+
datasetId: options.version.datasetId,
|
|
49
|
+
versionId: options.version.versionId,
|
|
50
|
+
},
|
|
51
|
+
}
|
|
47
52
|
)
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
function requireFinishedAt(
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
function requireFinishedAt(input: {
|
|
56
|
+
readonly syncId: string
|
|
57
|
+
readonly runId: string
|
|
58
|
+
readonly finishedAt: Date | undefined
|
|
59
|
+
}): Date {
|
|
60
|
+
if (input.finishedAt) {
|
|
61
|
+
return input.finishedAt
|
|
53
62
|
}
|
|
54
63
|
|
|
55
|
-
throw
|
|
64
|
+
throw createSixbError(
|
|
65
|
+
"internal.unexpected",
|
|
66
|
+
`[SixbSyncWorker] Sync run '${input.runId}' finished without a finishedAt timestamp.`,
|
|
67
|
+
{ details: { syncId: input.syncId, runId: input.runId } }
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function translateSyncExecutionError(
|
|
72
|
+
error: unknown,
|
|
73
|
+
input: {
|
|
74
|
+
readonly syncId: string
|
|
75
|
+
readonly runId: string
|
|
76
|
+
readonly datasetId: string
|
|
77
|
+
}
|
|
78
|
+
): unknown {
|
|
79
|
+
if (
|
|
80
|
+
isSixbError(error) &&
|
|
81
|
+
(error.code === "internal.unexpected" || error.code === "sync.execution_failed")
|
|
82
|
+
) {
|
|
83
|
+
return error
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return createSixbError(
|
|
87
|
+
"sync.execution_failed",
|
|
88
|
+
summarizeErrorMessage(error, "Sync execution failed."),
|
|
89
|
+
{
|
|
90
|
+
cause: error,
|
|
91
|
+
details: {
|
|
92
|
+
syncId: input.syncId,
|
|
93
|
+
runId: input.runId,
|
|
94
|
+
datasetId: input.datasetId,
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
)
|
|
56
98
|
}
|
|
57
99
|
|
|
58
100
|
async function verifyFileRef(options: {
|
|
@@ -110,27 +152,146 @@ async function verifyRowFileRefs(options: {
|
|
|
110
152
|
}
|
|
111
153
|
}
|
|
112
154
|
|
|
155
|
+
function assertMergeChange(
|
|
156
|
+
value: unknown,
|
|
157
|
+
syncId: string,
|
|
158
|
+
dataset: DatasetDefinition,
|
|
159
|
+
itemIndex: number
|
|
160
|
+
): MergeChange<DatasetRow, DatasetRow> {
|
|
161
|
+
const validationError = getDatasetMergeChangeValidationError(value, dataset)
|
|
162
|
+
if (validationError) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`[SixbSyncWorker] Sync '${syncId}' returned an invalid merge change at item ${itemIndex}. ${validationError}`
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
return value as MergeChange<DatasetRow, DatasetRow>
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function* validatedDatasetRows(options: {
|
|
171
|
+
values: AsyncIterable<SyncSourceValue>
|
|
172
|
+
signal: AbortSignal
|
|
173
|
+
blobStorage: BlobStorage
|
|
174
|
+
syncId: string
|
|
175
|
+
runId: string
|
|
176
|
+
dataset: DatasetDefinition
|
|
177
|
+
onRead(): void
|
|
178
|
+
}): AsyncIterable<DatasetRow> {
|
|
179
|
+
let itemIndex = 0
|
|
180
|
+
for await (const sourceValue of options.values) {
|
|
181
|
+
throwIfAborted(options.signal)
|
|
182
|
+
itemIndex += 1
|
|
183
|
+
|
|
184
|
+
let row: DatasetRow
|
|
185
|
+
try {
|
|
186
|
+
row = assertDatasetRow(sourceValue.value, options.syncId, itemIndex)
|
|
187
|
+
// Validate before handing rows to lake storage so sync failures include
|
|
188
|
+
// the source item index; lake storage still re-validates as the final boundary.
|
|
189
|
+
const validationError = getDatasetRowValidationError(row, options.dataset)
|
|
190
|
+
if (validationError) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`[SixbSyncWorker] Sync '${options.syncId}' returned an invalid row at item ${itemIndex}. ${validationError}`
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Lake storage validates fileRef shape; the worker owns existence checks against blob storage.
|
|
197
|
+
await verifyRowFileRefs({
|
|
198
|
+
blobStorage: options.blobStorage,
|
|
199
|
+
syncId: options.syncId,
|
|
200
|
+
dataset: options.dataset,
|
|
201
|
+
row,
|
|
202
|
+
itemIndex,
|
|
203
|
+
})
|
|
204
|
+
options.onRead()
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw sourceValidationError(error, sourceValue, options, itemIndex)
|
|
207
|
+
}
|
|
208
|
+
yield row
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function* validatedMergeChanges(options: {
|
|
213
|
+
values: AsyncIterable<SyncSourceValue>
|
|
214
|
+
signal: AbortSignal
|
|
215
|
+
blobStorage: BlobStorage
|
|
216
|
+
syncId: string
|
|
217
|
+
runId: string
|
|
218
|
+
dataset: DatasetDefinition
|
|
219
|
+
onRead(): void
|
|
220
|
+
}): AsyncIterable<MergeChange<DatasetRow, DatasetRow>> {
|
|
221
|
+
let itemIndex = 0
|
|
222
|
+
for await (const sourceValue of options.values) {
|
|
223
|
+
throwIfAborted(options.signal)
|
|
224
|
+
itemIndex += 1
|
|
225
|
+
let mergeChange: MergeChange<DatasetRow, DatasetRow>
|
|
226
|
+
try {
|
|
227
|
+
mergeChange = assertMergeChange(sourceValue.value, options.syncId, options.dataset, itemIndex)
|
|
228
|
+
if (mergeChange.kind === "upsert") {
|
|
229
|
+
await verifyRowFileRefs({
|
|
230
|
+
blobStorage: options.blobStorage,
|
|
231
|
+
syncId: options.syncId,
|
|
232
|
+
dataset: options.dataset,
|
|
233
|
+
row: mergeChange.row,
|
|
234
|
+
itemIndex,
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
options.onRead()
|
|
238
|
+
} catch (error) {
|
|
239
|
+
throw sourceValidationError(error, sourceValue, options, itemIndex)
|
|
240
|
+
}
|
|
241
|
+
yield mergeChange
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function sourceValidationError(
|
|
246
|
+
error: unknown,
|
|
247
|
+
sourceValue: SyncSourceValue,
|
|
248
|
+
options: { readonly syncId: string; readonly runId: string; readonly dataset: DatasetDefinition },
|
|
249
|
+
itemIndex: number
|
|
250
|
+
): unknown {
|
|
251
|
+
const connection = sourceValue.connection
|
|
252
|
+
if (!connection) return error
|
|
253
|
+
const code =
|
|
254
|
+
isSixbError(error) && error.code === "internal.unexpected"
|
|
255
|
+
? "internal.unexpected"
|
|
256
|
+
: "sync.execution_failed"
|
|
257
|
+
return createSixbError(code, summarizeErrorMessage(error, "Sync source validation failed."), {
|
|
258
|
+
cause: error,
|
|
259
|
+
details: {
|
|
260
|
+
syncId: options.syncId,
|
|
261
|
+
runId: options.runId,
|
|
262
|
+
datasetId: options.dataset.id,
|
|
263
|
+
itemIndex,
|
|
264
|
+
connectionId: connection.id,
|
|
265
|
+
accountId: connection.account.id,
|
|
266
|
+
},
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
interface SyncCommitResult {
|
|
271
|
+
readonly outcome: "created" | "unchanged"
|
|
272
|
+
readonly version?: DatasetVersion
|
|
273
|
+
}
|
|
274
|
+
|
|
113
275
|
/**
|
|
114
|
-
* Run one
|
|
276
|
+
* Run one already-persisted Sync run end to end.
|
|
115
277
|
*
|
|
116
|
-
* The worker
|
|
117
|
-
*
|
|
118
|
-
* the outcome. It does not own scheduling, retries, or run-id generation.
|
|
278
|
+
* Dispatch owns identity and queueing. The worker only transitions the durable run from queued to
|
|
279
|
+
* running, streams source values, and records its terminal outcome.
|
|
119
280
|
*/
|
|
120
281
|
export async function runSyncJob(input: RunSyncJobInput): Promise<SyncRunResult> {
|
|
121
|
-
const { runtime,
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
throw new Error(`[SixbSyncWorker] Unknown sync '${job.syncId}'.`)
|
|
282
|
+
const { runtime, run } = input
|
|
283
|
+
const job = {
|
|
284
|
+
id: run.id,
|
|
285
|
+
syncId: run.syncId,
|
|
286
|
+
expectedLatestVersionId: run.expectedLatestVersionId,
|
|
287
|
+
commitMessage: run.commitMessage,
|
|
128
288
|
}
|
|
289
|
+
const signal = input.signal ?? new AbortController().signal
|
|
290
|
+
const { syncRunsStorage, lakeStorage, blobs: blobStorage } = runtime
|
|
129
291
|
|
|
130
|
-
|
|
131
|
-
if (!dataset) {
|
|
292
|
+
if (run.projectId !== runtime.id) {
|
|
132
293
|
throw new Error(
|
|
133
|
-
`[SixbSyncWorker] Sync '${
|
|
294
|
+
`[SixbSyncWorker] Sync run '${run.id}' belongs to project '${run.projectId}', not '${runtime.id}'.`
|
|
134
295
|
)
|
|
135
296
|
}
|
|
136
297
|
|
|
@@ -141,31 +302,43 @@ export async function runSyncJob(input: RunSyncJobInput): Promise<SyncRunResult>
|
|
|
141
302
|
startedRun = await syncRunsStorage.start({
|
|
142
303
|
projectId: runtime.id,
|
|
143
304
|
id: job.id,
|
|
144
|
-
syncId: sync.id,
|
|
145
|
-
datasetId: dataset.id,
|
|
146
|
-
mode: sync.config.mode,
|
|
147
|
-
expectedLatestVersionId: job.expectedLatestVersionId,
|
|
148
|
-
commitMessage: job.commitMessage,
|
|
149
305
|
})
|
|
150
306
|
} catch (error) {
|
|
151
307
|
const existing = await syncRunsStorage.getById({ projectId: runtime.id, id: job.id })
|
|
152
|
-
if (existing?.syncId ===
|
|
308
|
+
if (existing?.syncId === run.syncId && existing.executionId === run.executionId) {
|
|
153
309
|
throw new SyncRunAlreadyStartedError(existing)
|
|
154
310
|
}
|
|
155
311
|
throw error
|
|
156
312
|
}
|
|
157
313
|
await input.onRunStarted?.(startedRun)
|
|
158
314
|
|
|
159
|
-
const logSession =
|
|
315
|
+
const logSession = resolveLoggingService(runtime.id, runtime.logging).startExecution({
|
|
160
316
|
kind: "sync",
|
|
161
317
|
id: job.id,
|
|
162
318
|
})
|
|
163
319
|
const logger = logSession.logger
|
|
164
|
-
let
|
|
320
|
+
let abortWrite: (() => Promise<void>) | undefined
|
|
165
321
|
let rowsRead = 0
|
|
166
322
|
let committedVersion: DatasetVersion | undefined
|
|
167
323
|
|
|
168
324
|
try {
|
|
325
|
+
const sync = runtime.syncs.getById(run.syncId)
|
|
326
|
+
if (!sync) {
|
|
327
|
+
throw new Error(`[SixbSyncWorker] Unknown sync '${run.syncId}'.`)
|
|
328
|
+
}
|
|
329
|
+
if (sync.target.dataset.id !== run.datasetId || sync.config.mode !== run.mode) {
|
|
330
|
+
throw new Error(
|
|
331
|
+
`[SixbSyncWorker] Sync run '${run.id}' no longer matches Sync '${sync.id}' configuration.`
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const dataset = runtime.datasets.getById(run.datasetId)
|
|
336
|
+
if (!dataset) {
|
|
337
|
+
throw new Error(
|
|
338
|
+
`[SixbSyncWorker] Sync '${sync.id}' targets unknown dataset '${run.datasetId}'.`
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
|
|
169
342
|
throwIfAborted(signal)
|
|
170
343
|
await lakeStorage.createDataset(dataset)
|
|
171
344
|
|
|
@@ -180,102 +353,138 @@ export async function runSyncJob(input: RunSyncJobInput): Promise<SyncRunResult>
|
|
|
180
353
|
let nextCheckpoint: JsonValue | undefined =
|
|
181
354
|
previousCheckpoint !== undefined ? cloneJsonValue(previousCheckpoint) : undefined
|
|
182
355
|
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
assertJsonValue(next, `Sync '${sync.id}' checkpoint`)
|
|
193
|
-
nextCheckpoint = cloneJsonValue(next)
|
|
194
|
-
},
|
|
195
|
-
})
|
|
196
|
-
const rows = normalizeReadResult(readResult, sync.id)
|
|
197
|
-
|
|
198
|
-
throwIfAborted(signal)
|
|
199
|
-
|
|
200
|
-
write = await lakeStorage.beginWrite({
|
|
201
|
-
dataset,
|
|
202
|
-
mode: sync.config.mode,
|
|
203
|
-
producer: {
|
|
204
|
-
kind: "sync",
|
|
205
|
-
id: sync.id,
|
|
356
|
+
const producer = {
|
|
357
|
+
kind: "sync" as const,
|
|
358
|
+
id: sync.id,
|
|
359
|
+
runId: job.id,
|
|
360
|
+
}
|
|
361
|
+
const readValues = () =>
|
|
362
|
+
readSyncValues({
|
|
363
|
+
runtime,
|
|
364
|
+
sync,
|
|
206
365
|
runId: job.id,
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
366
|
+
datasetId: dataset.id,
|
|
367
|
+
signal,
|
|
368
|
+
blobStorage,
|
|
369
|
+
logger,
|
|
370
|
+
previousCheckpoint,
|
|
371
|
+
setCheckpoint(next) {
|
|
372
|
+
nextCheckpoint = next === undefined ? undefined : cloneJsonValue(next)
|
|
373
|
+
},
|
|
374
|
+
})
|
|
375
|
+
const onRead = () => {
|
|
376
|
+
rowsRead += 1
|
|
377
|
+
}
|
|
378
|
+
const commitMessage = job.commitMessage ?? `sync ${sync.id} run ${job.id}`
|
|
379
|
+
let commitResult: SyncCommitResult
|
|
380
|
+
|
|
381
|
+
if (sync.config.mode === "merge") {
|
|
382
|
+
const mergeWrite = await lakeStorage.beginMerge({
|
|
383
|
+
dataset,
|
|
384
|
+
expectedLatestVersionId: job.expectedLatestVersionId,
|
|
385
|
+
producer,
|
|
386
|
+
})
|
|
387
|
+
abortWrite = () => mergeWrite.abort()
|
|
388
|
+
|
|
389
|
+
const values = await readValues()
|
|
390
|
+
throwIfAborted(signal)
|
|
391
|
+
await mergeWrite.writeChanges(
|
|
392
|
+
validatedMergeChanges({
|
|
393
|
+
values,
|
|
394
|
+
signal,
|
|
395
|
+
blobStorage,
|
|
396
|
+
syncId: sync.id,
|
|
397
|
+
runId: job.id,
|
|
398
|
+
dataset,
|
|
399
|
+
onRead,
|
|
400
|
+
})
|
|
401
|
+
)
|
|
402
|
+
throwIfAborted(signal)
|
|
403
|
+
|
|
404
|
+
const commit = await mergeWrite.commit({ commitMessage })
|
|
405
|
+
abortWrite = undefined
|
|
406
|
+
commitResult = {
|
|
407
|
+
outcome: commit.outcome,
|
|
408
|
+
...(commit.version ? { version: commit.version } : {}),
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
const values = await readValues()
|
|
412
|
+
throwIfAborted(signal)
|
|
227
413
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
414
|
+
const rowWrite = await lakeStorage.beginWrite({
|
|
415
|
+
dataset,
|
|
416
|
+
mode: sync.config.mode,
|
|
417
|
+
producer,
|
|
418
|
+
})
|
|
419
|
+
abortWrite = () => rowWrite.abort()
|
|
420
|
+
await rowWrite.writeRows(
|
|
421
|
+
validatedDatasetRows({
|
|
422
|
+
values,
|
|
423
|
+
signal,
|
|
424
|
+
blobStorage,
|
|
425
|
+
syncId: sync.id,
|
|
426
|
+
runId: job.id,
|
|
427
|
+
dataset,
|
|
428
|
+
onRead,
|
|
429
|
+
})
|
|
430
|
+
)
|
|
431
|
+
throwIfAborted(signal)
|
|
432
|
+
|
|
433
|
+
if (rowsRead === 0) {
|
|
434
|
+
const version = await lakeStorage.getLatestVersion(dataset.id)
|
|
435
|
+
if (sync.config.mode === "append" || !version) {
|
|
436
|
+
await rowWrite.abort()
|
|
437
|
+
abortWrite = undefined
|
|
438
|
+
const finishedRun = await syncRunsStorage.finish({
|
|
439
|
+
projectId: runtime.id,
|
|
440
|
+
id: job.id,
|
|
441
|
+
status: "succeeded",
|
|
442
|
+
rowsRead,
|
|
443
|
+
...(version
|
|
444
|
+
? { output: { datasetId: version.datasetId, versionId: version.versionId } }
|
|
445
|
+
: {}),
|
|
446
|
+
checkpoint: nextCheckpoint,
|
|
447
|
+
})
|
|
448
|
+
const finishedAt = requireFinishedAt({
|
|
231
449
|
syncId: sync.id,
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
itemIndex,
|
|
450
|
+
runId: job.id,
|
|
451
|
+
finishedAt: finishedRun.finishedAt,
|
|
235
452
|
})
|
|
453
|
+
await notifyRunFinished(input.onRunFinished, finishedRun)
|
|
236
454
|
|
|
237
|
-
|
|
238
|
-
|
|
455
|
+
return {
|
|
456
|
+
id: job.id,
|
|
457
|
+
syncId: sync.id,
|
|
458
|
+
datasetId: dataset.id,
|
|
459
|
+
mode: sync.config.mode,
|
|
460
|
+
startedAt: requireStartedAt(job.id, startedRun.startedAt),
|
|
461
|
+
finishedAt,
|
|
462
|
+
rowsRead,
|
|
463
|
+
...(version ? { version } : {}),
|
|
464
|
+
versionCreated: false,
|
|
465
|
+
}
|
|
239
466
|
}
|
|
240
|
-
}
|
|
241
|
-
)
|
|
242
|
-
|
|
243
|
-
throwIfAborted(signal)
|
|
467
|
+
}
|
|
244
468
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
const version = await lakeStorage.getLatestVersion(dataset.id)
|
|
249
|
-
const finishedRun = await syncRunsStorage.finish({
|
|
250
|
-
projectId: runtime.id,
|
|
251
|
-
id: job.id,
|
|
252
|
-
status: "succeeded",
|
|
253
|
-
rowsRead,
|
|
254
|
-
...(version
|
|
255
|
-
? { output: { datasetId: version.datasetId, versionId: version.versionId } }
|
|
256
|
-
: {}),
|
|
257
|
-
checkpoint: nextCheckpoint,
|
|
469
|
+
const { outcome, ...version } = await rowWrite.commit({
|
|
470
|
+
expectedLatestVersionId: job.expectedLatestVersionId,
|
|
471
|
+
commitMessage,
|
|
258
472
|
})
|
|
473
|
+
abortWrite = undefined
|
|
474
|
+
commitResult = { outcome, version }
|
|
475
|
+
}
|
|
259
476
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
...(version ? { version } : {}),
|
|
269
|
-
versionCreated: false,
|
|
477
|
+
const { outcome, version } = commitResult
|
|
478
|
+
if (outcome === "created") {
|
|
479
|
+
if (!version) {
|
|
480
|
+
throw createSixbError(
|
|
481
|
+
"internal.unexpected",
|
|
482
|
+
`[SixbSyncWorker] Sync '${sync.id}' created no dataset version.`,
|
|
483
|
+
{ details: { syncId: sync.id, runId: job.id, datasetId: dataset.id } }
|
|
484
|
+
)
|
|
270
485
|
}
|
|
486
|
+
committedVersion = version
|
|
271
487
|
}
|
|
272
|
-
|
|
273
|
-
const commit = await write.commit({
|
|
274
|
-
expectedLatestVersionId: job.expectedLatestVersionId,
|
|
275
|
-
commitMessage: job.commitMessage ?? `sync ${sync.id} run ${job.id}`,
|
|
276
|
-
})
|
|
277
|
-
const { outcome, ...version } = commit
|
|
278
|
-
committedVersion = version
|
|
279
488
|
let finishedRun: SyncRunRecord
|
|
280
489
|
|
|
281
490
|
try {
|
|
@@ -284,47 +493,72 @@ export async function runSyncJob(input: RunSyncJobInput): Promise<SyncRunResult>
|
|
|
284
493
|
id: job.id,
|
|
285
494
|
status: "succeeded",
|
|
286
495
|
rowsRead,
|
|
287
|
-
|
|
288
|
-
datasetId: version.datasetId,
|
|
289
|
-
|
|
290
|
-
},
|
|
496
|
+
...(version
|
|
497
|
+
? { output: { datasetId: version.datasetId, versionId: version.versionId } }
|
|
498
|
+
: {}),
|
|
291
499
|
checkpoint: nextCheckpoint,
|
|
292
500
|
})
|
|
293
501
|
} catch (error) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
502
|
+
if (committedVersion) {
|
|
503
|
+
throw createBookkeepingError({
|
|
504
|
+
syncId: sync.id,
|
|
505
|
+
runId: job.id,
|
|
506
|
+
version: committedVersion,
|
|
507
|
+
cause: error,
|
|
508
|
+
})
|
|
509
|
+
}
|
|
510
|
+
throw error
|
|
300
511
|
}
|
|
301
512
|
|
|
302
|
-
|
|
513
|
+
const finishedAt = requireFinishedAt({
|
|
514
|
+
syncId: sync.id,
|
|
515
|
+
runId: job.id,
|
|
516
|
+
finishedAt: finishedRun.finishedAt,
|
|
517
|
+
})
|
|
518
|
+
await notifyRunFinished(input.onRunFinished, finishedRun, committedVersion)
|
|
519
|
+
const result = {
|
|
303
520
|
id: job.id,
|
|
304
521
|
syncId: sync.id,
|
|
305
522
|
datasetId: dataset.id,
|
|
306
523
|
mode: sync.config.mode,
|
|
307
|
-
startedAt: startedRun.startedAt,
|
|
308
|
-
finishedAt
|
|
524
|
+
startedAt: requireStartedAt(job.id, startedRun.startedAt),
|
|
525
|
+
finishedAt,
|
|
309
526
|
rowsRead,
|
|
310
|
-
version,
|
|
311
|
-
versionCreated: outcome === "created",
|
|
312
527
|
}
|
|
528
|
+
return version
|
|
529
|
+
? { ...result, version, versionCreated: outcome === "created" }
|
|
530
|
+
: { ...result, versionCreated: false }
|
|
313
531
|
} catch (error) {
|
|
314
532
|
if (!committedVersion) {
|
|
315
|
-
//
|
|
316
|
-
await
|
|
533
|
+
// Without a created commit, best-effort clean up any open session and the run record.
|
|
534
|
+
await abortWrite?.().catch(() => {})
|
|
317
535
|
|
|
318
536
|
const status = signal.aborted ? "cancelled" : "failed"
|
|
319
537
|
try {
|
|
320
|
-
const
|
|
538
|
+
const failureError =
|
|
539
|
+
status === "failed"
|
|
540
|
+
? translateSyncExecutionError(error, {
|
|
541
|
+
syncId: run.syncId,
|
|
542
|
+
runId: job.id,
|
|
543
|
+
datasetId: run.datasetId,
|
|
544
|
+
})
|
|
545
|
+
: error
|
|
546
|
+
const failure = captureSixbFailure(failureError, {
|
|
547
|
+
allowedCodes: SYNC_RUN_FAILURE_CODES,
|
|
548
|
+
defaultCode: status === "cancelled" ? "runtime.cancelled" : "internal.unexpected",
|
|
549
|
+
details: { syncId: run.syncId, runId: job.id, datasetId: run.datasetId },
|
|
550
|
+
})
|
|
551
|
+
const failedRun = await syncRunsStorage.finish({
|
|
321
552
|
projectId: runtime.id,
|
|
322
553
|
id: job.id,
|
|
323
554
|
status,
|
|
324
555
|
rowsRead,
|
|
325
|
-
error:
|
|
556
|
+
error: failure,
|
|
326
557
|
})
|
|
327
|
-
|
|
558
|
+
await notifyRunFinished(input.onRunFinished, failedRun)
|
|
559
|
+
if (status === "failed" && failedRun.status === "failed") {
|
|
560
|
+
input.onRunFailed?.(error, failedRun, failure)
|
|
561
|
+
}
|
|
328
562
|
} catch {
|
|
329
563
|
// The run did not transition to the requested terminal status.
|
|
330
564
|
}
|
|
@@ -335,3 +569,26 @@ export async function runSyncJob(input: RunSyncJobInput): Promise<SyncRunResult>
|
|
|
335
569
|
await logSession.flush()
|
|
336
570
|
}
|
|
337
571
|
}
|
|
572
|
+
|
|
573
|
+
async function notifyRunFinished(
|
|
574
|
+
handler: SyncRunFinishedHandler | undefined,
|
|
575
|
+
run: SyncRunRecord,
|
|
576
|
+
createdVersion?: DatasetVersion
|
|
577
|
+
): Promise<void> {
|
|
578
|
+
try {
|
|
579
|
+
await handler?.(run, createdVersion)
|
|
580
|
+
} catch (error) {
|
|
581
|
+
// The built-in event log reports lost batches itself. Anything reaching here is a broken
|
|
582
|
+
// invariant in a custom lifecycle handler and must not change an already durable outcome.
|
|
583
|
+
console.error("[SixbSyncWorker] Sync run lifecycle handler failed:", error)
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function requireStartedAt(runId: string, startedAt: Date | undefined): Date {
|
|
588
|
+
if (startedAt) return startedAt
|
|
589
|
+
throw createSixbError(
|
|
590
|
+
"internal.unexpected",
|
|
591
|
+
`[SixbSyncWorker] Sync run '${runId}' started without a startedAt timestamp.`,
|
|
592
|
+
{ details: { runId } }
|
|
593
|
+
)
|
|
594
|
+
}
|