Search in sources :

Example 1 with Monitor

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

the class Cluster method getStats.

public final ClusterStats getStats() {
    // Get sync statistics.
    final Node[] nodeArray = nodes;
    NodeStats[] nodeStats = new NodeStats[nodeArray.length];
    int count = 0;
    for (Node node : nodeArray) {
        nodeStats[count++] = new NodeStats(node);
    }
    int threadsInUse = 0;
    if (threadPool instanceof ThreadPoolExecutor) {
        ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadPool;
        threadsInUse = tpe.getActiveCount();
    }
    // Get async statistics.
    EventLoopStats[] eventLoopStats = null;
    if (eventLoops != null) {
        EventLoop[] eventLoopArray = eventLoops.getArray();
        for (EventLoop eventLoop : eventLoopArray) {
            if (eventLoop.inEventLoop()) {
                // We can't block in an eventloop thread, so use non-blocking approximate statistics.
                // The statistics might be less accurate because cross-thread references are made
                // without a lock for the pool's queue size and other counters.
                eventLoopStats = new EventLoopStats[eventLoopArray.length];
                for (int i = 0; i < eventLoopArray.length; i++) {
                    eventLoopStats[i] = new EventLoopStats(eventLoopArray[i]);
                }
                for (int i = 0; i < nodeArray.length; i++) {
                    nodeStats[i].async = nodeArray[i].getAsyncConnectionStats();
                }
                return new ClusterStats(nodeStats, eventLoopStats, threadsInUse, recoverCount.get(), invalidNodeCount);
            }
        }
        final EventLoopStats[] loopStats = new EventLoopStats[eventLoopArray.length];
        final ConnectionStats[][] connStats = new ConnectionStats[nodeArray.length][eventLoopArray.length];
        final AtomicInteger eventLoopCount = new AtomicInteger(eventLoopArray.length);
        final Monitor monitor = new Monitor();
        for (EventLoop eventLoop : eventLoopArray) {
            eventLoop.execute(new Runnable() {

                public void run() {
                    int index = eventLoop.getIndex();
                    loopStats[index] = new EventLoopStats(eventLoop);
                    for (int i = 0; i < nodeArray.length; i++) {
                        AsyncPool pool = nodeArray[i].getAsyncPool(index);
                        int inPool = pool.queue.size();
                        connStats[i][index] = new ConnectionStats(pool.total - inPool, inPool, pool.opened, pool.closed);
                    }
                    if (eventLoopCount.decrementAndGet() == 0) {
                        monitor.notifyComplete();
                    }
                }
            });
        }
        // Not running in eventloop thread, so it's ok to wait for results.
        monitor.waitTillComplete();
        eventLoopStats = loopStats;
        for (int i = 0; i < nodeArray.length; i++) {
            int inUse = 0;
            int inPool = 0;
            int opened = 0;
            int closed = 0;
            for (EventLoop eventLoop : eventLoopArray) {
                ConnectionStats cs = connStats[i][eventLoop.getIndex()];
                inUse += cs.inUse;
                inPool += cs.inPool;
                opened += cs.opened;
                closed += cs.closed;
            }
            nodeStats[i].async = new ConnectionStats(inUse, inPool, opened, closed);
        }
    }
    return new ClusterStats(nodeStats, eventLoopStats, threadsInUse, recoverCount.get(), invalidNodeCount);
}
Also used : EventLoop(com.aerospike.client.async.EventLoop) Monitor(com.aerospike.client.async.Monitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) EventLoopStats(com.aerospike.client.async.EventLoopStats) AsyncPool(com.aerospike.client.cluster.Node.AsyncPool)

Example 2 with Monitor

use of com.aerospike.client.async.Monitor 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)

Aggregations

EventLoop (com.aerospike.client.async.EventLoop)2 Monitor (com.aerospike.client.async.Monitor)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AerospikeException (com.aerospike.client.AerospikeException)1 AsyncConnectorExecutor (com.aerospike.client.async.AsyncConnectorExecutor)1 EventLoopStats (com.aerospike.client.async.EventLoopStats)1 EventState (com.aerospike.client.async.EventState)1 AsyncPool (com.aerospike.client.cluster.Node.AsyncPool)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1