Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.client.hotrod.impl.operations;
  
 
 import java.util.Set;
Base class for all the operations that need retry logic: if the operation fails due to connection problems, try with another available connection.

Parameters:
T the return type of this operation
Author(s):
Mircea.Markus@jboss.com
Since:
4.1
 
 public abstract class RetryOnFailureOperation<T> extends HotRodOperation {
 
    private static final Log log = LogFactory.getLog(RetryOnFailureOperation.classLog.class);
 
    protected final TransportFactory transportFactory;
 
    private boolean triedCompleteRestart = false;
 
    protected RetryOnFailureOperation(Codec codecTransportFactory transportFactory,
             byte[] cacheNameAtomicInteger topologyIdFlag[] flags) {
       super(codecflagscacheNametopologyId);
       this. = transportFactory;
    }
 
    @Override
    public T execute() {
       int retryCount = 0;
       Set<SocketAddressfailedServers = null;
       while (shouldRetry(retryCount)) {
          Transport transport = null;
          try {
             // Transport retrieval should be retried
             transport = getTransport(retryCountfailedServers);
             return executeOperation(transport);
          } catch (TransportException te) {
             SocketAddress address = te.getServerAddress();
             failedServers = updateFailedServers(addressfailedServers);
             // Invalidate transport since this exception means that this
             // instance is no longer usable and should be destroyed.
             invalidateTransport(transportaddress);
             retryCount = logTransportErrorAndThrowExceptionIfNeeded(retryCountte);
          } catch (RemoteIllegalLifecycleStateException e) {
             SocketAddress address = e.getServerAddress();
             failedServers = updateFailedServers(addressfailedServers);
             // Invalidate transport since this exception means that this
             // instance is no longer usable and should be destroyed.
             invalidateTransport(transportaddress);
             retryCount = logTransportErrorAndThrowExceptionIfNeeded(retryCounte);
          } catch (RemoteNodeSuspectException e) {
             // Do not invalidate transport because this exception is caused
             // as a result of a server finding out that another node has
             // been suspected, so there's nothing really wrong with the server
             // from which this node was received.
             logErrorAndThrowExceptionIfNeeded(retryCounte);
          } finally {
             releaseTransport(transport);
          }
 
          retryCount++;
       }
       throw new IllegalStateException("We should not reach here!");
    }
 
    private void invalidateTransport(Transport transportSocketAddress address) {
       if (transport != null) {
          if (.isTraceEnabled())
             .tracef("Invalidating transport %s as a result of transport exception"transport);
 
          .invalidateTransport(addresstransport);
       }
    }
 
    private Set<SocketAddressupdateFailedServers(SocketAddress addressSet<SocketAddressfailedServers) {
       if (failedServers == null) {
          failedServers = new HashSet<SocketAddress>();
       }
 
       if (.isTraceEnabled())
          .tracef("Add %s to failed servers"address);
 
       failedServers.add(address);
      return failedServers;
   }
   protected boolean shouldRetry(int retryCount) {
      return retryCount <= .getMaxRetries();
   }
      String message = "Exception encountered. Retry %d out of %d";
      if (i >= .getMaxRetries() || .getMaxRetries() < 0) {
         if (!) {
            .debug("Cluster might have completely shut down, try resetting transport layer and topology id"e);
            .reset();
             = true;
            return -1; // reset retry count
         } else {
            throw e;
         }
      } else {
         .tracef(emessagei.getMaxRetries());
         return i;
      }
   }
   protected void logErrorAndThrowExceptionIfNeeded(int iHotRodClientException e) {
      String message = "Exception encountered. Retry %d out of %d";
      if (i >= .getMaxRetries() || .getMaxRetries() < 0) {
         throw e;
      } else {
         .tracef(emessagei.getMaxRetries());
      }
   }
   protected void releaseTransport(Transport transport) {
      if (transport != null)
         .releaseTransport(transport);
   }
   protected abstract Transport getTransport(int retryCountSet<SocketAddressfailedServers);
   protected abstract T executeOperation(Transport transport);
New to GrepCode? Check out our FAQ X