Search in sources :

Example 1 with UnknownHostException

use of java.rmi.UnknownHostException in project jstuff by sebthom.

the class CircuitBreakerTest method testCircuitBreaker.

@Test
public void testCircuitBreaker() {
    @SuppressWarnings("unchecked") final CircuitBreaker cb = // 
    CircuitBreaker.builder().name(// 
    "test").failureThreshold(// 
    2).failureTrackingPeriod(2, // 
    TimeUnit.SECONDS).resetPeriod(2, // 
    TimeUnit.SECONDS).hardTrippingExceptionTypes(// 
    UnknownHostException.class).build();
    /*
       * testing best case scenario
       */
    for (int i = 0; i < 100; i++) {
        assertThat(cb.tryAcquire()).isTrue();
    }
    for (int i = 0; i < 100; i++) {
        cb.release();
    }
    assertThat(cb.tryExecute(() -> {
    /* */
    })).isTrue();
    /*
       * testing open-state
       */
    assertThat(cb.tryAcquire()).isTrue();
    // 1st error
    cb.reportFailure(new IllegalArgumentException());
    cb.release();
    assertThat(cb.tryAcquire()).isTrue();
    // 2nd error
    cb.reportFailure(new IllegalArgumentException());
    cb.release();
    assertThat(cb.getState()).isEqualTo(State.OPEN);
    assertThat(cb.tryAcquire()).isFalse();
    assertThat(cb.tryExecute(() -> {
    /* */
    })).isFalse();
    assertThat(cb.getState()).isEqualTo(State.OPEN);
    /*
       * testing half-open state
       */
    Threads.sleep(3 * 1000);
    assertThat(cb.getState()).isEqualTo(State.HALF_OPEN);
    assertThat(cb.tryAcquire()).isTrue();
    assertThat(cb.getState()).isEqualTo(State.HALF_OPEN);
    assertThat(cb.tryAcquire()).isFalse();
    // not reporting success or failure, thus releasing the permit does not change the circuit breaker's state
    cb.release();
    assertThat(cb.getState()).isEqualTo(State.HALF_OPEN);
    assertThat(cb.tryAcquire()).isTrue();
    cb.reportSuccess();
    cb.release();
    assertThat(cb.getState()).isEqualTo(State.CLOSE);
    /*
       * testing instant state switch
       */
    assertThat(cb.tryAcquire()).isTrue();
    cb.reportFailure(new UnknownHostException("foo.bar"));
    cb.release();
    assertThat(cb.getState()).isEqualTo(State.OPEN);
}
Also used : UnknownHostException(java.rmi.UnknownHostException) Test(org.junit.Test)

Example 2 with UnknownHostException

use of java.rmi.UnknownHostException in project xap by xap.

the class AbortJob method doWork.

/**
 * The work to be performed by each <code>TaskManager.Task</code> is provided by the
 * <code>Job</code> that creates it. The work performed by a task belonging to the AbortJob
 * contacts a participant, instructs it to roll-back and log appropriately.
 *
 * @param who   The task performing the work
 * @param param A parameter, of the task's choosing, useful in performing work.
 * @see com.sun.jini.mahalo.Job
 * @see com.sun.jini.thread.TaskManager.Task
 */
Object doWork(TaskManager.Task who, Object param) {
    Integer response = null;
    try {
        ParticipantHandle handle = (ParticipantHandle) param;
        TransactionParticipant par = null;
        if (logger.isTraceEnabled()) {
            logger.trace("AbortJob:doWork aborting handle: {}", handle);
        }
        int vote = 0;
        vote = handle.getPrepState();
        switch(vote) {
            case NOTCHANGED:
                return new Integer(ABORTED);
            case ABORTED:
                if (!handle.isPrepared())
                    return new Integer(ABORTED);
        }
        if (par == null)
            par = handle.getParticipant();
        try {
            if (!_directCall && attempt(who) > maxtries) {
                response = ABORTED;
                return response;
            }
        } catch (JobException je) {
            return null;
        }
        // must be an error unpacking, so retry later
        if (par == null)
            return null;
        try {
            if (handle.isSuitableForCommitFailover()) {
                abortPartitionWithEnabledFailover(handle, tr.mgr, tr.id, _xid);
            } else {
                if (_xid == null)
                    par.abort(tr.mgr, tr.id);
                else
                    ((IRemoteSpace) par).abort(tr.mgr, _xid);
            }
            response = ABORTED;
        } catch (TransactionException bte) {
            // The participant doesn't have record of the
            // transaction, so it must have already rolled
            // back.
            response = ABORTED;
        } catch (NoSuchObjectException nsoe) {
            // No definition for object in VM, so stop
            // and consider committed.
            response = ABORTED;
        } catch (ConnectException ce) {
            // participant more time by retrying
            if (numberOfRetriesDueToConnectionExceptionExceeded(who))
                response = ABORTED;
        } catch (UnknownHostException uhe) {
            // could not resolve host for participant, so
            // stop and consider committed
            response = ABORTED;
        } catch (ConnectIOException cioe) {
            // connection problem, give participant more time
            if (numberOfRetriesDueToConnectionExceptionExceeded(who))
                response = ABORTED;
        } catch (MarshalException me) {
            // cannot send parameters, so stop and consider done
            response = ABORTED;
        } catch (AccessException ae) {
            // Access error on registry or rmid consider done
            response = ABORTED;
        } catch (RemoteException re) {
            // Something happened with the network, so
            // retry at a later time.
            response = ABORTED;
        } catch (RuntimeException rte) {
            // Something happened with the participant, so
            // stop retrying
            response = ABORTED;
        }
        if (response != null) {
            handle.setPrepState(ABORTED);
            try {
                log.write(new ParticipantAbortRecord(handle));
            } catch (com.sun.jini.mahalo.log.LogException le) {
            // the full package name used to disambiguate
            // the LogException
            }
            return response;
        }
        return null;
    } finally {
        if (_directCall && response != null) {
            this.results[0] = response;
            pending = 0;
        }
    }
}
Also used : MarshalException(java.rmi.MarshalException) UnknownHostException(java.rmi.UnknownHostException) ConnectIOException(java.rmi.ConnectIOException) TransactionParticipant(net.jini.core.transaction.server.TransactionParticipant) TransactionException(net.jini.core.transaction.TransactionException) AccessException(java.rmi.AccessException) NoSuchObjectException(java.rmi.NoSuchObjectException) RemoteException(java.rmi.RemoteException) ConnectException(java.rmi.ConnectException)

Example 3 with UnknownHostException

use of java.rmi.UnknownHostException in project seldon-core by SeldonIO.

