Search in sources :

Example 76 with VectorClock

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

the class DefaultStoreClientTest method testPutVersioned.

@Test
public void testPutVersioned() {
    client.put("k", Versioned.value("v"));
    Versioned<String> v = client.get("k");
    assertEquals("GET should return the version set by PUT.", "v", v.getValue());
    VectorClock expected = new VectorClock();
    expected.incrementVersion(nodeId, time.getMilliseconds());
    assertEquals("The version should be incremented after a put.", expected, v.getVersion());
    try {
        client.put("k", Versioned.value("v"));
        fail("Put of obsolete version should throw exception.");
    } catch (ObsoleteVersionException e) {
    // this is good
    }
    // PUT of a concurrent version should succeed
    client.put("k", new Versioned<String>("v2", new VectorClock().incremented(nodeId + 1, time.getMilliseconds())));
    assertEquals("GET should return the new value set by PUT.", "v2", client.getValue("k"));
    assertEquals("GET should return the new version set by PUT.", expected.incremented(nodeId + 1, time.getMilliseconds()), client.get("k").getVersion());
}
Also used : ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) VectorClock(voldemort.versioning.VectorClock) Test(org.junit.Test)

Example 77 with VectorClock

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

the class ImportTextDumpToBDB method lineToEntry.

public static Pair<ByteArray, Versioned<byte[]>> lineToEntry(String line) throws DecoderException {
    String[] components = line.split(" ");
    String keyBytesString = components[0];
    byte[] keyBytes = ByteUtils.fromHexString(keyBytesString);
    ByteArray key = new ByteArray(keyBytes);
    String versionBytesString = components[1];
    byte[] versionBytes = ByteUtils.fromHexString(versionBytesString);
    Version version = new VectorClock(versionBytes, 0);
    String valueBytesString = components[1];
    byte[] value = ByteUtils.fromHexString(valueBytesString);
    return new Pair<ByteArray, Versioned<byte[]>>(key, new Versioned<byte[]>(value, version));
}
Also used : Version(voldemort.versioning.Version) VectorClock(voldemort.versioning.VectorClock) ByteArray(voldemort.utils.ByteArray) Pair(voldemort.utils.Pair)

Example 78 with VectorClock

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

the class VersionIncrementingStore method put.

@Override
public void put(K key, Versioned<V> value, T transforms) throws VoldemortException {
    value = value.cloneVersioned();
    VectorClock clock = (VectorClock) value.getVersion();
    clock.incrementVersion(nodeId, time.getMilliseconds());
    super.put(key, value, transforms);
}
Also used : VectorClock(voldemort.versioning.VectorClock)

Example 79 with VectorClock

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

the class DataCleanupJob method run.

