code: 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 {
  			// Serialize the new state handle. This writes the state to the backend.
  			byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle);
  
  			// Replace state handle in ZooKeeper.
  			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();
  			}
  		}
  
  	}
docs: Replaces a state handle in ZooKeeper and discards the old state handle.
  
  @param pathInZooKeeper Destination path in ZooKeeper (expected to exist and start with a '/')
  @param expectedVersion Expected version of the node to replace
  @param state           The new state to replace the old one
  @throws Exception If a ZooKeeper or state handle operation fails