/** * Get new vector clock based on this clock but incremented on index nodeId * * @param nodeId The id of the node to increment * @return A vector clock equal on each element execept that indexed by * nodeId */ public VectorClock incremented(int nodeId, long time) { VectorClock copyClock = this.clone(); copyClock.incrementVersion(nodeId, time); return copyClock; }
/** * Create a clone of this Versioned object such that the object pointed to * is the same, but the VectorClock and Versioned wrapper is a shallow copy. */ public Versioned<T> cloneVersioned() { return new Versioned<T>(this.getValue(), this.version.clone()); }
Version obsoleteVersion = ((VectorClock) versionedValue.getVersion()).clone();
((VectorClock) v.getVersion()).clone()); toReadRepair.add(new NodeValue<ByteArray, byte[]>(v.getNodeId(), v.getKey(), versioned));
((VectorClock) v.getVersion()).clone()); toReadRepair.add(new NodeValue<ByteArray, byte[]>(v.getNodeId(), v.getKey(),
((VectorClock) v.getVersion()).clone()); toReadRepair.add(new NodeValue<ByteArray, byte[]>(v.getNodeId(), v.getKey(),
private void testObsoletePutFails(String message, Store<K, V, T> store, K key, Versioned<V> versioned) { VectorClock clock = (VectorClock) versioned.getVersion(); clock = clock.clone(); try { store.put(key, versioned, null); fail(message); } catch(ObsoleteVersionException e) { // this is good, but check that we didn't fuck with the version assertEquals(clock, versioned.getVersion()); } }
@Test public void testPutIncrementsVersion() throws Exception { Store<ByteArray, byte[], byte[]> store = getStore(); VectorClock clock = new VectorClock(); VectorClock copy = clock.clone(); store.put(aKey, new Versioned<byte[]>(getValue(), clock), aTransform); List<Versioned<byte[]>> found = store.get(aKey, aTransform); assertEquals("Invalid number of items found.", 1, found.size()); assertEquals("Version not incremented properly", Occurred.BEFORE, copy.compare(found.get(0).getVersion())); }
@Test public void testPutIncrementsVersionZZZ() throws Exception { Store<ByteArray, byte[], byte[]> store = getZonedStore(); VectorClock clock = new VectorClock(); VectorClock copy = clock.clone(); store.put(aKey, new Versioned<byte[]>(getValue(), clock), aTransform); List<Versioned<byte[]>> found = store.get(aKey, aTransform); assertEquals("Invalid number of items found.", 1, found.size()); assertEquals("Version not incremented properly", Occurred.BEFORE, copy.compare(found.get(0).getVersion())); }
@Override @Test public void testPutIfNotObsolete() { VectorClock vc = new VectorClock(); vc.incrementVersion(this.nodeId, System.currentTimeMillis()); VectorClock initialVC = vc.clone(); client.putIfNotObsolete("k", new Versioned<String>("v", vc)); assertEquals("PUT of non-obsolete version should succeed.", "v", client.getValue("k")); assertFalse(client.putIfNotObsolete("k", new Versioned<String>("v2", initialVC))); assertEquals("Failed PUT should not change the value stored.", "v", client.getValue("k")); }
/** * Test the new put that returns the new version */ @Test public void testPutReturnVersion() { Version baseVersion = new VectorClock(); Version oldVersion = null; Version newVersion = null; Versioned<String> getVersioned = null; String oldValue = null; String newValue = null; for(int i = 0; i < 5; i++) { oldValue = "value" + i; newValue = "value" + (i + 1); oldVersion = storeClient.put("key1", Versioned.value(oldValue, baseVersion)); newVersion = storeClient.put("key1", Versioned.value(newValue, ((VectorClock) oldVersion).clone())); getVersioned = storeClient.get("key1"); baseVersion = newVersion; verifyResults(oldVersion, newVersion, getVersioned, newValue); } }
Version newVersion = ((VectorClock) mainVersion).clone(); store.put(aKey, new Versioned<byte[]>(anotherValue, newVersion), null);
@Override @Test public void testDeleteVersion() { VectorClock vc = new VectorClock(); vc.incrementVersion(this.nodeId, System.currentTimeMillis()); VectorClock initialVC = vc.clone(); assertFalse("Delete of non-existant key should be false.", client.delete("k", vc)); client.put("k", new Versioned<String>("v", vc)); assertFalse("Delete of a lesser version should be false.", client.delete("k", initialVC)); assertNotNull("After failed delete, value should still be there.", client.get("k")); assertTrue("Delete of k, with the current version should succeed.", client.delete("k", initialVC.incremented(this.nodeId, time.getMilliseconds()))); assertNull("After a successful delete(k), get(k) should return null.", client.get("k")); } }
@Override @Test public void testPutVersioned() { VectorClock vc = new VectorClock(); vc.incrementVersion(this.nodeId, System.currentTimeMillis()); VectorClock initialVC = vc.clone(); client.put("k", new Versioned<String>("v", vc)); Versioned<String> v = client.get("k"); assertEquals("GET should return the version set by PUT.", "v", v.getValue()); VectorClock expected = initialVC.clone(); expected.incrementVersion(this.nodeId, System.currentTimeMillis()); assertEquals("The version should be incremented after a put.", expected.getEntries(), ((VectorClock) v.getVersion()).getEntries()); try { client.put("k", new Versioned<String>("v", initialVC)); 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()); }
Store<K, V, T> store = getStore(); VectorClock clock = getClock(1, 1); VectorClock clockCopy = clock.clone(); V value = getValue(); assertEquals("Store not empty at start!", 0, store.get(key, null).size());