use of org.apache.zookeeper.CreateMode in project lucene-solr by apache.
the class SolrZkClient method makePath.
/**
* Creates the path in ZooKeeper, creating each node as necessary.
*
* e.g. If <code>path=/solr/group/node</code> and none of the nodes, solr,
* group, node exist, each will be created.
*
* skipPathParts will force the call to fail if the first skipPathParts do not exist already.
*
* Note: retryOnConnLoss is only respected for the final node - nodes
* before that are always retried on connection loss.
*/
public void makePath(String path, byte[] data, CreateMode createMode, Watcher watcher, boolean failOnExists, boolean retryOnConnLoss, int skipPathParts) throws KeeperException, InterruptedException {
log.debug("makePath: {}", path);
boolean retry = true;
if (path.startsWith("/")) {
path = path.substring(1, path.length());
}
String[] paths = path.split("/");
StringBuilder sbPath = new StringBuilder();
for (int i = 0; i < paths.length; i++) {
String pathPiece = paths[i];
sbPath.append("/" + pathPiece);
if (i < skipPathParts) {
continue;
}
byte[] bytes = null;
final String currentPath = sbPath.toString();
Object exists = exists(currentPath, watcher, retryOnConnLoss);
if (exists == null || ((i == paths.length - 1) && failOnExists)) {
CreateMode mode = CreateMode.PERSISTENT;
if (i == paths.length - 1) {
mode = createMode;
bytes = data;
if (!retryOnConnLoss)
retry = false;
}
try {
if (retry) {
final CreateMode finalMode = mode;
final byte[] finalBytes = bytes;
zkCmdExecutor.retryOperation(new ZkOperation() {
@Override
public Object execute() throws KeeperException, InterruptedException {
keeper.create(currentPath, finalBytes, zkACLProvider.getACLsToAdd(currentPath), finalMode);
return null;
}
});
} else {
keeper.create(currentPath, bytes, zkACLProvider.getACLsToAdd(currentPath), mode);
}
} catch (NodeExistsException e) {
if (!failOnExists) {
// TODO: version ? for now, don't worry about race
setData(currentPath, data, -1, retryOnConnLoss);
// set new watch
exists(currentPath, watcher, retryOnConnLoss);
return;
}
// ignore unless it's the last node in the path
if (i == paths.length - 1) {
throw e;
}
}
if (i == paths.length - 1) {
// set new watch
exists(currentPath, watcher, retryOnConnLoss);
}
} else if (i == paths.length - 1) {
// TODO: version ? for now, don't worry about race
setData(currentPath, data, -1, retryOnConnLoss);
// set new watch
exists(currentPath, watcher, retryOnConnLoss);
}
}
}
use of org.apache.zookeeper.CreateMode in project flink by apache.
the class ZooKeeperStateHandleStoreITCase method testAddWithCreateMode.
/**
* Tests that {@link CreateMode} is respected.
*/
@Test
public void testAddWithCreateMode() throws Exception {
LongStateStorage longStateStorage = new LongStateStorage();
ZooKeeperStateHandleStore<Long> store = new ZooKeeperStateHandleStore<Long>(ZooKeeper.getClient(), longStateStorage, Executors.directExecutor());
// Config
Long state = 3457347234L;
CreateMode[] modes = CreateMode.values();
for (int i = 0; i < modes.length; i++) {
CreateMode mode = modes[i];
state += i;
String pathInZooKeeper = "/testAddWithCreateMode" + mode.name();
// Test
store.add(pathInZooKeeper, state, mode);
if (mode.isSequential()) {
// Figure out the sequential ID
List<String> paths = ZooKeeper.getClient().getChildren().forPath("/");
for (String p : paths) {
if (p.startsWith("testAddWithCreateMode" + mode.name())) {
pathInZooKeeper = "/" + p;
break;
}
}
}
// Verify
// State handle created
assertEquals(i + 1, store.getAll().size());
assertEquals(state, longStateStorage.getStateHandles().get(i).retrieveState());
// Path created
Stat stat = ZooKeeper.getClient().checkExists().forPath(pathInZooKeeper);
assertNotNull(stat);
// Is ephemeral or persistent
if (mode.isEphemeral()) {
assertTrue(stat.getEphemeralOwner() != 0);
} else {
assertEquals(0, stat.getEphemeralOwner());
}
// Data is equal
@SuppressWarnings("unchecked") Long actual = ((RetrievableStateHandle<Long>) InstantiationUtil.deserializeObject(ZooKeeper.getClient().getData().forPath(pathInZooKeeper), ClassLoader.getSystemClassLoader())).retrieveState();
assertEquals(state, actual);
}
}
use of org.apache.zookeeper.CreateMode 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;
}
}
}
use of org.apache.zookeeper.CreateMode in project tesb-rt-se by Talend.
the class RegisterEndpointProviderTest method createEndpointStatus.
private void createEndpointStatus(String endpointPath, boolean persistent) throws KeeperException, InterruptedException {
String endpointStatusPath = endpointPath + "/" + STATUS_NODE;
CreateMode mode = persistent ? PERSISTENT : EPHEMERAL;
createNode(endpointStatusPath, mode);
}
use of org.apache.zookeeper.CreateMode in project tesb-rt-se by Talend.
the class EndpointNodeImpl method setLive.
public void setLive(boolean persistent) throws ServiceLocatorException, InterruptedException {
CreateMode mode = persistent ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
NodePath endpointStatusNodePath = child(LIVE);
zkBackend.ensurePathExists(endpointStatusNodePath, mode);
// the old expiration time is not valid after re-registering the endpoint
zkBackend.ensurePathDeleted(child(TIMETOLIVE), false);
}
Aggregations