public boolean isBackOff() { count++; if (count > lastBackOff) { try { lastBackOff = backOff.nextBackOffMillis(); } catch (IOException e) { // we don't care IOException here } return false; } else { return true; } }
private static long nextBackOffMillis(BackOff backoff) throws SpannerException { try { return backoff.nextBackOffMillis(); } catch (IOException e) { throw newSpannerException(ErrorCode.INTERNAL, e.getMessage(), e); } }
private long getBackOffTime() { long backOffMillis = BackOff.STOP; try { backOffMillis = backOff.nextBackOffMillis(); } catch (IOException e) { // Use the default. } return backOffMillis; }
@Override public long nextBackOffMillis() throws IOException { if (retriesAttempted >= maxRetries) { return BackOff.STOP; } long next = backoff.nextBackOffMillis(); if (next == BackOff.STOP) { return BackOff.STOP; } retriesAttempted++; return next; } }
@Override public long nextBackOffMillis() throws IOException { if (retriesAttempted >= maxRetries) { return BackOff.STOP; } long next = backoff.nextBackOffMillis(); if (next == BackOff.STOP) { return BackOff.STOP; } retriesAttempted++; return next; } }
private static long nextBackOffMillis(BackOff backoff) throws SpannerException { try { return backoff.nextBackOffMillis(); } catch (IOException e) { throw newSpannerException(ErrorCode.INTERNAL, e.getMessage(), e); } }
@VisibleForTesting void waitForReplication(BigtableTableName tableName, BackOff backOff) throws InterruptedException, TimeoutException { String token = generateConsistencyToken(tableName); while (!checkConsistency(tableName, token)) { long backOffMillis; try { backOffMillis = backOff.nextBackOffMillis(); } catch (IOException e) { // Should never happen, we only use ExponentialBackOff which doesn't throw. throw new RuntimeException("Problem getting backoff: " + e); } if (backOffMillis == BackOff.STOP) { throw new TimeoutException( "Table " + tableName.toString() + " is not consistent after timeout."); } else { // sleep for backOffMillis milliseconds and retry operation. Thread.sleep(backOffMillis); } } }
/** * Determines the amount to sleep for and sleeps if needed. * * @param backoff BackOff to determine how long to sleep for * @param sleeper Used to sleep * @param currentException exception that caused the retry and sleep. For logging. * @throws InterruptedException if sleep is interrupted */ private static boolean nextSleep(BackOff backoff, Sleeper sleeper, Exception currentException) throws InterruptedException { long backOffTime; try { backOffTime = backoff.nextBackOffMillis(); } catch (IOException e) { throw new RuntimeException("Failed to to get next back off time", e); } if (backOffTime == BackOff.STOP) { return false; } logger.atInfo().withCause(currentException).log( "Transient exception caught. Sleeping for %d, then retrying.", backOffTime); sleeper.sleep(backOffTime); return true; }
/** * Determines the amount to sleep for and sleeps if needed. * * @param backoff BackOff to determine how long to sleep for * @param sleeper Used to sleep * @param currentException exception that caused the retry and sleep. For logging. * @throws InterruptedException if sleep is interrupted */ private static boolean nextSleep(BackOff backoff, Sleeper sleeper, Exception currentException) throws InterruptedException { long backOffTime; try { backOffTime = backoff.nextBackOffMillis(); } catch (IOException e) { throw new RuntimeException("Failed to to get next back off time", e); } if (backOffTime == BackOff.STOP) { return false; } logger.atInfo().withCause(currentException).log( "Transient exception caught. Sleeping for %d, then retrying.", backOffTime); sleeper.sleep(backOffTime); return true; }
long sleep = backoff.nextBackOffMillis(); if (sleep == BackOff.STOP) { LOG.error(
@Test public void stopsAfterAttempts() throws Exception { BackOff backoff = new RetryBoundedBackOff(5, new BackOffTester()); for (int i = 0; i < 5; i++) { assertThat(i + 1).isEqualTo(backoff.nextBackOffMillis()); } assertThat(backoff.nextBackOffMillis()).isEqualTo(BackOff.STOP); assertThat(backoff.nextBackOffMillis()).isEqualTo(BackOff.STOP); }
@Test public void resetsCorrectly() throws Exception { BackOff backoff = new RetryBoundedBackOff(5, new BackOffTester()); for (int i = 0; i < 5; i++) { assertThat(i + 1).isEqualTo(backoff.nextBackOffMillis()); } assertThat(backoff.nextBackOffMillis()).isEqualTo(BackOff.STOP); assertThat(backoff.nextBackOffMillis()).isEqualTo(BackOff.STOP); backoff.reset(); for (int i = 0; i < 3; i++) { assertThat(i + 1).isEqualTo(backoff.nextBackOffMillis()); } backoff.reset(); for (int i = 0; i < 5; i++) { assertThat(i + 1).isEqualTo(backoff.nextBackOffMillis()); } assertThat(backoff.nextBackOffMillis()).isEqualTo(BackOff.STOP); assertThat(backoff.nextBackOffMillis()).isEqualTo(BackOff.STOP); }
LOG.info("BigQuery insertAll exceeded rate limit, retrying"); try { sleeper.sleep(backoff1.nextBackOffMillis()); } catch (InterruptedException interrupted) { throw new IOException( break; long nextBackoffMillis = backoff.nextBackOffMillis(); if (nextBackoffMillis == BackOff.STOP) { break;
@Test public void testRetryFileSizeNonBatch() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class); Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class); BackOff mockBackOff = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.withMaxRetries(2).backoff()); when(mockStorage.objects()).thenReturn(mockStorageObjects); when(mockStorageObjects.get("testbucket", "testobject")).thenReturn(mockStorageGet); when(mockStorageGet.execute()) .thenThrow(new SocketTimeoutException("SocketException")) .thenThrow(new SocketTimeoutException("SocketException")) .thenReturn(new StorageObject().setSize(BigInteger.valueOf(1000))); assertEquals( 1000, gcsUtil .getObject( GcsPath.fromComponents("testbucket", "testobject"), mockBackOff, new FastNanoClockAndSleeper()) .getSize() .longValue()); assertEquals(BackOff.STOP, mockBackOff.nextBackOffMillis()); }
backOffSleep = backOff.nextBackOffMillis();
long millisToWait = pollBackOff.nextBackOffMillis(); if (millisToWait == BackOff.STOP) { throw new IOException(
nextSleep = nextSleep == BackOff.STOP ? BackOff.STOP : backOff.nextBackOffMillis(); } while (!existingInfo.exists() && nextSleep != BackOff.STOP);