Search in sources :

Example 76 with AerospikeException

use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.

the class NettyEventLoops method initTlsContext.

/**
 * Initialize TLS context. For internal use only.
 */
public void initTlsContext(TlsPolicy policy) {
    if (this.tlsPolicy != null) {
        // Already initialized.
        return;
    }
    this.tlsPolicy = policy;
    if (policy.context != null) {
        // I assume the real protocol used is defined by SSLContext.getProtocol().
        if (policy.ciphers == null) {
            sslContext = new JdkSslContext(policy.context, true, ClientAuth.NONE);
        } else {
            // Ciphers are filtered in filterCipherSuites().
            // Use null for ApplicationProtocolConfig argument.
            sslContext = new JdkSslContext(policy.context, true, null, this, null, ClientAuth.NONE);
        }
        return;
    }
    try {
        SslContextBuilder builder = SslContextBuilder.forClient();
        if (policy.protocols != null) {
            builder.protocols(policy.protocols);
        }
        if (policy.ciphers != null) {
            builder.ciphers(Arrays.asList(policy.ciphers));
        }
        sslContext = builder.build();
    } catch (Exception e) {
        throw new AerospikeException("Failed to init netty TLS: " + Util.getErrorMessage(e));
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) AerospikeException(com.aerospike.client.AerospikeException)

Example 77 with AerospikeException

use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.

the class NioCommand method executeCommand.

protected final void executeCommand() {
    state = AsyncCommand.CONNECT;
    try {
        Node node = command.getNode(cluster);
        byteBuffer = eventLoop.getByteBuffer();
        conn = (NioConnection) node.getAsyncConnection(eventLoop.index, byteBuffer);
        if (conn != null) {
            conn.attach(this);
            writeCommand();
            return;
        }
        try {
            conn = new NioConnection(node.getAddress(), cluster.maxSocketIdleNanos);
        } catch (Exception e) {
            node.decrAsyncConnection(eventLoop.index);
            throw e;
        }
        state = (cluster.getUser() != null) ? AsyncCommand.AUTH_WRITE : AsyncCommand.COMMAND_WRITE;
        conn.registerConnect(this);
        eventState.errors = 0;
    } catch (AerospikeException.Connection ac) {
        eventState.errors++;
        onNetworkError(ac, true);
    } catch (IOException ioe) {
        eventState.errors++;
        onNetworkError(new AerospikeException(ioe), true);
    } catch (Exception e) {
        // Fail without retry on unknown errors.
        eventState.errors++;
        fail();
        notifyFailure(new AerospikeException(e));
        tryDelayQueue();
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Node(com.aerospike.client.cluster.Node) IOException(java.io.IOException) IOException(java.io.IOException) AerospikeException(com.aerospike.client.AerospikeException)

Example 78 with AerospikeException

use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.

the class Node method refresh.

/**
 * Request current status from server node.
 */
public final void refresh(Peers peers) {
    if (!active) {
        return;
    }
    try {
        if (tendConnection.isClosed()) {
            tendConnection = new Connection(cluster.tlsPolicy, host.tlsName, address, cluster.getConnectionTimeout(), cluster.maxSocketIdleNanos, null);
            if (cluster.user != null) {
                try {
                    AdminCommand command = new AdminCommand(ThreadLocalData.getBuffer());
                    command.authenticate(cluster, this, tendConnection);
                } catch (AerospikeException ae) {
                    tendConnection.close();
                    throw ae;
                } catch (Exception e) {
                    tendConnection.close();
                    throw new AerospikeException(e);
                }
            }
        }
        if (peers.usePeers) {
            HashMap<String, String> infoMap = Info.request(tendConnection, "node", "peers-generation", "partition-generation");
            verifyNodeName(infoMap);
            verifyPeersGeneration(infoMap, peers);
            verifyPartitionGeneration(infoMap);
        } else {
            String[] commands = cluster.useServicesAlternate ? new String[] { "node", "partition-generation", "services-alternate" } : new String[] { "node", "partition-generation", "services" };
            HashMap<String, String> infoMap = Info.request(tendConnection, commands);
            verifyNodeName(infoMap);
            verifyPartitionGeneration(infoMap);
            addFriends(infoMap, peers);
        }
        peers.refreshCount++;
        failures = 0;
    } catch (Exception e) {
        if (peers.usePeers) {
            peers.genChanged = true;
        }
        refreshFailed(e);
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) AsyncConnection(com.aerospike.client.async.AsyncConnection) AdminCommand(com.aerospike.client.admin.AdminCommand) AerospikeException(com.aerospike.client.AerospikeException)

Example 79 with AerospikeException

use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.

the class BatchNode method generateList.

public static List<BatchNode> generateList(Cluster cluster, BatchPolicy policy, List<BatchRead> records) throws AerospikeException {
    Node[] nodes = cluster.getNodes();
    if (nodes.length == 0) {
        throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Command failed because cluster is empty.");
    }
    // Create initial key capacity for each node as average + 25%.
    int max = records.size();
    int keysPerNode = max / nodes.length;
    keysPerNode += keysPerNode >>> 2;
    // The minimum key capacity is 10.
    if (keysPerNode < 10) {
        keysPerNode = 10;
    }
    // Split keys by server node.
    List<BatchNode> batchNodes = new ArrayList<BatchNode>(nodes.length);
    for (int i = 0; i < max; i++) {
        Partition partition = new Partition(records.get(i).key);
        Node node = cluster.getMasterNode(partition);
        BatchNode batchNode = findBatchNode(batchNodes, node);
        if (batchNode == null) {
            batchNodes.add(new BatchNode(node, keysPerNode, i));
        } else {
            batchNode.addKey(i);
        }
    }
    return batchNodes;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Partition(com.aerospike.client.cluster.Partition) Node(com.aerospike.client.cluster.Node) ArrayList(java.util.ArrayList)

Example 80 with AerospikeException

use of com.aerospike.client.AerospikeException in project aerospike-client-java by aerospike.

the class BatchNode method generateList.

public static List<BatchNode> generateList(Cluster cluster, BatchPolicy policy, Key[] keys) throws AerospikeException {
    Node[] nodes = cluster.getNodes();
    if (nodes.length == 0) {
        throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Command failed because cluster is empty.");
    }
    // Create initial key capacity for each node as average + 25%.
    int keysPerNode = keys.length / nodes.length;
    keysPerNode += keysPerNode >>> 2;
    // The minimum key capacity is 10.
    if (keysPerNode < 10) {
        keysPerNode = 10;
    }
    // Split keys by server node.
    List<BatchNode> batchNodes = new ArrayList<BatchNode>(nodes.length);
    for (int i = 0; i < keys.length; i++) {
        Partition partition = new Partition(keys[i]);
        Node node = cluster.getMasterNode(partition);
        BatchNode batchNode = findBatchNode(batchNodes, node);
        if (batchNode == null) {
            batchNodes.add(new BatchNode(node, keysPerNode, i));
        } else {
            batchNode.addKey(i);
        }
    }
    return batchNodes;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Partition(com.aerospike.client.cluster.Partition) Node(com.aerospike.client.cluster.Node) ArrayList(java.util.ArrayList)

Aggregations

AerospikeException (com.aerospike.client.AerospikeException)175 Record (com.aerospike.client.Record)58 Test (org.junit.Test)58 Policy (com.aerospike.client.policy.Policy)57 Key (com.aerospike.client.Key)56 WritePolicy (com.aerospike.client.policy.WritePolicy)42 ThrowingRunnable (org.junit.function.ThrowingRunnable)41 Bin (com.aerospike.client.Bin)32 IndexTask (com.aerospike.client.task.IndexTask)29 BatchPolicy (com.aerospike.client.policy.BatchPolicy)28 BeforeClass (org.junit.BeforeClass)14 IOException (java.io.IOException)12 Node (com.aerospike.client.cluster.Node)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)8 RegisterTask (com.aerospike.client.task.RegisterTask)7 Expression (com.aerospike.client.exp.Expression)6 SocketTimeoutException (java.net.SocketTimeoutException)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 Value (com.aerospike.client.Value)5