Search in sources :

Example 1 with EventLoopStats

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

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

the class Cluster method getStats.

public final void getStats(ClusterStatsListener listener) {
    try {
        // 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();
        }
        if (eventLoops == null) {
            try {
                listener.onSuccess(new ClusterStats(nodeStats, null, threadsInUse, recoverCount.get(), invalidNodeCount));
            } catch (Throwable e) {
            }
            return;
        }
        // Get async statistics.
        EventLoop[] eventLoopArray = eventLoops.getArray();
        final EventLoopStats[] loopStats = new EventLoopStats[eventLoopArray.length];
        final ConnectionStats[][] connStats = new ConnectionStats[nodeArray.length][eventLoopArray.length];
        final AtomicInteger eventLoopCount = new AtomicInteger(eventLoopArray.length);
        final int threadCount = threadsInUse;
        for (EventLoop eventLoop : eventLoopArray) {
            Runnable fetch = 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) {
                        // All eventloops reported. Pass results to listener.
                        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);
                        }
                        try {
                            listener.onSuccess(new ClusterStats(nodeStats, loopStats, threadCount, recoverCount.get(), invalidNodeCount));
                        } catch (Throwable e) {
                        }
                    }
                }
            };
            if (eventLoop.inEventLoop()) {
                fetch.run();
            } else {
                eventLoop.execute(fetch);
            }
        }
    } catch (AerospikeException ae) {
        listener.onFailure(ae);
    } catch (Throwable e) {
        listener.onFailure(new AerospikeException(e));
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) EventLoop(com.aerospike.client.async.EventLoop) 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 3 with EventLoopStats

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

the class ClusterStats method toString.

/**
 * Convert statistics to string.
 */
public String toString() {
    StringBuilder sb = new StringBuilder(1024);
    sb.append("nodes(inUse,inPool,opened,closed):");
    sb.append(System.lineSeparator());
    for (NodeStats stat : nodes) {
        sb.append(stat);
        sb.append(System.lineSeparator());
    }
    if (eventLoops != null) {
        sb.append("eventLoops(processSize,queueSize): ");
        for (int i = 0; i < eventLoops.length; i++) {
            EventLoopStats stat = eventLoops[i];
            if (i > 0) {
                sb.append(',');
            }
            sb.append('(');
            sb.append(stat);
            sb.append(')');
        }
        sb.append(System.lineSeparator());
    }
    sb.append("threadsInUse: " + threadsInUse);
    sb.append(System.lineSeparator());
    sb.append("recoverQueueSize: " + recoverQueueSize);
    sb.append(System.lineSeparator());
    sb.append("invalidNodeCount: " + invalidNodeCount);
    return sb.toString();
}
Also used : EventLoopStats(com.aerospike.client.async.EventLoopStats)

Aggregations

EventLoopStats (com.aerospike.client.async.EventLoopStats)3 EventLoop (com.aerospike.client.async.EventLoop)2 AsyncPool (com.aerospike.client.cluster.Node.AsyncPool)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AerospikeException (com.aerospike.client.AerospikeException)1 Monitor (com.aerospike.client.async.Monitor)1