Search in sources :

Example 1 with NioConnectionException

use of com.cloud.utils.exception.NioConnectionException in project cloudstack by apache.

the class NioConnection method start.

public void start() throws NioConnectionException {
    _todos = new ArrayList<ChangeRequest>();
    try {
        init();
    } catch (final ConnectException e) {
        s_logger.warn("Unable to connect to remote: is there a server running on port " + _port);
        return;
    } catch (final IOException e) {
        s_logger.error("Unable to initialize the threads.", e);
        throw new NioConnectionException(e.getMessage(), e);
    } catch (final Exception e) {
        s_logger.error("Unable to initialize the threads due to unknown exception.", e);
        throw new NioConnectionException(e.getMessage(), e);
    }
    _isStartup = true;
    _threadExecutor = Executors.newSingleThreadExecutor(new NamedThreadFactory(this._name + "-NioConnectionHandler"));
    _isRunning = true;
    _futureTask = _threadExecutor.submit(this);
}
Also used : NamedThreadFactory(com.cloud.utils.concurrency.NamedThreadFactory) IOException(java.io.IOException) NioConnectionException(com.cloud.utils.exception.NioConnectionException) CancelledKeyException(java.nio.channels.CancelledKeyException) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) NioConnectionException(com.cloud.utils.exception.NioConnectionException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ConnectException(java.net.ConnectException)

Example 2 with NioConnectionException

use of com.cloud.utils.exception.NioConnectionException in project cloudstack by apache.

the class NioTest method setUp.

@Before
public void setUp() {
    LOGGER.info("Setting up Benchmark Test");
    completedTestCount = 0;
    testBytes = new byte[1000000];
    randomGenerator.nextBytes(testBytes);
    server = new NioServer("NioTestServer", 0, 1, new NioTestServer());
    try {
        server.start();
    } catch (final NioConnectionException e) {
        Assert.fail(e.getMessage());
    }
    for (int i = 0; i < totalTestCount; i++) {
        final NioClient maliciousClient = new NioMaliciousClient("NioMaliciousTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioMaliciousTestClient());
        maliciousClients.add(maliciousClient);
        maliciousExecutor.submit(new ThreadedNioClient(maliciousClient));
        final NioClient client = new NioClient("NioTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioTestClient());
        clients.add(client);
        clientExecutor.submit(new ThreadedNioClient(client));
    }
}
Also used : NioServer(com.cloud.utils.nio.NioServer) NioConnectionException(com.cloud.utils.exception.NioConnectionException) NioClient(com.cloud.utils.nio.NioClient) Before(org.junit.Before)

Example 3 with NioConnectionException

use of com.cloud.utils.exception.NioConnectionException in project cloudstack by apache.

the class Agent method start.

public void start() {
    if (!_resource.start()) {
        s_logger.error("Unable to start the resource: " + _resource.getName());
        throw new CloudRuntimeException("Unable to start the resource: " + _resource.getName());
    }
    try {
        _connection.start();
    } catch (final NioConnectionException e) {
        s_logger.warn("NIO Connection Exception  " + e);
        s_logger.info("Attempted to connect to the server, but received an unexpected exception, trying again...");
    }
    while (!_connection.isStartup()) {
        _shell.getBackoffAlgorithm().waitBeforeRetry();
        _connection = new NioClient("Agent", _shell.getHost(), _shell.getPort(), _shell.getWorkers(), this);
        try {
            _connection.start();
        } catch (final NioConnectionException e) {
            s_logger.warn("NIO Connection Exception  " + e);
            s_logger.info("Attempted to connect to the server, but received an unexpected exception, trying again...");
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NioConnectionException(com.cloud.utils.exception.NioConnectionException) NioClient(com.cloud.utils.nio.NioClient)

Example 4 with NioConnectionException

use of com.cloud.utils.exception.NioConnectionException in project cloudstack by apache.

the class Agent method reconnect.

protected void reconnect(final Link link) {
    if (!_reconnectAllowed) {
        return;
    }
    synchronized (this) {
        if (_startup != null) {
            _startup.cancel();
            _startup = null;
        }
    }
    link.close();
    link.terminated();
    setLink(null);
    cancelTasks();
    _resource.disconnected();
    int inProgress = 0;
    do {
        _shell.getBackoffAlgorithm().waitBeforeRetry();
        s_logger.info("Lost connection to the server. Dealing with the remaining commands...");
        inProgress = _inProgress.get();
        if (inProgress > 0) {
            s_logger.info("Cannot connect because we still have " + inProgress + " commands in progress.");
        }
    } while (inProgress > 0);
    _connection.stop();
    try {
        _connection.cleanUp();
    } catch (final IOException e) {
        s_logger.warn("Fail to clean up old connection. " + e);
    }
    while (_connection.isStartup()) {
        _shell.getBackoffAlgorithm().waitBeforeRetry();
    }
    _connection = new NioClient("Agent", _shell.getHost(), _shell.getPort(), _shell.getWorkers(), this);
    do {
        s_logger.info("Reconnecting...");
        try {
            _connection.start();
        } catch (final NioConnectionException e) {
            s_logger.warn("NIO Connection Exception  " + e);
            s_logger.info("Attempted to connect to the server, but received an unexpected exception, trying again...");
        }
        _shell.getBackoffAlgorithm().waitBeforeRetry();
    } while (!_connection.isStartup());
    s_logger.info("Connected to the server");
}
Also used : IOException(java.io.IOException) NioConnectionException(com.cloud.utils.exception.NioConnectionException) NioClient(com.cloud.utils.nio.NioClient)

Example 5 with NioConnectionException

use of com.cloud.utils.exception.NioConnectionException in project cloudstack by apache.

the class NioConnection method call.

@Override
public Boolean call() throws NioConnectionException {
    while (_isRunning) {
        try {
            _selector.select(100);
            // Someone is ready for I/O, get the ready keys
            final Set<SelectionKey> readyKeys = _selector.selectedKeys();
            final Iterator<SelectionKey> i = readyKeys.iterator();
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("Keys Processing: " + readyKeys.size());
            }
            // Walk through the ready keys collection.
            while (i.hasNext()) {
                final SelectionKey sk = i.next();
                i.remove();
                if (!sk.isValid()) {
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Selection Key is invalid: " + sk.toString());
                    }
                    final Link link = (Link) sk.attachment();
                    if (link != null) {
                        link.terminated();
                    } else {
                        closeConnection(sk);
                    }
                } else if (sk.isReadable()) {
                    read(sk);
                } else if (sk.isWritable()) {
                    write(sk);
                } else if (sk.isAcceptable()) {
                    accept(sk);
                } else if (sk.isConnectable()) {
                    connect(sk);
                }
            }
            s_logger.trace("Keys Done Processing.");
            processTodos();
        } catch (final ClosedSelectorException e) {
        /*
                 * Exception occurred when calling java.nio.channels.Selector.selectedKeys() method. It means the connection has not yet been established. Let's continue trying
                 * We do not log it here otherwise we will fill the disk with messages.
                 */
        } catch (final IOException e) {
            s_logger.error("Agent will die due to this IOException!", e);
            throw new NioConnectionException(e.getMessage(), e);
        }
    }
    _isStartup = false;
    return true;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) NioConnectionException(com.cloud.utils.exception.NioConnectionException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

NioConnectionException (com.cloud.utils.exception.NioConnectionException)5 NioClient (com.cloud.utils.nio.NioClient)3 IOException (java.io.IOException)3 ClosedSelectorException (java.nio.channels.ClosedSelectorException)2 NamedThreadFactory (com.cloud.utils.concurrency.NamedThreadFactory)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 NioServer (com.cloud.utils.nio.NioServer)1 ConnectException (java.net.ConnectException)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 SelectionKey (java.nio.channels.SelectionKey)1 Before (org.junit.Before)1