Search in sources :

Example 41 with Versioned

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

the class TimeBasedUpdatePartitionEntriesStreamRequestHandler method processEntry.

@Override
protected void processEntry(ByteArray key, Versioned<byte[]> value) throws IOException {
    KeyLockHandle<byte[]> handle = null;
    try {
        handle = storageEngine.getAndLock(key);
        // determine if there is a version already with a greater ts.
        boolean foundGreaterTs = false;
        VectorClock streamedClock = (VectorClock) value.getVersion();
        for (Versioned<byte[]> versioned : handle.getValues()) {
            VectorClock storedClock = (VectorClock) versioned.getVersion();
            if (storedClock.getTimestamp() >= streamedClock.getTimestamp()) {
                foundGreaterTs = true;
                break;
            }
        }
        if (!foundGreaterTs) {
            // if what we are trying to write is the greatest version,
            // write it in and let go of lock
            List<Versioned<byte[]>> streamedVals = new ArrayList<Versioned<byte[]>>(1);
            streamedVals.add(value);
            handle.setValues(streamedVals);
            storageEngine.putAndUnlock(key, handle);
        } else {
            // back off and let go of lock, if found a version that is
            // greater than what we are trying to write in
            storageEngine.releaseLock(handle);
        }
    } catch (Exception e) {
        logger.error("Error in time based update entries", e);
        throw new IOException(e);
    } finally {
        if (handle != null && !handle.isClosed()) {
            storageEngine.releaseLock(handle);
        }
    }
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException)

Example 42 with Versioned

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

the class BdbStorageEngine method getAndLock.

@Override
public KeyLockHandle<byte[]> getAndLock(ByteArray key) {
    long startTimeNs = -1;
    if (logger.isTraceEnabled())
        startTimeNs = System.nanoTime();
    StoreUtils.assertValidKey(key);
    DatabaseEntry keyEntry = new DatabaseEntry(key.get());
    DatabaseEntry valueEntry = new DatabaseEntry();
    Transaction transaction = null;
    List<Versioned<byte[]>> vals = null;
    KeyLockHandle<byte[]> handle;
    try {
        transaction = environment.beginTransaction(null, null);
        // do a get for the existing values
        OperationStatus status = getBdbDatabase().get(transaction, keyEntry, valueEntry, LockMode.RMW);
        if (OperationStatus.SUCCESS == status) {
            vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
        } else {
            vals = new ArrayList<Versioned<byte[]>>(0);
        }
        handle = new KeyLockHandle<byte[]>(vals, transaction);
    } catch (DatabaseException e) {
        this.bdbEnvironmentStats.reportException(e);
        // Unless we return out properly from this method, we need to ensure
        // the transaction handle is closed on exception..
        attemptAbort(transaction);
        logger.error("Error in getAndLock for store " + this.getName(), e);
        throw new PersistenceFailureException(e);
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("Completed getAndLock (" + getName() + ") to key " + key + " (keyRef: " + System.identityHashCode(key) + " in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
        }
    }
    return handle;
}
Also used : Transaction(com.sleepycat.je.Transaction) Versioned(voldemort.versioning.Versioned) OperationStatus(com.sleepycat.je.OperationStatus) AsyncOperationStatus(voldemort.server.protocol.admin.AsyncOperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) PersistenceFailureException(voldemort.store.PersistenceFailureException)

Example 43 with Versioned

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

the class BdbConvertBaseToNewDup method transfer.

