Search in sources :

Example 6 with CreateBuilder

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

Example 7 with CreateBuilder

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

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 8 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.

private CompletableFuture<Integer> createZNodeIfNotExist(final String path, final boolean createParent) {
    final CompletableFuture<Integer> result = new CompletableFuture<>();
    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(x.getStat().getVersion()), 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) StoreException(io.pravega.controller.store.stream.StoreException)

Example 9 with CreateBuilder

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

the class ZKStoreHelper method createPersistentSequentialZNode.

public CompletableFuture<String> createPersistentSequentialZNode(final String path, final byte[] data) {
    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.PERSISTENT_SEQUENTIAL).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 10 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.

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

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