Search in sources :

Example 46 with AerospikeException

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

the class NettyCommand method executeCommand.

private void executeCommand(long deadline, int tstate) {
    state = AsyncCommand.CHANNEL_INIT;
    iteration++;
    try {
        node = command.getNode(cluster);
        node.validateErrorCount();
        conn = (NettyConnection) node.getAsyncConnection(eventState.index, null);
        if (conn != null) {
            setTimeoutTask(deadline, tstate);
            InboundHandler handler = (InboundHandler) conn.channel.pipeline().last();
            handler.setCommand(this);
            writeCommand();
            return;
        }
        connectInProgress = true;
        if (command.policy.connectTimeout > 0) {
            timeoutState = new TimeoutState(deadline, tstate);
            deadline = timeoutState.start + TimeUnit.MILLISECONDS.toNanos(command.policy.connectTimeout);
            timeoutTask.cancel();
            eventLoop.timer.addTimeout(timeoutTask, deadline);
        } else {
            setTimeoutTask(deadline, tstate);
        }
        final InboundHandler handler = new InboundHandler(this);
        Bootstrap b = new Bootstrap();
        initBootstrap(b, cluster, eventLoop);
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) {
                if (state != AsyncCommand.CHANNEL_INIT) {
                    // Timeout occurred. Close channel.
                    try {
                        ch.close();
                    } catch (Throwable e) {
                    }
                    connectInProgress = false;
                    return;
                }
                state = AsyncCommand.CONNECT;
                conn = new NettyConnection(ch);
                node.connectionOpened(eventLoop.index);
                connectInProgress = false;
                ChannelPipeline p = ch.pipeline();
                if (cluster.tlsPolicy != null && !cluster.tlsPolicy.forLoginOnly) {
                    state = AsyncCommand.TLS_HANDSHAKE;
                    cluster.nettyTlsContext.addHandler(ch, p);
                }
                p.addLast(handler);
            }
        });
        b.connect(node.getAddress());
        eventState.errors = 0;
    } catch (AerospikeException.Connection ac) {
        eventState.errors++;
        onNetworkError(ac);
    } catch (AerospikeException.Backoff ab) {
        eventState.errors++;
        onBackoffError(ab);
    } catch (AerospikeException ae) {
        // Fail without retry on non-connection errors.
        eventState.errors++;
        onFatalError(ae);
    } catch (Throwable e) {
        // Fail without retry on unknown errors.
        eventState.errors++;
        onFatalError(new AerospikeException(e));
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) KQueueSocketChannel(io.netty.channel.kqueue.KQueueSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) IOUringSocketChannel(io.netty.incubator.channel.uring.IOUringSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 47 with AerospikeException

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

the class NodeValidator method setAddress.

private void setAddress(Cluster cluster, HashMap<String, String> map, String addressCommand, String tlsName) {
    String result = map.get(addressCommand);
    if (result == null || result.length() == 0) {
        // Load balancer detection is not possible.
        return;
    }
    List<Host> hosts = Host.parseServiceHosts(result);
    Host h;
    // Search real hosts for seed.
    for (Host host : hosts) {
        h = host;
        if (cluster.ipMap != null) {
            String alt = cluster.ipMap.get(h.name);
            if (alt != null) {
                h = new Host(alt, h.port);
            }
        }
        if (h.equals(this.primaryHost)) {
            // Found seed which is not a load balancer.
            return;
        }
    }
    // Find first valid real host.
    for (Host host : hosts) {
        try {
            h = host;
            if (cluster.ipMap != null) {
                String alt = cluster.ipMap.get(h.name);
                if (alt != null) {
                    h = new Host(alt, h.port);
                }
            }
            InetAddress[] addresses = InetAddress.getAllByName(h.name);
            for (InetAddress address : addresses) {
                try {
                    InetSocketAddress socketAddress = new InetSocketAddress(address, h.port);
                    Connection conn = (cluster.tlsPolicy != null) ? new Connection(cluster.tlsPolicy, tlsName, socketAddress, cluster.connectTimeout) : new Connection(socketAddress, cluster.connectTimeout);
                    try {
                        if (this.sessionToken != null) {
                            if (!AdminCommand.authenticate(cluster, conn, this.sessionToken)) {
                                throw new AerospikeException("Authentication failed");
                            }
                        }
                        // Authenticated connection.  Set real host.
                        setAliases(address, tlsName, h.port);
                        this.primaryHost = new Host(address.getHostAddress(), tlsName, h.port);
                        this.primaryAddress = socketAddress;
                        this.primaryConn.close();
                        this.primaryConn = conn;
                        return;
                    } catch (Exception e) {
                        conn.close();
                    }
                } catch (Exception e) {
                // Try next address.
                }
            }
        } catch (Exception e) {
        // Try next host.
        }
    }
    // with original seed.
    if (Log.infoEnabled()) {
        Log.info("Invalid address " + result + ". access-address is probably not configured on server.");
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) InetSocketAddress(java.net.InetSocketAddress) Host(com.aerospike.client.Host) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) AerospikeException(com.aerospike.client.AerospikeException)

Example 48 with AerospikeException

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

the class AsyncQuery method parseRow.

@Override
protected void parseRow(Key key) throws AerospikeException {
    if (resultCode != 0) {
        throw new AerospikeException(resultCode);
    }
    Record record = parseRecord();
    listener.onRecord(key, record);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Record(com.aerospike.client.Record)

Example 49 with AerospikeException

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

the class AsyncRead method handleUdfError.

private final void handleUdfError(int resultCode) {
    String ret = (String) record.bins.get("FAILURE");
    if (ret == null) {
        throw new AerospikeException(resultCode);
    }
    String message;
    int code;
    try {
        String[] list = ret.split(":");
        code = Integer.parseInt(list[2].trim());
        message = list[0] + ':' + list[1] + ' ' + list[3];
    } catch (Exception e) {
        // Use generic exception if parse error occurs.
        throw new AerospikeException(resultCode, ret);
    }
    throw new AerospikeException(code, message);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) AerospikeException(com.aerospike.client.AerospikeException)

Example 50 with AerospikeException

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

the class AsyncScanPartition method parseRow.

@Override
protected void parseRow(Key key) {
    if ((info3 & Command.INFO3_PARTITION_DONE) != 0) {
        // specified partition will need to be requested on the scan retry.
        if (resultCode == 0) {
            tracker.partitionDone(nodePartitions, generation);
        }
        return;
    }
    if (resultCode != 0) {
        throw new AerospikeException(resultCode);
    }
    Record record = parseRecord();
    listener.onRecord(key, record);
    tracker.setDigest(nodePartitions, key);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Record(com.aerospike.client.Record)

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