use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class Cluster method seedNodes.
private final boolean seedNodes(boolean failIfNotConnected) throws AerospikeException {
// Must copy array reference for copy on write semantics to work.
Host[] seedArray = seeds;
Exception[] exceptions = null;
// Add all nodes at once to avoid copying entire array multiple times.
HashMap<String, Node> nodesToAdd = new HashMap<String, Node>(seedArray.length + 16);
for (int i = 0; i < seedArray.length; i++) {
Host seed = seedArray[i];
try {
NodeValidator nv = new NodeValidator();
nv.seedNodes(this, seed, nodesToAdd);
} catch (Exception e) {
// Store exception and try next host
if (failIfNotConnected) {
if (exceptions == null) {
exceptions = new Exception[seedArray.length];
}
exceptions[i] = e;
} else {
if (Log.warnEnabled()) {
Log.warn("Seed " + seed + " failed: " + Util.getErrorMessage(e));
}
}
}
}
if (nodesToAdd.size() > 0) {
addNodes(nodesToAdd);
return true;
} else if (failIfNotConnected) {
StringBuilder sb = new StringBuilder(500);
sb.append("Failed to connect to host(s): ");
sb.append(Environment.Newline);
for (int i = 0; i < seedArray.length; i++) {
sb.append(seedArray[i]);
sb.append(' ');
Exception ex = exceptions == null ? null : exceptions[i];
if (ex != null) {
sb.append(ex.getMessage());
sb.append(Environment.Newline);
}
}
throw new AerospikeException.Connection(sb.toString());
}
return false;
}
use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class MultiCommand method parseGroup.
/**
* Parse all records in the group.
*/
private final boolean parseGroup(int receiveSize) throws IOException {
// Parse each message response and add it to the result array
dataOffset = 0;
while (dataOffset < receiveSize) {
readBytes(MSG_REMAINING_HEADER_SIZE);
resultCode = dataBuffer[5] & 0xFF;
// If other return codes are received, then abort the batch.
if (resultCode != 0) {
if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR) {
if (stopOnNotFound) {
return false;
}
} else {
throw new AerospikeException(resultCode);
}
}
byte info3 = dataBuffer[3];
// If this is the end marker of the response, do not proceed further
if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST) {
return false;
}
generation = Buffer.bytesToInt(dataBuffer, 6);
expiration = Buffer.bytesToInt(dataBuffer, 10);
batchIndex = Buffer.bytesToInt(dataBuffer, 14);
fieldCount = Buffer.bytesToShort(dataBuffer, 18);
opCount = Buffer.bytesToShort(dataBuffer, 20);
Key key = parseKey(fieldCount);
parseRow(key);
}
return true;
}
use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class NettyCommand method onServerTimeout.
protected final void onServerTimeout() {
if (state == AsyncCommand.COMPLETE) {
return;
}
putConnection();
if (command.isRead) {
// Read commands shift to prole node on timeout.
command.sequence++;
}
AerospikeException ae = new AerospikeException.Timeout(command.node, command.policy, iteration, false);
retry(ae, false);
}
use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class NettyCommand method run.
public void run() {
if (eventState.pending++ == -1) {
eventState.pending = -1;
eventState.errors++;
state = AsyncCommand.COMPLETE;
notifyFailure(new AerospikeException("Cluster has been closed"));
return;
}
long currentTime = 0;
if (hasTotalTimeout) {
currentTime = System.nanoTime();
if (state == AsyncCommand.REGISTERED) {
// Command was queued to event loop thread.
if (currentTime >= totalDeadline) {
// Command already timed out.
queueError(new AerospikeException.Timeout(null, command.policy, iteration, true));
return;
}
} else {
totalDeadline = currentTime + TimeUnit.MILLISECONDS.toNanos(command.policy.totalTimeout);
}
}
if (eventLoop.maxCommandsInProcess > 0) {
// Delay queue takes precedence over new commands.
executeFromDelayQueue();
// Handle new command.
if (eventLoop.pending >= eventLoop.maxCommandsInProcess) {
// Pending queue full. Append new command to delay queue.
if (eventLoop.maxCommandsInQueue > 0 && eventLoop.delayQueue.size() >= eventLoop.maxCommandsInQueue) {
queueError(new AerospikeException.AsyncQueueFull());
return;
}
eventLoop.delayQueue.addLast(this);
if (hasTotalTimeout) {
timeoutTask = eventLoop.timer.addTimeout(this, totalDeadline);
}
state = AsyncCommand.DELAY_QUEUE;
return;
}
}
if (hasTotalTimeout) {
long deadline;
if (command.policy.socketTimeout > 0) {
deadline = currentTime + TimeUnit.MILLISECONDS.toNanos(command.policy.socketTimeout);
if (deadline < totalDeadline) {
usingSocketTimeout = true;
} else {
deadline = totalDeadline;
}
} else {
deadline = totalDeadline;
}
timeoutTask = eventLoop.timer.addTimeout(this, deadline);
} else if (command.policy.socketTimeout > 0) {
usingSocketTimeout = true;
timeoutTask = eventLoop.timer.addTimeout(this, System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(command.policy.socketTimeout));
}
eventLoop.pending++;
executeCommand();
}
use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.
the class NettyCommand method executeCommand.
private void executeCommand() {
state = AsyncCommand.CONNECT;
try {
Node node = command.getNode(cluster);
conn = (NettyConnection) node.getAsyncConnection(eventState.index, null);
if (conn != null) {
InboundHandler handler = (InboundHandler) conn.channel.pipeline().last();
handler.command = this;
writeCommand();
return;
}
try {
final InboundHandler handler = new InboundHandler();
handler.command = this;
Bootstrap b = new Bootstrap();
b.group(eventLoop.eventLoop);
if (eventLoop.parent.isEpoll) {
b.channel(EpollSocketChannel.class);
} else {
b.channel(NioSocketChannel.class);
}
b.option(ChannelOption.TCP_NODELAY, true);
b.option(ChannelOption.AUTO_READ, false);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
conn = new NettyConnection(ch, cluster.maxSocketIdleNanos);
ChannelPipeline p = ch.pipeline();
if (eventLoop.parent.sslContext != null) {
// InetSocketAddress address = node.getAddress();
// p.addLast(eventLoop.parent.sslContext.newHandler(ch.alloc(), address.getHostString(), address.getPort()));
p.addLast(eventLoop.parent.sslContext.newHandler(ch.alloc()));
}
p.addLast(handler);
}
});
b.connect(node.getAddress());
} catch (Exception e) {
node.decrAsyncConnection(eventState.index);
throw e;
}
eventState.errors = 0;
} catch (AerospikeException.Connection ac) {
eventState.errors++;
onNetworkError(ac);
} catch (Exception e) {
// Fail without retry on unknown errors.
eventState.errors++;
fail();
notifyFailure(new AerospikeException(e));
tryDelayQueue();
}
}
Aggregations