/** * <p> * Create a proxy for an interface of an implementation class * using the same retry policy for each method in the interface. * </p> * @param iface the interface that the retry will implement * @param implementation the instance whose methods should be retried * @param retryPolicy the policy for retrying method call failures * @return the retry proxy */ public static <T> Object create(Class<T> iface, T implementation, RetryPolicy retryPolicy) { return RetryProxy.create(iface, new DefaultFailoverProxyProvider<T>(iface, implementation), retryPolicy); }
/** * Create a proxy for an interface of an implementation class * using the a set of retry policies specified by method name. * If no retry policy is defined for a method then a default of * {@link RetryPolicies#TRY_ONCE_THEN_FAIL} is used. * * @param iface the interface that the retry will implement * @param implementation the instance whose methods should be retried * @param methodNameToPolicyMap a map of method names to retry policies * @return the retry proxy */ public static <T> Object create(Class<T> iface, T implementation, Map<String,RetryPolicy> methodNameToPolicyMap) { return create(iface, new DefaultFailoverProxyProvider<T>(iface, implementation), methodNameToPolicyMap, RetryPolicies.TRY_ONCE_THEN_FAIL); }
/** * Writes to filesystem serialized form of segment descriptor if an existing file exists it will try to replace it. * * @param outputFS filesystem. * @param segment DataSegment object. * @param descriptorPath path. * * @throws IOException in case any IO issues occur. */ public static void writeSegmentDescriptor(final FileSystem outputFS, final DataSegment segment, final Path descriptorPath) throws IOException { final DataPusher descriptorPusher = (DataPusher) RetryProxy.create(DataPusher.class, () -> { if (outputFS.exists(descriptorPath)) { if (!outputFS.delete(descriptorPath, false)) { throw new IOException(String.format("Failed to delete descriptor at [%s]", descriptorPath)); } } try (final OutputStream descriptorOut = outputFS.create(descriptorPath, true, DEFAULT_FS_BUFFER_SIZE)) { JSON_MAPPER.writeValue(descriptorOut, segment); descriptorOut.flush(); } }, RetryPolicies.exponentialBackoffRetry(NUM_RETRIES, SECONDS_BETWEEN_RETRIES, TimeUnit.SECONDS)); descriptorPusher.push(); }
final DataPusher zipPusher = (DataPusher) RetryProxy.create( DataPusher.class, new DataPusher()
final DataPusher descriptorPusher = (DataPusher) RetryProxy.create( DataPusher.class, new DataPusher()
final DataPusher zipPusher = (DataPusher) RetryProxy.create( DataPusher.class, new DataPusher()
private static NamenodeProtocol createNNProxyWithNamenodeProtocol( InetSocketAddress address, Configuration conf, UserGroupInformation ugi, boolean withRetries) throws IOException { NamenodeProtocolPB proxy = (NamenodeProtocolPB) createNameNodeProxy( address, conf, ugi, NamenodeProtocolPB.class, 0); if (withRetries) { // create the proxy with retries RetryPolicy timeoutPolicy = RetryPolicies.exponentialBackoffRetry(5, 200, TimeUnit.MILLISECONDS); Map<String, RetryPolicy> methodNameToPolicyMap = new HashMap<String, RetryPolicy>(); methodNameToPolicyMap.put("getBlocks", timeoutPolicy); methodNameToPolicyMap.put("getAccessKeys", timeoutPolicy); NamenodeProtocol translatorProxy = new NamenodeProtocolTranslatorPB(proxy); return (NamenodeProtocol) RetryProxy.create( NamenodeProtocol.class, translatorProxy, methodNameToPolicyMap); } else { return new NamenodeProtocolTranslatorPB(proxy); } }
@SuppressWarnings("unchecked") protected static <T> T createRetriableProxy(final Configuration conf, final Class<T> protocol, final UserGroupInformation user, final YarnRPC rpc, final InetSocketAddress serverAddress, RetryPolicy retryPolicy) { T proxy = user.doAs(new PrivilegedAction<T>() { @Override public T run() { return (T) rpc.getProxy(protocol, serverAddress, conf); } }); return (T) RetryProxy.create(protocol, proxy, retryPolicy); } }
@SuppressWarnings("unchecked") protected static <T> T createRetriableProxy(final Configuration conf, final Class<T> protocol, final UserGroupInformation user, final YarnRPC rpc, final InetSocketAddress serverAddress, RetryPolicy retryPolicy) { T proxy = user.doAs(new PrivilegedAction<T>() { @Override public T run() { return (T) rpc.getProxy(protocol, serverAddress, conf); } }); return (T) RetryProxy.create(protocol, proxy, retryPolicy); } }
@SuppressWarnings("unchecked") protected static <T> T createRetriableProxy(final Configuration conf, final Class<T> protocol, final UserGroupInformation user, final YarnRPC rpc, final InetSocketAddress serverAddress, RetryPolicy retryPolicy) { T proxy = user.doAs(new PrivilegedAction<T>() { @Override public T run() { return (T) rpc.getProxy(protocol, serverAddress, conf); } }); return (T) RetryProxy.create(protocol, proxy, retryPolicy); } }
private static <T> T newProxyInstance(final YarnConfiguration conf, final Class<T> protocol, RMProxy<T> instance, RetryPolicy retryPolicy) throws IOException{ if (HAUtil.isHAEnabled(conf) || HAUtil.isFederationEnabled(conf)) { RMFailoverProxyProvider<T> provider = instance.createRMFailoverProxyProvider(conf, protocol); return (T) RetryProxy.create(protocol, provider, retryPolicy); } else { InetSocketAddress rmAddress = instance.getRMAddress(conf, protocol); LOG.info("Connecting to ResourceManager at " + rmAddress); T proxy = instance.getProxy(conf, protocol, rmAddress); return (T) RetryProxy.create(protocol, proxy, retryPolicy); } }
public void testRetryByRemoteException() throws UnreliableException { Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap = Collections.<Class<? extends Exception>, RetryPolicy>singletonMap(FatalException.class, TRY_ONCE_THEN_FAIL); UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, retryByRemoteException(RETRY_FOREVER, exceptionToPolicyMap)); try { unreliable.alwaysFailsWithRemoteFatalException(); fail("Should fail"); } catch (RemoteException e) { // expected } }
public void testRetryForever() throws UnreliableException { UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, RETRY_FOREVER); unreliable.alwaysSucceeds(); unreliable.failsOnceThenSucceeds(); unreliable.failsTenTimesThenSucceeds(); }
private static <T> T getProxy(final Configuration conf, final YarnRPC rpc, final UserGroupInformation user, final InetSocketAddress serverAddress, final Class<T> protocol, RetryPolicy retryPolicy) { YarnClient client = YarnClient.createYarnClient(); client.init(conf); client.start(); T proxy = user.doAs((PrivilegedAction<T>) () -> (T) rpc.getProxy(protocol, serverAddress, conf)); return (T) RetryProxy.create(protocol, proxy, retryPolicy); }
public void testTryOnceThenFail() throws UnreliableException { UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, TRY_ONCE_THEN_FAIL); unreliable.alwaysSucceeds(); try { unreliable.failsOnceThenSucceeds(); fail("Should fail"); } catch (UnreliableException e) { // expected } }
@SuppressWarnings("unchecked") protected T createRetriableProxy() { try { // Create proxy that can retry exceptions properly. RetryPolicy retryPolicy = RMProxy.createRetryPolicy(conf, false); InetSocketAddress rmAddress = rmProxy.getRMAddress(conf, protocol); T proxy = rmProxy.getProxy(conf, protocol, rmAddress); return (T) RetryProxy.create(protocol, proxy, retryPolicy); } catch (IOException ioe) { LOG.error("Unable to create proxy to the ResourceManager " + HAUtil.getRMHAId(conf), ioe); return null; } }
public void testTryOnceDontFail() throws UnreliableException { UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, TRY_ONCE_DONT_FAIL); unreliable.alwaysSucceeds(); unreliable.failsOnceThenSucceeds(); try { unreliable.failsOnceThenSucceedsWithReturnValue(); fail("Should fail"); } catch (UnreliableException e) { // expected } }
public void testExponentialRetry() throws UnreliableException { UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, exponentialBackoffRetry(5, 1L, TimeUnit.NANOSECONDS)); unreliable.alwaysSucceeds(); unreliable.failsOnceThenSucceeds(); try { unreliable.failsTenTimesThenSucceeds(); fail("Should fail"); } catch (UnreliableException e) { // expected } }
public void testRetryUpToMaximumTimeWithFixedSleep() throws UnreliableException { UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, retryUpToMaximumTimeWithFixedSleep(80, 10, TimeUnit.NANOSECONDS)); unreliable.alwaysSucceeds(); unreliable.failsOnceThenSucceeds(); try { unreliable.failsTenTimesThenSucceeds(); fail("Should fail"); } catch (UnreliableException e) { // expected } }
public void testRetryUpToMaximumCountWithProportionalSleep() throws UnreliableException { UnreliableInterface unreliable = (UnreliableInterface) RetryProxy.create(UnreliableInterface.class, unreliableImpl, retryUpToMaximumCountWithProportionalSleep(8, 1, TimeUnit.NANOSECONDS)); unreliable.alwaysSucceeds(); unreliable.failsOnceThenSucceeds(); try { unreliable.failsTenTimesThenSucceeds(); fail("Should fail"); } catch (UnreliableException e) { // expected } }