Refine search
@Test public void testBigQueryException() { BigQueryException exception = new BigQueryException(500, "message"); assertEquals(500, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertNull(exception.getError()); assertTrue(exception.isRetryable()); exception = new BigQueryException(502, "message"); assertEquals(502, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertNull(exception.getError()); assertTrue(exception.isRetryable()); exception = new BigQueryException(503, "message"); assertEquals(503, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertNull(exception.getError()); assertTrue(exception.isRetryable()); exception = new BigQueryException(504, "message"); assertEquals(504, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertNull(exception.getError()); assertTrue(exception.isRetryable());
@Override public boolean delete(DatasetId datasetId, DatasetDeleteOption... options) { final DatasetId completeDatasetId = datasetId.setProjectId(getOptions().getProjectId()); final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options); try { return runWithRetries( new Callable<Boolean>() { @Override public Boolean call() { return bigQueryRpc.deleteDataset( completeDatasetId.getProject(), completeDatasetId.getDataset(), optionsMap); } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } }
public BigQueryException(IOException exception) { super(exception, true, RETRYABLE_ERRORS); BigQueryError error = null; if (getReason() != null) { error = new BigQueryError(getReason(), getLocation(), getMessage(), getDebugInfo()); } this.error = error; }
@Test public void testTranslateAndThrow() throws Exception { Exception cause = new BigQueryException(503, "message"); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { BigQueryException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(503, ex.getCode()); replay(exceptionMock); try { BigQueryException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(BigQueryException.UNKNOWN_CODE, ex.getCode());
@Test public void testNonRetryableException() { String exceptionMessage = "Not Implemented"; EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(501, exceptionMessage)); EasyMock.replay(bigqueryRpcMock); bigquery = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); thrown.expect(BigQueryException.class); thrown.expectMessage(exceptionMessage); bigquery.getDataset(DatasetId.of(DATASET)); }
private static BigQueryException translate(IOException exception) { return new BigQueryException(exception); }
@Override protected void flushBuffer(final int length, final boolean last) { try { com.google.api.services.bigquery.model.Job jobPb = runWithRetries( new Callable<com.google.api.services.bigquery.model.Job>() { @Override public com.google.api.services.bigquery.model.Job call() { return getOptions() .getBigQueryRpcV2() .write(getUploadId(), getBuffer(), 0, getPosition(), length, last); } }, getOptions().getRetrySettings(), BigQueryImpl.EXCEPTION_HANDLER, getOptions().getClock()); job = jobPb != null ? Job.fromPb(getOptions().getService(), jobPb) : null; } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } }
@Test public void testCreateJobNoGet() { String id = "testCreateJobNoGet-id"; JobId jobId = JobId.of(id); String query = "SELECT * in FOO"; Capture<com.google.api.services.bigquery.model.Job> jobCapture = EasyMock.newCapture(); EasyMock.expect( bigqueryRpcMock.create(EasyMock.capture(jobCapture), EasyMock.eq(EMPTY_RPC_OPTIONS))) .andThrow(new BigQueryException(409, "already exists, for some reason")); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); try { bigquery.create(JobInfo.of(jobId, QueryJobConfiguration.of(query))); fail("should throw"); } catch (BigQueryException e) { assertThat(jobCapture.getValue().getJobReference().getJobId()).isEqualTo(id); } }
@Override public boolean cancel(final JobId jobId) { try { return runWithRetries(new Callable<Boolean>() { @Override public Boolean call() { return bigQueryRpc.cancel(jobId.job()); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } }
private static String open( final BigQueryOptions options, final JobId jobId, final WriteChannelConfiguration writeChannelConfiguration) { try { return runWithRetries( new Callable<String>() { @Override public String call() { return options .getBigQueryRpcV2() .open( new com.google.api.services.bigquery.model.Job() .setConfiguration(writeChannelConfiguration.toPb()) .setJobReference(jobId.toPb())); } }, options.getRetrySettings(), BigQueryImpl.EXCEPTION_HANDLER, options.getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } }
@Override public Table create(TableInfo table, TableOption... options) { final com.google.api.services.bigquery.model.Table tablePb = table.setProjectId(options().projectId()).toPb(); final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options); try { return Table.fromPb(this, runWithRetries(new Callable<com.google.api.services.bigquery.model.Table>() { @Override public com.google.api.services.bigquery.model.Table call() { return bigQueryRpc.create(tablePb, optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } }
/** * @param exception the {@link BigQueryException} to check. * @return true if this error is an error that can be fixed by retrying with a smaller batch * size, or false otherwise. */ private static boolean isBatchSizeError(BigQueryException exception) { if (exception.getCode() == BAD_REQUEST_CODE && exception.getError() == null && exception.getReason() == null) { /* * 400 with no error or reason represents a request that is more than 10MB. This is not * documented but is referenced slightly under "Error codes" here: * https://cloud.google.com/bigquery/quota-policy * (by decreasing the batch size we can eventually expect to end up with a request under 10MB) */ return true; } else if (exception.getCode() == BAD_REQUEST_CODE && INVALID_REASON.equals(exception.getReason())) { /* * this is the error that the documentation claims google will return if a request exceeds * 10MB. if this actually ever happens... * todo distinguish this from other invalids (like invalid table schema). */ return true; } return false; }
@Override public boolean deleteTable(String projectId, String datasetId, String tableId) { try { bigquery.tables().delete(projectId, datasetId, tableId).execute(); return true; } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { return false; } throw serviceException; } }
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); } }
private boolean isTableMissingSchema(BigQueryException exception) { // If a table is missing a schema, it will raise a BigQueryException that the input is invalid // For more information about BigQueryExceptions, see: https://cloud.google.com/bigquery/troubleshooting-errors return exception.getReason() != null && exception.getReason().equalsIgnoreCase("invalid"); }
public BigQueryException(IOException exception) { super(exception, true); BigQueryError error = null; if (reason() != null) { error = new BigQueryError(reason(), location(), getMessage(), debugInfo()); } this.error = error; }
/** Example usage of {@code waitFor()}. */ // [TARGET waitFor(RetryOption...)] public boolean waitFor() throws InterruptedException { try { // [START ] 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 } // [END ] } catch (BigQueryException e) { // Timeouts shouldn't happen without a timeout option. if (e.getCause() instanceof PollException) { return false; } throw e; } return true; }
@Test public void testUpdateNonExistingTable() { TableInfo tableInfo = TableInfo.of( TableId.of(DATASET, "test_update_non_existing_table"), StandardTableDefinition.of(SIMPLE_SCHEMA)); try { bigquery.update(tableInfo); fail("BigQueryException was expected"); } catch (BigQueryException e) { BigQueryError error = e.getError(); assertNotNull(error); assertEquals("notFound", error.getReason()); assertNotNull(error.getMessage()); } }
@Test public void testRetryableException() { EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(500, "InternalError")) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); Dataset dataset = bigquery.getDataset(DATASET); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); }
static BaseServiceException translateAndThrow(ExecutionException ex) { BaseServiceException.translate(ex); throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }