Search in sources :

Example 6 with TestingLongStateHandleHelper

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");
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Test(org.junit.Test)

Example 7 with TestingLongStateHandleHelper

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());
}
Also used : CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Test(org.junit.Test)

Example 8 with TestingLongStateHandleHelper

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);
}
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 9 with TestingLongStateHandleHelper

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);
}
Also used : TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper)

Example 10 with TestingLongStateHandleHelper

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());
    }
}
Also used : ZooKeeperUtils(org.apache.flink.runtime.util.ZooKeeperUtils) Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) Assert.assertThrows(org.junit.Assert.assertThrows) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) Mockito.spy(org.mockito.Mockito.spy) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) InstantiationUtil(org.apache.flink.util.InstantiationUtil) TestLogger(org.apache.flink.util.TestLogger) StateHandleStore(org.apache.flink.runtime.persistence.StateHandleStore) Assert.fail(org.junit.Assert.fail) IsInstanceOf(org.hamcrest.core.IsInstanceOf) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Matchers.empty(org.hamcrest.Matchers.empty) RetrievableStateHandle(org.apache.flink.runtime.state.RetrievableStateHandle) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Configuration(org.apache.flink.configuration.Configuration) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Stat(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) RetrievableStateStorageHelper(org.apache.flink.runtime.persistence.RetrievableStateStorageHelper) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Matchers.is(org.hamcrest.Matchers.is) NoOpFatalErrorHandler(org.apache.flink.runtime.rest.util.NoOpFatalErrorHandler) Comparator(java.util.Comparator) Collections(java.util.Collections) HighAvailabilityOptions(org.apache.flink.configuration.HighAvailabilityOptions) Assert.assertEquals(org.junit.Assert.assertEquals) IntegerResourceVersion(org.apache.flink.runtime.persistence.IntegerResourceVersion) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Tuple2(org.apache.flink.api.java.tuple.Tuple2) 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