/
Job.java
490 lines (443 loc) · 15.8 KB
/
Job.java
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright 2015 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.gax.retrying.BasicResultRetryAlgorithm;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.retrying.TimedAttemptSettings;
import com.google.cloud.RetryHelper;
import com.google.cloud.RetryOption;
import com.google.cloud.bigquery.BigQuery.JobOption;
import com.google.cloud.bigquery.BigQuery.QueryResultsOption;
import com.google.cloud.bigquery.BigQuery.TableDataListOption;
import com.google.cloud.bigquery.JobConfiguration.Type;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;
/**
* A Google BigQuery Job.
*
* <p>Objects of this class are immutable. To get a {@code Job} object with the most recent
* information use {@link #reload}. {@code Job} adds a layer of service-related functionality over
* {@link JobInfo}.
*/
public class Job extends JobInfo {
private static final long serialVersionUID = -4324100991693024704L;
private static final RetrySettings DEFAULT_JOB_WAIT_SETTINGS =
RetrySettings.newBuilder()
.setTotalTimeout(Duration.ofHours(12L))
.setInitialRetryDelay(Duration.ofSeconds(1L))
.setRetryDelayMultiplier(2.0)
.setJittered(true)
.setMaxRetryDelay(Duration.ofMinutes(1L))
.build();
static final RetrySettings DEFAULT_QUERY_JOB_WAIT_SETTINGS =
RetrySettings.newBuilder()
.setTotalTimeout(Duration.ofHours(12L))
.setInitialRetryDelay(Duration.ofSeconds(3L))
.setRetryDelayMultiplier(1.0)
.setJittered(true)
.setMaxRetryDelay(Duration.ofSeconds(3L))
.build();
static final QueryResultsOption[] DEFAULT_QUERY_WAIT_OPTIONS = {
QueryResultsOption.pageSize(0L),
};
private final BigQueryOptions options;
private transient BigQuery bigquery;
/** A builder for {@code Job} objects. */
public static final class Builder extends JobInfo.Builder {
private final BigQuery bigquery;
private final JobInfo.BuilderImpl infoBuilder;
Builder(BigQuery bigquery, JobConfiguration configuration) {
this.bigquery = bigquery;
this.infoBuilder = new JobInfo.BuilderImpl();
this.infoBuilder.setConfiguration(configuration);
}
Builder(Job job) {
this.bigquery = job.bigquery;
this.infoBuilder = new JobInfo.BuilderImpl(job);
}
@Override
Builder setEtag(String etag) {
infoBuilder.setEtag(etag);
return this;
}
@Override
Builder setGeneratedId(String generatedId) {
infoBuilder.setGeneratedId(generatedId);
return this;
}
@Override
public Builder setJobId(JobId jobId) {
infoBuilder.setJobId(jobId);
return this;
}
@Override
Builder setSelfLink(String selfLink) {
infoBuilder.setSelfLink(selfLink);
return this;
}
@Override
Builder setStatus(JobStatus status) {
infoBuilder.setStatus(status);
return this;
}
@Override
Builder setStatistics(JobStatistics statistics) {
infoBuilder.setStatistics(statistics);
return this;
}
@Override
Builder setUserEmail(String userEmail) {
infoBuilder.setUserEmail(userEmail);
return this;
}
@Override
public Builder setConfiguration(JobConfiguration configuration) {
infoBuilder.setConfiguration(configuration);
return this;
}
@Override
public Job build() {
return new Job(bigquery, infoBuilder);
}
}
Job(BigQuery bigquery, JobInfo.BuilderImpl infoBuilder) {
super(infoBuilder);
this.bigquery = checkNotNull(bigquery);
this.options = bigquery.getOptions();
}
/**
* Checks if this job exists.
*
* <p>Example of checking that a job exists.
*
* <pre>{@code
* if (!job.exists()) {
* // job doesn't exist
* }
* }</pre>
*
* @return {@code true} if this job exists, {@code false} otherwise
* @throws BigQueryException upon failure
*/
public boolean exists() {
checkNotDryRun("exists");
return bigquery.getJob(getJobId(), JobOption.fields()) != null;
}
/**
* Checks if this job has completed its execution, either failing or succeeding. If the job does
* not exist this method returns {@code true}.
*
* <p>Example of waiting for a job until it reports that it is done.
*
* <pre>{@code
* while (!job.isDone()) {
* Thread.sleep(1000L);
* }
* }</pre>
*
* @return {@code true} if this job is in {@link JobStatus.State#DONE} state or if it does not
* exist, {@code false} if the state is not {@link JobStatus.State#DONE}
* @throws BigQueryException upon failure
*/
public boolean isDone() {
checkNotDryRun("isDone");
Job job = bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS));
return job == null || JobStatus.State.DONE.equals(job.getStatus().getState());
}
/**
* Blocks until this job completes its execution, either failing or succeeding. This method
* returns current job's latest information. If the job no longer exists, this method returns
* {@code null}. By default, the job status is checked using jittered exponential backoff with 1
* second as an initial delay, 2.0 as a backoff factor, 1 minute as maximum delay between polls,
* 12 hours as a total timeout and unlimited number of attempts.
*
* <p>Example usage of {@code waitFor()}.
*
* <pre>{@code
* Job completedJob = job.waitFor();
* if (completedJob == null) {
* // job no longer exists
* } else if (completedJob.getStatus().getError() != null) {
* // job failed, handle error
* } else {
* // job completed successfully
* }
* }</pre>
*
* <p>Example usage of {@code waitFor()} with checking period and timeout.
*
* <pre>{@code
* Job completedJob =
* job.waitFor(
* RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
* RetryOption.totalTimeout(Duration.ofMinutes(1)));
* if (completedJob == null) {
* // job no longer exists
* } else if (completedJob.getStatus().getError() != null) {
* // job failed, handle error
* } else {
* // job completed successfully
* }
* }</pre>
*
* @param waitOptions options to configure checking period and timeout
* @throws BigQueryException upon failure, check {@link BigQueryException#getCause()} for details
* @throws InterruptedException if the current thread gets interrupted while waiting for the job
* to complete
*/
public Job waitFor(RetryOption... waitOptions) throws InterruptedException {
checkNotDryRun("waitFor");
Object completedJobResponse;
if (getConfiguration().getType() == Type.QUERY) {
completedJobResponse =
waitForQueryResults(
RetryOption.mergeToSettings(DEFAULT_JOB_WAIT_SETTINGS, waitOptions),
DEFAULT_QUERY_WAIT_OPTIONS);
} else {
completedJobResponse =
waitForJob(RetryOption.mergeToSettings(DEFAULT_QUERY_JOB_WAIT_SETTINGS, waitOptions));
}
return completedJobResponse == null ? null : reload();
}
/**
* Gets the query results of this job. This job must be of type {@code
* JobConfiguration.Type.QUERY}, otherwise this method will throw {@link
* UnsupportedOperationException}.
*
* <p>If the job hasn't finished, this method waits for the job to complete. However, the state of
* the current {@code Job} instance is not updated. To get the new state, call {@link
* #waitFor(RetryOption...)} or {@link #reload(JobOption...)}.
*
* @throws BigQueryException upon failure
*/
public TableResult getQueryResults(QueryResultsOption... options)
throws InterruptedException, JobException {
checkNotDryRun("getQueryResults");
if (getConfiguration().getType() != Type.QUERY) {
throw new UnsupportedOperationException(
"Getting query results is supported only for " + Type.QUERY + " jobs");
}
List<QueryResultsOption> waitOptions =
new ArrayList<>(Arrays.asList(DEFAULT_QUERY_WAIT_OPTIONS));
List<TableDataListOption> listOptions = new ArrayList<>();
for (QueryResultsOption option : options) {
switch (option.getRpcOption()) {
case MAX_RESULTS:
listOptions.add(TableDataListOption.pageSize((Long) option.getValue()));
break;
case PAGE_TOKEN:
listOptions.add(TableDataListOption.pageToken((String) option.getValue()));
break;
case START_INDEX:
listOptions.add(TableDataListOption.startIndex((Long) option.getValue()));
break;
case TIMEOUT:
waitOptions.add(QueryResultsOption.maxWaitTime((Long) option.getValue()));
break;
}
}
QueryResponse response =
waitForQueryResults(
DEFAULT_JOB_WAIT_SETTINGS, waitOptions.toArray(new QueryResultsOption[0]));
// Get the job resource to determine if it has errored.
Job job = this;
if (job.getStatus() == null || !JobStatus.State.DONE.equals(job.getStatus().getState())) {
job = reload();
}
if (job.getStatus() != null && job.getStatus().getError() != null) {
throw new JobException(
getJobId(), ImmutableList.copyOf(job.getStatus().getExecutionErrors()));
}
// If there are no rows in the result, this may have been a DDL query.
// Listing table data might fail, such as with CREATE VIEW queries.
// Avoid a tabledata.list API request by returning an empty TableResult.
if (response.getTotalRows() == 0) {
return new EmptyTableResult(response.getSchema());
}
TableId table =
((QueryJobConfiguration) getConfiguration()).getDestinationTable() == null
? ((QueryJobConfiguration) job.getConfiguration()).getDestinationTable()
: ((QueryJobConfiguration) getConfiguration()).getDestinationTable();
return bigquery.listTableData(
table, response.getSchema(), listOptions.toArray(new TableDataListOption[0]));
}
private QueryResponse waitForQueryResults(
RetrySettings waitSettings, final QueryResultsOption... resultsOptions)
throws InterruptedException {
if (getConfiguration().getType() != Type.QUERY) {
throw new UnsupportedOperationException(
"Waiting for query results is supported only for " + Type.QUERY + " jobs");
}
try {
return RetryHelper.poll(
new Callable<QueryResponse>() {
@Override
public QueryResponse call() {
return bigquery.getQueryResults(getJobId(), resultsOptions);
}
},
waitSettings,
new BasicResultRetryAlgorithm<QueryResponse>() {
@Override
public boolean shouldRetry(Throwable prevThrowable, QueryResponse prevResponse) {
return prevResponse != null && !prevResponse.getCompleted();
}
},
options.getClock());
} catch (ExecutionException e) {
throw BigQueryException.translateAndThrow(e);
}
}
private Job waitForJob(RetrySettings waitSettings) throws InterruptedException {
try {
return RetryHelper.poll(
new Callable<Job>() {
@Override
public Job call() throws Exception {
return bigquery.getJob(getJobId(), JobOption.fields(BigQuery.JobField.STATUS));
}
},
waitSettings,
new BasicResultRetryAlgorithm<Job>() {
@Override
public TimedAttemptSettings createNextAttempt(
Throwable prevThrowable, Job prevResponse, TimedAttemptSettings prevSettings) {
return null;
}
@Override
public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
return prevResponse != null
&& !JobStatus.State.DONE.equals(prevResponse.getStatus().getState());
}
},
options.getClock());
} catch (ExecutionException e) {
throw BigQueryException.translateAndThrow(e);
}
}
/**
* Fetches current job's latest information. Returns {@code null} if the job does not exist.
*
* <p>Example of reloading all fields until job status is DONE.
*
* <pre>{@code
* while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
* Thread.sleep(1000L);
* job = job.reload();
* }
* }</pre>
*
* <p>Example of reloading status field until job status is DONE.
*
* <pre>{@code
* while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
* Thread.sleep(1000L);
* job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
* }
* }</pre>
*
* @param options job options
* @return a {@code Job} object with latest information or {@code null} if not found
* @throws BigQueryException upon failure
*/
public Job reload(JobOption... options) {
checkNotDryRun("reload");
return bigquery.getJob(getJobId(), options);
}
/**
* Sends a job cancel request.
*
* <p>Example of cancelling a job.
*
* <pre>{@code
* if (job.cancel()) {
* return true; // job successfully cancelled
* } else {
* // job not found
* }
* }</pre>
*
* @return {@code true} if cancel request was sent successfully, {@code false} if job was not
* found
* @throws BigQueryException upon failure
*/
public boolean cancel() {
checkNotDryRun("cancel");
return bigquery.cancel(getJobId());
}
private void checkNotDryRun(String op) {
checkNotDryRun(getConfiguration(), op);
}
static void checkNotDryRun(JobConfiguration jobConfig, String op) {
QueryJobConfiguration config;
if (jobConfig instanceof QueryJobConfiguration) {
config = (QueryJobConfiguration) jobConfig;
} else {
return;
}
Boolean dryRun = config.dryRun();
if (dryRun == null) {
dryRun = false;
}
if (dryRun) {
String msg =
"Operation \"%s\" does not work for dryrun queries, "
+ "since a dry run does not actually create a job. "
+ "To validate a query and obtain some processing statistics, consider calling "
+ "BigQuery.create(JobInfo).";
throw new UnsupportedOperationException(String.format(msg, op));
}
}
/** Returns the job's {@code BigQuery} object used to issue requests. */
public BigQuery getBigQuery() {
return bigquery;
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(Job.class)) {
return false;
}
Job other = (Job) obj;
return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options);
}
@Override
public final int hashCode() {
return Objects.hash(super.hashCode(), options);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.bigquery = options.getService();
}
static Job fromPb(BigQuery bigquery, com.google.api.services.bigquery.model.Job jobPb) {
return new Job(bigquery, new JobInfo.BuilderImpl(jobPb));
}
}