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;
}
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);
}
}
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;
}
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();
}
}
}
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;
}
Aggregations