Search in sources :

Example 66 with AerospikeException

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

the class ScanPartitionCommand 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();
    if (!valid) {
        throw new AerospikeException.ScanTerminated();
    }
    callback.scanCallback(key, record);
    tracker.setDigest(nodePartitions, key);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Record(com.aerospike.client.Record)

Example 67 with AerospikeException

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

the class LuaCache method loadPackageFromResource.

/**
 * Load lua package from a resource.
 */
public static final Prototype loadPackageFromResource(ClassLoader resourceLoader, String resourcePath, String packageName) throws AerospikeException {
    Prototype prototype = Packages.get(packageName);
    if (prototype == null) {
        try {
            InputStream is = resourceLoader.getResourceAsStream(resourcePath);
            if (is == null) {
                throw new Exception();
            }
            prototype = compile(packageName, is);
            Packages.put(packageName, prototype);
        } catch (Exception e) {
            throw new AerospikeException("Failed to read resource: " + resourcePath);
        }
    }
    return prototype;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Prototype(org.luaj.vm2.Prototype) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AerospikeException(com.aerospike.client.AerospikeException)

Example 68 with AerospikeException

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

the class LuaCache method loadPackageFromFile.

/**
 * Load lua package from a file.
 */
public static final Prototype loadPackageFromFile(String packageName) throws AerospikeException {
    Prototype prototype = Packages.get(packageName);
    if (prototype == null) {
        File source = new File(LuaConfig.SourceDirectory, packageName + ".lua");
        try {
            InputStream is = new FileInputStream(source);
            prototype = compile(packageName, is);
            Packages.put(packageName, prototype);
        } catch (Exception e) {
            throw new AerospikeException("Failed to read file: " + source.getAbsolutePath());
        }
    }
    return prototype;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Prototype(org.luaj.vm2.Prototype) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) AerospikeException(com.aerospike.client.AerospikeException)

Example 69 with AerospikeException

use of com.aerospike.client.AerospikeException 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 70 with AerospikeException

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

the class TestLargeList method simpleLargeList.

@Test
public void simpleLargeList() {
    if (!args.validateLDT()) {
        return;
    }
    Key key = new Key(args.namespace, args.set, "setkey");
    // Delete record if it already exists.
    client.delete(null, key);
    // Initialize large set operator.
    LargeList llist = client.getLargeList(null, key, binName);
    String orig1 = "llistValue1";
    String orig2 = "llistValue2";
    String orig3 = "llistValue3";
    // Write values.
    llist.add(Value.get(orig1));
    llist.add(Value.get(orig2));
    llist.add(Value.get(orig3));
    // Perform exists.
    boolean b = llist.exists(Value.get(orig2));
    assertTrue(b);
    b = llist.exists(Value.get("notfound"));
    assertFalse(b);
    // Test record not found.
    LargeList nflist = client.getLargeList(null, new Key(args.namespace, args.set, "sfdfdqw"), binName);
    try {
        b = nflist.exists(Value.get(orig2));
        assertFalse(b);
    } catch (AerospikeException ae) {
        assertEquals(ResultCode.KEY_NOT_FOUND_ERROR, ae.getResultCode());
    }
    List<Value> klist = new ArrayList<Value>();
    klist.add(Value.get(orig2));
    klist.add(Value.get(orig1));
    klist.add(Value.get("notfound"));
    List<Boolean> blist = llist.exists(klist);
    assertTrue(blist.get(0));
    assertTrue(blist.get(1));
    assertFalse(blist.get(2));
    // Test record not found.
    try {
        List<Boolean> blist2 = nflist.exists(klist);
        assertFalse(blist2.get(0));
        assertFalse(blist2.get(1));
        assertFalse(blist2.get(2));
    } catch (AerospikeException ae) {
        assertEquals(ResultCode.KEY_NOT_FOUND_ERROR, ae.getResultCode());
    }
    // Perform a Range Query -- look for "llistValue2" to "llistValue3"
    List<?> rangeList = llist.range(Value.get(orig2), Value.get(orig3));
    assertNotNull(rangeList);
    assertEquals(2, rangeList.size());
    String v2 = (String) rangeList.get(0);
    String v3 = (String) rangeList.get(1);
    assertEquals(orig2, v2);
    assertEquals(orig3, v3);
    // Remove last value.
    llist.remove(Value.get(orig3));
    int size = llist.size();
    assertEquals(2, size);
    List<?> listReceived = llist.find(Value.get(orig2));
    String expected = orig2;
    assertNotNull(listReceived);
    String stringReceived = (String) listReceived.get(0);
    assertNotNull(stringReceived);
    assertEquals(expected, stringReceived);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) LargeList(com.aerospike.client.large.LargeList) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Key(com.aerospike.client.Key) Test(org.junit.Test)

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