Search in sources :

Example 6 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class KratiStorageEngine method entries.

@Override
public ClosableIterator<Pair<ByteArray, Versioned<byte[]>>> entries() {
    List<Pair<ByteArray, Versioned<byte[]>>> returnedList = new ArrayList<Pair<ByteArray, Versioned<byte[]>>>();
    DataArray array = datastore.getDataArray();
    for (int index = 0; index < array.length(); index++) {
        byte[] returnedBytes = array.get(index);
        if (returnedBytes != null) {
            // Extract the key value pair from this
            // TODO: Move to DynamicDataStore code
            ByteBuffer bb = ByteBuffer.wrap(returnedBytes);
            int cnt = bb.getInt();
            // Loop over all keys at this index
            for (int i = 0; i < cnt; i++) {
                int keyLen = bb.getInt();
                byte[] key = new byte[keyLen];
                bb.get(key);
                int valueLen = bb.getInt();
                byte[] value = new byte[valueLen];
                bb.get(value);
                List<Versioned<byte[]>> versions;
                try {
                    versions = disassembleValues(value);
                } catch (IOException e) {
                    versions = null;
                }
                if (versions != null) {
                    Iterator<Versioned<byte[]>> iterVersions = versions.iterator();
                    while (iterVersions.hasNext()) {
                        Versioned<byte[]> currentVersion = iterVersions.next();
                        returnedList.add(Pair.create(new ByteArray(key), currentVersion));
                    }
                }
            }
        }
    }
    return new KratiClosableIterator(returnedList);
}
Also used : Versioned(voldemort.versioning.Versioned) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DataArray(krati.array.DataArray) ByteArray(voldemort.utils.ByteArray) Pair(voldemort.utils.Pair)

Example 7 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class SampleRESTClient method main.

public static void main(String[] args) {
    // Create the client
    Properties props = new Properties();
    props.setProperty(ClientConfig.BOOTSTRAP_URLS_PROPERTY, "http://localhost:8080");
    props.setProperty(ClientConfig.ROUTING_TIMEOUT_MS_PROPERTY, "1500");
    RESTClientFactoryConfig mainConfig = new RESTClientFactoryConfig(props, null);
    RESTClientFactory factory = new RESTClientFactory(mainConfig);
    StoreClient<String, String> storeClient = factory.getStoreClient("test");
    try {
        // Sample put
        System.out.println("First valid put");
        storeClient.put("a", "Howdy!!!!");
        System.out.println("Second valid put");
        storeClient.put("b", "Partner!!!!");
        // Do a sample get operation:
        Versioned<String> versionedValue = storeClient.get("a");
        System.out.println("Received response : " + versionedValue);
        Version obsoleteVersion = ((VectorClock) versionedValue.getVersion()).clone();
        // Do a versioned put operation:
        System.out.println("First versioned put");
        versionedValue.setObject("New Value !!!");
        System.out.println("************* original version : " + versionedValue.getVersion());
        Version putVersion = storeClient.put("a", versionedValue);
        System.out.println("************* Updated version : " + putVersion);
        // Obsolete version put
        System.out.println("Obsolete put");
        Versioned<String> obsoleteVersionedValue = new Versioned<String>("Obsolete value", obsoleteVersion);
        try {
            storeClient.put("a", obsoleteVersionedValue);
            System.err.println(" **************** Should not reach this point **************** ");
        } catch (Exception e) {
            System.out.println("Exception occurred as expected: " + e.getMessage());
        }
        // Do a get again on the last versioned put operation:
        versionedValue = storeClient.get("a");
        System.out.println("Received response on the versioned put: " + versionedValue);
        System.out.println("Versioned put based on the last put ");
        Versioned<String> newVersionedPut = new Versioned<String>("Yet another value !!!", putVersion);
        storeClient.put("a", newVersionedPut);
        // Do a get again on the last versioned put operation:
        versionedValue = storeClient.get("a");
        System.out.println("Received response on the (second) versioned put: " + versionedValue);
        List<String> keyList = new ArrayList<String>();
        keyList.add("a");
        keyList.add("b");
        System.out.println("Received response : " + storeClient.getAll(keyList));
    } finally {
        factory.close();
    }
}
Also used : Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 8 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class RestServerAPITest method testGetAllWithConflictingVersions.