@Override
public void run() {
    // if the server is neither normal nor offline , skip this run.
    if (!isServerInNormalState() && !isServerInOfflineState()) {
        return;
    }
    StoreDefinition storeDef = null;
    try {
        storeDef = MetadataStore.getStoreDef(storeName, metadataStore);
    } catch (VoldemortException ex) {
        logger.info("Error retrieving store " + storeName + " for data cleanup job ", ex);
        return;
    }
    Integer retentionDays = storeDef.getRetentionDays();
    if (retentionDays == null || retentionDays <= 0) {
        logger.info("Store " + storeName + " does not have retention period set, skipping cleanup job . RetentionDays " + retentionDays);
        return;
    }
    long maxAgeMs = retentionDays * Time.MS_PER_DAY;
    logger.info("Store " + storeName + " cleanup job is starting with RetentionDays " + retentionDays);
    acquireCleanupPermit(scanProgressThisRun, deleteProgressThisRun);
    ClosableIterator<Pair<K, Versioned<V>>> iterator = null;
    try {
        int maxReadRate = storeDef.hasRetentionScanThrottleRate() ? storeDef.getRetentionScanThrottleRate() : Integer.MAX_VALUE;
        EventThrottler throttler = new EventThrottler(maxReadRate);
        store.beginBatchModifications();
        logger.info("Starting data cleanup on store \"" + store.getName() + "\"...");
        long now = time.getMilliseconds();
        iterator = store.entries();
        while (iterator.hasNext()) {
            // check if we have been interrupted
            if (Thread.currentThread().isInterrupted()) {
                logger.info("Datacleanup job halted.");
                return;
            }
            final long INETERVAL = 10000;
            long entriesScanned = scanProgressThisRun.get();
            if (entriesScanned % INETERVAL == 0) {
                if (!isServerInNormalState() && !isServerInOfflineState()) {
                    return;
                }
            }
            scanProgressThisRun.incrementAndGet();
            Pair<K, Versioned<V>> keyAndVal = iterator.next();
            VectorClock clock = (VectorClock) keyAndVal.getSecond().getVersion();
            if (now - clock.getTimestamp() > maxAgeMs) {
                store.delete(keyAndVal.getFirst(), clock);
                final long entriesDeleted = this.deleteProgressThisRun.incrementAndGet();
                if (logger.isDebugEnabled() && entriesDeleted % INETERVAL == 0) {
                    logger.debug("Deleted item " + this.deleteProgressThisRun.get());
                }
            }
            // throttle on number of entries.
            throttler.maybeThrottle(1);
        }
        // log the total items scanned, so we will get an idea of data
        // growth in a cheap, periodic way
        logger.info("Data cleanup on store \"" + store.getName() + "\" is complete; " + this.deleteProgressThisRun.get() + " items deleted. " + scanProgressThisRun.get() + " items scanned");
    } catch (Exception e) {
        logger.error("Error in data cleanup job for store " + store.getName() + ": ", e);
    } finally {
        closeIterator(iterator);
        logger.info("Releasing lock  after data cleanup on \"" + store.getName() + "\".");
        this.cleanupPermits.release(this.getClass().getCanonicalName());
        synchronized (this) {
            totalEntriesScanned += scanProgressThisRun.get();
            scanProgressThisRun.set(0);
            totalEntriesDeleted += deleteProgressThisRun.get();
            deleteProgressThisRun.set(0);
        }
        store.endBatchModifications();
    }
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) EventThrottler(voldemort.utils.EventThrottler) VoldemortException(voldemort.VoldemortException) VoldemortException(voldemort.VoldemortException) StoreDefinition(voldemort.store.StoreDefinition) Pair(voldemort.utils.Pair)

Example 80 with VectorClock

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

the class ConfigurationStorageEngine method put.

@Override
public synchronized void put(String key, Versioned<String> value, String transforms) throws VoldemortException {
    StoreUtils.assertValidKey(key);
    if (null == value.getValue()) {
        throw new VoldemortException("metadata cannot be null !!");
    }
    // Check for obsolete version
    File[] files = getDirectory(key).listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().equals(key)) {
                VectorClock clock = readVersion(key);
                if (value.getVersion().compare(clock) == Occurred.AFTER) {
                // continue
                } else if (value.getVersion().compare(clock) == Occurred.BEFORE) {
                    throw new ObsoleteVersionException("A successor version " + clock + "  to this " + value.getVersion() + " exists for key " + key);
                } else if (value.getVersion().compare(clock) == Occurred.CONCURRENTLY) {
                    throw new ObsoleteVersionException("Concurrent Operation not allowed on Metadata.");
                }
            }
        }
    }
    File keyFile = new File(getDirectory(key), key);
    VectorClock newClock = (VectorClock) value.getVersion();
    if (!keyFile.exists() || keyFile.delete()) {
        try {
            FileUtils.writeStringToFile(keyFile, value.getValue(), "UTF-8");
            writeVersion(key, newClock);
        } catch (IOException e) {
            throw new VoldemortException(e);
        }
    }
}
Also used : ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) VectorClock(voldemort.versioning.VectorClock) IOException(java.io.IOException) VoldemortException(voldemort.VoldemortException) File(java.io.File)

Aggregations

VectorClock (voldemort.versioning.VectorClock)162 Versioned (voldemort.versioning.Versioned)73 Test (org.junit.Test)65 ByteArray (voldemort.utils.ByteArray)65 ArrayList (java.util.ArrayList)33 IOException (java.io.IOException)25 VoldemortException (voldemort.VoldemortException)24 List (java.util.List)22 HashMap (java.util.HashMap)21 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)21 Version (voldemort.versioning.Version)16 Node (voldemort.cluster.Node)15 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)11 StoreDefinition (voldemort.store.StoreDefinition)11 AdminClient (voldemort.client.protocol.admin.AdminClient)10 VoldemortServer (voldemort.server.VoldemortServer)10 Pair (voldemort.utils.Pair)10 ClockEntry (voldemort.versioning.ClockEntry)10 File (java.io.File)8 Map (java.util.Map)8