-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathrun.ts
463 lines (421 loc) · 16.3 KB
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import EventEmitter from "events";
import Long from "long";
import * as dbadapters from "df/cli/api/dbadapters";
import { IBigQueryExecutionOptions } from "df/cli/api/dbadapters/bigquery";
import { Flags } from "df/common/flags";
import { retry } from "df/common/promises";
import { deepClone, equals } from "df/common/protos";
import { StringifiedMap, StringifiedSet } from "df/common/strings/stringifier";
import { IBigQueryOptions } from "df/core/actions/table";
import { targetStringifier } from "df/core/targets";
import { dataform } from "df/protos/ts";
const CANCEL_EVENT = "jobCancel";
const flags = {
runnerNotificationPeriodMillis: Flags.number("runner-notification-period-millis", 5000)
};
const isSuccessfulAction = (actionResult: dataform.IActionResult) =>
actionResult.status === dataform.ActionResult.ExecutionStatus.SUCCESSFUL ||
actionResult.status === dataform.ActionResult.ExecutionStatus.DISABLED;
export interface IExecutedAction {
executionAction: dataform.IExecutionAction;
actionResult: dataform.IActionResult;
}
export interface IExecutionOptions {
bigquery?: { jobPrefix?: string; actionRetryLimit?: number; dryRun?: boolean };
}
export function run(
dbadapter: dbadapters.IDbAdapter,
graph: dataform.IExecutionGraph,
executionOptions?: IExecutionOptions,
partiallyExecutedRunResult: dataform.IRunResult = {},
runnerNotificationPeriodMillis: number = flags.runnerNotificationPeriodMillis.get()
): Runner {
return new Runner(
dbadapter,
graph,
executionOptions,
partiallyExecutedRunResult,
runnerNotificationPeriodMillis
).execute();
}
export class Runner {
private readonly warehouseStateByTarget: StringifiedMap<
dataform.ITarget,
dataform.ITableMetadata
>;
private readonly allActionTargets: StringifiedSet<dataform.ITarget>;
private readonly runResult: dataform.IRunResult;
private readonly changeListeners: Array<(graph: dataform.IRunResult) => void> = [];
private readonly eEmitter: EventEmitter;
private executedActionTargets: StringifiedSet<dataform.ITarget>;
private successfullyExecutedActionTargets: StringifiedSet<dataform.ITarget>;
private pendingActions: dataform.IExecutionAction[];
private lastNotificationTimestampMillis = 0;
private stopped = false;
private cancelled = false;
private timeout: NodeJS.Timer;
private timedOut = false;
private executionTask: Promise<dataform.IRunResult>;
constructor(
private readonly dbadapter: dbadapters.IDbAdapter,
private readonly graph: dataform.IExecutionGraph,
private readonly executionOptions: IExecutionOptions = {},
partiallyExecutedRunResult: dataform.IRunResult = {},
private readonly runnerNotificationPeriodMillis: number = flags.runnerNotificationPeriodMillis.get()
) {
this.allActionTargets = new StringifiedSet<dataform.ITarget>(
targetStringifier,
graph.actions.map(action => action.target)
);
this.runResult = {
actions: [],
...partiallyExecutedRunResult
};
this.warehouseStateByTarget = new StringifiedMap(
targetStringifier,
graph.warehouseState.tables?.map(tableMetadata => [tableMetadata.target, tableMetadata])
);
this.executedActionTargets = new StringifiedSet(
targetStringifier,
this.runResult.actions
.filter(action => action.status !== dataform.ActionResult.ExecutionStatus.RUNNING)
.map(action => action.target)
);
this.successfullyExecutedActionTargets = new StringifiedSet(
targetStringifier,
this.runResult.actions.filter(isSuccessfulAction).map(action => action.target)
);
this.pendingActions = graph.actions.filter(
action => !this.executedActionTargets.has(action.target)
);
this.eEmitter = new EventEmitter();
// There could feasibly be thousands of listeners to this, 0 makes the limit infinite.
this.eEmitter.setMaxListeners(0);
}
public onChange(listener: (graph: dataform.IRunResult) => void): Runner {
this.changeListeners.push(listener);
return this;
}
public execute(): this {
if (!!this.executionTask) {
throw new Error("Executor already started.");
}
this.executionTask = this.executeGraph();
if (!!this.graph.runConfig && !!this.graph.runConfig.timeoutMillis) {
const now = Date.now();
const runStartMillis = this.runResult.timing?.startTimeMillis?.toNumber?.() || now;
const elapsedTimeMillis = now - runStartMillis;
const timeoutMillis = this.graph.runConfig.timeoutMillis - elapsedTimeMillis;
this.timeout = setTimeout(() => {
this.timedOut = true;
this.cancel();
}, timeoutMillis);
}
return this;
}
public stop() {
this.stopped = true;
}
public cancel() {
this.cancelled = true;
this.eEmitter.emit(CANCEL_EVENT, undefined, undefined);
}
public async result(): Promise<dataform.IRunResult> {
try {
return await this.executionTask;
} finally {
if (!!this.timeout) {
clearTimeout(this.timeout);
}
}
}
private notifyListeners() {
if (Date.now() - this.runnerNotificationPeriodMillis < this.lastNotificationTimestampMillis) {
return;
}
const runResultClone = deepClone(dataform.RunResult, this.runResult);
this.lastNotificationTimestampMillis = Date.now();
this.changeListeners.forEach(listener => listener(runResultClone));
}
private async executeGraph() {
const timer = Timer.start(this.runResult.timing);
this.runResult.status = dataform.RunResult.ExecutionStatus.RUNNING;
this.runResult.timing = timer.current();
this.notifyListeners();
// If we're not resuming an existing run, prepare schemas.
if (this.runResult.actions.length === 0) {
await this.prepareAllSchemas();
}
// Recursively execute all actions as they become executable.
await this.executeAllActionsReadyForExecution();
if (this.stopped) {
return this.runResult;
}
this.runResult.timing = timer.end();
this.runResult.status = dataform.RunResult.ExecutionStatus.SUCCESSFUL;
if (this.timedOut) {
this.runResult.status = dataform.RunResult.ExecutionStatus.TIMED_OUT;
} else if (this.cancelled) {
this.runResult.status = dataform.RunResult.ExecutionStatus.CANCELLED;
} else if (
this.runResult.actions.some(
action => action.status === dataform.ActionResult.ExecutionStatus.FAILED
)
) {
this.runResult.status = dataform.RunResult.ExecutionStatus.FAILED;
}
return this.runResult;
}
private async prepareAllSchemas() {
// Work out all the schemas we are going to need to create first.
const databaseSchemas = new Map<string, Set<string>>();
this.graph.actions
.filter(action => !!action.target && !!action.target.schema)
.forEach(({ target }) => {
// This field may not be present for older versions of dataform.
const trueDatabase = target.database || this.graph.projectConfig.defaultDatabase;
if (!databaseSchemas.has(trueDatabase)) {
databaseSchemas.set(trueDatabase, new Set<string>());
}
databaseSchemas.get(trueDatabase).add(target.schema);
});
// Create all nonexistent schemas.
await Promise.all(
Array.from(databaseSchemas.entries()).map(async ([database, schemas]) => {
const existingSchemas = new Set(await this.dbadapter.schemas(database));
await Promise.all(
Array.from(schemas)
.filter(schema => !existingSchemas.has(schema))
.map(schema => this.dbadapter.createSchema(database, schema))
);
})
);
}
private async executeAllActionsReadyForExecution() {
if (this.stopped) {
return;
}
// If the run has been cancelled, cancel all pending actions.
if (this.cancelled) {
const allPendingActions = this.pendingActions;
this.pendingActions = [];
allPendingActions.forEach(pendingAction =>
this.runResult.actions.push({
target: pendingAction.target,
status: dataform.ActionResult.ExecutionStatus.SKIPPED,
tasks: pendingAction.tasks.map(() => ({
status: dataform.TaskResult.ExecutionStatus.SKIPPED
}))
})
);
this.notifyListeners();
return;
}
const executableActions = [];
const skippableActions = [];
const stillPendingActions = [];
for (const pendingAction of this.pendingActions) {
if (
// An action is executable if all dependencies either: do not exist in the graph, or
// have executed successfully.
pendingAction.dependencyTargets.every(
dependency =>
!this.allActionTargets.has(dependency) ||
this.successfullyExecutedActionTargets.has(dependency)
)
) {
executableActions.push(pendingAction);
} else if (
// An action is skippable if it is not executable and all dependencies either: do not
// exist in the graph, or have completed execution.
pendingAction.dependencyTargets.every(
dependency =>
!this.allActionTargets.has(dependency) || this.executedActionTargets.has(dependency)
)
) {
skippableActions.push(pendingAction);
} else {
// Otherwise, the action is still pending.
stillPendingActions.push(pendingAction);
}
}
this.pendingActions = stillPendingActions;
await Promise.all([
(async () => {
skippableActions.forEach(skippableAction => {
this.runResult.actions.push({
target: skippableAction.target,
status: dataform.ActionResult.ExecutionStatus.SKIPPED,
tasks: skippableAction.tasks.map(() => ({
status: dataform.TaskResult.ExecutionStatus.SKIPPED
}))
});
});
if (skippableActions.length > 0) {
this.notifyListeners();
await this.executeAllActionsReadyForExecution();
}
})(),
Promise.all(
executableActions.map(async executableAction => {
const actionResult = await this.executeAction(executableAction);
this.executedActionTargets.add(executableAction.target);
if (isSuccessfulAction(actionResult)) {
this.successfullyExecutedActionTargets.add(executableAction.target);
}
await this.executeAllActionsReadyForExecution();
})
)
]);
}
private async executeAction(action: dataform.IExecutionAction): Promise<dataform.IActionResult> {
let actionResult: dataform.IActionResult = {
target: action.target,
tasks: []
};
if (action.tasks.length === 0) {
actionResult.status = dataform.ActionResult.ExecutionStatus.DISABLED;
this.runResult.actions.push(actionResult);
this.notifyListeners();
return actionResult;
}
const resumedActionResult = this.runResult.actions.find(existingActionResult =>
equals(dataform.Target, existingActionResult.target, action.target)
);
if (resumedActionResult) {
actionResult = resumedActionResult;
} else {
this.runResult.actions.push(actionResult);
}
actionResult.status = dataform.ActionResult.ExecutionStatus.RUNNING;
const timer = Timer.start(resumedActionResult?.timing);
actionResult.timing = timer.current();
this.notifyListeners();
await this.dbadapter.withClientLock(async client => {
// Start running tasks from the last executed task (if any), onwards.
for (const task of action.tasks.slice(actionResult.tasks.length)) {
if (this.stopped) {
return actionResult;
}
if (
actionResult.status === dataform.ActionResult.ExecutionStatus.RUNNING &&
!this.cancelled
) {
const taskStatus = await this.executeTask(client, task, actionResult, {
bigquery: {
labels: action.actionDescriptor?.bigqueryLabels,
actionRetryLimit: this.executionOptions?.bigquery?.actionRetryLimit,
jobPrefix: this.executionOptions?.bigquery?.jobPrefix,
dryRun: this.executionOptions?.bigquery?.dryRun
}
});
if (taskStatus === dataform.TaskResult.ExecutionStatus.FAILED) {
actionResult.status = dataform.ActionResult.ExecutionStatus.FAILED;
} else if (taskStatus === dataform.TaskResult.ExecutionStatus.CANCELLED) {
actionResult.status = dataform.ActionResult.ExecutionStatus.CANCELLED;
}
} else {
actionResult.tasks.push({
status: dataform.TaskResult.ExecutionStatus.SKIPPED
});
}
}
});
if (this.stopped) {
return actionResult;
}
if (
action.actionDescriptor &&
// Only set metadata if we expect the action to complete in SUCCESSFUL state
// (i.e. it must still be RUNNING, and not FAILED).
actionResult.status === dataform.ActionResult.ExecutionStatus.RUNNING &&
!(this.graph.runConfig && this.graph.runConfig.disableSetMetadata) &&
action.type === "table"
) {
try {
await this.dbadapter.setMetadata(action);
} catch (e) {
// TODO: Setting the metadata is not a task itself, so we have nowhere to surface this error cleanly.
// For now, we can attach the error to the last task in the action so it gets
// surfaced properly without ending the entire run, but also not failing silently.
if (actionResult.tasks.length > 0) {
actionResult.tasks[
actionResult.tasks.length - 1
].errorMessage = `Error setting metadata: ${e.message}`;
actionResult.tasks[actionResult.tasks.length - 1].status =
dataform.TaskResult.ExecutionStatus.FAILED;
}
actionResult.status = dataform.ActionResult.ExecutionStatus.FAILED;
}
}
this.warehouseStateByTarget.delete(action.target);
if (actionResult.status === dataform.ActionResult.ExecutionStatus.RUNNING) {
actionResult.status = dataform.ActionResult.ExecutionStatus.SUCCESSFUL;
}
actionResult.timing = timer.end();
this.notifyListeners();
return actionResult;
}
private async executeTask(
client: dbadapters.IDbClient,
task: dataform.IExecutionTask,
parentAction: dataform.IActionResult,
options: { bigquery?: IBigQueryOptions & IBigQueryExecutionOptions }
): Promise<dataform.TaskResult.ExecutionStatus> {
const timer = Timer.start();
const taskResult: dataform.ITaskResult = {
status: dataform.TaskResult.ExecutionStatus.RUNNING,
timing: timer.current(),
metadata: {}
};
parentAction.tasks.push(taskResult);
this.notifyListeners();
try {
// Retry this function a given number of times, configurable by user
const { rows, metadata } = await retry(
() =>
client.execute(task.statement, {
onCancel: handleCancel => this.eEmitter.on(CANCEL_EVENT, handleCancel),
rowLimit: 1,
bigquery: options.bigquery
}),
task.type === "operation" ? 1 : options.bigquery.actionRetryLimit + 1 || 1
);
taskResult.metadata = metadata;
if (task.type === "assertion") {
// We expect that an assertion query returns 1 row, with 1 field that is the row count.
// We don't really care what that field/column is called.
const rowCount = rows[0][Object.keys(rows[0])[0]];
if (rowCount > 0) {
throw new Error(`Assertion failed: query returned ${rowCount} row(s).`);
}
}
taskResult.status = dataform.TaskResult.ExecutionStatus.SUCCESSFUL;
} catch (e) {
taskResult.status = this.cancelled
? dataform.TaskResult.ExecutionStatus.CANCELLED
: dataform.TaskResult.ExecutionStatus.FAILED;
taskResult.errorMessage = `${this.graph.projectConfig.warehouse} error: ${e.message}`;
}
taskResult.timing = timer.end();
this.notifyListeners();
return taskResult.status;
}
}
class Timer {
public static start(existingTiming?: dataform.ITiming) {
return new Timer(existingTiming?.startTimeMillis.toNumber() || new Date().valueOf());
}
private constructor(readonly startTimeMillis: number) {}
public current(): dataform.ITiming {
return {
startTimeMillis: Long.fromNumber(this.startTimeMillis)
};
}
public end(): dataform.ITiming {
return {
startTimeMillis: Long.fromNumber(this.startTimeMillis),
endTimeMillis: Long.fromNumber(new Date().valueOf())
};
}
}