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