Search in sources :

Example 1 with EventState

use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.

the class Node method createMinConnections.

public final void createMinConnections() {
    // Create sync connections.
    for (Pool pool : connectionPools) {
        if (pool.minSize > 0) {
            createConnections(pool, pool.minSize);
        }
    }
    EventState[] eventState = cluster.eventState;
    if (eventState == null || cluster.asyncMinConnsPerNode <= 0) {
        return;
    }
    // Create async connections.
    final Monitor monitor = new Monitor();
    final AtomicInteger eventLoopCount = new AtomicInteger(eventState.length);
    final int maxConcurrent = 20 / eventState.length + 1;
    for (int i = 0; i < eventState.length; i++) {
        final int minSize = asyncConnectionPools[i].minSize;
        if (minSize > 0) {
            final EventLoop eventLoop = eventState[i].eventLoop;
            final Node node = this;
            eventLoop.execute(new Runnable() {

                public void run() {
                    try {
                        new AsyncConnectorExecutor(eventLoop, cluster, node, minSize, maxConcurrent, monitor, eventLoopCount);
                    } catch (Exception e) {
                        if (Log.warnEnabled()) {
                            Log.warn("AsyncConnectorExecutor failed: " + Util.getErrorMessage(e));
                        }
                    }
                }
            });
        } else {
            AsyncConnectorExecutor.eventLoopComplete(monitor, eventLoopCount);
        }
    }
    // Wait until all async connections are created.
    monitor.waitTillComplete();
}
Also used : EventState(com.aerospike.client.async.EventState) Monitor(com.aerospike.client.async.Monitor) EventLoop(com.aerospike.client.async.EventLoop) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncConnectorExecutor(com.aerospike.client.async.AsyncConnectorExecutor) IOException(java.io.IOException) AerospikeException(com.aerospike.client.AerospikeException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 2 with EventState

use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.

the class Cluster method close.

public void close() {
    // Stop cluster tend thread.
    tendValid = false;
    tendThread.interrupt();
    if (!sharedThreadPool) {
        // Shutdown synchronous thread pool.
        threadPool.shutdown();
    }
    if (eventLoops == null) {
        // Close synchronous node connections.
        Node[] nodeArray = nodes;
        for (Node node : nodeArray) {
            node.closeSyncConnections();
        }
    } else {
        // Send cluster close notification to async event loops.
        final AtomicInteger eventLoopCount = new AtomicInteger(eventState.length);
        boolean inEventLoop = false;
        // Send close node notification to async event loops.
        for (final EventState state : eventState) {
            if (state.eventLoop.inEventLoop()) {
                inEventLoop = true;
            }
            state.eventLoop.execute(new Runnable() {

                public void run() {
                    if (state.closed) {
                        // Cluster's event loop connections are already closed.
                        return;
                    }
                    if (state.pending > 0) {
                        // Cluster has pending commands.
                        // Check again in 200ms.
                        state.eventLoop.schedule(this, 200, TimeUnit.MILLISECONDS);
                        return;
                    }
                    // Cluster's event loop connections can now be closed.
                    closeEventLoop(eventLoopCount, state);
                }
            });
        }
        // Only wait when not in event loop thread.
        if (!inEventLoop) {
            waitAsyncComplete();
        }
    }
}
Also used : EventState(com.aerospike.client.async.EventState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with EventState

use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.

the class Cluster method tend.

/**
 * Check health of all nodes in the cluster.
 */
private final void tend(boolean failIfNotConnected) {
    // All node additions/deletions are performed in tend thread.
    // Initialize tend iteration node statistics.
    Peers peers = new Peers(nodes.length + 16);
    // Clear node reference counts.
    for (Node node : nodes) {
        node.referenceCount = 0;
        node.partitionChanged = false;
        node.rebalanceChanged = false;
    }
    // If active nodes don't exist, seed cluster.
    if (nodes.length == 0) {
        seedNode(peers, failIfNotConnected);
    } else {
        // Refresh all known nodes.
        for (Node node : nodes) {
            node.refresh(peers);
        }
        // Refresh peers when necessary.
        if (peers.genChanged) {
            // Refresh peers for all nodes that responded the first time even if only one node's peers changed.
            peers.refreshCount = 0;
            for (Node node : nodes) {
                node.refreshPeers(peers);
            }
            // Handle nodes changes determined from refreshes.
            ArrayList<Node> removeList = findNodesToRemove(peers.refreshCount);
            // Remove nodes in a batch.
            if (removeList.size() > 0) {
                removeNodes(removeList);
            }
        }
        // Add peer nodes to cluster.
        if (peers.nodes.size() > 0) {
            addNodes(peers.nodes);
            refreshPeers(peers);
        }
    }
    invalidNodeCount = peers.getInvalidCount();
    // Refresh partition map when necessary.
    for (Node node : nodes) {
        if (node.partitionChanged) {
            node.refreshPartitions(peers);
        }
        if (node.rebalanceChanged) {
            node.refreshRacks();
        }
    }
    tendCount++;
    // Balance connections every 30 tend iterations.
    if (tendCount % 30 == 0) {
        for (Node node : nodes) {
            node.balanceConnections();
        }
        if (eventState != null) {
            for (EventState es : eventState) {
                final EventLoop eventLoop = es.eventLoop;
                eventLoop.execute(new Runnable() {

                    public void run() {
                        try {
                            final Node[] nodeArray = nodes;
                            for (Node node : nodeArray) {
                                node.balanceAsyncConnections(eventLoop);
                            }
                        } catch (Exception e) {
                            if (Log.warnEnabled()) {
                                Log.warn("balanceAsyncConnections failed: " + Util.getErrorMessage(e));
                            }
                        }
                    }
                });
            }
        }
    }
    // Reset connection error window for all nodes every connErrorWindow tend iterations.
    if (maxErrorRate > 0 && tendCount % errorRateWindow == 0) {
        for (Node node : nodes) {
            node.resetErrorCount();
        }
    }
    processRecoverQueue();
}
Also used : EventState(com.aerospike.client.async.EventState) EventLoop(com.aerospike.client.async.EventLoop) AerospikeException(com.aerospike.client.AerospikeException)

Example 4 with EventState

use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.

the class Node method restart.

private final void restart() {
    try {
        // Reset error rate.
        if (cluster.maxErrorRate > 0) {
            resetErrorCount();
        }
        // Login when user authentication is enabled.
        if (cluster.authEnabled) {
            login();
        }
        // Balance sync connections.
        balanceConnections();
        // Balance async connections.
        if (cluster.eventState != null) {
            for (EventState es : cluster.eventState) {
                final EventLoop eventLoop = es.eventLoop;
                eventLoop.execute(new Runnable() {

                    public void run() {
                        try {
                            balanceAsyncConnections(eventLoop);
                        } catch (Throwable e) {
                            if (Log.warnEnabled()) {
                                Log.warn("balanceAsyncConnections failed: " + this + ' ' + Util.getErrorMessage(e));
                            }
                        }
                    }
                });
            }
        }
    } catch (Throwable e) {
        if (Log.warnEnabled()) {
            Log.warn("Node restart failed: " + this + ' ' + Util.getErrorMessage(e));
        }
    }
}
Also used : EventState(com.aerospike.client.async.EventState) EventLoop(com.aerospike.client.async.EventLoop)

Aggregations

EventState (com.aerospike.client.async.EventState)4 EventLoop (com.aerospike.client.async.EventLoop)3 AerospikeException (com.aerospike.client.AerospikeException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AsyncConnectorExecutor (com.aerospike.client.async.AsyncConnectorExecutor)1 Monitor (com.aerospike.client.async.Monitor)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1