Skip to content

Instantly share code, notes, and snippets.

@timrobertson100
Created March 27, 2018 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrobertson100/9d653ab82af74f6b709f2bbf25cafa00 to your computer and use it in GitHub Desktop.
Save timrobertson100/9d653ab82af74f6b709f2bbf25cafa00 to your computer and use it in GitHub Desktop.
AuthorizedSolrClient update
void processWithRetry(
String collection, UpdateRequest request, int numberAttempts, Duration maxDuration)
throws IOException, InterruptedException {
// TODO: sanitize those params
request.setBasicAuthCredentials(username, password);
Sleeper sleeper = Sleeper.DEFAULT;
// Note: FluentBackoff counts retries excluding the original while we count attempts
// to remove any notion of ambiguity (hence the -1)
BackOff backoff =
FluentBackoff.DEFAULT
.withMaxRetries(numberAttempts - 1)
.withInitialBackoff(MIN_BACKOFF_SECONDS)
.withMaxCumulativeBackoff(maxDuration)
.backoff();
int attempt = 0;
while (true) {
attempt++;
try {
request.process(solrClient, collection);
} catch (HttpSolrClient.RemoteSolrException | SolrServerException | IOException exception) {
if (!BackOffUtils.next(sleeper, backoff)) {
throw new IOException("Error writing to Solr after " + attempt + " attempts", exception);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment