Search in sources :

Example 21 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project pravega by pravega.

the class ZKStoreHelper method createEphemeralSequentialZNode.

CompletableFuture<String> createEphemeralSequentialZNode(final String path) {
    final CompletableFuture<String> result = new CompletableFuture<>();
    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(x.getName()), result::completeExceptionally, path);
        createBuilder.creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(callback, executor).forPath(path);
    } catch (Exception e) {
        result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e, path));
    }
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) KeeperException(org.apache.zookeeper.KeeperException) StoreException(io.pravega.controller.store.stream.StoreException)

Example 22 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project heron by twitter.

the class CuratorStateManagerTest method testCreateNode.

/**
 * test createNode method
 * @throws Exception
 */
@Test
public void testCreateNode() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    CuratorFramework mockClient = mock(CuratorFramework.class);
    CreateBuilder mockCreateBuilder = mock(CreateBuilder.class);
    // Mockito doesn't support mock type-parametrized class, thus suppress the warning
    @SuppressWarnings("rawtypes") ACLBackgroundPathAndBytesable mockPath = spy(ACLBackgroundPathAndBytesable.class);
    final byte[] data = new byte[10];
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    doReturn(mockCreateBuilder).when(mockClient).create();
    doReturn(mockPath).when(mockCreateBuilder).withMode(any(CreateMode.class));
    spyStateManager.initialize(config);
    // Verify the node is created successfully
    ListenableFuture<Boolean> result = spyStateManager.createNode(PATH, data, false);
    verify(mockCreateBuilder).withMode(any(CreateMode.class));
    verify(mockPath).forPath(PATH, data);
    assertTrue(result.get());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACLBackgroundPathAndBytesable(org.apache.curator.framework.api.ACLBackgroundPathAndBytesable) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Example 23 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project jstorm by alibaba.

the class ZkState method writeBytes.

public void writeBytes(String path, byte[] bytes) {
    try {
        if (_curator.checkExists().forPath(path) == null) {
            CreateBuilder builder = _curator.create();
            ProtectACLCreateModePathAndBytesable<String> createAble = (ProtectACLCreateModePathAndBytesable<String>) builder.creatingParentsIfNeeded();
            createAble.withMode(CreateMode.PERSISTENT).forPath(path, bytes);
        } else {
            _curator.setData().forPath(path, bytes);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ProtectACLCreateModePathAndBytesable(org.apache.curator.framework.api.ProtectACLCreateModePathAndBytesable) CreateBuilder(org.apache.curator.framework.api.CreateBuilder)

Example 24 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project incubator-atlas by apache.

the class SetupStepsTest method setupSetupInProgressPathMocks.

private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls, Stat stat) throws Exception {
    when(curatorFactory.clientInstance()).thenReturn(client);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(createBuilder.withACL(acls)).thenReturn(createBuilder);
    when(client.create()).thenReturn(createBuilder);
    DeleteBuilder deleteBuilder = mock(DeleteBuilder.class);
    when(client.delete()).thenReturn(deleteBuilder);
    Pair<CreateBuilder, DeleteBuilder> pair = Pair.of(createBuilder, deleteBuilder);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(client.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT + SetupSteps.SETUP_IN_PROGRESS_NODE)).thenReturn(stat);
    return pair;
}
Also used : CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) DeleteBuilder(org.apache.curator.framework.api.DeleteBuilder)

Example 25 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project flink by apache.

the class ZooKeeperLeaderElectionTest method testExceptionForwarding.

/**
 * Test that errors in the {@link LeaderElectionDriver} are correctly forwarded to the {@link
 * LeaderContender}.
 */
@Test
public void testExceptionForwarding() throws Exception {
    LeaderElectionDriver leaderElectionDriver = null;
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    CuratorFramework client = null;
    final CreateBuilder mockCreateBuilder = mock(CreateBuilder.class, Mockito.RETURNS_DEEP_STUBS);
    final String exMsg = "Test exception";
    final Exception testException = new Exception(exMsg);
    final CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, NoOpFatalErrorHandler.INSTANCE);
    try {
        client = spy(curatorFrameworkWrapper.asCuratorFramework());
        doAnswer(invocation -> mockCreateBuilder).when(client).create();
        when(mockCreateBuilder.creatingParentsIfNeeded().withMode(Matchers.any(CreateMode.class)).forPath(anyString(), any(byte[].class))).thenThrow(testException);
        leaderElectionDriver = createAndInitLeaderElectionDriver(client, electionEventHandler);
        electionEventHandler.waitForError(timeout);
        assertNotNull(electionEventHandler.getError());
        assertThat(ExceptionUtils.findThrowableWithMessage(electionEventHandler.getError(), exMsg).isPresent(), is(true));
    } finally {
        electionEventHandler.close();
        if (leaderElectionDriver != null) {
            leaderElectionDriver.close();
        }
        if (curatorFrameworkWrapper != null) {
            curatorFrameworkWrapper.close();
        }
    }
}
Also used : CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) CreateBuilder(org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) Mockito.anyString(org.mockito.Mockito.anyString) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

CreateBuilder (org.apache.curator.framework.api.CreateBuilder)24 KeeperException (org.apache.zookeeper.KeeperException)12 CompletableFuture (java.util.concurrent.CompletableFuture)9 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)9 StoreException (io.pravega.controller.store.stream.StoreException)6 ExistsBuilder (org.apache.curator.framework.api.ExistsBuilder)6 Test (org.testng.annotations.Test)6 ACL (org.apache.zookeeper.data.ACL)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 SetDataBuilder (org.apache.curator.framework.api.SetDataBuilder)4 CreateMode (org.apache.zookeeper.CreateMode)4 Id (org.apache.zookeeper.data.Id)4 Test (org.junit.Test)4 BeforeTest (org.testng.annotations.BeforeTest)4 TimeUnit (java.util.concurrent.TimeUnit)3 ACLBackgroundPathAndBytesable (org.apache.curator.framework.api.ACLBackgroundPathAndBytesable)3 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)3 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 SetupStep (org.apache.atlas.setup.SetupStep)2