the class HttpRetryHandler method retryRequest.

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    if (executionCount >= 3) {
        logger.info("Got too many exceptions");
        // Do not retry if over max retry count
        return false;
    }
    if (exception instanceof InterruptedIOException) {
        // Timeout
        logger.info("Got interrupted exception");
        return true;
    }
    if (exception instanceof SocketException) {
        logger.info("Got socket exception");
        return true;
    }
    if (exception instanceof org.apache.http.NoHttpResponseException) {
        logger.warn("got no http response exception");
        return true;
    }
    if (exception instanceof UnknownHostException) {
        // Unknown host
        return false;
    }
    if (exception instanceof ConnectTimeoutException) {
        // Connection refused
        return true;
    }
    if (exception instanceof SSLException) {
        // SSL handshake exception
        return false;
    }
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    HttpRequest request = clientContext.getRequest();
    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
    if (idempotent) {
        // Retry if the request is considered idempotent
        return true;
    }
    return false;
}
Also used : HttpRequest(org.apache.http.HttpRequest) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) UnknownHostException(java.rmi.UnknownHostException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) SSLException(javax.net.ssl.SSLException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 4 with UnknownHostException

use of java.rmi.UnknownHostException in project xap by xap.

the class CommitJob method doWork.

/**
 * The work to be performed by each <code>TaskManager.Task</code> is provided by the
 * <code>Job</code> that creates it. The work performed by a task belonging to the CommitJob
 * contacts a participant, instructs it to roll-forward and log appropriately.
 *
 * @param who   The task performing the work
 * @param param A parameter, of the task's choosing, useful in performing work.
 * @see com.sun.jini.mahalo.Job
 * @see com.sun.jini.thread.TaskManager.Task
 */
Object doWork(TaskManager.Task who, Object param) {
    ParticipantHandle handle = (ParticipantHandle) param;
    TransactionParticipant par = null;
    if (logger.isTraceEnabled()) {
        logger.trace("CommitJob:doWork committing handle: {}", handle);
    }
    int vote = handle.getPrepState();
    switch(vote) {
        case COMMITTED:
        case NOTCHANGED:
        case ABORTED:
            return new Integer(vote);
    }
    if (par == null)
        par = handle.getParticipant();
    try {
        if (attempt(who) > maxtries) {
            return new Integer(COMMITTED);
        }
    } catch (JobException je) {
        return null;
    }
    // must be an error unpacking, so retry later
    if (par == null)
        return null;
    // Here we actually need to instruct the participant to
    // roll forward.  Note the RemoteException causes a
    // retry. Here we only log info for the cases
    // where a final outcome is available.
    Object response = null;
    try {
        int preparedParticipants = 0;
        for (int i = 0; i < handles.length; i++) {
            // count only participants of the same cluster
            if (handles[i].getPrepState() != NOTCHANGED && (handle.getClusterName() == handles[i].getClusterName() || handle.getClusterName() != null && handle.getClusterName().equals(handles[i].getClusterName())))
                preparedParticipants++;
        }
        if (handle.isSuitableForCommitFailover()) {
            commitPartitionWithEnabledFailover(handle, tr.mgr, tr.id, _xid, preparedParticipants);
        } else {
            if (_xid == null)
                par.commit(tr.mgr, tr.id, preparedParticipants);
            else
                ((IRemoteSpace) par).commit(tr.mgr, _xid, preparedParticipants);
        }
        response = new Integer(COMMITTED);
    } catch (TransactionException bte) {
        // The participant doesn't have record of the
        // transaction, so it must have already rolled
        // forward.
        response = new Integer(COMMITTED);
    } catch (NoSuchObjectException nsoe) {
        // No definition for object in VM, so stop
        // and consider committed.
        response = new Integer(COMMITTED);
    } catch (ConnectException ce) {
        // participant more time by retrying
        if (numberOfRetriesDueToConnectionExceptionExceeded(who))
            response = new Integer(COMMITTED);
    } catch (UnknownHostException uhe) {
        // could not resolve host for participant, so
        // stop and consider committed
        response = new Integer(COMMITTED);
    } catch (ConnectIOException cioe) {
        // connection problem, give participant more time
        if (numberOfRetriesDueToConnectionExceptionExceeded(who))
            response = new Integer(COMMITTED);
    } catch (MarshalException me) {
        // cannot send parameters, so stop and consider done
        response = new Integer(COMMITTED);
    } catch (AccessException ae) {
        // Access error on registry or rmid consider done
        response = new Integer(COMMITTED);
    } catch (RemoteException re) {
        // Something happened with the network, so
        // return null to retry at a later time.
        response = new Integer(COMMITTED);
    } catch (RuntimeException rte) {
        // Something happened with the participant, so
        // stop and consider done
        response = new Integer(COMMITTED);
    }
    if (response != null) {
        handle.setPrepState(COMMITTED);
        try {
            log.write(new ParticipantCommitRecord(handle));
        } catch (com.sun.jini.mahalo.log.LogException le) {
        // the full package name used to disambiguate
        // the LogException
        }
        return response;
    }
    return null;
}
Also used : MarshalException(java.rmi.MarshalException) UnknownHostException(java.rmi.UnknownHostException) ConnectIOException(java.rmi.ConnectIOException) TransactionParticipant(net.jini.core.transaction.server.TransactionParticipant) TransactionException(net.jini.core.transaction.TransactionException) AccessException(java.rmi.AccessException) NoSuchObjectException(java.rmi.NoSuchObjectException) RemoteException(java.rmi.RemoteException) ConnectException(java.rmi.ConnectException)

Example 5 with UnknownHostException

use of java.rmi.UnknownHostException in project xap by xap.

the class ThrowableConstants method retryable.

/**
 * Attempt to classify the passed <code>Throwable</code> in terms of
 * what it implies about the probability of success of future operations
 * on the object that threw the exception. <p>
 *
 * Note, the classification used by this method tends to assume
 * the worst. For exceptions that represent conditions that could
 * get better by themselves but probably will not, it will return
 * <code>BAD_OBJECT</code> or <code>BAD_INVOCATION</code> instead
 * of <code>INDEFINITE</code>. This makes it suitable for
 * situations where it is better to give up, fail early, and
 * notify the next layer up that something is wrong than to
 * continue silently and retry. It is probably not a good choice
 * for situations where the stakes are higher, like deciding when
 * to give up on a prepared transaction.
 *
 * @return <code>INDEFINITE</code>, <code>BAD_INVOCATION</code>,
 * or <code>BAD_OBJECT</code> if the exception is a
 * <code>RuntimeException</code>, <code>Error</code>, or
 * <code>java.rmi.RemoteException</code> depending on the details of
 * the <code>Throwable</code>.  Otherwise return
 * <code>UNCATEGORIZED</code>
 * @throws NullPointerException if the passed <code>Throwable</code> is
 * <code>null</code>
 */
public static int retryable(Throwable t) {
    if (logger.isTraceEnabled()) {
        logger.trace("Inside ThrowableConstants.retryable() ", t);
    }
    if (t == null)
        throw new NullPointerException("Must pass a non-null Throwable");
    if (t instanceof RuntimeException) {
        return BAD_INVOCATION;
    }
    if (t instanceof Error) {
        if ((t instanceof OutOfMemoryError) || (t instanceof LinkageError)) {
            return INDEFINITE;
        }
        if (t instanceof StackOverflowError)
            return BAD_INVOCATION;
        return BAD_OBJECT;
    }
    if (t instanceof RemoteException) {
        final RemoteException re = (RemoteException) t;
        if (re instanceof NoSuchObjectException || re instanceof UnexpectedException || re instanceof UnknownHostException || re instanceof ConnectException) {
            return BAD_OBJECT;
        }
        final Throwable detail = re.getCause();
        if (detail == null)
            return INDEFINITE;
        if (re instanceof MarshalException || re instanceof UnmarshalException) {
            if (detail instanceof ObjectStreamException)
                return BAD_INVOCATION;
            final int drs = retryable(detail);
            if (drs == BAD_OBJECT || drs == BAD_INVOCATION)
                return BAD_INVOCATION;
            return INDEFINITE;
        }
        if (re instanceof ConnectIOException) {
            if (detail instanceof NoRouteToHostException || detail instanceof PortUnreachableException || detail instanceof ProtocolException) {
                return BAD_OBJECT;
            }
            if (detail instanceof UnsupportedConstraintException || detail instanceof ObjectStreamException) {
                return BAD_INVOCATION;
            }
            return INDEFINITE;
        }
        if (re instanceof ServerException) {
            final int drs = retryable(detail);
            if (drs == BAD_OBJECT)
                return BAD_INVOCATION;
            return drs;
        }
        if (re instanceof ServerError) {
            return retryable(detail);
        }
        return INDEFINITE;
    }
    return UNCATEGORIZED;
}
Also used : ProtocolException(java.net.ProtocolException) UnexpectedException(java.rmi.UnexpectedException) MarshalException(java.rmi.MarshalException) PortUnreachableException(java.net.PortUnreachableException) ServerException(java.rmi.ServerException) UnknownHostException(java.rmi.UnknownHostException) UnsupportedConstraintException(net.jini.io.UnsupportedConstraintException) ServerError(java.rmi.ServerError) ConnectIOException(java.rmi.ConnectIOException) ServerError(java.rmi.ServerError) NoRouteToHostException(java.net.NoRouteToHostException) UnmarshalException(java.rmi.UnmarshalException) NoSuchObjectException(java.rmi.NoSuchObjectException) RemoteException(java.rmi.RemoteException) ObjectStreamException(java.io.ObjectStreamException) ConnectException(java.rmi.ConnectException)

Aggregations

UnknownHostException (java.rmi.UnknownHostException)5 ConnectException (java.rmi.ConnectException)3 ConnectIOException (java.rmi.ConnectIOException)3 MarshalException (java.rmi.MarshalException)3 NoSuchObjectException (java.rmi.NoSuchObjectException)3 RemoteException (java.rmi.RemoteException)3 AccessException (java.rmi.AccessException)2 TransactionException (net.jini.core.transaction.TransactionException)2 TransactionParticipant (net.jini.core.transaction.server.TransactionParticipant)2 InterruptedIOException (java.io.InterruptedIOException)1 ObjectStreamException (java.io.ObjectStreamException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 PortUnreachableException (java.net.PortUnreachableException)1 ProtocolException (java.net.ProtocolException)1 SocketException (java.net.SocketException)1 ServerError (java.rmi.ServerError)1 ServerException (java.rmi.ServerException)1 UnexpectedException (java.rmi.UnexpectedException)1 UnmarshalException (java.rmi.UnmarshalException)1 SSLException (javax.net.ssl.SSLException)1