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);
}
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));
}
}
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);
}
}
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;
}
}
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));
}
}
}
Aggregations