Search in sources :

Example 1 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 2 with CreateBuilder

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

the class ZKStoreHelper method createZNodeIfNotExist.

CompletableFuture<Void> createZNodeIfNotExist(final String path, final boolean createParent) {
    final CompletableFuture<Void> result = new CompletableFuture<>();
    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(null), e -> {
            if (e instanceof StoreException.DataExistsException) {
                result.complete(null);
            } else {
                result.completeExceptionally(e);
            }
        }, path);
        if (createParent) {
            createBuilder.creatingParentsIfNeeded().inBackground(callback, executor).forPath(path);
        } else {
            createBuilder.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)

Example 3 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.

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) KeeperException(org.apache.zookeeper.KeeperException)

Example 4 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project fabric8 by jboss-fuse.

the class CreateAction method doExecute.

@Override
protected void doExecute(CuratorFramework curator) throws Exception {
    List<ACL> acls = acl == null ? ZooDefs.Ids.OPEN_ACL_UNSAFE : parseACLs(acl);
    CreateMode mode;
    if (ephemeral && sequential) {
        mode = CreateMode.EPHEMERAL_SEQUENTIAL;
    } else if (ephemeral) {
        mode = CreateMode.EPHEMERAL;
    } else if (sequential) {
        mode = CreateMode.PERSISTENT_SEQUENTIAL;
    } else {
        mode = CreateMode.PERSISTENT;
    }
    String nodeData = data;
    if (importUrl) {
        nodeData = loadUrl(new URL(data));
    }
    try {
        CreateBuilder createBuilder = curator.create();
        if (recursive) {
            createBuilder.creatingParentsIfNeeded();
        }
        BackgroundPathAndBytesable<String> create = createBuilder.withMode(mode).withACL(acls);
        if (nodeData == null) {
            create.forPath(path);
        } else {
            create.forPath(path, nodeData.getBytes());
        }
    } catch (KeeperException.NodeExistsException e) {
        if (overwrite) {
            curator.setData().forPath(path, nodeData.getBytes());
        } else {
            throw e;
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACL(org.apache.zookeeper.data.ACL) URL(java.net.URL) KeeperException(org.apache.zookeeper.KeeperException)

Example 5 with CreateBuilder

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.CreateBuilder in project 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)

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