@Test(expected = Exception.class) public void testReplaceNonExistingPath() throws Exception { RetrievableStateStorageHelper<Long> stateStorage = new LongStateStorage(); ZooKeeperStateHandleStore<Long> store = new ZooKeeperStateHandleStore<>( ZOOKEEPER.getClient(), stateStorage); store.replace("/testReplaceNonExistingPath", 0, 1L); }
public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception { checkNotNull(pathInZooKeeper, "Path in ZooKeeper"); checkNotNull(state, "State"); final String path = normalizePath(pathInZooKeeper); RetrievableStateHandle<T> oldStateHandle = get(path, false); RetrievableStateHandle<T> newStateHandle = storage.store(state); boolean success = false; try { byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle); client.setData() .withVersion(expectedVersion) .forPath(path, serializedStateHandle); success = true; } catch (KeeperException.NoNodeException e) { throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e); } finally { if (success) { oldStateHandle.discardState(); } else { newStateHandle.discardState(); } } }
ZooKeeperStateHandleStore { public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception { checkNotNull(pathInZooKeeper, "Path in ZooKeeper"); checkNotNull(state, "State"); final String path = normalizePath(pathInZooKeeper); RetrievableStateHandle<T> oldStateHandle = get(path, false); RetrievableStateHandle<T> newStateHandle = storage.store(state); boolean success = false; try { byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle); client.setData() .withVersion(expectedVersion) .forPath(path, serializedStateHandle); success = true; } catch (KeeperException.NoNodeException e) { throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e); } finally { if (success) { oldStateHandle.discardState(); } else { newStateHandle.discardState(); } } } }
ZooKeeperStateHandleStore { public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception { checkNotNull(pathInZooKeeper, "Path in ZooKeeper"); checkNotNull(state, "State"); final String path = normalizePath(pathInZooKeeper); RetrievableStateHandle<T> oldStateHandle = get(path, false); RetrievableStateHandle<T> newStateHandle = storage.store(state); boolean success = false; try { byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle); client.setData() .withVersion(expectedVersion) .forPath(path, serializedStateHandle); success = true; } catch (KeeperException.NoNodeException e) { throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e); } finally { if (success) { oldStateHandle.discardState(); } else { newStateHandle.discardState(); } } } ZooKeeperStateHandleStore(
		CuratorFramework client,
		RetrievableStateStorageHelper<T> storage); }
ZooKeeperStateHandleStore { public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception { checkNotNull(pathInZooKeeper, "Path in ZooKeeper"); checkNotNull(state, "State"); final String path = normalizePath(pathInZooKeeper); RetrievableStateHandle<T> oldStateHandle = get(path, false); RetrievableStateHandle<T> newStateHandle = storage.store(state); boolean success = false; try { byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle); client.setData() .withVersion(expectedVersion) .forPath(path, serializedStateHandle); success = true; } catch (KeeperException.NoNodeException e) { throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e); } finally { if (success) { oldStateHandle.discardState(); } else { newStateHandle.discardState(); } } } ZooKeeperStateHandleStore(
		CuratorFramework client,
		RetrievableStateStorageHelper<T> storage); RetrievableStateHandle<T> addAndLock(
			String pathInZooKeeper,
			T state); void replace(String pathInZooKeeper, int expectedVersion, T state); int exists(String pathInZooKeeper); RetrievableStateHandle<T> getAndLock(String pathInZooKeeper); Collection<String> getAllPaths(); @SuppressWarnings("unchecked") List<Tuple2<RetrievableStateHandle<T>, String>> getAllAndLock(); @Nullable boolean releaseAndTryRemove(String pathInZooKeeper); void releaseAndTryRemoveAll(); void release(String pathInZooKeeper); void releaseAll(); void deleteChildren(); }
ZooKeeperStateHandleStore { public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception { checkNotNull(pathInZooKeeper, "Path in ZooKeeper"); checkNotNull(state, "State"); final String path = normalizePath(pathInZooKeeper); RetrievableStateHandle<T> oldStateHandle = get(path, false); RetrievableStateHandle<T> newStateHandle = storage.store(state); boolean success = false; try { byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle); client.setData() .withVersion(expectedVersion) .forPath(path, serializedStateHandle); success = true; } catch (KeeperException.NoNodeException e) { throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e); } finally { if (success) { oldStateHandle.discardState(); } else { newStateHandle.discardState(); } } } ZooKeeperStateHandleStore(
		CuratorFramework client,
		RetrievableStateStorageHelper<T> storage); RetrievableStateHandle<T> addAndLock(
			String pathInZooKeeper,
			T state); void replace(String pathInZooKeeper, int expectedVersion, T state); int exists(String pathInZooKeeper); RetrievableStateHandle<T> getAndLock(String pathInZooKeeper); Collection<String> getAllPaths(); @SuppressWarnings("unchecked") List<Tuple2<RetrievableStateHandle<T>, String>> getAllAndLock(); @Nullable boolean releaseAndTryRemove(String pathInZooKeeper); void releaseAndTryRemoveAll(); void release(String pathInZooKeeper); void releaseAll(); void deleteChildren();  }