Search in sources :

Example 71 with AerospikeException

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

the class LargeList method validateWithDistinctBins.

private void validateWithDistinctBins(List<Map<String, Object>> list, int index, Calendar expectedTime, String expectedTicker, int expectedQty, double expectedPrice) throws AerospikeException {
    Map<String, Object> map = list.get(index);
    Calendar receivedTime = new GregorianCalendar();
    receivedTime.setTimeInMillis((Long) map.get("key"));
    if (!expectedTime.equals(receivedTime)) {
        throw new AerospikeException("Time mismatch: Expected " + expectedTime + ". Received " + receivedTime);
    }
    String receivedTicker = (String) map.get("ticker");
    if (!expectedTicker.equals(receivedTicker)) {
        throw new AerospikeException("Ticker mismatch: Expected " + expectedTicker + ". Received " + receivedTicker);
    }
    long receivedQty = (Long) map.get("qty");
    if (expectedQty != receivedQty) {
        throw new AerospikeException("Quantity mismatch: Expected " + expectedQty + ". Received " + receivedQty);
    }
    double receivedPrice = Double.longBitsToDouble((Long) map.get("price"));
    if (expectedPrice != receivedPrice) {
        throw new AerospikeException("Price mismatch: Expected " + expectedPrice + ". Received " + receivedPrice);
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar)

Example 72 with AerospikeException

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

the class LargeList method runWithSerializedBin.

/**
	 * Use serialized bin for row in largelist bin.
	 */
@SuppressWarnings("unchecked")
public void runWithSerializedBin(AerospikeClient client, Parameters params) throws AerospikeException, IOException {
    Key key = new Key(params.namespace, params.set, "accountId");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    // Initialize large list operator.
    com.aerospike.client.large.LargeList list = client.getLargeList(params.writePolicy, key, "trades");
    // Write trades
    Map<String, Value> map = new HashMap<String, Value>();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(500);
    DataOutputStream writer = new DataOutputStream(byteStream);
    Calendar timestamp1 = new GregorianCalendar(2014, 6, 25, 12, 18, 43);
    map.put("key", Value.get(timestamp1.getTimeInMillis()));
    // ticker
    writer.writeUTF("IBM");
    // qty
    writer.writeInt(100);
    // price
    writer.writeDouble(181.82);
    map.put("value", Value.get(byteStream.toByteArray()));
    list.add(Value.get(map));
    Calendar timestamp2 = new GregorianCalendar(2014, 6, 26, 9, 33, 17);
    map.put("key", Value.get(timestamp2.getTimeInMillis()));
    byteStream.reset();
    // ticker
    writer.writeUTF("GE");
    // qty
    writer.writeInt(500);
    // price
    writer.writeDouble(26.36);
    map.put("value", Value.get(byteStream.toByteArray()));
    list.add(Value.get(map));
    Calendar timestamp3 = new GregorianCalendar(2014, 6, 27, 14, 40, 19);
    map.put("key", Value.get(timestamp3.getTimeInMillis()));
    byteStream.reset();
    // ticker
    writer.writeUTF("AAPL");
    // qty
    writer.writeInt(75);
    // price
    writer.writeDouble(91.85);
    map.put("value", Value.get(byteStream.toByteArray()));
    list.add(Value.get(map));
    // Verify list size
    int size = list.size();
    if (size != 3) {
        throw new AerospikeException("List size mismatch. Expected 3 Received " + size);
    }
    // Filter on range of timestamps
    Calendar begin = new GregorianCalendar(2014, 6, 26);
    Calendar end = new GregorianCalendar(2014, 6, 28);
    List<Map<String, Object>> results = (List<Map<String, Object>>) list.range(Value.get(begin.getTimeInMillis()), Value.get(end.getTimeInMillis()));
    if (results == null) {
        throw new AerospikeException("Range returned null.");
    }
    if (results.size() != 2) {
        throw new AerospikeException("Query results size mismatch. Expected 2 Received " + results.size());
    }
    // Verify data.
    validateWithSerializedBin(results, 0, timestamp2, "GE", 500, 26.36);
    validateWithSerializedBin(results, 1, timestamp3, "AAPL", 75, 91.85);
    console.info("Data matched.");
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Value(com.aerospike.client.Value) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.aerospike.client.Key)

Example 73 with AerospikeException

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

the class LargeList method validateWithSerializedBin.

private void validateWithSerializedBin(List<Map<String, Object>> list, int index, Calendar expectedTime, String expectedTicker, int expectedQty, double expectedPrice) throws AerospikeException, IOException {
    Map<String, Object> map = list.get(index);
    Calendar receivedTime = new GregorianCalendar();
    receivedTime.setTimeInMillis((Long) map.get("key"));
    if (!expectedTime.equals(receivedTime)) {
        throw new AerospikeException("Time mismatch: Expected " + expectedTime + ". Received " + receivedTime);
    }
    byte[] value = (byte[]) map.get("value");
    ByteArrayInputStream ms = new ByteArrayInputStream(value);
    DataInputStream reader = new DataInputStream(ms);
    String receivedTicker = reader.readUTF();
    if (!expectedTicker.equals(receivedTicker)) {
        throw new AerospikeException("Ticker mismatch: Expected " + expectedTicker + ". Received " + receivedTicker);
    }
    int receivedQty = reader.readInt();
    if (expectedQty != receivedQty) {
        throw new AerospikeException("Quantity mismatch: Expected " + expectedQty + ". Received " + receivedQty);
    }
    double receivedPrice = reader.readDouble();
    if (expectedPrice != receivedPrice) {
        throw new AerospikeException("Price mismatch: Expected " + expectedPrice + ". Received " + receivedPrice);
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) ByteArrayInputStream(java.io.ByteArrayInputStream) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) DataInputStream(java.io.DataInputStream)

Example 74 with AerospikeException

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

the class LargeList method runWithDistinctBins.

/**
	 * Use distinct sub-bins for row in largelist bin. 
	 */
@SuppressWarnings("unchecked")
public void runWithDistinctBins(AerospikeClient client, Parameters params) throws AerospikeException {
    Key key = new Key(params.namespace, params.set, "accountId");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    // Initialize large list operator.
    com.aerospike.client.large.LargeList list = client.getLargeList(params.writePolicy, key, "trades");
    // Write trades
    Map<String, Value> map = new HashMap<String, Value>();
    Calendar timestamp1 = new GregorianCalendar(2014, 6, 25, 12, 18, 43);
    map.put("key", Value.get(timestamp1.getTimeInMillis()));
    map.put("ticker", Value.get("IBM"));
    map.put("qty", Value.get(100));
    map.put("price", Value.get(Double.doubleToLongBits(181.82)));
    list.add(Value.get(map));
    Calendar timestamp2 = new GregorianCalendar(2014, 6, 26, 9, 33, 17);
    map.put("key", Value.get(timestamp2.getTimeInMillis()));
    map.put("ticker", Value.get("GE"));
    map.put("qty", Value.get(500));
    map.put("price", Value.get(Double.doubleToLongBits(26.36)));
    list.add(Value.get(map));
    Calendar timestamp3 = new GregorianCalendar(2014, 6, 27, 14, 40, 19);
    map.put("key", Value.get(timestamp3.getTimeInMillis()));
    map.put("ticker", Value.get("AAPL"));
    map.put("qty", Value.get(75));
    map.put("price", Value.get(Double.doubleToLongBits(91.85)));
    list.add(Value.get(map));
    // Verify list size
    int size = list.size();
    if (size != 3) {
        throw new AerospikeException("List size mismatch. Expected 3 Received " + size);
    }
    // Filter on range of timestamps
    Calendar begin = new GregorianCalendar(2014, 6, 26);
    Calendar end = new GregorianCalendar(2014, 6, 28);
    List<Map<String, Object>> results = (List<Map<String, Object>>) list.range(Value.get(begin.getTimeInMillis()), Value.get(end.getTimeInMillis()));
    if (results == null) {
        throw new AerospikeException("Range returned null.");
    }
    if (results.size() != 2) {
        throw new AerospikeException("Query results size mismatch. Expected 2 Received " + results.size());
    }
    // Verify data.
    validateWithDistinctBins(results, 0, timestamp2, "GE", 500, 26.36);
    validateWithDistinctBins(results, 1, timestamp3, "AAPL", 75, 91.85);
    console.info("Data matched.");
    console.info("Run large list scan.");
    List<Map<String, Object>> rows = (List<Map<String, Object>>) list.scan();
    for (Map<String, Object> row : rows) {
        for (@SuppressWarnings("unused") Map.Entry<String, Object> entry : row.entrySet()) {
        //console.Info(entry.Key.ToString());
        //console.Info(entry.Value.ToString());
        }
    }
    console.info("Large list scan complete.");
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) HashMap(java.util.HashMap) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Value(com.aerospike.client.Value) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.aerospike.client.Key)

Example 75 with AerospikeException

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

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