use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.
the class ZooKeeperStateHandleStoreTest method testGetNonExistingPath.
/**
* Tests that a non existing path throws an Exception.
*/
@Test(expected = Exception.class)
public void testGetNonExistingPath() throws Exception {
final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
store.getAndLock("/testGetNonExistingPath");
}
use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.
the class ZooKeeperStateHandleStoreTest method testAddAndLockRetrySuccessfulTransaction.
/**
* Transactions are not idempotent in the Curator version we're currently using, therefore we
* may end up retrying the transaction that has already (eg. in case of connection failure).
* Retry of a successful transaction would result in {@link KeeperException.NodeExistsException}
* in this case.
*
* @see <a href="https://issues.apache.org/jira/browse/CURATOR-584">CURATOR-584</a>
*/
@Test
public void testAddAndLockRetrySuccessfulTransaction() throws Exception {
final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
final CuratorFramework client = ZOOKEEPER.getClient();
final ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle>(client, stateHandleProvider) {
@Override
protected void writeStoreHandleTransactionally(String path, byte[] serializedStoreHandle) throws Exception {
super.writeStoreHandleTransactionally(path, serializedStoreHandle);
throw new KeeperException.NodeExistsException("Committed transaction has been retried.");
}
};
final String path = "/test";
final long firstState = 1337L;
store.addAndLock(path, new TestingLongStateHandleHelper.LongStateHandle(firstState));
// There should be only single state handle from the first successful attempt.
assertEquals(1, TestingLongStateHandleHelper.getGlobalStorageSize());
assertEquals(firstState, TestingLongStateHandleHelper.getStateHandleValueByIndex(0));
// No state should have been discarded.
assertEquals(0, TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0));
// Get state handle from zookeeper.
assertEquals(firstState, store.getAndLock(path).retrieveState().getValue());
}
use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.
the class ZooKeeperStateHandleStoreTest method testConcurrentDeleteOperation.
/**
* FLINK-6612
*
* <p>Tests that a concurrent delete operation cannot succeed if another instance holds a lock
* on the specified node.
*/
@Test
public void testConcurrentDeleteOperation() throws Exception {
final TestingLongStateHandleHelper longStateStorage = new TestingLongStateHandleHelper();
ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> zkStore1 = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), longStateStorage);
ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> zkStore2 = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), longStateStorage);
final String statePath = "/state";
zkStore1.addAndLock(statePath, new TestingLongStateHandleHelper.LongStateHandle(42L));
RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle> stateHandle = zkStore2.getAndLock(statePath);
// this should not remove the referenced node because we are still holding a state handle
// reference via zkStore2
zkStore1.releaseAndTryRemove(statePath);
// sanity check
assertEquals(42L, stateHandle.retrieveState().getValue());
Stat nodeStat = ZOOKEEPER.getClient().checkExists().forPath(statePath);
assertNotNull("NodeStat should not be null, otherwise the referenced node does not exist.", nodeStat);
zkStore2.releaseAndTryRemove(statePath);
nodeStat = ZOOKEEPER.getClient().checkExists().forPath(statePath);
assertNull("NodeState should be null, because the referenced node should no longer exist.", nodeStat);
}
use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.
the class ZooKeeperStateHandleStoreTest method testDiscardAfterReplaceFailureWith.
private static void testDiscardAfterReplaceFailureWith(Exception actualException, Class<? extends Throwable> expectedException) throws Exception {
final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle>(ZOOKEEPER.getClient(), stateHandleProvider) {
@Override
protected void setStateHandle(String path, byte[] serializedStateHandle, int expectedVersion) throws Exception {
throw actualException;
}
};
// Config
final String pathInZooKeeper = "/testReplaceDiscardStateHandleAfterFailure-" + expectedException.getSimpleName();
final long initialState = 30968470898L;
final long replaceState = 88383776661L;
// Test
store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(initialState));
try {
store.replace(pathInZooKeeper, IntegerResourceVersion.valueOf(0), new TestingLongStateHandleHelper.LongStateHandle(replaceState));
fail("Did not throw expected exception");
} catch (Throwable t) {
assertThat(t, IsInstanceOf.instanceOf(expectedException));
}
// State handle created and discarded
assertEquals(2, TestingLongStateHandleHelper.getGlobalStorageSize());
assertEquals(initialState, TestingLongStateHandleHelper.getStateHandleValueByIndex(0));
assertEquals(replaceState, TestingLongStateHandleHelper.getStateHandleValueByIndex(1));
assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0), is(0));
assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(1), is(1));
// Initial value
@SuppressWarnings("unchecked") final long actual = ((RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle>) InstantiationUtil.deserializeObject(ZOOKEEPER.getClient().getData().forPath(pathInZooKeeper), ClassLoader.getSystemClassLoader())).retrieveState().getValue();
assertEquals(initialState, actual);
}
use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.
the class ZooKeeperStateHandleStoreTest method testGetAllSortedByName.
/**
* Tests that the state is returned sorted.
*/
@Test
public void testGetAllSortedByName() throws Exception {
// Setup
final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
// Config
final String basePath = "/testGetAllSortedByName";
final Long[] expected = new Long[] { 311222268470898L, 132812888L, 27255442L, 11122233124L };
// Test
for (long val : expected) {
final String pathInZooKeeper = String.format("%s%016d", basePath, val);
store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(val));
}
List<Tuple2<RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle>, String>> actual = store.getAllAndLock();
assertEquals(expected.length, actual.size());
// bring the elements in sort order
Arrays.sort(expected);
Collections.sort(actual, Comparator.comparing(o -> o.f1));
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], (Long) actual.get(i).f0.retrieveState().getValue());
}
}
Aggregations