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());
}
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));
}
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);
}
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();
}
}
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);
}
}
}
Aggregations