public static <T> T retryAzureOperation(Task<T> f, int maxTries) throws Exception { return RetryUtils.retry(f, AZURE_RETRY, maxTries); } }
/** * Retries S3 operations that fail due to io-related exceptions. Service-level exceptions (access denied, file not * found, etc) are not retried. */ public static <T> T retryS3Operation(Task<T> f) throws Exception { final int maxTries = 10; return RetryUtils.retry(f, S3RETRY, maxTries); }
/** * Retries CloudFiles operations that fail due to io-related exceptions. */ public static <T> T retryCloudFilesOperation(Task<T> f, final int maxTries) throws Exception { return RetryUtils.retry(f, CLOUDFILESRETRY, maxTries); }
public static <T> T retry( final Task<T> f, final Predicate<Throwable> shouldRetry, final int quietTries, final int maxTries ) throws Exception { return retry(f, shouldRetry, quietTries, maxTries, null, null); }
public static <T> T retry( final Task<T> f, final Predicate<Throwable> shouldRetry, final CleanupAfterFailure onEachFailure, final int maxTries, final String messageOnRetry ) throws Exception { return retry(f, shouldRetry, 0, maxTries, onEachFailure, messageOnRetry); }
public <T> T retryWithHandle( HandleCallback<T> callback, Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, MAX_RETRIES); } catch (Exception e) { throw new RuntimeException(e); } }
public <T> T retryTransaction(final TransactionCallback<T> callback, final int quietTries, final int maxTries) { try { return RetryUtils.retry(() -> getDBI().inTransaction(TransactionIsolationLevel.READ_COMMITTED, callback), shouldRetry, quietTries, maxTries); } catch (Exception e) { throw Throwables.propagate(e); } }
public static boolean deleteWithRetry(final FileSystem fs, final Path path, final boolean recursive) { try { return RetryUtils.retry( () -> fs.delete(path, recursive), shouldRetryPredicate(), NUM_RETRIES ); } catch (Exception e) { log.error(e, "Failed to cleanup path[%s]", path); throw Throwables.propagate(e); } }
public <T> T retryWithHandle( final HandleCallback<T> callback, final Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, DEFAULT_MAX_TRIES); } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Matches based on a pattern in the file name. Returns the file with the latest timestamp. * * @param uri If it is a file, then the parent is searched. If it is a directory, then the directory is searched. * @param pattern The matching filter to down-select the file names in the directory of interest. Passing `null` * results in matching any file * * @return The URI of the most recently modified file which matches the pattern, or `null` if it cannot be found */ @Override public URI getLatestVersion(URI uri, final @Nullable Pattern pattern) { final File file = new File(uri); try { return RetryUtils.retry( () -> mostRecentInDir( file.isDirectory() ? file.toPath() : file.getParentFile().toPath(), pattern ), shouldRetryPredicate(), DEFAULT_RETRY_COUNT ); } catch (Exception e) { if (e instanceof FileNotFoundException) { return null; } throw Throwables.propagate(e); } }
/** * Downloads an object. It retries downloading {@link #maxFetchRetry} * times and throws an exception. * * @param object an object to be downloaded * @param outFile a file which the object data is stored * * @return number of downloaded bytes */ @Override protected long download(T object, File outFile) throws IOException { try { return RetryUtils.retry( () -> { try (final InputStream is = openObjectFunction.open(object); final OutputStream os = new FileOutputStream(outFile)) { return IOUtils.copyLarge(is, os, buffer); } }, retryCondition, outFile::delete, maxFetchRetry + 1, StringUtils.format("Failed to download object[%s]", object) ); } catch (Exception e) { throw new IOException(e); } }
private void deleteWithRetries(final String s3Bucket, final String s3Path) throws Exception { RetryUtils.retry( () -> { try { s3Client.deleteObject(s3Bucket, s3Path); return null; } catch (Exception e) { log.info(e, "Error while trying to delete [s3://%s/%s]", s3Bucket, s3Path); throw e; } }, S3Utils.S3RETRY, 3 ); } }
private void pushTaskFile(final File logFile, final String taskKey) throws IOException { FileInputStream fileStream = new FileInputStream(logFile); InputStreamContent mediaContent = new InputStreamContent("text/plain", fileStream); mediaContent.setLength(logFile.length()); try { RetryUtils.retry( (RetryUtils.Task<Void>) () -> { storage.insert(config.getBucket(), taskKey, mediaContent); return null; }, GoogleUtils::isRetryable, 1, 5 ); } catch (IOException e) { throw e; } catch (Exception e) { throw new RE(e, "Failed to upload [%s] to [%s]", logFile, taskKey); } }
@Test public void testImmediateSuccess() throws Exception { final AtomicInteger count = new AtomicInteger(); final String result = RetryUtils.retry( () -> { count.incrementAndGet(); return "hey"; }, isTransient, 2 ); Assert.assertEquals("result", "hey", result); Assert.assertEquals("count", 1, count.get()); }
@Test public void testEventualFailure() throws Exception { final AtomicInteger count = new AtomicInteger(); boolean threwExpectedException = false; try { RetryUtils.retry( () -> { count.incrementAndGet(); throw new IOException("what"); }, isTransient, 2 ); } catch (IOException e) { threwExpectedException = e.getMessage().equals("what"); } Assert.assertTrue("threw expected exception", threwExpectedException); Assert.assertEquals("count", 2, count.get()); }
@Test public void testEventualSuccess() throws Exception { final AtomicInteger count = new AtomicInteger(); final String result = RetryUtils.retry( () -> { if (count.incrementAndGet() >= 2) { return "hey"; } else { throw new IOException("what"); } }, isTransient, 3 ); Assert.assertEquals("result", "hey", result); Assert.assertEquals("count", 2, count.get()); }
@Test public void testExceptionPredicateNotMatching() throws Exception { final AtomicInteger count = new AtomicInteger(); boolean threwExpectedException = false; try { RetryUtils.retry( () -> { if (count.incrementAndGet() >= 2) { return "hey"; } else { throw new IOException("uhh"); } }, isTransient, 3 ); } catch (IOException e) { threwExpectedException = e.getMessage().equals("uhh"); } Assert.assertTrue("threw expected exception", threwExpectedException); Assert.assertEquals("count", 1, count.get()); } }
private void deleteIfPresent(String bucket, String path) throws IOException { try { RetryUtils.retry( (RetryUtils.Task<Void>) () -> { storage.delete(bucket, path); return null; }, GoogleUtils::isRetryable, 1, 5 ); } catch (HttpResponseException e) { if (e.getStatusCode() != 404) { throw e; } LOG.debug("Already deleted: [%s] [%s]", bucket, path); } catch (IOException ioe) { throw ioe; } catch (Exception e) { throw new RE(e, "Failed to delete [%s] [%s]", bucket, path); } }
public void insert(final File file, final String contentType, final String path) throws IOException { LOG.info("Inserting [%s] to [%s]", file, path); try { RetryUtils.retry( (RetryUtils.Task<Void>) () -> { storage.insert(config.getBucket(), path, new FileContent(contentType, file)); return null; }, GoogleUtils::isRetryable, 1, 5 ); } catch (IOException e) { throw e; } catch (Exception e) { throw new RE(e, "Failed to upload [%s] to [%s]", file, path); } }
@Nullable private UserAndRoleMap fetchUserAndRoleMapFromCoordinator(String prefix, boolean isInit) { try { return RetryUtils.retry( () -> { return tryFetchMapsFromCoordinator(prefix); }, e -> true, commonCacheConfig.getMaxSyncRetries() ); } catch (Exception e) { LOG.makeAlert(e, "Encountered exception while fetching user and role map for authorizer [%s]", prefix).emit(); if (isInit) { if (commonCacheConfig.getCacheDirectory() != null) { try { LOG.info("Attempting to load user map snapshot from disk."); return loadUserAndRoleMapFromDisk(prefix); } catch (Exception e2) { e2.addSuppressed(e); LOG.makeAlert(e2, "Encountered exception while loading user-role map snapshot for authorizer [%s]", prefix) .emit(); } } } return null; } }