Search in sources :

Example 1 with RandomBackoff

use of com.ms.silverking.net.async.time.RandomBackoff in project SilverKing by Morgan-Stanley.

the class PersistentAsyncServer method createConnection.

/**
 * only called when the given connection does not exist
 * @return
 */
private Connection createConnection(InetSocketAddress dest, long deadline) throws ConnectException {
    RandomBackoff backoff;
    Log.info("createConnection: ", dest);
    if (addressStatusProvider != null && !addressStatusProvider.isAddressStatusProviderThread() && !addressStatusProvider.isHealthy(dest)) {
        throw new UnhealthyConnectionAttemptException("Connection attempted to unhealthy address: " + dest);
    }
    deadline = Math.min(deadline, System.currentTimeMillis() + connectionCreationTimeoutMS);
    backoff = null;
    while (true) {
        try {
            T connection;
            connection = asyncServer.newOutgoingConnection(dest, this);
            connections.putIfAbsent(dest, connection);
            if (suspectAddressListener != null) {
                suspectAddressListener.removeSuspect(dest);
            }
            return connection;
        } catch (ConnectException ce) {
            if (addressStatusProvider != null && addressStatusProvider.isAddressStatusProviderThread()) {
                throw new ConnectException("addressStatusProvider failed to connect: " + dest);
            }
            if (suspectAddressListener != null) {
                suspectAddressListener.addSuspect(dest, SuspectProblem.ConnectionEstablishmentFailed);
            }
            System.out.println(System.currentTimeMillis() + "\t" + deadline);
            System.err.println(System.currentTimeMillis() + "\t" + deadline);
            Log.warning(ce + " " + dest);
            Log.logErrorWarning(ce);
            if (backoff == null) {
                backoff = new RandomBackoff(maxConnectBackoffNum, initialConnectBackoffValue);
            }
            if (System.currentTimeMillis() < deadline && !backoff.maxBackoffExceeded()) {
                backoff.backoff();
            } else {
                informSuspectAddressListener(dest);
                throw ce;
            }
        } catch (SocketTimeoutException ste) {
            if (addressStatusProvider != null && addressStatusProvider.isAddressStatusProviderThread()) {
                throw new ConnectException("addressStatusProvider failed to connect: " + dest);
            }
            System.out.println(System.currentTimeMillis() + "\t" + deadline);
            System.err.println(System.currentTimeMillis() + "\t" + deadline);
            Log.warning(ste + " " + dest);
            Log.logErrorWarning(ste);
            if (backoff == null) {
                backoff = new RandomBackoff(maxConnectBackoffNum, initialConnectBackoffValue);
            }
            if (System.currentTimeMillis() < deadline && !backoff.maxBackoffExceeded()) {
                backoff.backoff();
            } else {
                if (suspectAddressListener != null) {
                    suspectAddressListener.addSuspect(dest, SuspectProblem.ConnectionEstablishmentFailed);
                }
                throw new ConnectException(ste.toString());
            }
        } catch (IOException ioe) {
            Log.warning(ioe + " " + dest);
            Log.logErrorWarning(ioe);
            informSuspectAddressListener(dest);
            throw new RuntimeException("Unexpected IOException", ioe);
        }
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) RandomBackoff(com.ms.silverking.net.async.time.RandomBackoff) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Example 2 with RandomBackoff

use of com.ms.silverking.net.async.time.RandomBackoff in project SilverKing by Morgan-Stanley.

the class PersistentAsyncServer method sendSynchronous.

public void sendSynchronous(InetSocketAddress dest, Object data, UUIDBase uuid, AsyncSendListener listener, long deadline) throws IOException {
    Connection connection;
    RandomBackoff backoff;
    if (uuid == null) {
        uuid = new UUIDBase();
        Log.fine("null send uuid, picking new uuid ", uuid);
        listener = null;
    }
    backoff = null;
    while (true) {
        connection = getConnectionFast(dest, deadline);
        try {
            connection.sendSynchronous(data, uuid, listener, deadline);
            return;
        } catch (IOException ioe) {
            connections.remove(dest);
            informSuspectAddressListener(dest);
            Log.warning(ioe + " " + dest);
            Log.logErrorWarning(ioe);
            if (backoff == null) {
                backoff = new RandomBackoff(maxSendBackoffNum, initialSendBackoffValue);
            }
            if (!backoff.maxBackoffExceeded()) {
                backoff.backoff();
            } else {
                listener.failed(uuid);
                throw ioe;
            }
        }
    }
}
Also used : UUIDBase(com.ms.silverking.id.UUIDBase) RandomBackoff(com.ms.silverking.net.async.time.RandomBackoff) IOException(java.io.IOException)

Aggregations

RandomBackoff (com.ms.silverking.net.async.time.RandomBackoff)2 IOException (java.io.IOException)2 UUIDBase (com.ms.silverking.id.UUIDBase)1 ConnectException (java.net.ConnectException)1 SocketTimeoutException (java.net.SocketTimeoutException)1