Search in sources :

Example 11 with CreateBuilder

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

the class ZKStoreHelper method createEphemeralZNode.

public CompletableFuture<Boolean> createEphemeralZNode(final String path, byte[] data) {
    final CompletableFuture<Boolean> result = new CompletableFuture<>();
    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(true), e -> {
            if (e instanceof StoreException.DataExistsException) {
                result.complete(false);
            } else {
                result.completeExceptionally(e);
            }
        }, path);
        createBuilder.creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).inBackground(callback, executor).forPath(path, data);
    } 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KeeperException(org.apache.zookeeper.KeeperException) StoreException(io.pravega.controller.store.stream.StoreException)

Example 12 with CreateBuilder

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

the class ZKStoreHelper method createZNode.

public CompletableFuture<Integer> createZNode(final String path, final byte[] data) {
    final CompletableFuture<Integer> result = new CompletableFuture<>();
    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(x.getStat().getVersion()), e -> result.completeExceptionally(e), path);
        createBuilder.creatingParentsIfNeeded().inBackground(callback, executor).forPath(path, data);
    } 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 13 with CreateBuilder

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

the class CuratorZookeeperClient method createNode0.

private void createNode0(CreateNodeMessage message, boolean orSet) throws PinpointZookeeperException {
    checkState();
    try {
        CuratorFramework curator = connectionManager.getCuratorFramework();
        CreateBuilder createBuilder = curator.create();
        if (message.isCreatingParentPathsIfNeeded()) {
            createBuilder.creatingParentsIfNeeded();
        }
        if (orSet) {
            createBuilder.orSetData();
        }
        String nodePath = message.getNodePath();
        byte[] data = message.getData();
        createBuilder.withMode(CreateMode.EPHEMERAL).forPath(nodePath, data);
    } catch (Exception e) {
        ZookeeperExceptionResolver.resolveAndThrow(e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) PinpointZookeeperException(com.navercorp.pinpoint.common.server.cluster.zookeeper.exception.PinpointZookeeperException) KeeperException(org.apache.zookeeper.KeeperException) ConnectionException(com.navercorp.pinpoint.common.server.cluster.zookeeper.exception.ConnectionException)

Example 14 with CreateBuilder

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

the class ActiveInstanceStateTest method testSharedPathIsCreatedIfNotExists.

@Test
public void testSharedPathIsCreatedIfNotExists() throws Exception {
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX + "id1")).thenReturn(HOST_PORT);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    when(createBuilder.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)).thenReturn(createBuilder);
    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);
    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");
    verify(createBuilder).forPath(getPath());
}
Also used : SetDataBuilder(org.apache.curator.framework.api.SetDataBuilder) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 15 with CreateBuilder

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

the class ActiveInstanceStateTest method testSharedPathIsCreatedWithRightACLIfNotExists.

@Test
public void testSharedPathIsCreatedWithRightACLIfNotExists() throws Exception {
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX + "id1")).thenReturn(HOST_PORT);
    when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("sasl:myclient@EXAMPLE.COM");
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    ACL expectedAcl = new ACL(ZooDefs.Perms.ALL, new Id("sasl", "myclient@EXAMPLE.COM"));
    when(createBuilder.withACL(Arrays.asList(new ACL[] { expectedAcl }))).thenReturn(createBuilder);
    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);
    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");
    verify(createBuilder).forPath(getPath());
}
Also used : SetDataBuilder(org.apache.curator.framework.api.SetDataBuilder) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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