Search in sources :

Example 1 with AsyncPool

use of com.aerospike.client.cluster.Node.AsyncPool 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 AsyncPool

use of com.aerospike.client.cluster.Node.AsyncPool 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 AsyncPool

use of com.aerospike.client.cluster.Node.AsyncPool in project aerospike-client-java by aerospike.

the class NettyCommand method putConnection.

private void putConnection() {
    try {
        SocketChannel channel = conn.channel;
        channel.config().setAutoRead(false);
        InboundHandler handler = (InboundHandler) channel.pipeline().last();
        if (cluster.keepAlive == null) {
            handler.clear();
        } else {
            AsyncPool pool = node.getAsyncPool(eventState.index);
            handler.setPool(pool);
        }
        node.putAsyncConnection(conn, eventState.index);
    } catch (Throwable e) {
        logError(e);
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) KQueueSocketChannel(io.netty.channel.kqueue.KQueueSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) IOUringSocketChannel(io.netty.incubator.channel.uring.IOUringSocketChannel) AsyncPool(com.aerospike.client.cluster.Node.AsyncPool)

Example 4 with AsyncPool

use of com.aerospike.client.cluster.Node.AsyncPool in project aerospike-client-java by aerospike.

the class NettyConnector method finish.

private final void finish() {
    try {
        // Assign normal InboundHandler to connection.
        SocketChannel channel = conn.channel;
        channel.config().setAutoRead(false);
        ChannelPipeline p = channel.pipeline();
        p.removeLast();
        if (cluster.keepAlive == null) {
            p.addLast(new NettyCommand.InboundHandler());
        } else {
            AsyncPool pool = node.getAsyncPool(eventState.index);
            p.addLast(new NettyCommand.InboundHandler(pool));
        }
        conn.updateLastUsed();
        success();
    } catch (Throwable e) {
        Log.error("NettyConnector fatal error: " + Util.getStackTrace(e));
        throw e;
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) ChannelPipeline(io.netty.channel.ChannelPipeline) AsyncPool(com.aerospike.client.cluster.Node.AsyncPool)

Example 5 with AsyncPool

use of com.aerospike.client.cluster.Node.AsyncPool in project aerospike-client-java by aerospike.

the class NettyRecover method recover.

private final void recover() {
    // System.out.println("" + tranId + " connection drained");
    if (state == AsyncCommand.COMPLETE) {
        return;
    }
    state = AsyncCommand.COMPLETE;
    try {
        // Assign normal InboundHandler to connection.
        SocketChannel channel = conn.channel;
        channel.config().setAutoRead(false);
        ChannelPipeline p = channel.pipeline();
        p.removeLast();
        if (cluster.keepAlive == null) {
            p.addLast(new NettyCommand.InboundHandler());
        } else {
            AsyncPool pool = node.getAsyncPool(eventState.index);
            p.addLast(new NettyCommand.InboundHandler(pool));
        }
        // Put connection into pool.
        conn.updateLastUsed();
        node.putAsyncConnection(conn, eventLoop.index);
        // Close recovery command.
        close(true);
    } catch (Throwable e) {
        if (!eventState.closed) {
            Log.error("NettyRecover recover failed: " + Util.getStackTrace(e));
        }
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) ChannelPipeline(io.netty.channel.ChannelPipeline) AsyncPool(com.aerospike.client.cluster.Node.AsyncPool)

Aggregations

AsyncPool (com.aerospike.client.cluster.Node.AsyncPool)5 SocketChannel (io.netty.channel.socket.SocketChannel)3 EventLoop (com.aerospike.client.async.EventLoop)2 EventLoopStats (com.aerospike.client.async.EventLoopStats)2 ChannelPipeline (io.netty.channel.ChannelPipeline)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 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)1 KQueueSocketChannel (io.netty.channel.kqueue.KQueueSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 IOUringSocketChannel (io.netty.incubator.channel.uring.IOUringSocketChannel)1