use of com.aerospike.client.async.EventState 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();
}
use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.
the class Cluster method close.
public void close() {
// Stop cluster tend thread.
tendValid = false;
tendThread.interrupt();
if (!sharedThreadPool) {
// Shutdown synchronous thread pool.
threadPool.shutdown();
}
if (eventLoops == null) {
// Close synchronous node connections.
Node[] nodeArray = nodes;
for (Node node : nodeArray) {
node.closeSyncConnections();
}
} else {
// Send cluster close notification to async event loops.
final AtomicInteger eventLoopCount = new AtomicInteger(eventState.length);
boolean inEventLoop = false;
// Send close node notification to async event loops.
for (final EventState state : eventState) {
if (state.eventLoop.inEventLoop()) {
inEventLoop = true;
}
state.eventLoop.execute(new Runnable() {
public void run() {
if (state.closed) {
// Cluster's event loop connections are already closed.
return;
}
if (state.pending > 0) {
// Cluster has pending commands.
// Check again in 200ms.
state.eventLoop.schedule(this, 200, TimeUnit.MILLISECONDS);
return;
}
// Cluster's event loop connections can now be closed.
closeEventLoop(eventLoopCount, state);
}
});
}
// Only wait when not in event loop thread.
if (!inEventLoop) {
waitAsyncComplete();
}
}
}
use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.
the class Cluster method tend.
/**
* Check health of all nodes in the cluster.
*/
private final void tend(boolean failIfNotConnected) {
// All node additions/deletions are performed in tend thread.
// Initialize tend iteration node statistics.
Peers peers = new Peers(nodes.length + 16);
// Clear node reference counts.
for (Node node : nodes) {
node.referenceCount = 0;
node.partitionChanged = false;
node.rebalanceChanged = false;
}
// If active nodes don't exist, seed cluster.
if (nodes.length == 0) {
seedNode(peers, failIfNotConnected);
} else {
// Refresh all known nodes.
for (Node node : nodes) {
node.refresh(peers);
}
// Refresh peers when necessary.
if (peers.genChanged) {
// Refresh peers for all nodes that responded the first time even if only one node's peers changed.
peers.refreshCount = 0;
for (Node node : nodes) {
node.refreshPeers(peers);
}
// Handle nodes changes determined from refreshes.
ArrayList<Node> removeList = findNodesToRemove(peers.refreshCount);
// Remove nodes in a batch.
if (removeList.size() > 0) {
removeNodes(removeList);
}
}
// Add peer nodes to cluster.
if (peers.nodes.size() > 0) {
addNodes(peers.nodes);
refreshPeers(peers);
}
}
invalidNodeCount = peers.getInvalidCount();
// Refresh partition map when necessary.
for (Node node : nodes) {
if (node.partitionChanged) {
node.refreshPartitions(peers);
}
if (node.rebalanceChanged) {
node.refreshRacks();
}
}
tendCount++;
// Balance connections every 30 tend iterations.
if (tendCount % 30 == 0) {
for (Node node : nodes) {
node.balanceConnections();
}
if (eventState != null) {
for (EventState es : eventState) {
final EventLoop eventLoop = es.eventLoop;
eventLoop.execute(new Runnable() {
public void run() {
try {
final Node[] nodeArray = nodes;
for (Node node : nodeArray) {
node.balanceAsyncConnections(eventLoop);
}
} catch (Exception e) {
if (Log.warnEnabled()) {
Log.warn("balanceAsyncConnections failed: " + Util.getErrorMessage(e));
}
}
}
});
}
}
}
// Reset connection error window for all nodes every connErrorWindow tend iterations.
if (maxErrorRate > 0 && tendCount % errorRateWindow == 0) {
for (Node node : nodes) {
node.resetErrorCount();
}
}
processRecoverQueue();
}
use of com.aerospike.client.async.EventState in project aerospike-client-java by aerospike.
the class Node method restart.
private final void restart() {
try {
// Reset error rate.
if (cluster.maxErrorRate > 0) {
resetErrorCount();
}
// Login when user authentication is enabled.
if (cluster.authEnabled) {
login();
}
// Balance sync connections.
balanceConnections();
// Balance async connections.
if (cluster.eventState != null) {
for (EventState es : cluster.eventState) {
final EventLoop eventLoop = es.eventLoop;
eventLoop.execute(new Runnable() {
public void run() {
try {
balanceAsyncConnections(eventLoop);
} catch (Throwable e) {
if (Log.warnEnabled()) {
Log.warn("balanceAsyncConnections failed: " + this + ' ' + Util.getErrorMessage(e));
}
}
}
});
}
}
} catch (Throwable e) {
if (Log.warnEnabled()) {
Log.warn("Node restart failed: " + this + ' ' + Util.getErrorMessage(e));
}
}
}
Aggregations