@flytedesk/app-kit 8.0.0 → 10.0.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/README.md +135 -4
- package/dist/auth/errors.d.ts +20 -0
- package/dist/auth/errors.js +24 -0
- package/dist/auth/errors.js.map +1 -1
- package/dist/auth/guards.d.ts +18 -0
- package/dist/auth/guards.js +34 -0
- package/dist/auth/guards.js.map +1 -1
- package/dist/auth/idpFetch.d.ts +26 -0
- package/dist/auth/idpFetch.js +38 -0
- package/dist/auth/idpFetch.js.map +1 -0
- package/dist/auth/index.d.ts +10 -4
- package/dist/auth/index.js +8 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/oidc-client.d.ts +35 -7
- package/dist/auth/oidc-client.js +69 -37
- package/dist/auth/oidc-client.js.map +1 -1
- package/dist/auth/plugin.d.ts +5 -0
- package/dist/auth/plugin.js +158 -56
- package/dist/auth/plugin.js.map +1 -1
- package/dist/auth/readRoute.d.ts +20 -0
- package/dist/auth/readRoute.js +20 -0
- package/dist/auth/readRoute.js.map +1 -0
- package/dist/auth/service/index.d.ts +61 -0
- package/dist/auth/service/index.js +145 -0
- package/dist/auth/service/index.js.map +1 -0
- package/dist/auth/serviceTokenContract.d.ts +33 -0
- package/dist/auth/serviceTokenContract.js +40 -0
- package/dist/auth/serviceTokenContract.js.map +1 -0
- package/dist/auth/shared-types.d.ts +6 -3
- package/dist/auth/shared-types.js.map +1 -1
- package/dist/auth/testing/fake-idp.d.ts +97 -1
- package/dist/auth/testing/fake-idp.js +179 -1
- package/dist/auth/testing/fake-idp.js.map +1 -1
- package/dist/auth/testing/index.d.ts +1 -1
- package/dist/auth/testing/index.js +1 -1
- package/dist/auth/testing/index.js.map +1 -1
- package/dist/auth/types.d.ts +29 -5
- package/dist/bigquery/errors.d.ts +34 -8
- package/dist/bigquery/errors.js +180 -22
- package/dist/bigquery/errors.js.map +1 -1
- package/dist/bigquery/index.d.ts +2 -1
- package/dist/bigquery/index.js +1 -1
- package/dist/bigquery/index.js.map +1 -1
- package/dist/bigquery/jobIdentity.d.ts +39 -0
- package/dist/bigquery/jobIdentity.js +83 -0
- package/dist/bigquery/jobIdentity.js.map +1 -0
- package/dist/bigquery/load.d.ts +11 -14
- package/dist/bigquery/load.js +132 -56
- package/dist/bigquery/load.js.map +1 -1
- package/dist/bigquery/query.d.ts +19 -1
- package/dist/bigquery/query.js +143 -50
- package/dist/bigquery/query.js.map +1 -1
- package/dist/bigquery/retry.d.ts +56 -0
- package/dist/bigquery/retry.js +110 -0
- package/dist/bigquery/retry.js.map +1 -0
- package/dist/bigquery/types.d.ts +40 -5
- package/dist/profile/plugin.js +11 -7
- package/dist/profile/plugin.js.map +1 -1
- package/package.json +5 -1
package/dist/bigquery/load.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Load-job capability (AK-14 follow-on, MP-200/PLN-2): loads a stream of rows
|
|
3
|
-
* into a BigQuery table in one load job, with retry-with-backoff
|
|
4
|
-
*
|
|
3
|
+
* into a BigQuery table in one load job, with retry-with-backoff for BigQuery's
|
|
4
|
+
* transient errors that never loads the rows twice (one job id for every
|
|
5
|
+
* submission; see `loadWithRetry`) — generalized out of
|
|
5
6
|
* media-planner's `apps/api/src/lib/bigqueryLoad.ts` (via flytedesk-id's
|
|
6
7
|
* verbatim port at `src/lib/bigqueryLoad.ts`, MP-200), which this module
|
|
7
8
|
* replaces.
|
|
@@ -21,14 +22,11 @@
|
|
|
21
22
|
*/
|
|
22
23
|
import { once } from "node:events";
|
|
23
24
|
import { pipeline } from "node:stream/promises";
|
|
24
|
-
import { BigQueryReadError, classifyBigQueryError,
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
import { BigQueryReadError, classifyBigQueryError, isJobLevelFailure, isTransientRequestError, isUnfinishedJobError, unfinishedJobLocation, } from "./errors.js";
|
|
26
|
+
import { findJob, isJobAlreadyExists, JobIdentity, locationOption, } from "./jobIdentity.js";
|
|
27
|
+
import { RetryPolicy } from "./retry.js";
|
|
27
28
|
const DEFAULT_CHUNK_BYTES = 1024 * 1024;
|
|
28
29
|
const DEFAULT_SOURCE_FORMAT = "NEWLINE_DELIMITED_JSON";
|
|
29
|
-
function defaultSleep(ms) {
|
|
30
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
31
|
-
}
|
|
32
30
|
/** Serializes `rows` into NDJSON, one `Buffer` of about `chunkBytes` at a
|
|
33
31
|
* time, counting rows into `counter` as they are consumed. */
|
|
34
32
|
async function* ndjsonChunks(rows, chunkBytes, counter) {
|
|
@@ -48,29 +46,8 @@ async function* ndjsonChunks(rows, chunkBytes, counter) {
|
|
|
48
46
|
if (lines.length > 0)
|
|
49
47
|
yield Buffer.from(lines.join(""));
|
|
50
48
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw new BigQueryReadError("this BigQueryLike does not implement createJob — it was not constructed for load jobs from Cloud Storage");
|
|
54
|
-
}
|
|
55
|
-
const [job] = await bq.createJob({
|
|
56
|
-
configuration: {
|
|
57
|
-
labels: options.labels,
|
|
58
|
-
load: {
|
|
59
|
-
destinationTable: {
|
|
60
|
-
projectId: options.projectId,
|
|
61
|
-
datasetId: options.datasetId,
|
|
62
|
-
tableId: options.tableId,
|
|
63
|
-
},
|
|
64
|
-
sourceUris: [...sourceUris],
|
|
65
|
-
sourceFormat: options.sourceFormat ?? DEFAULT_SOURCE_FORMAT,
|
|
66
|
-
schema: options.schema,
|
|
67
|
-
writeDisposition: options.writeDisposition,
|
|
68
|
-
createDisposition: options.createDisposition,
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
await job.promise();
|
|
73
|
-
const [metadata] = await job.getMetadata();
|
|
49
|
+
const LOAD_TRANSIENT = { quota: true };
|
|
50
|
+
function rowCountFromMetadata(options, metadata) {
|
|
74
51
|
const outputRows = metadata.statistics?.load?.outputRows;
|
|
75
52
|
const rowCount = Number(outputRows);
|
|
76
53
|
if (outputRows === undefined ||
|
|
@@ -80,8 +57,51 @@ async function runUriLoadJob(bq, options, sourceUris) {
|
|
|
80
57
|
}
|
|
81
58
|
return { rowCount };
|
|
82
59
|
}
|
|
83
|
-
async function
|
|
84
|
-
|
|
60
|
+
async function runUriLoadJob(bq, options, sourceUris, progress) {
|
|
61
|
+
if (!bq.createJob) {
|
|
62
|
+
throw new BigQueryReadError("this BigQueryLike does not implement createJob — it was not constructed for load jobs from Cloud Storage");
|
|
63
|
+
}
|
|
64
|
+
let job;
|
|
65
|
+
try {
|
|
66
|
+
[job] = await bq.createJob({
|
|
67
|
+
jobId: progress.identity.id,
|
|
68
|
+
location: options.location,
|
|
69
|
+
configuration: {
|
|
70
|
+
labels: options.labels,
|
|
71
|
+
load: {
|
|
72
|
+
destinationTable: {
|
|
73
|
+
projectId: options.projectId,
|
|
74
|
+
datasetId: options.datasetId,
|
|
75
|
+
tableId: options.tableId,
|
|
76
|
+
},
|
|
77
|
+
sourceUris: [...sourceUris],
|
|
78
|
+
sourceFormat: options.sourceFormat ?? DEFAULT_SOURCE_FORMAT,
|
|
79
|
+
schema: options.schema,
|
|
80
|
+
writeDisposition: options.writeDisposition,
|
|
81
|
+
createDisposition: options.createDisposition,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
// The SDK rejects with the job's status.errors even for warnings. A job that
|
|
88
|
+
// has not finished failed exists under its id and may still finish: wait on it.
|
|
89
|
+
if (!isUnfinishedJobError(err))
|
|
90
|
+
throw err;
|
|
91
|
+
progress.confirmed = true;
|
|
92
|
+
progress.identity.confirmLocation(unfinishedJobLocation(err));
|
|
93
|
+
return waitOnExistingJob(bq, options, progress);
|
|
94
|
+
}
|
|
95
|
+
progress.confirmed = true;
|
|
96
|
+
progress.identity.confirmLocation(job.location);
|
|
97
|
+
await job.promise();
|
|
98
|
+
const [metadata] = await job.getMetadata();
|
|
99
|
+
return rowCountFromMetadata(options, metadata);
|
|
100
|
+
}
|
|
101
|
+
async function runLoadJob(bq, options, source, progress) {
|
|
102
|
+
const table = bq
|
|
103
|
+
.dataset(options.datasetId, locationOption(options.location))
|
|
104
|
+
.table(options.tableId);
|
|
85
105
|
const createWriteStream = table.createWriteStream;
|
|
86
106
|
if (!createWriteStream) {
|
|
87
107
|
throw new BigQueryReadError(`dataset(${options.datasetId}).table(${options.tableId}) does not implement createWriteStream — this BigQueryLike was not constructed for load jobs`);
|
|
@@ -92,9 +112,17 @@ async function runLoadJob(bq, options, source) {
|
|
|
92
112
|
writeDisposition: options.writeDisposition,
|
|
93
113
|
createDisposition: options.createDisposition,
|
|
94
114
|
labels: options.labels,
|
|
95
|
-
jobId:
|
|
115
|
+
jobId: progress.identity.id,
|
|
96
116
|
});
|
|
97
117
|
const counter = { rows: 0 };
|
|
118
|
+
// The library emits "job" once the upload is finished and BigQuery has
|
|
119
|
+
// created the load job. From then on a failure while waiting for the job is
|
|
120
|
+
// about THIS job, which must be asked about again, never loaded a second time.
|
|
121
|
+
stream.once("job", (job) => {
|
|
122
|
+
progress.confirmed = true;
|
|
123
|
+
progress.identity.confirmLocation(job?.location);
|
|
124
|
+
progress.rowCount = counter.rows;
|
|
125
|
+
});
|
|
98
126
|
// The load is done only when BigQuery reports the job "complete" AND the
|
|
99
127
|
// pipeline has flushed every chunk. `once` rejects on the stream's "error",
|
|
100
128
|
// which is how a failed job surfaces; `pipeline` rejects (and destroys the
|
|
@@ -105,48 +133,96 @@ async function runLoadJob(bq, options, source) {
|
|
|
105
133
|
]);
|
|
106
134
|
return { rowCount: counter.rows };
|
|
107
135
|
}
|
|
108
|
-
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
136
|
+
/** Waits on the load job that already exists, through a fresh handle (a handle
|
|
137
|
+
* whose `promise()` rejected stays rejected). A stream load answers with the
|
|
138
|
+
* rows it counted; a load from URIs with the rows BigQuery reports. */
|
|
139
|
+
async function waitOnExistingJob(bq, options, progress) {
|
|
140
|
+
const jobId = progress.identity.id;
|
|
141
|
+
const job = bq.job(jobId, progress.identity.jobOptions);
|
|
142
|
+
if (!job.promise) {
|
|
143
|
+
throw new BigQueryReadError(`job(${jobId}) does not implement promise — this BigQueryLike was not constructed for load jobs`);
|
|
144
|
+
}
|
|
145
|
+
await job.promise();
|
|
146
|
+
if (progress.rowCount !== undefined)
|
|
147
|
+
return { rowCount: progress.rowCount };
|
|
148
|
+
const [metadata] = await job.getMetadata();
|
|
149
|
+
return rowCountFromMetadata(options, metadata);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Runs one load through the module's one bounded retry, following Google's
|
|
153
|
+
* error table the way `runQuery` does. A load is submitted under ONE job id,
|
|
154
|
+
* chosen once and reused by every submission, because Google says the outcome
|
|
155
|
+
* of a failed `jobs.insert` is unclear. So:
|
|
156
|
+
* - once the job is known to exist, a failed request about it (`backendError`,
|
|
157
|
+
* `internalError`, `tableUnavailable`, `rateLimitExceeded`, and `quotaExceeded`,
|
|
158
|
+
* which loads have always retried) waits on THAT job again;
|
|
159
|
+
* - when a submission failed before the job was confirmed, the id is looked up
|
|
160
|
+
* before submitting again: found means wait on it, 404 means submit under the
|
|
161
|
+
* same id, and a 409 on submitting means it exists;
|
|
162
|
+
* - a new id, and a new load, only when the job itself reports a failure
|
|
163
|
+
* (`isJobLevelFailure`), because that job is final.
|
|
164
|
+
* Any other error is classified and rethrown on the first attempt. The one
|
|
165
|
+
* retry path of every source.
|
|
166
|
+
*/
|
|
167
|
+
async function loadWithRetry(bq, options, submit) {
|
|
168
|
+
const policy = new RetryPolicy(options, `load into ${options.datasetId}.${options.tableId}`);
|
|
169
|
+
const progress = {
|
|
170
|
+
identity: new JobIdentity(options.jobId, options.location),
|
|
171
|
+
confirmed: false,
|
|
172
|
+
};
|
|
173
|
+
for (;;) {
|
|
174
|
+
policy.begin();
|
|
117
175
|
try {
|
|
118
|
-
|
|
176
|
+
if (!progress.confirmed && progress.identity.mayExist) {
|
|
177
|
+
const found = await findJob(bq, progress.identity.id, progress.identity.location);
|
|
178
|
+
progress.confirmed = found !== undefined;
|
|
179
|
+
progress.identity.confirmLocation(found?.location);
|
|
180
|
+
}
|
|
181
|
+
if (progress.confirmed)
|
|
182
|
+
return await waitOnExistingJob(bq, options, progress);
|
|
183
|
+
progress.identity.markSubmitted();
|
|
184
|
+
return await submit(progress);
|
|
119
185
|
}
|
|
120
186
|
catch (err) {
|
|
121
|
-
if (
|
|
187
|
+
if (isJobAlreadyExists(err)) {
|
|
188
|
+
progress.confirmed = true;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const jobFailed = isJobLevelFailure(err, LOAD_TRANSIENT);
|
|
192
|
+
if (!jobFailed && !isTransientRequestError(err, LOAD_TRANSIENT)) {
|
|
122
193
|
throw classifyBigQueryError(err);
|
|
123
194
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
195
|
+
await policy.retryAfter(err);
|
|
196
|
+
if (jobFailed) {
|
|
197
|
+
progress.identity.renew();
|
|
198
|
+
progress.confirmed = false;
|
|
199
|
+
progress.rowCount = undefined;
|
|
200
|
+
}
|
|
127
201
|
}
|
|
128
202
|
}
|
|
129
203
|
}
|
|
130
204
|
/**
|
|
131
205
|
* Streams the rows `source` yields into `datasetId.tableId` in one load job,
|
|
132
|
-
* retrying with exponential backoff ONLY
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* throw from the source, etc.) is classified and rethrown on the
|
|
136
|
-
* attempt. Each
|
|
206
|
+
* retrying with exponential backoff ONLY for BigQuery's transient errors, and
|
|
207
|
+
* never loading the rows twice: see `loadWithRetry` for when the same job is
|
|
208
|
+
* waited on and when a new one is submitted. Any other error (bad schema, auth
|
|
209
|
+
* failure, a throw from the source, etc.) is classified and rethrown on the
|
|
210
|
+
* first attempt. Each submission calls `source` afresh — see `LoadRowSource`.
|
|
211
|
+
* `options.jobId` is the id the load job is submitted under (random when
|
|
212
|
+
* omitted).
|
|
137
213
|
*
|
|
138
214
|
* Pass `writeDisposition: "WRITE_TRUNCATE"` for a full-refresh reload; omit it
|
|
139
215
|
* for an append load.
|
|
140
216
|
*/
|
|
141
217
|
export async function loadRowsWithRetry(bq, options, source) {
|
|
142
|
-
return loadWithRetry(options, () => runLoadJob(bq, options, source));
|
|
218
|
+
return loadWithRetry(bq, options, (progress) => runLoadJob(bq, options, source, progress));
|
|
143
219
|
}
|
|
144
220
|
/**
|
|
145
221
|
* Loads the newline-delimited JSON files at `sourceUris` (`gs://` addresses,
|
|
146
222
|
* gzip-compressed or not) into `datasetId.tableId` in ONE load job, which
|
|
147
223
|
* BigQuery runs reading the files in parallel. Nothing is parsed or
|
|
148
224
|
* re-serialised here. Retry and error classes are those of
|
|
149
|
-
* `loadRowsWithRetry
|
|
225
|
+
* `loadRowsWithRetry`.
|
|
150
226
|
*
|
|
151
227
|
* `rowCount` is what BigQuery reports the job wrote
|
|
152
228
|
* (`statistics.load.outputRows`), never a count taken locally; a job that
|
|
@@ -154,6 +230,6 @@ export async function loadRowsWithRetry(bq, options, source) {
|
|
|
154
230
|
* putting them in Cloud Storage and removing them afterwards.
|
|
155
231
|
*/
|
|
156
232
|
export async function loadFromUrisWithRetry(bq, options, sourceUris) {
|
|
157
|
-
return loadWithRetry(options, () => runUriLoadJob(bq, options, sourceUris));
|
|
233
|
+
return loadWithRetry(bq, options, (progress) => runUriLoadJob(bq, options, sourceUris, progress));
|
|
158
234
|
}
|
|
159
235
|
//# sourceMappingURL=load.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.js","sourceRoot":"","sources":["../../src/bigquery/load.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"load.js","sourceRoot":"","sources":["../../src/bigquery/load.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,IAA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ED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,EAAgB,EAChB,OAA4B,EAC5B,UAA6B;IAE7B,OAAO,aAAa,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CAC7C,aAAa,CAAC,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,CACjD,CAAC;AACJ,CAAC"}
|
package/dist/bigquery/query.d.ts
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
import type { BigQueryLike, DryRunEstimate, EstimateQueryBytesOptions, QueryPage, RunQueryOptions } from "./types.js";
|
|
2
|
-
/** Runs one page of a parameterised
|
|
2
|
+
/** Runs one page of a parameterised query. Always bind
|
|
3
3
|
* user-supplied values through `options.params`/`options.types` — never
|
|
4
4
|
* string-interpolate them into `options.sql`.
|
|
5
5
|
*
|
|
6
|
+
* Transient BigQuery failures are retried by Google's error table, up to
|
|
7
|
+
* `options.attempts` attempts in all, with exponential backoff, inside ONE
|
|
8
|
+
* total time budget of `options.jobTimeoutMs` (no attempt or backoff starts
|
|
9
|
+
* once it is spent):
|
|
10
|
+
* - creating the job failed: the job id (chosen once, reused by every attempt)
|
|
11
|
+
* is looked up first, since the outcome of a failed insert is unclear; found
|
|
12
|
+
* means wait on it, 404 means submit under the same id, a 409 means it exists;
|
|
13
|
+
* - a request about the existing job failed (`backendError`, `internalError`,
|
|
14
|
+
* `rateLimitExceeded`): the SAME job is asked again, never a second job;
|
|
15
|
+
* - the job itself failed (`jobBackendError`, `jobInternalError`,
|
|
16
|
+
* `jobRateLimitExceeded`, or it finished failed with another transient reason
|
|
17
|
+
* and no HTTP code, which a failure the job reports does not carry): a new job
|
|
18
|
+
* is submitted. A caller running a script
|
|
19
|
+
* that changes data outside one transaction passes `attempts: 1`.
|
|
20
|
+
* `quotaExceeded` is not retried: Google gives no retry guidance for it. Fetching
|
|
21
|
+
* a page of an existing job (`pageToken`) is never retried: a new job could not
|
|
22
|
+
* serve its token.
|
|
23
|
+
*
|
|
6
24
|
* A BigQuery page token is only valid against the job that produced it
|
|
7
25
|
* (AK-26) — passing `pageToken` therefore REQUIRES `jobId` (the `jobId` a
|
|
8
26
|
* prior `QueryPage` returned) so this fetches the *same* job's next page
|
package/dist/bigquery/query.js
CHANGED
|
@@ -4,70 +4,156 @@
|
|
|
4
4
|
* (AK-11). Inventory-specific SQL stays in media-planner; this module only
|
|
5
5
|
* ever takes SQL + params from the caller and runs it.
|
|
6
6
|
*/
|
|
7
|
-
import { BadQueryError, classifyBigQueryError, isJobTimeoutError, isQueryWaitTimeoutError, JobTimeoutError, } from "./errors.js";
|
|
7
|
+
import { BadQueryError, classifyBigQueryError, isJobTimeoutError, isJobLevelFailure, isQueryWaitTimeoutError, isTransientRequestError, isUnfinishedJobError, unfinishedJobLocation, JobTimeoutError, } from "./errors.js";
|
|
8
8
|
import { DryRunBudgetExceededError } from "./errors.js";
|
|
9
|
+
import { findJob, isJobAlreadyExists, JobIdentity, locationOption, } from "./jobIdentity.js";
|
|
9
10
|
import { assertBindableParams } from "./params.js";
|
|
11
|
+
import { RetryPolicy } from "./retry.js";
|
|
10
12
|
import { normalizeResultSchema } from "./schema.js";
|
|
11
|
-
/** Runs one page of a parameterised
|
|
13
|
+
/** Runs one page of a parameterised query. Always bind
|
|
12
14
|
* user-supplied values through `options.params`/`options.types` — never
|
|
13
15
|
* string-interpolate them into `options.sql`.
|
|
14
16
|
*
|
|
17
|
+
* Transient BigQuery failures are retried by Google's error table, up to
|
|
18
|
+
* `options.attempts` attempts in all, with exponential backoff, inside ONE
|
|
19
|
+
* total time budget of `options.jobTimeoutMs` (no attempt or backoff starts
|
|
20
|
+
* once it is spent):
|
|
21
|
+
* - creating the job failed: the job id (chosen once, reused by every attempt)
|
|
22
|
+
* is looked up first, since the outcome of a failed insert is unclear; found
|
|
23
|
+
* means wait on it, 404 means submit under the same id, a 409 means it exists;
|
|
24
|
+
* - a request about the existing job failed (`backendError`, `internalError`,
|
|
25
|
+
* `rateLimitExceeded`): the SAME job is asked again, never a second job;
|
|
26
|
+
* - the job itself failed (`jobBackendError`, `jobInternalError`,
|
|
27
|
+
* `jobRateLimitExceeded`, or it finished failed with another transient reason
|
|
28
|
+
* and no HTTP code, which a failure the job reports does not carry): a new job
|
|
29
|
+
* is submitted. A caller running a script
|
|
30
|
+
* that changes data outside one transaction passes `attempts: 1`.
|
|
31
|
+
* `quotaExceeded` is not retried: Google gives no retry guidance for it. Fetching
|
|
32
|
+
* a page of an existing job (`pageToken`) is never retried: a new job could not
|
|
33
|
+
* serve its token.
|
|
34
|
+
*
|
|
15
35
|
* A BigQuery page token is only valid against the job that produced it
|
|
16
36
|
* (AK-26) — passing `pageToken` therefore REQUIRES `jobId` (the `jobId` a
|
|
17
37
|
* prior `QueryPage` returned) so this fetches the *same* job's next page
|
|
18
38
|
* instead of creating a fresh job and handing it a token it never issued. */
|
|
19
39
|
export async function runQuery(bq, options) {
|
|
40
|
+
if (options.pageToken !== undefined)
|
|
41
|
+
return fetchLaterPage(bq, options);
|
|
42
|
+
const policy = new RetryPolicy(options, "query", options.jobTimeoutMs);
|
|
43
|
+
assertBindableParams(options.params, options.types);
|
|
44
|
+
const identity = new JobIdentity(options.jobId, options.location);
|
|
20
45
|
let job;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
throw new BadQueryError("runQuery: pageToken requires the jobId of the job that produced it (QueryPage.jobId)");
|
|
24
|
-
}
|
|
25
|
-
job = bq.job(options.jobId);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
assertBindableParams(options.params, options.types);
|
|
46
|
+
for (;;) {
|
|
47
|
+
const remainingMs = policy.begin();
|
|
29
48
|
try {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
params: options.params,
|
|
33
|
-
types: options.types,
|
|
34
|
-
labels: options.labels,
|
|
35
|
-
location: options.location,
|
|
36
|
-
maxResults: options.pageSize,
|
|
37
|
-
maximumBytesBilled: options.maximumBytesBilled !== undefined
|
|
38
|
-
? String(options.maximumBytesBilled)
|
|
39
|
-
: undefined,
|
|
40
|
-
jobTimeoutMs: options.jobTimeoutMs,
|
|
41
|
-
useQueryCache: options.useQueryCache,
|
|
42
|
-
});
|
|
49
|
+
job ??= await acquireJob(bq, options, identity, remainingMs);
|
|
50
|
+
return await fetchPage(job, options, policy.timeLeft(), identity.id, identity.location);
|
|
43
51
|
}
|
|
44
52
|
catch (err) {
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
const jobFailed = isJobLevelFailure(err);
|
|
54
|
+
if (!jobFailed && !isTransientRequestError(err)) {
|
|
55
|
+
throw classifyBigQueryError(err);
|
|
56
|
+
}
|
|
57
|
+
await policy.retryAfter(err);
|
|
58
|
+
if (jobFailed) {
|
|
59
|
+
// The job itself reported a failure, so it is final: the next one is a
|
|
60
|
+
// new job with a new id.
|
|
61
|
+
job = undefined;
|
|
62
|
+
identity.renew();
|
|
47
63
|
}
|
|
48
|
-
throw classifyBigQueryError(err);
|
|
49
64
|
}
|
|
50
65
|
}
|
|
66
|
+
}
|
|
67
|
+
/** The job `identity` names: looked up first when an earlier submission under
|
|
68
|
+
* that id may have reached BigQuery (Google says the outcome of a failed
|
|
69
|
+
* `jobs.insert` is unclear), and submitted under that same id when the lookup
|
|
70
|
+
* answers 404. A 409 on submitting means the job exists. */
|
|
71
|
+
async function acquireJob(bq, options, identity, remainingMs) {
|
|
72
|
+
if (identity.mayExist) {
|
|
73
|
+
const found = await findJob(bq, identity.id, identity.location);
|
|
74
|
+
if (found) {
|
|
75
|
+
identity.confirmLocation(found.location);
|
|
76
|
+
return found;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
identity.markSubmitted();
|
|
80
|
+
try {
|
|
81
|
+
const job = await createJob(bq, options, identity.id, remainingMs);
|
|
82
|
+
identity.confirmLocation(job.location);
|
|
83
|
+
return job;
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
// A 409 means the job exists. So does a rejection for a job that has not
|
|
87
|
+
// finished failed (warnings in its status.errors): wait on it under its id.
|
|
88
|
+
if (!isJobAlreadyExists(err) && !isUnfinishedJobError(err))
|
|
89
|
+
throw err;
|
|
90
|
+
identity.confirmLocation(unfinishedJobLocation(err));
|
|
91
|
+
return bq.job(identity.id, identity.jobOptions);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** The next page of `options.jobId`'s results — one request, never retried. */
|
|
95
|
+
async function fetchLaterPage(bq, options) {
|
|
96
|
+
if (!options.jobId) {
|
|
97
|
+
throw new BadQueryError("runQuery: pageToken requires the jobId of the job that produced it (QueryPage.jobId)");
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
return await fetchPage(bq.job(options.jobId, locationOption(options.location)), options, options.jobTimeoutMs, options.jobId, options.location);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
throw classifyBigQueryError(err);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Creates a query job whose own `jobTimeoutMs` is what is left of the call's
|
|
107
|
+
* budget. Throws the raw error (`runQuery` decides whether to retry it), or a
|
|
108
|
+
* `JobTimeoutError` when BigQuery stopped the job for running too long. */
|
|
109
|
+
async function createJob(bq, options, jobId, jobTimeoutMs) {
|
|
110
|
+
try {
|
|
111
|
+
const [job] = await bq.createQueryJob({
|
|
112
|
+
query: options.sql,
|
|
113
|
+
params: options.params,
|
|
114
|
+
types: options.types,
|
|
115
|
+
labels: options.labels,
|
|
116
|
+
location: options.location,
|
|
117
|
+
jobId,
|
|
118
|
+
maxResults: options.pageSize,
|
|
119
|
+
maximumBytesBilled: options.maximumBytesBilled !== undefined
|
|
120
|
+
? String(options.maximumBytesBilled)
|
|
121
|
+
: undefined,
|
|
122
|
+
jobTimeoutMs,
|
|
123
|
+
useQueryCache: options.useQueryCache,
|
|
124
|
+
});
|
|
125
|
+
return job;
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
throw timeoutOrRaw(err, options.jobTimeoutMs, isJobTimeoutError);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** One results request against `job`, waiting at most `timeoutMs`. Throws the
|
|
132
|
+
* raw error, or a `JobTimeoutError` when BigQuery stopped the job or the SDK
|
|
133
|
+
* gave up waiting. */
|
|
134
|
+
async function fetchPage(job, options, timeoutMs, fallbackJobId, fallbackLocation) {
|
|
51
135
|
try {
|
|
52
136
|
const [rows, next] = await job.getQueryResults({
|
|
53
137
|
maxResults: options.pageSize,
|
|
54
138
|
pageToken: options.pageToken,
|
|
55
|
-
timeoutMs
|
|
139
|
+
timeoutMs,
|
|
56
140
|
});
|
|
57
141
|
return {
|
|
58
142
|
rows,
|
|
59
|
-
jobId: job.id ??
|
|
143
|
+
jobId: job.id ?? fallbackJobId ?? "",
|
|
144
|
+
location: job.location ?? fallbackLocation,
|
|
60
145
|
nextPageToken: next?.pageToken,
|
|
61
146
|
};
|
|
62
147
|
}
|
|
63
148
|
catch (err) {
|
|
64
|
-
|
|
65
|
-
(isJobTimeoutError(err) || isQueryWaitTimeoutError(err))) {
|
|
66
|
-
throw new JobTimeoutError(options.jobTimeoutMs, err);
|
|
67
|
-
}
|
|
68
|
-
throw classifyBigQueryError(err);
|
|
149
|
+
throw timeoutOrRaw(err, options.jobTimeoutMs, (e) => isJobTimeoutError(e) || isQueryWaitTimeoutError(e));
|
|
69
150
|
}
|
|
70
151
|
}
|
|
152
|
+
function timeoutOrRaw(err, jobTimeoutMs, isTimeout) {
|
|
153
|
+
return jobTimeoutMs !== undefined && isTimeout(err)
|
|
154
|
+
? new JobTimeoutError(jobTimeoutMs, err)
|
|
155
|
+
: err;
|
|
156
|
+
}
|
|
71
157
|
/** Pages through every result of a query, yielding one row-array per page.
|
|
72
158
|
* Stops once BigQuery reports no further `nextPageToken`. Carries the
|
|
73
159
|
* producing job's id forward across pages (AK-26) so every page after the
|
|
@@ -75,13 +161,20 @@ export async function runQuery(bq, options) {
|
|
|
75
161
|
export async function* runQueryAllPages(bq, options) {
|
|
76
162
|
let pageToken = options.pageToken;
|
|
77
163
|
let jobId = options.jobId;
|
|
164
|
+
let location = options.location;
|
|
78
165
|
for (;;) {
|
|
79
|
-
const page = await runQuery(bq, {
|
|
166
|
+
const page = await runQuery(bq, {
|
|
167
|
+
...options,
|
|
168
|
+
pageToken,
|
|
169
|
+
jobId,
|
|
170
|
+
location,
|
|
171
|
+
});
|
|
80
172
|
yield page.rows;
|
|
81
173
|
if (!page.nextPageToken)
|
|
82
174
|
return;
|
|
83
175
|
pageToken = page.nextPageToken;
|
|
84
176
|
jobId = page.jobId;
|
|
177
|
+
location = page.location;
|
|
85
178
|
}
|
|
86
179
|
}
|
|
87
180
|
/**
|
|
@@ -92,24 +185,24 @@ export async function* runQueryAllPages(bq, options) {
|
|
|
92
185
|
* "don't let a user run an unbounded scan" guard.
|
|
93
186
|
*/
|
|
94
187
|
export async function estimateQueryBytes(bq, options) {
|
|
95
|
-
let metadata;
|
|
96
188
|
assertBindableParams(options.params, options.types);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
throw new JobTimeoutError(options.jobTimeoutMs, err);
|
|
189
|
+
const policy = new RetryPolicy(options, "dry run", options.jobTimeoutMs);
|
|
190
|
+
const metadata = await policy.run(async (jobTimeoutMs) => {
|
|
191
|
+
try {
|
|
192
|
+
const [, jobMetadata] = await bq.createQueryJob({
|
|
193
|
+
query: options.sql,
|
|
194
|
+
params: options.params,
|
|
195
|
+
types: options.types,
|
|
196
|
+
location: options.location,
|
|
197
|
+
dryRun: true,
|
|
198
|
+
jobTimeoutMs,
|
|
199
|
+
});
|
|
200
|
+
return jobMetadata;
|
|
110
201
|
}
|
|
111
|
-
|
|
112
|
-
|
|
202
|
+
catch (err) {
|
|
203
|
+
throw timeoutOrRaw(err, options.jobTimeoutMs, isJobTimeoutError);
|
|
204
|
+
}
|
|
205
|
+
}, (err) => isJobLevelFailure(err) || isTransientRequestError(err));
|
|
113
206
|
const stats = metadata.statistics?.query;
|
|
114
207
|
const estimate = {
|
|
115
208
|
totalBytesProcessed: Number(stats?.totalBytesProcessed ?? 0),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/bigquery/query.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/bigquery/query.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,uBAAuB,EACvB,oBAAoB,EACpB,qBAAqB,EACrB,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EACL,OAAO,EACP,kBAAkB,EAClB,WAAW,EACX,cAAc,GACf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAUpD;;;;;;;;;;;;;;;;;;;;;;;;;8EAyB8E;AAC9E,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,EAAgB,EAChB,OAAwB;IAExB,IAAI,OAAO,CAAC,SAAS,KAAK,SA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