Search in sources :

Example 6 with Version

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

the class RocksDbStorageEngine method delete.

@Override
public boolean delete(ByteArray key, Version version) throws PersistenceFailureException {
    StoreUtils.assertValidKey(key);
    long startTimeNs = -1;
    if (logger.isTraceEnabled())
        startTimeNs = System.nanoTime();
    synchronized (this.locks.lockFor(key.get())) {
        try {
            byte[] value = getRocksDB().get(storeHandle, key.get());
            if (value == null) {
                return false;
            }
            if (version == null) {
                // unversioned delete. Just blow away the whole thing
                getRocksDB().remove(storeHandle, key.get());
                return true;
            } else {
                // versioned deletes; need to determine what to delete
                List<Versioned<byte[]>> vals = StoreBinaryFormat.fromByteArray(value);
                Iterator<Versioned<byte[]>> iter = vals.iterator();
                int numVersions = vals.size();
                int numDeletedVersions = 0;
                // supplied version
                while (iter.hasNext()) {
                    Versioned<byte[]> curr = iter.next();
                    Version currentVersion = curr.getVersion();
                    if (currentVersion.compare(version) == Occurred.BEFORE) {
                        iter.remove();
                        numDeletedVersions++;
                    }
                }
                if (numDeletedVersions < numVersions) {
                    // we still have some valid versions
                    value = StoreBinaryFormat.toByteArray(vals);
                    getRocksDB().put(storeHandle, key.get(), value);
                } else {
                    // we have deleted all the versions; so get rid of the
                    // entry
                    // in the database
                    getRocksDB().remove(storeHandle, key.get());
                }
                return numDeletedVersions > 0;
            }
        } catch (RocksDBException e) {
            logger.error(e);
            throw new PersistenceFailureException(e);
        } finally {
            if (logger.isTraceEnabled()) {
                logger.trace("Completed DELETE (" + getName() + ") of key " + ByteUtils.toHexString(key.get()) + " (keyRef: " + System.identityHashCode(key) + ") in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
            }
        }
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) PersistenceFailureException(voldemort.store.PersistenceFailureException)

Example 7 with Version

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

the class UpdateElementById method update.

@Override
public void update(StoreClient<Map<String, Object>, Map<String, Object>> storeClient) {
    Versioned<Map<String, Object>> nodeMap = storeClient.get(_key.mapValue());
    if (nodeMap == null)
        throw new IndexOutOfBoundsException("invalid id " + _key.getId());
    Version version = (_version != null) ? _version : nodeMap.getVersion();
    VListNode<E> listNode = VListNode.valueOf(nodeMap.getValue());
    if (!listNode.isStable()) {
        throw new ObsoleteVersionException("node " + _key.getId() + " not stable.");
    }
    _result = listNode.getValue();
    VListNode<E> newNode = new VListNode<E>(_element, listNode.getId(), listNode.getPreviousId(), listNode.getNextId(), true);
    storeClient.put(_key.mapValue(), new Versioned<Map<String, Object>>(newNode.mapValue(), version));
}
Also used : ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) Version(voldemort.versioning.Version) Map(java.util.Map)

Example 8 with Version

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

the class GetVersionRequestHandler method writeResponse.

@Override
public void writeResponse(DataOutputStream outputStream) throws IOException {
    outputStream.writeShort(0);
    outputStream.writeInt(results.size());
    for (Version v : results) {
        byte[] clock = ((VectorClock) v).toBytes();
        outputStream.writeInt(clock.length);
        outputStream.write(clock);
    }
}
Also used : Version(voldemort.versioning.Version) VectorClock(voldemort.versioning.VectorClock)

Example 9 with Version

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

the class DynamicTimeoutStoreClient method putWithCustomTimeout.

/**
     * Performs a put operation with the specified composite request object
     * 
     * @param requestWrapper A composite request object containing the key and
     *        value
     * @return Version of the value for the successful put
     */
public Version putWithCustomTimeout(CompositeVoldemortRequest<K, V> requestWrapper) {
    validateTimeout(requestWrapper.getRoutingTimeoutInMs());
    List<Versioned<V>> versionedValues;
    long startTime = System.currentTimeMillis();
    String keyHexString = "";
    if (logger.isDebugEnabled()) {
        ByteArray key = (ByteArray) requestWrapper.getKey();
        keyHexString = RestUtils.getKeyHexString(key);
        logger.debug("PUT requested for key: " + keyHexString + " , for store: " + this.storeName + " at time(in ms): " + startTime + " . Nested GET and PUT VERSION requests to follow ---");
    }
    // We use the full timeout for doing the Get. In this, we're being
    // optimistic that the subsequent put might be faster such that all the
    // steps might finish within the allotted time
    requestWrapper.setResolveConflicts(true);
    versionedValues = getWithCustomTimeout(requestWrapper);
    Versioned<V> versioned = getItemOrThrow(requestWrapper.getKey(), null, versionedValues);
    long endTime = System.currentTimeMillis();
    if (versioned == null)
        versioned = Versioned.value(requestWrapper.getRawValue(), new VectorClock());
    else
        versioned.setObject(requestWrapper.getRawValue());
    // This should not happen unless there's a bug in the
    // getWithCustomTimeout
    long timeLeft = requestWrapper.getRoutingTimeoutInMs() - (endTime - startTime);
    if (timeLeft <= 0) {
        throw new StoreTimeoutException("PUT request timed out");
    }
    CompositeVersionedPutVoldemortRequest<K, V> putVersionedRequestObject = new CompositeVersionedPutVoldemortRequest<K, V>(requestWrapper.getKey(), versioned, timeLeft);
    putVersionedRequestObject.setRequestOriginTimeInMs(requestWrapper.getRequestOriginTimeInMs());
    Version result = putVersionedWithCustomTimeout(putVersionedRequestObject);
    long endTimeInMs = System.currentTimeMillis();
    if (logger.isDebugEnabled()) {
        logger.debug("PUT response received for key: " + keyHexString + " , for store: " + this.storeName + " at time(in ms): " + endTimeInMs);
    }
    return result;
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) CompositeVersionedPutVoldemortRequest(voldemort.store.CompositeVersionedPutVoldemortRequest) Version(voldemort.versioning.Version) StoreTimeoutException(voldemort.store.StoreTimeoutException) ByteArray(voldemort.utils.ByteArray)

Example 10 with Version

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

the class DefaultStoreClient method put.

public Version put(K key, V value) {
    Version version = getVersionForPut(key);
    Versioned<V> versioned = Versioned.value(value, version);
    return put(key, versioned);
}
Also used : Version(voldemort.versioning.Version)

Aggregations

Version (voldemort.versioning.Version)35 VectorClock (voldemort.versioning.VectorClock)16 Versioned (voldemort.versioning.Versioned)13 Test (org.junit.Test)11 ByteArray (voldemort.utils.ByteArray)10 VoldemortException (voldemort.VoldemortException)7 ArrayList (java.util.ArrayList)6 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)5 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 List (java.util.List)3 ByteString (com.linkedin.data.ByteString)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 InvalidMetadataException (voldemort.store.InvalidMetadataException)2 Pair (voldemort.utils.Pair)2 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)2 ImmutableList (com.google.common.collect.ImmutableList)1 RestException (com.linkedin.r2.message.rest.RestException)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1