/**
     * test getall with k1,k2. k1 has v1,v2 and k2 has v3
     */
@Test
public void testGetAllWithConflictingVersions() {
    logger.info("\n\n********************  Testing Get All with multiple versions *******************\n\n");
    Map<ByteArray, List<Versioned<byte[]>>> input = new HashMap<ByteArray, List<Versioned<byte[]>>>();
    List<Versioned<byte[]>> valuesList2 = new ArrayList<Versioned<byte[]>>();
    VectorClock vectorClock1 = new VectorClock();
    vectorClock1.incrementVersion(voldemortConfig.getNodeId(), System.currentTimeMillis());
    ByteArray key2 = new ByteArray("key22".getBytes());
    Versioned<byte[]> value1 = new Versioned<byte[]>("value22".getBytes(), vectorClock1);
    store.put(key2, value1, null);
    valuesList2.add(value1);
    VectorClock vectorClock2 = new VectorClock();
    vectorClock2.incrementVersion(1, System.currentTimeMillis());
    Versioned<byte[]> value2 = new Versioned<byte[]>("value23".getBytes(), vectorClock2);
    store.put(key2, value2, null);
    valuesList2.add(value2);
    input.put(key2, valuesList2);
    List<Versioned<byte[]>> valuesList3 = new ArrayList<Versioned<byte[]>>();
    VectorClock vectorClock3 = new VectorClock();
    vectorClock3.incrementVersion(voldemortConfig.getNodeId(), System.currentTimeMillis());
    ByteArray key3 = new ByteArray("key23".getBytes());
    Versioned<byte[]> value3 = new Versioned<byte[]>("value43".getBytes(), vectorClock3);
    store.put(key3, value3, null);
    valuesList3.add(value3);
    input.put(key3, valuesList3);
    Map<ByteArray, List<Versioned<byte[]>>> output = store.getAll(input.keySet(), null);
    assertEquals(input, output);
    // cleanup specific to this test case
    deleteCreatedKeys(key2);
    deleteCreatedKeys(key3);
}
Also used : Versioned(voldemort.versioning.Versioned) HashMap(java.util.HashMap) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) ByteArray(voldemort.utils.ByteArray) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 9 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class RestServiceR2StoreTest method testGetVersions.

@Override
@Test
public void testGetVersions() throws Exception {
    List<ByteArray> keys = getKeys(2);
    ByteArray key = keys.get(0);
    byte[] value = getValue();
    VectorClock vc = getClock(0, 0);
    Store<ByteArray, byte[], byte[]> store = getStore();
    store.put(key, Versioned.value(value, vc), null);
    List<Versioned<byte[]>> versioneds = store.get(key, null);
    List<Version> versions = store.getVersions(key);
    assertEquals(1, versioneds.size());
    assertTrue(versions.size() > 0);
    for (int i = 0; i < versions.size(); i++) assertEquals(versioneds.get(0).getVersion(), versions.get(i));
    assertEquals(0, store.getVersions(keys.get(1)).size());
}
Also used : Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) VectorClock(voldemort.versioning.VectorClock) ByteArray(voldemort.utils.ByteArray) AbstractByteArrayStoreTest(voldemort.store.AbstractByteArrayStoreTest) Test(org.junit.Test)

Example 10 with Versioned

use of voldemort.versioning.Versioned in project voldemort by voldemort.

the class ThreadPoolRoutedStore method getAll.