@Override
public void transfer() throws Exception {
    cursor = srcDB.openCursor(null, null);
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    byte[] prevKey = null;
    List<Versioned<byte[]>> vals = new ArrayList<Versioned<byte[]>>();
    long startTime = System.currentTimeMillis();
    int scanCount = 0;
    int keyCount = 0;
    while (cursor.getNext(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
        scanCount++;
        if (scanCount % 1000000 == 0)
            logger.info("Converted " + scanCount + " entries in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
        // read the value as a versioned Object
        VectorClock clock = new VectorClock(valueEntry.getData());
        byte[] bytes = ByteUtils.copy(valueEntry.getData(), clock.sizeInBytes(), valueEntry.getData().length);
        Versioned<byte[]> value = new Versioned<byte[]>(bytes, clock);
        byte[] key = keyEntry.getData();
        if (prevKey != null && (ByteUtils.compare(prevKey, key) != 0)) {
            // there is a new key; write out the buffered values and
            // previous key
            OperationStatus putStatus = dstDB.put(null, new DatabaseEntry(prevKey), new DatabaseEntry(StoreBinaryFormat.toByteArray(vals)));
            if (OperationStatus.SUCCESS != putStatus) {
                String errorStr = "Put failed with " + putStatus + " for key" + BdbConvertData.writeAsciiString(prevKey);
                logger.error(errorStr);
                throw new Exception(errorStr);
            }
            vals = new ArrayList<Versioned<byte[]>>();
            keyCount++;
        }
        vals.add(value);
        prevKey = key;
    }
    if (vals.size() > 0) {
        OperationStatus putStatus = dstDB.put(null, new DatabaseEntry(prevKey), new DatabaseEntry(StoreBinaryFormat.toByteArray(vals)));
        if (OperationStatus.SUCCESS != putStatus) {
            String errorStr = "Put failed with " + putStatus + " for key" + BdbConvertData.writeAsciiString(prevKey);
            logger.error(errorStr);
            throw new Exception(errorStr);
        }
        keyCount++;
    }
    logger.info("Completed " + scanCount + " entries and " + keyCount + " keys in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) DatabaseEntry(com.sleepycat.je.DatabaseEntry) OperationStatus(com.sleepycat.je.OperationStatus)

Example 44 with Versioned

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

the class BdbConvertBaseToPidScan method transfer.

@Override
public void transfer() throws Exception {
    cursor = srcDB.openCursor(null, null);
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    byte[] prevKey = null;
    List<Versioned<byte[]>> vals = new ArrayList<Versioned<byte[]>>();
    HashFunction hash = new FnvHashFunction();
    int totalPartitions = cluster.getNumberOfPartitions();
    long startTime = System.currentTimeMillis();
    int scanCount = 0;
    int keyCount = 0;
    while (cursor.getNext(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
        scanCount++;
        if (scanCount % 1000000 == 0)
            logger.info("Converted " + scanCount + " entries in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
        // read the value as a versioned Object
        VectorClock clock = new VectorClock(valueEntry.getData());
        byte[] bytes = ByteUtils.copy(valueEntry.getData(), clock.sizeInBytes(), valueEntry.getData().length);
        Versioned<byte[]> value = new Versioned<byte[]>(bytes, clock);
        byte[] key = keyEntry.getData();
        if (prevKey != null && (ByteUtils.compare(prevKey, key) != 0)) {
            // there is a new key; write out the buffered values and
            // previous key
            int partition = BdbConvertData.abs(hash.hash(prevKey)) % (Math.max(1, totalPartitions));
            OperationStatus putStatus = dstDB.put(null, new DatabaseEntry(StoreBinaryFormat.makePrefixedKey(prevKey, partition)), new DatabaseEntry(StoreBinaryFormat.toByteArray(vals)));
            if (OperationStatus.SUCCESS != putStatus) {
                String errorStr = "Put failed with " + putStatus + " for key" + BdbConvertData.writeAsciiString(prevKey);
                logger.error(errorStr);
                throw new Exception(errorStr);
            }
            vals = new ArrayList<Versioned<byte[]>>();
            keyCount++;
        }
        vals.add(value);
        prevKey = key;
    }
    if (vals.size() > 0) {
        int partition = BdbConvertData.abs(hash.hash(prevKey)) % (Math.max(1, totalPartitions));
        OperationStatus putStatus = dstDB.put(null, new DatabaseEntry(StoreBinaryFormat.makePrefixedKey(prevKey, partition)), new DatabaseEntry(StoreBinaryFormat.toByteArray(vals)));
        if (OperationStatus.SUCCESS != putStatus) {
            String errorStr = "Put failed with " + putStatus + " for key" + BdbConvertData.writeAsciiString(prevKey);
            logger.error(errorStr);
            throw new Exception(errorStr);
        }
        keyCount++;
    }
    logger.info("Completed " + scanCount + " entries and " + keyCount + " keys in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) DatabaseEntry(com.sleepycat.je.DatabaseEntry) FnvHashFunction(voldemort.utils.FnvHashFunction) HashFunction(voldemort.utils.HashFunction) FnvHashFunction(voldemort.utils.FnvHashFunction) OperationStatus(com.sleepycat.je.OperationStatus)

Example 45 with Versioned

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

the class BdbRevertNewDupToBase method transfer.

@Override
public void transfer() throws Exception {
    cursor = srcDB.openCursor(null, null);
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    VersionedSerializer<byte[]> versionedSerializer = new VersionedSerializer<byte[]>(new IdentitySerializer());
    List<Versioned<byte[]>> vals;
    long startTime = System.currentTimeMillis();
    int scanCount = 0;
    int keyCount = 0;
    while (cursor.getNext(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
        keyCount++;
        vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
        for (Versioned<byte[]> val : vals) {
            OperationStatus putStatus = dstDB.put(null, keyEntry, new DatabaseEntry(versionedSerializer.toBytes(val)));
            if (OperationStatus.SUCCESS != putStatus) {
                String errorStr = "Put failed with " + putStatus + " for key" + BdbConvertData.writeAsciiString(keyEntry.getData());
                logger.error(errorStr);
                throw new Exception(errorStr);
            }
            scanCount++;
        }
        if (scanCount % 1000000 == 0)
            logger.info("Reverted " + scanCount + " entries in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
    }
    logger.info("Reverted " + scanCount + " entries and " + keyCount + " keys in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}
Also used : Versioned(voldemort.versioning.Versioned) OperationStatus(com.sleepycat.je.OperationStatus) IdentitySerializer(voldemort.serialization.IdentitySerializer) DatabaseEntry(com.sleepycat.je.DatabaseEntry) VersionedSerializer(voldemort.serialization.VersionedSerializer)

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