private VectorClock getVectorClock(byte[] bytes) { if(bytes[0] >= 0) return new VectorClock(bytes); return null; }
public Versioned(T object, Version version) { this.version = version == null ? new VectorClock() : (VectorClock) version; this.object = object; }
@Override public VectorClock clone() { return new VectorClock(Maps.newTreeMap(versionMap), this.timestamp); }
private Version getVersionForPut(K key) { Version version = getVersionWithResolution(key); if(version == null) { version = new VectorClock(); } return version; }
public static VectorClock deserializeVectorClock(String serializedVC) { VectorClock vc = null; if(serializedVC == null) { return null; } try { VectorClockWrapper vcWrapper = mapper.readValue(serializedVC, VectorClockWrapper.class); vc = new VectorClock(vcWrapper.getVersions(), vcWrapper.getTimestamp()); } catch(Exception e) { e.printStackTrace(); } return vc; }
@Override public boolean parseRequest(DataInputStream inputStream) throws IOException { key = ClientRequestHandler.readKey(inputStream); int versionSize = inputStream.readShort(); byte[] versionBytes = new byte[versionSize]; ByteUtils.read(inputStream, versionBytes); version = new VectorClock(versionBytes); return false; }
public static VectorClock getClockWithTs(long ts, int... nodes) { VectorClock clock = new VectorClock(ts); increment(clock, nodes); return clock; }
public VectorClock getNewIncrementedVectorClock() { VectorClock vectorClock = new VectorClock(); vectorClock.incrementVersion(this.nodeId, System.currentTimeMillis()); return vectorClock; }
public void run() { while(keepRunning.get()) { byte[] bytes = Integer.toString(rand.nextInt(count.get())).getBytes(); store.delete(new ByteArray(bytes), new VectorClock()); count.incrementAndGet(); } } });
public static VectorClock decodeClock(VProto.VectorClock encoded) { List<ClockEntry> entries = new ArrayList<ClockEntry>(encoded.getEntriesCount()); for(VProto.ClockEntry entry: encoded.getEntriesList()) entries.add(new ClockEntry((short) entry.getNodeId(), entry.getVersion())); return new VectorClock(entries, encoded.getTimestamp()); }
private VectorClock makeSuperClock(long time) { List<ClockEntry> clockEntries = new ArrayList<ClockEntry>(); clockEntries.add(new ClockEntry((short) 0, time)); clockEntries.add(new ClockEntry((short) 1, time)); clockEntries.add(new ClockEntry((short) 2, time)); return new VectorClock(clockEntries, time); }
private void putWithTimeStamp(int start, int end, long milliseconds) { for (int i = start; i < end; i++) { ByteArray b = new ByteArray(Integer.toString(i).getBytes()); VectorClock clock = new VectorClock(milliseconds); Versioned<byte[]> value = new Versioned<byte[]>(b.get(), clock); engine.put(b, value, null); } }
private void updateStoreDef(int retentionDays) { StoreDefinition storeDef = getStoreDef(retentionDays); StoreDefinitionsMapper mapper = new StoreDefinitionsMapper(); String storeStr = mapper.writeStoreList(Arrays.asList(storeDef)); VectorClock clock = new VectorClock(System.currentTimeMillis()); clock.incrementVersion(0, System.currentTimeMillis()); Versioned<byte[]> storeSerialized = new Versioned<byte[]>(ByteUtils.getBytes(storeStr, "UTF-8"), clock); metadataStore.updateStoreDefinitions(storeSerialized); }
@Test public void testSerializationWraps() { VectorClock clock = getClock(1, 1, 2, 3, 3, 6); for(int i = 0; i < 300; i++) clock.incrementVersion(2, System.currentTimeMillis()); assertEquals("Clock does not serialize to itself.", clock, new VectorClock(clock.toBytes())); }
@Test public void testSerialization() { assertEquals("The empty clock serializes incorrectly.", getClock(), new VectorClock(getClock().toBytes())); VectorClock clock = getClock(1, 1, 2, 3, 4, 4, 6); assertEquals("This clock does not serialize to itself.", clock, new VectorClock(clock.toBytes())); }
@Test public void testDeleteVersion() { assertFalse("Delete of non-existant key should be false.", client.delete("k", new VectorClock())); client.put("k", new Versioned<String>("v")); assertFalse("Delete of a lesser version should be false.", client.delete("k", new VectorClock())); 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", new VectorClock().incremented(nodeId, time.getMilliseconds()))); assertNull("After a successful delete(k), get(k) should return null.", client.get("k")); }
@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())); }
@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())); }
@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 public void testIncrementAndSerialize() { int node = 1; VectorClock vc = getClock(node); assertEquals(node, vc.getMaxVersion()); int increments = 3000; for(int i = 0; i < increments; i++) { vc.incrementVersion(node, 45); // serialize vc = new VectorClock(vc.toBytes()); } assertEquals(increments + 1, vc.getMaxVersion()); }