Search in sources :

Example 16 with TestingLongStateHandleHelper

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

the class ZooKeeperStateHandleStoreTest method testFailingAddWithPossiblyInconsistentState.

/**
 * Tests that the created state handle is not discarded if ZooKeeper create fails with an
 * generic exception.
 */
@Test
public void testFailingAddWithPossiblyInconsistentState() throws Exception {
    final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
    CuratorFramework client = spy(ZOOKEEPER.getClient());
    when(client.inTransaction()).thenThrow(new RuntimeException("Expected test Exception."));
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(client, stateHandleProvider);
    // Config
    final String pathInZooKeeper = "/testAddDiscardStateHandleAfterFailure";
    final long state = 81282227L;
    try {
        // Test
        store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(state));
        fail("PossibleInconsistentStateException should have been thrown.");
    } catch (PossibleInconsistentStateException ignored) {
    // PossibleInconsistentStateException expected
    }
    // State handle created and not discarded
    assertEquals(1, TestingLongStateHandleHelper.getGlobalStorageSize());
    assertEquals(state, TestingLongStateHandleHelper.getStateHandleValueByIndex(0));
    assertEquals(0, TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0));
}
Also used : CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Test(org.junit.Test)

Example 17 with TestingLongStateHandleHelper

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

the class ZooKeeperStateHandleStoreTest method testGetAll.

/**
 * Tests that all added state is returned.
 */
@Test
public void testGetAll() throws Exception {
    // Setup
    final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
    // Config
    final String pathInZooKeeper = "/testGetAll";
    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));
    }
    for (Tuple2<RetrievableStateHandle<TestingLongStateHandleHelper.LongStateHandle>, String> val : store.getAllAndLock()) {
        assertTrue(expected.remove(val.f0.retrieveState().getValue()));
    }
    assertEquals(0, expected.size());
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) RetrievableStateHandle(org.apache.flink.runtime.state.RetrievableStateHandle) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with TestingLongStateHandleHelper

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

the class ZooKeeperStateHandleStoreTest method testRemove.

/**
 * Tests that state handles are correctly removed.
 */
@Test
public void testRemove() throws Exception {
    // Setup
    final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
    // Config
    final String pathInZooKeeper = "/testRemove";
    store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(27255442L));
    final int numberOfGlobalDiscardCalls = TestingLongStateHandleHelper.getGlobalDiscardCount();
    // Test
    store.releaseAndTryRemove(pathInZooKeeper);
    // Verify discarded
    assertEquals(0, ZOOKEEPER.getClient().getChildren().forPath("/").size());
    assertEquals(numberOfGlobalDiscardCalls + 1, TestingLongStateHandleHelper.getGlobalDiscardCount());
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Test(org.junit.Test)

Example 19 with TestingLongStateHandleHelper

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

the class ZooKeeperStateHandleStoreTest method testReplace.

/**
 * Tests that a state handle is replaced.
 */
@Test
public void testReplace() throws Exception {
    // Setup
    final TestingLongStateHandleHelper stateHandleProvider = new TestingLongStateHandleHelper();
    ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new ZooKeeperStateHandleStore<>(ZOOKEEPER.getClient(), stateHandleProvider);
    // Config
    final String pathInZooKeeper = "/testReplace";
    final long initialState = 30968470898L;
    final long replaceState = 88383776661L;
    // Test
    store.addAndLock(pathInZooKeeper, new TestingLongStateHandleHelper.LongStateHandle(initialState));
    store.replace(pathInZooKeeper, IntegerResourceVersion.valueOf(0), new TestingLongStateHandleHelper.LongStateHandle(replaceState));
    // Verify
    // State handles created
    assertEquals(2, TestingLongStateHandleHelper.getGlobalStorageSize());
    assertEquals(initialState, TestingLongStateHandleHelper.getStateHandleValueByIndex(0));
    assertEquals(replaceState, TestingLongStateHandleHelper.getStateHandleValueByIndex(1));
    // Path created and is persistent
    Stat stat = ZOOKEEPER.getClient().checkExists().forPath(pathInZooKeeper);
    assertNotNull(stat);
    assertEquals(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(replaceState, 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 20 with TestingLongStateHandleHelper

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

the class ZooKeeperStateHandleStoreTest method testLockCleanupWhenClientTimesOut.

/**
 * FLINK-6612
 *
 * <p>Tests that lock nodes will be released if the client dies.
 */
@Test
public void testLockCleanupWhenClientTimesOut() throws Exception {
    final TestingLongStateHandleHelper longStateStorage = new TestingLongStateHandleHelper();
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, ZOOKEEPER.getConnectString());
    configuration.setInteger(HighAvailabilityOptions.ZOOKEEPER_SESSION_TIMEOUT, 100);
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_ROOT, "timeout");
    try (CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, NoOpFatalErrorHandler.INSTANCE);
        CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper2 = ZooKeeperUtils.startCuratorFramework(configuration, NoOpFatalErrorHandler.INSTANCE)) {
        CuratorFramework client = curatorFrameworkWrapper.asCuratorFramework();
        CuratorFramework client2 = curatorFrameworkWrapper2.asCuratorFramework();
        ZooKeeperStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> zkStore = new ZooKeeperStateHandleStore<>(client, longStateStorage);
        final String path = "/state";
        zkStore.addAndLock(path, new TestingLongStateHandleHelper.LongStateHandle(42L));
        // this should delete all ephemeral nodes
        client.close();
        Stat stat = client2.checkExists().forPath(path);
        // check that our state node still exists
        assertNotNull(stat);
        Collection<String> children = client2.getChildren().forPath(path);
        // check that the lock node has been released
        assertEquals(0, children.size());
    }
}
Also used : CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) Stat(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat) Configuration(org.apache.flink.configuration.Configuration) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) 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