@Override
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys, Map<ByteArray, byte[]> transforms) throws VoldemortException {
    StoreUtils.assertValidKeys(keys);
    Map<ByteArray, List<Versioned<byte[]>>> result = StoreUtils.newEmptyHashMap(keys);
    // Keys for each node needed to satisfy storeDef.getPreferredReads() if
    // no failures.
    Map<Node, List<ByteArray>> nodeToKeysMap = Maps.newHashMap();
    // Keep track of nodes per key that might be needed if there are
    // failures during getAll
    Map<ByteArray, List<Node>> keyToExtraNodesMap = Maps.newHashMap();
    for (ByteArray key : keys) {
        List<Node> availableNodes = availableNodes(routingStrategy.routeRequest(key.get()));
        // quickly fail if there aren't enough nodes to meet the requirement
        checkRequiredReads(availableNodes);
        int preferredReads = storeDef.getPreferredReads();
        List<Node> preferredNodes = Lists.newArrayListWithCapacity(preferredReads);
        List<Node> extraNodes = Lists.newArrayListWithCapacity(3);
        for (Node node : availableNodes) {
            if (preferredNodes.size() < preferredReads)
                preferredNodes.add(node);
            else
                extraNodes.add(node);
        }
        for (Node node : preferredNodes) {
            List<ByteArray> nodeKeys = nodeToKeysMap.get(node);
            if (nodeKeys == null) {
                nodeKeys = Lists.newArrayList();
                nodeToKeysMap.put(node, nodeKeys);
            }
            nodeKeys.add(key);
        }
        if (!extraNodes.isEmpty()) {
            List<Node> nodes = keyToExtraNodesMap.get(key);
            if (nodes == null)
                keyToExtraNodesMap.put(key, extraNodes);
            else
                nodes.addAll(extraNodes);
        }
    }
    List<Callable<GetAllResult>> callables = Lists.newArrayList();
    for (Map.Entry<Node, List<ByteArray>> entry : nodeToKeysMap.entrySet()) {
        final Node node = entry.getKey();
        final Collection<ByteArray> nodeKeys = entry.getValue();
        if (failureDetector.isAvailable(node))
            callables.add(new GetAllCallable(node, nodeKeys, transforms));
    }
    // A list of thrown exceptions, indicating the number of failures
    List<Throwable> failures = new CopyOnWriteArrayList<Throwable>();
    List<NodeValue<ByteArray, byte[]>> nodeValues = Lists.newArrayList();
    Map<ByteArray, MutableInt> keyToSuccessCount = Maps.newHashMap();
    for (ByteArray key : keys) keyToSuccessCount.put(key, new MutableInt(0));
    List<Future<GetAllResult>> futures;
    long timeoutMs = timeoutConfig.getOperationTimeout(VoldemortOpCode.GET_ALL_OP_CODE);
    try {
        // TODO What to do about timeouts? They should be longer as getAll
        // is likely to
        // take longer. At the moment, it's just timeoutMs * 3, but should
        // this be based on the number of the keys?
        futures = executor.invokeAll(callables, timeoutMs * 3, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        throw new InsufficientOperationalNodesException("getAll operation interrupted.", e);
    }
    for (Future<GetAllResult> f : futures) {
        if (f.isCancelled()) {
            logger.warn("Get operation timed out after " + timeoutMs + " ms.");
            continue;
        }
        try {
            GetAllResult getResult = f.get();
            if (getResult.exception != null) {
                if (getResult.exception instanceof VoldemortApplicationException) {
                    throw (VoldemortException) getResult.exception;
                }
                failures.add(getResult.exception);
                continue;
            }
            for (ByteArray key : getResult.callable.nodeKeys) {
                List<Versioned<byte[]>> retrieved = getResult.retrieved.get(key);
                MutableInt successCount = keyToSuccessCount.get(key);
                successCount.increment();
                /*
                     * retrieved can be null if there are no values for the key
                     * provided
                     */
                if (retrieved != null) {
                    List<Versioned<byte[]>> existing = result.get(key);
                    if (existing == null)
                        result.put(key, Lists.newArrayList(retrieved));
                    else
                        existing.addAll(retrieved);
                }
            }
            nodeValues.addAll(getResult.nodeValues);
        } catch (InterruptedException e) {
            throw new InsufficientOperationalNodesException("getAll operation interrupted.", e);
        } catch (ExecutionException e) {
            // should never happen
            if (e.getCause() instanceof Error)
                throw (Error) e.getCause();
            else
                logger.error(e.getMessage(), e);
        }
    }
    for (ByteArray key : keys) {
        MutableInt successCountWrapper = keyToSuccessCount.get(key);
        int successCount = successCountWrapper.intValue();
        if (successCount < storeDef.getPreferredReads()) {
            List<Node> extraNodes = keyToExtraNodesMap.get(key);
            if (extraNodes != null) {
                for (Node node : extraNodes) {
                    long startNs = System.nanoTime();
                    try {
                        List<Versioned<byte[]>> values = innerStores.get(node.getId()).get(key, transforms == null ? null : transforms.get(key));
                        fillRepairReadsValues(nodeValues, key, node, values);
                        List<Versioned<byte[]>> versioneds = result.get(key);
                        if (versioneds == null)
                            result.put(key, Lists.newArrayList(values));
                        else
                            versioneds.addAll(values);
                        recordSuccess(node, startNs);
                        if (++successCount >= storeDef.getPreferredReads())
                            break;
                    } catch (UnreachableStoreException e) {
                        failures.add(e);
                        recordException(node, startNs, e);
                    } catch (VoldemortApplicationException e) {
                        throw e;
                    } catch (Exception e) {
                        logger.warn("Error in GET_ALL on node " + node.getId() + "(" + node.getHost() + ")", e);
                        failures.add(e);
                    }
                }
            }
        }
        successCountWrapper.setValue(successCount);
    }
    repairReads(nodeValues, repairReads && (transforms == null || transforms.size() == 0));
    for (Map.Entry<ByteArray, MutableInt> mapEntry : keyToSuccessCount.entrySet()) {
        int successCount = mapEntry.getValue().intValue();
        if (successCount < storeDef.getRequiredReads())
            throw new InsufficientOperationalNodesException(this.storeDef.getRequiredReads() + " reads required, but " + successCount + " succeeded.", failures);
    }
    return result;
}
Also used : Versioned(voldemort.versioning.Versioned) Node(voldemort.cluster.Node) VoldemortException(voldemort.VoldemortException) Callable(java.util.concurrent.Callable) ByteArray(voldemort.utils.ByteArray) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) UnreachableStoreException(voldemort.store.UnreachableStoreException) ExecutionException(java.util.concurrent.ExecutionException) VoldemortApplicationException(voldemort.VoldemortApplicationException) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) InsufficientOperationalNodesException(voldemort.store.InsufficientOperationalNodesException) VoldemortException(voldemort.VoldemortException) VoldemortApplicationException(voldemort.VoldemortApplicationException) UnreachableStoreException(voldemort.store.UnreachableStoreException) ExecutionException(java.util.concurrent.ExecutionException) MutableInt(org.apache.commons.lang.mutable.MutableInt) InsufficientOperationalNodesException(voldemort.store.InsufficientOperationalNodesException) Future(java.util.concurrent.Future) Map(java.util.Map) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

Versioned (voldemort.versioning.Versioned)214 ByteArray (voldemort.utils.ByteArray)130 Test (org.junit.Test)88 VectorClock (voldemort.versioning.VectorClock)73 ArrayList (java.util.ArrayList)56 VoldemortException (voldemort.VoldemortException)48 List (java.util.List)36 StoreDefinition (voldemort.store.StoreDefinition)29 HashMap (java.util.HashMap)28 IOException (java.io.IOException)27 Node (voldemort.cluster.Node)27 Pair (voldemort.utils.Pair)25 Slop (voldemort.store.slop.Slop)22 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)22 Map (java.util.Map)21 Cluster (voldemort.cluster.Cluster)17 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)15 InvalidMetadataException (voldemort.store.InvalidMetadataException)13 PersistenceFailureException (voldemort.store.PersistenceFailureException)13 Version (voldemort.versioning.Version)13