use of org.apache.zookeeper.CreateMode in project zookeeper by apache.
the class InstanceContainer method mknod.
private void mknod(String path, CreateMode mode) throws KeeperException, InterruptedException {
String[] subpath = path.split("/");
StringBuilder sb = new StringBuilder();
// We start at 1 because / will create an empty part first
for (int i = 1; i < subpath.length; i++) {
sb.append("/");
sb.append(subpath[i]);
CreateMode m = CreateMode.PERSISTENT;
if (i == subpath.length - 1) {
m = mode;
}
mknod_inner(sb.toString(), m);
}
}
use of org.apache.zookeeper.CreateMode in project zookeeper by apache.
the class CreateModeTest method testBasicCreateMode.
@Test
public void testBasicCreateMode() {
CreateMode cm = CreateMode.PERSISTENT;
Assert.assertEquals(cm.toFlag(), 0);
Assert.assertFalse(cm.isEphemeral());
Assert.assertFalse(cm.isSequential());
Assert.assertFalse(cm.isContainer());
cm = CreateMode.EPHEMERAL;
Assert.assertEquals(cm.toFlag(), 1);
Assert.assertTrue(cm.isEphemeral());
Assert.assertFalse(cm.isSequential());
Assert.assertFalse(cm.isContainer());
cm = CreateMode.PERSISTENT_SEQUENTIAL;
Assert.assertEquals(cm.toFlag(), 2);
Assert.assertFalse(cm.isEphemeral());
Assert.assertTrue(cm.isSequential());
Assert.assertFalse(cm.isContainer());
cm = CreateMode.EPHEMERAL_SEQUENTIAL;
Assert.assertEquals(cm.toFlag(), 3);
Assert.assertTrue(cm.isEphemeral());
Assert.assertTrue(cm.isSequential());
Assert.assertFalse(cm.isContainer());
cm = CreateMode.CONTAINER;
Assert.assertEquals(cm.toFlag(), 4);
Assert.assertFalse(cm.isEphemeral());
Assert.assertFalse(cm.isSequential());
Assert.assertTrue(cm.isContainer());
}
use of org.apache.zookeeper.CreateMode in project zookeeper by apache.
the class PrepRequestProcessor method pRequest2TxnCreate.
private void pRequest2TxnCreate(int type, Request request, Record record, boolean deserialize) throws IOException, KeeperException {
if (deserialize) {
ByteBufferInputStream.byteBuffer2Record(request.request, record);
}
int flags;
String path;
List<ACL> acl;
byte[] data;
long ttl;
if (type == OpCode.createTTL) {
CreateTTLRequest createTtlRequest = (CreateTTLRequest) record;
flags = createTtlRequest.getFlags();
path = createTtlRequest.getPath();
acl = createTtlRequest.getAcl();
data = createTtlRequest.getData();
ttl = createTtlRequest.getTtl();
} else {
CreateRequest createRequest = (CreateRequest) record;
flags = createRequest.getFlags();
path = createRequest.getPath();
acl = createRequest.getAcl();
data = createRequest.getData();
ttl = 0;
}
CreateMode createMode = CreateMode.fromFlag(flags);
validateCreateRequest(createMode, request);
String parentPath = validatePathForCreate(path, request.sessionId);
List<ACL> listACL = fixupACL(path, request.authInfo, acl);
ChangeRecord parentRecord = getRecordForPath(parentPath);
checkACL(zks, request.cnxn, parentRecord.acl, ZooDefs.Perms.CREATE, request.authInfo, path, listACL);
int parentCVersion = parentRecord.stat.getCversion();
if (createMode.isSequential()) {
path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
}
validatePath(path, request.sessionId);
try {
if (getRecordForPath(path) != null) {
throw new KeeperException.NodeExistsException(path);
}
} catch (KeeperException.NoNodeException e) {
// ignore this one
}
boolean ephemeralParent = EphemeralType.get(parentRecord.stat.getEphemeralOwner()) == EphemeralType.NORMAL;
if (ephemeralParent) {
throw new KeeperException.NoChildrenForEphemeralsException(path);
}
int newCversion = parentRecord.stat.getCversion() + 1;
if (type == OpCode.createContainer) {
request.setTxn(new CreateContainerTxn(path, data, listACL, newCversion));
} else if (type == OpCode.createTTL) {
request.setTxn(new CreateTTLTxn(path, data, listACL, newCversion, ttl));
} else {
request.setTxn(new CreateTxn(path, data, listACL, createMode.isEphemeral(), newCversion));
}
StatPersisted s = new StatPersisted();
if (createMode.isEphemeral()) {
s.setEphemeralOwner(request.sessionId);
}
parentRecord = parentRecord.duplicate(request.getHdr().getZxid());
parentRecord.childCount++;
parentRecord.stat.setCversion(newCversion);
addChangeRecord(parentRecord);
addChangeRecord(new ChangeRecord(request.getHdr().getZxid(), path, s, 0, listACL));
}
use of org.apache.zookeeper.CreateMode in project zookeeper by apache.
the class CreateCommand method exec.
@Override
public boolean exec() throws CliException {
CreateMode flags = CreateMode.PERSISTENT;
boolean hasE = cl.hasOption("e");
boolean hasS = cl.hasOption("s");
boolean hasC = cl.hasOption("c");
if (hasC && (hasE || hasS)) {
throw new MalformedCommandException("-c cannot be combined with -s or -e. Containers cannot be ephemeral or sequential.");
}
if (hasE && hasS) {
flags = CreateMode.EPHEMERAL_SEQUENTIAL;
} else if (hasE) {
flags = CreateMode.EPHEMERAL;
} else if (hasS) {
flags = CreateMode.PERSISTENT_SEQUENTIAL;
} else if (hasC) {
flags = CreateMode.CONTAINER;
}
String path = args[1];
byte[] data = null;
if (args.length > 2) {
data = args[2].getBytes();
}
List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
if (args.length > 3) {
acl = AclParser.parse(args[3]);
}
try {
String newPath = zk.create(path, data, acl, flags);
err.println("Created " + newPath);
} catch (KeeperException.EphemeralOnLocalSessionException e) {
err.println("Unable to create ephemeral node on a local session");
throw new CliWrapperException(e);
} catch (KeeperException.InvalidACLException ex) {
err.println(ex.getMessage());
throw new CliWrapperException(ex);
} catch (KeeperException | InterruptedException ex) {
throw new CliWrapperException(ex);
}
return true;
}
use of org.apache.zookeeper.CreateMode in project hadoop by apache.
the class RegistryOperationsService method bind.
@Override
public void bind(String path, ServiceRecord record, int flags) throws IOException {
Preconditions.checkArgument(record != null, "null record");
validatePath(path);
// validate the record before putting it
RegistryTypeUtils.validateServiceRecord(path, record);
LOG.info("Bound at {} : {}", path, record);
CreateMode mode = CreateMode.PERSISTENT;
byte[] bytes = serviceRecordMarshal.toBytes(record);
zkSet(path, mode, bytes, getClientAcls(), ((flags & BindFlags.OVERWRITE) != 0));
}
Aggregations