Search in sources :

Example 11 with TestingLongStateHandleHelper

use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.

the class ZooKeeperStateHandleStoreTest method testAddAndLock.

/**
 * Tests add operation with lock.
 */
@Test
public void testAddAndLock() throws Exception {
    final TestingLongStateHandleHelper longStateStorage = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), longStateStorage);
    // Config
    final String pathInZooKeeper = "/testAdd";
    final long state = 1239712317L;
    // Test
    store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(state));
    // Verify
    // State handle created
    assertEquals(1, store.getAllAndLock().size());
    assertEquals(state, store.getAndLock(pathInZooKeeper).retrieveState().getValue());
    // Path created and is persistent
    Stat stat = ZOOKEEPER.getClient().checkExists().forPath(pathInZooKeeper);
    assertNotNull(stat);
    assertEquals(0, stat.getEphemeralOwner());
    List<String> children = ZOOKEEPER.getClient().getChildren().forPath(pathInZooKeeper);
    // there should be one child which is the lock
    assertEquals(1, children.size());
    stat = ZOOKEEPER.getClient().checkExists().forPath(pathInZooKeeper + '/' + children.get(0));
    assertNotNull(stat);
    // check that the child is an ephemeral node
    assertNotEquals(0, stat.getEphemeralOwner());
    // Data is equal
    @SuppressWarnings("unchecked") final long actual = ((RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle>) InstantiationUtil.deserializeObject(ZOOKEEPER.getClient().getData().forPath(pathInZooKeeper), ClassLoader.getSystemClassLoader())).retrieveState().getValue();
    assertEquals(state, actual);
}
Also used : Stat(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Test(org.junit.Test)

Example 12 with TestingLongStateHandleHelper

use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.

the class ZooKeeperStateHandleStoreTest method testGetAndExists.

/**
 * Tests get operation.
 */
@Test
public void testGetAndExists() throws Exception {
    // Setup
    final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
    // Config
    final String pathInZooKeeper = "/testGetAndExists";
    final long state = 311222268470898L;
    // Test
    assertThat(store.exists(pathInZooKeeper).isExisting(), is(false));
    store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(state));
    RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle> actual = store.getAndLock(pathInZooKeeper);
    // Verify
    assertEquals(state, actual.retrieveState().getValue());
    assertTrue(store.exists(pathInZooKeeper).getValue() >= 0);
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Test(org.junit.Test)

Example 13 with TestingLongStateHandleHelper

use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.

the class ZooKeeperStateHandleStoreTest method testReleaseAndTryRemoveAll.

/**
 * Tests that all state handles are correctly discarded.
 */
@Test
public void testReleaseAndTryRemoveAll() throws Exception {
    // Setup
    final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
    // Config
    final String pathInZooKeeper = "/testDiscardAll";
    final Set<Long> expected = new HashSet<>();
    expected.add(311222268470898L);
    expected.add(132812888L);
    expected.add(27255442L);
    expected.add(11122233124L);
    // Test
    for (long val : expected) {
        store.addAndLock(pathInZooKeeper + val, new TestingLongStateHandleHelper.LongStateHandle(val));
    }
    store.releaseAndTryRemoveAll();
    // Verify all discarded
    assertEquals(0, ZOOKEEPER.getClient().getChildren().forPath("/").size());
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with TestingLongStateHandleHelper

use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.

the class ZooKeeperStateHandleStoreTest method testRemoveAllHandlesShouldRemoveAllPaths.

@Test
public void testRemoveAllHandlesShouldRemoveAllPaths() throws Exception {
    final ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> zkStore = new ZooKeeperStateHandleStore<>(ZooKeeperUtils.useNamespaceAndEnsurePath(ZOOKEEPER.getClient(), "/path"), new TestingLongStateHandleHelper());
    zkStore.addAndLock("/state", new TestingLongStateHandleHelper.LongStateHandle(1L));
    zkStore.clearEntries();
    assertThat(zkStore.getAllHandles(), is(empty()));
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Test(org.junit.Test)

Example 15 with TestingLongStateHandleHelper

use of org.apache.flink.runtime.persistence.TestingLongStateHandleHelper in project flink by apache.

the class ZooKeeperStateHandleStoreTest method testCorruptedData.

/**
 * Tests that the ZooKeeperStateHandleStore can handle corrupted data by releasing and trying to
 * remove the respective ZooKeeper ZNodes.
 */
@Test
public void testCorruptedData() throws Exception {
    final TestingLongStateHandleHelper stateStorage = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateStorage);
    final Collection<Long> input = new HashSet<>();
    input.add(1L);
    input.add(2L);
    input.add(3L);
    for (Long aLong : input) {
        store.addAndLock("/" + aLong, new TestingLongStateHandleHelper.LongStateHandle(aLong));
    }
    // corrupt one of the entries
    ZOOKEEPER.getClient().setData().forPath("/" + 2, new byte[2]);
    List<Tuple2<RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle>, String>> allEntries = store.getAllAndLock();
    Collection<Long> expected = new HashSet<>(input);
    expected.remove(2L);
    Collection<Long> actual = new HashSet<>(expected.size());
    for (Tuple2<RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle>, String> entry : allEntries) {
        actual.add(entry.f0.retrieveState().getValue());
    }
    assertEquals(expected, actual);
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Tuple2(org.apache.flink.api.java.tuple.Tuple2) RetrievableStateHandle(org.apache.flink.runtime.state.RetrievableStateHandle) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TestingLongStateHandleHelper (org.apache.flink.runtime.persistence.TestingLongStateHandleHelper)22 Test (org.junit.Test)20 Stat (org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat)8 CuratorFramework (org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework)6 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 PossibleInconsistentStateException (org.apache.flink.runtime.persistence.PossibleInconsistentStateException)4 Configuration (org.apache.flink.configuration.Configuration)3 RetrievableStateHandle (org.apache.flink.runtime.state.RetrievableStateHandle)3 KeeperException (org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)2 CuratorFrameworkWithUnhandledErrorListener (org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)2 StateHandleStore (org.apache.flink.runtime.persistence.StateHandleStore)2 TestLogger (org.apache.flink.util.TestLogger)2 Matchers.is (org.hamcrest.Matchers.is)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1