Search in sources :

Example 1 with Connection

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

the class SyncCommand method executeCommand.

public final void executeCommand() {
    // final long tranId = TranCounter.getAndIncrement();
    Node node;
    AerospikeException exception = null;
    boolean isClientTimeout;
    // Execute command until successful, timed out or maximum iterations have been reached.
    while (true) {
        try {
            node = getNode();
        } catch (AerospikeException ae) {
            if (cluster.isActive()) {
                // Log.info("Throw AerospikeException: " + tranId + ',' + node + ',' + sequence + ',' + iteration + ',' + ae.getResultCode());
                ae.setPolicy(policy);
                ae.setIteration(iteration);
                ae.setInDoubt(isWrite(), commandSentCounter);
                throw ae;
            } else {
                throw new AerospikeException("Cluster has been closed");
            }
        }
        try {
            node.validateErrorCount();
            Connection conn = node.getConnection(policy.connectTimeout, socketTimeout, policy.timeoutDelay);
            try {
                // Set command buffer.
                writeBuffer();
                // Send command.
                conn.write(dataBuffer, dataOffset);
                commandSentCounter++;
                // Parse results.
                parseResult(conn);
                // Put connection back in pool.
                node.putConnection(conn);
                // Command has completed successfully.  Exit method.
                return;
            } catch (AerospikeException ae) {
                if (ae.keepConnection()) {
                    // Put connection back in pool.
                    node.putConnection(conn);
                } else {
                    // Close socket to flush out possible garbage.  Do not put back in pool.
                    node.closeConnection(conn);
                }
                if (ae.getResultCode() == ResultCode.TIMEOUT) {
                    // Retry on server timeout.
                    // Log.info("Server timeout: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                    exception = new AerospikeException.Timeout(policy, false);
                    isClientTimeout = false;
                    node.incrErrorCount();
                } else if (ae.getResultCode() == ResultCode.DEVICE_OVERLOAD) {
                    // Add to circuit breaker error count and retry.
                    exception = ae;
                    isClientTimeout = false;
                    node.incrErrorCount();
                } else {
                    throw ae;
                }
            } catch (Connection.ReadTimeout crt) {
                if (policy.timeoutDelay > 0) {
                    cluster.recoverConnection(new ConnectionRecover(conn, node, policy.timeoutDelay, crt, isSingle()));
                } else {
                    node.closeConnection(conn);
                }
                isClientTimeout = true;
            } catch (RuntimeException re) {
                // All runtime exceptions are considered fatal.  Do not retry.
                // Close socket to flush out possible garbage.  Do not put back in pool.
                // Log.info("Throw RuntimeException: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                node.closeConnection(conn);
                throw re;
            } catch (SocketTimeoutException ste) {
                // Full timeout has been reached.
                // Log.info("Socket timeout: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                node.closeConnection(conn);
                isClientTimeout = true;
            } catch (IOException ioe) {
                // IO errors are considered temporary anomalies.  Retry.
                // Log.info("IOException: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                node.closeConnection(conn);
                exception = new AerospikeException.Connection(ioe);
                isClientTimeout = false;
            }
        } catch (Connection.ReadTimeout crt) {
            // Connection already handled.
            isClientTimeout = true;
        } catch (AerospikeException.Connection ce) {
            // Socket connection error has occurred. Retry.
            // Log.info("Connection error: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
            exception = ce;
            isClientTimeout = false;
        } catch (AerospikeException.Backoff be) {
            // Node is in backoff state. Retry, hopefully on another node.
            // Log.info("Backoff error: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
            exception = be;
            isClientTimeout = false;
        } catch (AerospikeException ae) {
            // Log.info("Throw AerospikeException: " + tranId + ',' + node + ',' + sequence + ',' + iteration + ',' + ae.getResultCode());
            ae.setNode(node);
            ae.setPolicy(policy);
            ae.setIteration(iteration);
            ae.setInDoubt(isWrite(), commandSentCounter);
            throw ae;
        }
        // Check maxRetries.
        if (iteration > maxRetries) {
            break;
        }
        if (totalTimeout > 0) {
            // Check for total timeout.
            long remaining = deadline - System.nanoTime() - TimeUnit.MILLISECONDS.toNanos(policy.sleepBetweenRetries);
            if (remaining <= 0) {
                break;
            }
            // Convert back to milliseconds for remaining check.
            remaining = TimeUnit.NANOSECONDS.toMillis(remaining);
            if (remaining < totalTimeout) {
                totalTimeout = (int) remaining;
                if (socketTimeout > totalTimeout) {
                    socketTimeout = totalTimeout;
                }
            }
        }
        if (!isClientTimeout && policy.sleepBetweenRetries > 0) {
            // Sleep before trying again.
            Util.sleep(policy.sleepBetweenRetries);
        }
        iteration++;
        if (!prepareRetry(isClientTimeout || exception.getResultCode() != ResultCode.SERVER_NOT_AVAILABLE)) {
            // Batch may be retried in separate commands.
            if (retryBatch(cluster, socketTimeout, totalTimeout, deadline, iteration, commandSentCounter)) {
                // Batch was retried in separate commands.  Complete this command.
                return;
            }
        }
    }
    // Retries have been exhausted.  Throw last exception.
    if (isClientTimeout) {
        // Log.info("SocketTimeoutException: " + tranId + ',' + sequence + ',' + iteration);
        exception = new AerospikeException.Timeout(policy, true);
    }
    // Log.info("Runtime exception: " + tranId + ',' + sequence + ',' + iteration + ',' + exception.getMessage());
    exception.setNode(node);
    exception.setPolicy(policy);
    exception.setIteration(iteration);
    exception.setInDoubt(isWrite(), commandSentCounter);
    throw exception;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Node(com.aerospike.client.cluster.Node) Connection(com.aerospike.client.cluster.Connection) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectionRecover(com.aerospike.client.cluster.ConnectionRecover)

Example 2 with Connection

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

the class AdminCommand method executeQuery.

private void executeQuery(Cluster cluster, AdminPolicy policy) {
    writeSize();
    Node node = cluster.getRandomNode();
    int timeout = (policy == null) ? 1000 : policy.timeout;
    int status = 0;
    Connection conn = node.getConnection(timeout);
    try {
        conn.write(dataBuffer, dataOffset);
        status = readBlocks(conn);
        node.putConnection(conn);
    } catch (Exception e) {
        // Garbage may be in socket.  Do not put back into pool.
        node.closeConnection(conn);
        throw new AerospikeException(e);
    }
    if (status != QUERY_END && status > 0) {
        throw new AerospikeException(status, "Query failed.");
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Node(com.aerospike.client.cluster.Node) Connection(com.aerospike.client.cluster.Connection) IOException(java.io.IOException) AerospikeException(com.aerospike.client.AerospikeException)

Example 3 with Connection

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

the class Info method request.

/**
 * Get default info values from the specified database server node.
 * This method supports user authentication.
 *
 * @param policy	info command configuration parameters, pass in null for defaults
 * @param node		server node
 */
public static Map<String, String> request(InfoPolicy policy, Node node) throws AerospikeException {
    int timeout = (policy == null) ? DEFAULT_TIMEOUT : policy.timeout;
    Connection conn = node.getConnection(timeout);
    try {
        Map<String, String> result = request(conn);
        node.putConnection(conn);
        return result;
    } catch (RuntimeException re) {
        node.closeConnection(conn);
        throw re;
    }
}
Also used : Connection(com.aerospike.client.cluster.Connection)

Example 4 with Connection

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

the class SyncCommand method execute.

// private static final AtomicLong TranCounter = new AtomicLong();
public final void execute(Cluster cluster, Policy policy, Key key, Node node, boolean isRead) {
    // final long tranId = TranCounter.getAndIncrement();
    final Partition partition = (key != null) ? new Partition(key) : null;
    AerospikeException exception = null;
    long deadline = 0;
    int socketTimeout = policy.socketTimeout;
    int totalTimeout = policy.totalTimeout;
    int iteration = 0;
    int commandSentCounter = 0;
    boolean isClientTimeout;
    if (totalTimeout > 0) {
        deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(totalTimeout);
        if (socketTimeout > totalTimeout) {
            socketTimeout = totalTimeout;
        }
    }
    // Execute command until successful, timed out or maximum iterations have been reached.
    while (true) {
        try {
            if (partition != null) {
                // Single record command node retrieval.
                node = getNode(cluster, partition, policy.replica, isRead);
            // if (iteration > 0 && !isRead) {
            // Log.info("Retry: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
            // }
            }
            Connection conn = node.getConnection(socketTimeout);
            try {
                // Set command buffer.
                writeBuffer();
                // Check if total timeout needs to be changed in send buffer.
                if (totalTimeout != policy.totalTimeout) {
                    // Reset timeout in send buffer (destined for server) and socket.
                    Buffer.intToBytes(totalTimeout, dataBuffer, 22);
                }
                // Send command.
                conn.write(dataBuffer, dataOffset);
                commandSentCounter++;
                // Parse results.
                parseResult(conn);
                // Put connection back in pool.
                node.putConnection(conn);
                // Command has completed successfully.  Exit method.
                return;
            } catch (AerospikeException ae) {
                if (ae.keepConnection()) {
                    // Put connection back in pool.
                    node.putConnection(conn);
                } else {
                    // Close socket to flush out possible garbage.  Do not put back in pool.
                    node.closeConnection(conn);
                }
                if (ae.getResultCode() == ResultCode.TIMEOUT) {
                    // Go through retry logic on server timeout.
                    // Log.info("Server timeout: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                    exception = new AerospikeException.Timeout(node, policy, iteration + 1, false);
                    isClientTimeout = false;
                    if (isRead) {
                        super.sequence++;
                    }
                } else {
                    // Log.info("Throw AerospikeException: " + tranId + ',' + node + ',' + sequence + ',' + iteration + ',' + ae.getResultCode());
                    ae.setInDoubt(isRead, commandSentCounter);
                    throw ae;
                }
            } catch (RuntimeException re) {
                // All runtime exceptions are considered fatal.  Do not retry.
                // Close socket to flush out possible garbage.  Do not put back in pool.
                // Log.info("Throw RuntimeException: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                node.closeConnection(conn);
                throw re;
            } catch (SocketTimeoutException ste) {
                // Full timeout has been reached.
                // Log.info("Socket timeout: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                node.closeConnection(conn);
                isClientTimeout = true;
                if (isRead) {
                    super.sequence++;
                }
            } catch (IOException ioe) {
                // IO errors are considered temporary anomalies.  Retry.
                // Log.info("IOException: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
                node.closeConnection(conn);
                exception = new AerospikeException(ioe);
                isClientTimeout = false;
                super.sequence++;
            }
        } catch (AerospikeException.Connection ce) {
            // Socket connection error has occurred. Retry.
            // Log.info("Connection error: " + tranId + ',' + node + ',' + sequence + ',' + iteration);
            exception = ce;
            isClientTimeout = false;
            super.sequence++;
        }
        // Check maxRetries.
        if (++iteration > policy.maxRetries) {
            break;
        }
        if (policy.totalTimeout > 0) {
            // Check for total timeout.
            long remaining = deadline - System.nanoTime() - TimeUnit.MILLISECONDS.toNanos(policy.sleepBetweenRetries);
            if (remaining <= 0) {
                break;
            }
            // Convert back to milliseconds for remaining check.
            remaining = TimeUnit.NANOSECONDS.toMillis(remaining);
            if (remaining < totalTimeout) {
                totalTimeout = (int) remaining;
                if (socketTimeout > totalTimeout) {
                    socketTimeout = totalTimeout;
                }
            }
        }
        if (!isClientTimeout && policy.sleepBetweenRetries > 0) {
            // Sleep before trying again.
            Util.sleep(policy.sleepBetweenRetries);
        }
    }
    // Retries have been exhausted.  Throw last exception.
    if (isClientTimeout) {
        // Log.info("SocketTimeoutException: " + tranId + ',' + sequence + ',' + iteration);
        exception = new AerospikeException.Timeout(node, policy, iteration, true);
    }
    // Log.info("Runtime exception: " + tranId + ',' + sequence + ',' + iteration + ',' + exception.getMessage());
    exception.setInDoubt(isRead, commandSentCounter);
    throw exception;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Partition(com.aerospike.client.cluster.Partition) SocketTimeoutException(java.net.SocketTimeoutException) Connection(com.aerospike.client.cluster.Connection) IOException(java.io.IOException)

Example 5 with Connection

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

the class AerospikeClient method sendInfoCommand.

private String sendInfoCommand(Policy policy, String command) {
    Node node = cluster.getRandomNode();
    Connection conn = node.getConnection(policy.connectTimeout, policy.socketTimeout);
    Info info;
    try {
        info = new Info(conn, command);
        node.putConnection(conn);
    } catch (RuntimeException re) {
        node.closeConnection(conn);
        throw re;
    }
    return info.getValue();
}
Also used : Node(com.aerospike.client.cluster.Node) BatchNode(com.aerospike.client.command.BatchNode) Connection(com.aerospike.client.cluster.Connection)

Aggregations

Connection (com.aerospike.client.cluster.Connection)9 AerospikeException (com.aerospike.client.AerospikeException)5 Node (com.aerospike.client.cluster.Node)5 IOException (java.io.IOException)4 SocketTimeoutException (java.net.SocketTimeoutException)2 Info (com.aerospike.client.Info)1 NameValueParser (com.aerospike.client.Info.NameValueParser)1 ConnectionRecover (com.aerospike.client.cluster.ConnectionRecover)1 Partition (com.aerospike.client.cluster.Partition)1 BatchNode (com.aerospike.client.command.BatchNode)1 RegisterTask (com.aerospike.client.task.RegisterTask)1