Search in sources :

Example 6 with CreateBuilder

use of 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 7 with CreateBuilder

use of 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 8 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project hadoop by apache.

the class CuratorService method zkMkPath.

/**
   * Create a directory. It is not an error if it already exists
   * @param path path to create
   * @param mode mode for path
   * @param createParents flag to trigger parent creation
   * @param acls ACL for path
   * @throws IOException any problem
   */
public boolean zkMkPath(String path, CreateMode mode, boolean createParents, List<ACL> acls) throws IOException {
    checkServiceLive();
    path = createFullPath(path);
    if (acls == null || acls.isEmpty()) {
        throw new NoPathPermissionsException(path, "Empty ACL list");
    }
    try {
        RegistrySecurity.AclListInfo aclInfo = new RegistrySecurity.AclListInfo(acls);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating path {} with mode {} and ACL {}", path, mode, aclInfo);
        }
        CreateBuilder createBuilder = curator.create();
        createBuilder.withMode(mode).withACL(acls);
        if (createParents) {
            createBuilder.creatingParentsIfNeeded();
        }
        createBuilder.forPath(path);
    } catch (KeeperException.NodeExistsException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("path already present: {}", path, e);
        }
        return false;
    } catch (Exception e) {
        throw operationFailure(path, "mkdir() ", e, acls);
    }
    return true;
}
Also used : NoPathPermissionsException(org.apache.hadoop.registry.client.exceptions.NoPathPermissionsException) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) KeeperException(org.apache.zookeeper.KeeperException) NoChildrenForEphemeralsException(org.apache.hadoop.registry.client.exceptions.NoChildrenForEphemeralsException) AuthenticationFailedException(org.apache.hadoop.registry.client.exceptions.AuthenticationFailedException) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) KeeperException(org.apache.zookeeper.KeeperException) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException) IOException(java.io.IOException) RegistryIOException(org.apache.hadoop.registry.client.exceptions.RegistryIOException) PathIsNotEmptyDirectoryException(org.apache.hadoop.fs.PathIsNotEmptyDirectoryException) ServiceStateException(org.apache.hadoop.service.ServiceStateException) NoPathPermissionsException(org.apache.hadoop.registry.client.exceptions.NoPathPermissionsException)

Example 9 with CreateBuilder

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

the class ZooKeeperLeaderElectionTest method testExceptionForwarding.

/**
	 *  Test that errors in the {@link LeaderElectionService} are correctly forwarded to the
	 *  {@link LeaderContender}.
	 */
@Test
public void testExceptionForwarding() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    ZooKeeperLeaderElectionService leaderElectionService = null;
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    TestingListener listener = new TestingListener();
    TestingContender testingContender;
    CuratorFramework client;
    final CreateBuilder mockCreateBuilder = mock(CreateBuilder.class);
    final ProtectACLCreateModePathAndBytesable<String> mockCreateParentsIfNeeded = mock(ProtectACLCreateModePathAndBytesable.class);
    final Exception testException = new Exception("Test exception");
    try {
        client = spy(ZooKeeperUtils.startCuratorFramework(configuration));
        Answer<CreateBuilder> answer = new Answer<CreateBuilder>() {

            private int counter = 0;

            @Override
            public CreateBuilder answer(InvocationOnMock invocation) throws Throwable {
                counter++;
                // at first we have to create the leader latch, there it mustn't fail yet
                if (counter < 2) {
                    return (CreateBuilder) invocation.callRealMethod();
                } else {
                    return mockCreateBuilder;
                }
            }
        };
        doAnswer(answer).when(client).create();
        when(mockCreateBuilder.creatingParentsIfNeeded()).thenReturn(mockCreateParentsIfNeeded);
        when(mockCreateParentsIfNeeded.withMode(Matchers.any(CreateMode.class))).thenReturn(mockCreateParentsIfNeeded);
        when(mockCreateParentsIfNeeded.forPath(Matchers.any(String.class), Matchers.any(byte[].class))).thenThrow(testException);
        leaderElectionService = new ZooKeeperLeaderElectionService(client, "/latch", "/leader");
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        testingContender = new TestingContender(TEST_URL, leaderElectionService);
        leaderElectionService.start(testingContender);
        leaderRetrievalService.start(listener);
        testingContender.waitForError(timeout.toMillis());
        assertNotNull(testingContender.getError());
        assertEquals(testException, testingContender.getError().getCause());
    } finally {
        if (leaderElectionService != null) {
            leaderElectionService.stop();
        }
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) Answer(org.mockito.stubbing.Answer) CuratorFramework(org.apache.curator.framework.CuratorFramework) CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) Test(org.junit.Test)

Example 10 with CreateBuilder

use of 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)

Aggregations

CreateBuilder (org.apache.curator.framework.api.CreateBuilder)11 KeeperException (org.apache.zookeeper.KeeperException)5 CompletableFuture (java.util.concurrent.CompletableFuture)3 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)3 ExistsBuilder (org.apache.curator.framework.api.ExistsBuilder)3 Test (org.testng.annotations.Test)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 SetDataBuilder (org.apache.curator.framework.api.SetDataBuilder)2 CreateMode (org.apache.zookeeper.CreateMode)2 ACL (org.apache.zookeeper.data.ACL)2 Id (org.apache.zookeeper.data.Id)2 Test (org.junit.Test)2 BeforeTest (org.testng.annotations.BeforeTest)2 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 SetupStep (org.apache.atlas.setup.SetupStep)1 ACLBackgroundPathAndBytesable (org.apache.curator.framework.api.ACLBackgroundPathAndBytesable)1 DeleteBuilder (org.apache.curator.framework.api.DeleteBuilder)1