mirror of
https://github.com/google/nomulus.git
synced 2025-05-03 05:27:52 +02:00
More information: [] Tested: $ [] test //third_party/java_src/gtld/javatests/google/registry/bigquery:BigqueryUtilsTest //third_party/java_src/gtld/javatests/google/registry/cron:CommitLogFanoutActionTest //third_party/java_src/gtld/javatests/google/registry/dns:ReadDnsQueueActionTest //third_party/java_src/gtld/javatests/google/registry/documentation:FlowExceptionsTest //third_party/java_src/gtld/javatests/google/registry/export:BigqueryPollJobActionTest //third_party/java_src/gtld/javatests/google/registry/export:DatastoreBackupServiceTest //third_party/java_src/gtld/javatests/google/registry/export:ExportSnapshotServletTest //third_party/java_src/gtld/javatests/google/registry/export:SyncGroupMembersActionTest //third_party/java_src/gtld/javatests/google/registry/export:UpdateSnapshotViewActionTest //third_party/java_src/gtld/javatests/google/registry/flows:EppConsoleAsAdminServletTest //third_party/java_src/gtld/javatests/google/registry/flows:EppControllerTest //third_party/java_src/gtld/javatests/google/registry/flows:EppTlsServletTest //third_party/java_src/gtld/javatests/google/registry/flows:contact/ContactCheckFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:contact/ContactTransferCancelFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:domain/DomainAllocateFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:domain/DomainApplicationDeleteFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:domain/DomainDeleteFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:domain/DomainRestoreRequestFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:domain/DomainTransferApproveFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:host/HostCreateFlowTest //third_party/java_src/gtld/javatests/google/registry/flows:session/LoginFlowViaTlsTest //third_party/java_src/gtld/javatests/google/registry/flows:session/LogoutFlowTest //third_party/java_src/gtld/javatests/google/registry/[]/inputs:EppResourceInputsTest //third_party/java_src/gtld/javatests/google/registry/model:CreateAutoTimestampTest //third_party/java_src/gtld/javatests/google/registry/model:RoidSuffixesTest //third_party/java_src/gtld/javatests/google/registry/model:UpdateAutoTimestampTest //third_party/java_src/gtld/javatests/google/registry/model:billing/BillingEventTest //third_party/java_src/gtld/javatests/google/registry/model:contact/ContactResourceTest //third_party/java_src/gtld/javatests/google/registry/model:domain/DomainApplicationTest //third_party/java_src/gtld/javatests/google/registry/model:domain/DomainResourceTest //third_party/java_src/gtld/javatests/google/registry/model:export/LogsExportCursorTest //third_party/java_src/gtld/javatests/google/registry/model:ofy/OfyCommitLogTest //third_party/java_src/gtld/javatests/google/registry/model:ofy/OfyTest //third_party/java_src/gtld/javatests/google/registry/model:poll/PollMessageExternalKeyConverterTest //third_party/java_src/gtld/javatests/google/registry/model:rde/RdeNamingUtilsTest //third_party/java_src/gtld/javatests/google/registry/model:registry/RegistryCursorTest //third_party/java_src/gtld/javatests/google/registry/model:registry/label/PremiumListTest //third_party/java_src/gtld/javatests/google/registry/model:registry/label/ReservedListTest //third_party/java_src/gtld/javatests/google/registry/model:server/LockTest //third_party/java_src/gtld/javatests/google/registry/model:smd/SignedMarkRevocationListTest //third_party/java_src/gtld/javatests/google/registry/model:tmch/ClaimsListShardTest //third_party/java_src/gtld/javatests/google/registry/monitoring/blackbox:EppClientConnectionTest //third_party/java_src/gtld/javatests/google/registry/monitoring/blackbox:EppRunnableProberTest //third_party/java_src/gtld/javatests/google/registry/monitoring/blackbox:WhoisQueryTest //third_party/java_src/gtld/javatests/google/registry/rdap:RdapActionBaseTest //third_party/java_src/gtld/javatests/google/registry/rdap:RdapJsonFormatterTest //third_party/java_src/gtld/javatests/google/registry/rdap:RdapSearchPatternTest //third_party/java_src/gtld/javatests/google/registry/rde:DomainResourceToXjcConverterTest //third_party/java_src/gtld/javatests/google/registry/request:JsonResponseTest //third_party/java_src/gtld/javatests/google/registry/tmch:NordnVerifyActionTest //third_party/java_src/gtld/javatests/google/registry/tmch:TmchCrlActionTest //third_party/java_src/gtld/javatests/google/registry/tools/params:DurationParameterTest //third_party/java_src/gtld/javatests/google/registry/tools/params:MoneyParameterTest //third_party/java_src/gtld/javatests/google/registry/tools/params:PathParameterTest //third_party/java_src/gtld/javatests/google/registry/tools/server:CreateGroupsActionTest //third_party/java_src/gtld/javatests/google/registry/tools/server:DeleteEntityActionTest //third_party/java_src/gtld/javatests/google/registry/tools:CreateContactCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:CreateCreditBalanceCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:CreateReservedListCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:CreateTldCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:DeletePremiumListCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:DomainApplicationInfoCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:DomainCheckCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:EncryptEscrowDepositCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:GenerateAuctionDataCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:GetDomainCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:GetTldCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:ListDomainsCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:ListRegistrarsCommandTest //third_party/java_src/gtld/javatests/google/registry/tools:RegistryToolEnvironmentTest //third_party/java_src/gtld/javatests/google/registry/ui/server/registrar:RegistrarServletTest //third_party/java_src/gtld/javatests/google/registry/util:RetrierTest //third_party/java_src/gtld/javatests/google/registry/util:SendEmailUtilsTest //third_party/java_src/gtld/javatests/google/registry/util:UrlFetchUtilsTest //third_party/java_src/gtld/javatests/google/registry/xjc:XjcObjectTest [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=121294781
111 lines
4.3 KiB
Java
111 lines
4.3 KiB
Java
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
|
//
|
|
// 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 google.registry.util;
|
|
|
|
import static com.google.appengine.api.search.checkers.Preconditions.checkArgument;
|
|
import static com.google.common.base.Throwables.throwIfUnchecked;
|
|
import static com.google.common.collect.Iterables.any;
|
|
import static com.google.common.math.IntMath.pow;
|
|
import static google.registry.util.PredicateUtils.supertypeOf;
|
|
|
|
import com.google.common.base.Predicate;
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
import google.registry.config.ConfigModule.Config;
|
|
|
|
import org.joda.time.Duration;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.Set;
|
|
import java.util.concurrent.Callable;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
/** Wrapper that does retry with exponential backoff. */
|
|
public class Retrier implements Serializable {
|
|
|
|
private static final long serialVersionUID = 1167386907195735483L;
|
|
|
|
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
|
|
|
private final Sleeper sleeper;
|
|
private final int attempts;
|
|
|
|
@Inject
|
|
public Retrier(Sleeper sleeper, @Config("transientFailureRetries") int transientFailureRetries) {
|
|
this.sleeper = sleeper;
|
|
checkArgument(transientFailureRetries > 0, "Number of attempts must be positive");
|
|
this.attempts = transientFailureRetries;
|
|
}
|
|
|
|
/**
|
|
* Retries a unit of work in the face of transient errors.
|
|
*
|
|
* <p>Retrying is done a fixed number of times, with exponential backoff, if the exception that is
|
|
* thrown is deemed retryable by the predicate. If the error is not considered retryable, or if
|
|
* the thread is interrupted, or if the allowable number of attempts has been exhausted, the
|
|
* original exception is propagated through to the caller.
|
|
*
|
|
* @return <V> the value returned by the {@link Callable}.
|
|
*/
|
|
public final <V> V callWithRetry(Callable<V> callable, Predicate<Throwable> isRetryable) {
|
|
int failures = 0;
|
|
while (true) {
|
|
try {
|
|
return callable.call();
|
|
} catch (Throwable e) {
|
|
if (++failures == attempts || !isRetryable.apply(e)) {
|
|
throwIfUnchecked(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
logger.info(e, "Retrying transient error, attempt " + failures);
|
|
try {
|
|
// Wait 100ms on the first attempt, doubling on each subsequent attempt.
|
|
sleeper.sleep(Duration.millis(pow(2, failures) * 100));
|
|
} catch (InterruptedException e2) {
|
|
// Since we're not rethrowing InterruptedException, set the interrupt state on the thread
|
|
// so the next blocking operation will know to abort the thread.
|
|
Thread.currentThread().interrupt();
|
|
throwIfUnchecked(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retries a unit of work in the face of transient errors.
|
|
*
|
|
* <p>Retrying is done a fixed number of times, with exponential backoff, if the exception that is
|
|
* thrown is on a whitelist of retryable errors. If the error is not on the whitelist, or if the
|
|
* thread is interrupted, or if the allowable number of attempts has been exhausted, the original
|
|
* exception is propagated through to the caller.
|
|
*
|
|
* @return <V> the value returned by the {@link Callable}.
|
|
*/
|
|
@SafeVarargs
|
|
public final <V> V callWithRetry(
|
|
Callable<V> callable,
|
|
Class<? extends RuntimeException> retryableError,
|
|
Class<? extends RuntimeException>... moreRetryableErrors) {
|
|
final Set<Class<?>> retryables =
|
|
new ImmutableSet.Builder<Class<?>>().add(retryableError).add(moreRetryableErrors).build();
|
|
return callWithRetry(callable, new Predicate<Throwable>() {
|
|
@Override
|
|
public boolean apply(Throwable e) {
|
|
return any(retryables, supertypeOf(e.getClass()));
|
|
}});
|
|
}
|
|
}
|