throws ZookeeperClientFailedException, InterruptedException assert createMode.isSequential() : createMode; assert !createMode.isSequential() : createMode;
@Override void validate() throws KeeperException { CreateMode createMode = CreateMode.fromFlag(flags); PathUtils.validatePath(getPath(), createMode.isSequential()); EphemeralType.validateTTL(createMode, ttl); } }
@Override void validate() throws KeeperException { CreateMode createMode = CreateMode.fromFlag(flags); PathUtils.validatePath(getPath(), createMode.isSequential()); EphemeralType.validateTTL(createMode, -1); } }
@Override void validate() throws KeeperException { CreateMode createMode = CreateMode.fromFlag(flags); PathUtils.validatePath(getPath(), createMode.isSequential()); } }
/** * @param path Path. * @param data Data. * @param createMode Create mode. * @return Created path. * @throws ZookeeperClientFailedException If connection to zk was lost. * @throws InterruptedException If interrupted. */ String createIfNeeded(String path, byte[] data, CreateMode createMode) throws ZookeeperClientFailedException, InterruptedException { assert !createMode.isSequential() : createMode; if (data == null) data = EMPTY_BYTES; for (;;) { long connStartTime = this.connStartTime; try { return zk.create(path, data, ZK_ACL, createMode); } catch (KeeperException.NodeExistsException e) { if (log.isDebugEnabled()) log.debug("Node already exists: " + path); return path; } catch (Exception e) { onZookeeperError(connStartTime, e); } } }
/** * The asynchronous version of create with ttl. * * @see #create(String, byte[], List, CreateMode, Stat, long) */ public void create(final String path, byte data[], List<ACL> acl, CreateMode createMode, Create2Callback cb, Object ctx, long ttl) { final String clientPath = path; PathUtils.validatePath(clientPath, createMode.isSequential()); EphemeralType.validateTTL(createMode, ttl); final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); setCreateHeader(createMode, h); ReplyHeader r = new ReplyHeader(); Create2Response response = new Create2Response(); Record record = makeCreateRecord(createMode, serverPath, data, acl, ttl); cnxn.queuePacket(h, r, record, response, cb, clientPath, serverPath, ctx, null); }
throws KeeperException, InterruptedException { final String clientPath = path; PathUtils.validatePath(clientPath, createMode.isSequential()); EphemeralType.validateTTL(createMode, ttl); validateACL(acl);
/** * The asynchronous version of create. * * @see #create(String, byte[], List, CreateMode) */ public void create(final String path, byte data[], List<ACL> acl, CreateMode createMode, StringCallback cb, Object ctx) { final String clientPath = path; PathUtils.validatePath(clientPath, createMode.isSequential()); EphemeralType.validateTTL(createMode, -1); final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create); CreateRequest request = new CreateRequest(); CreateResponse response = new CreateResponse(); ReplyHeader r = new ReplyHeader(); request.setData(data); request.setFlags(createMode.toFlag()); request.setPath(serverPath); request.setAcl(acl); cnxn.queuePacket(h, r, request, response, cb, clientPath, serverPath, ctx, null); }
PathUtils.validatePath(clientPath, createMode.isSequential()); EphemeralType.validateTTL(createMode, -1); validateACL(acl);
@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()); }
PathUtils.validatePath(clientPath, createMode.isSequential());
/** * The asynchronous version of create. * * @see #create(String, byte[], List, CreateMode) */ public void create(final String path, byte data[], List<ACL> acl, CreateMode createMode, StringCallback cb, Object ctx) { final String clientPath = path; PathUtils.validatePath(clientPath, createMode.isSequential()); final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.create); CreateRequest request = new CreateRequest(); CreateResponse response = new CreateResponse(); ReplyHeader r = new ReplyHeader(); request.setData(data); request.setFlags(createMode.toFlag()); request.setPath(serverPath); request.setAcl(acl); cnxn.queuePacket(h, r, request, response, cb, clientPath, serverPath, ctx, null); }
CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags()); if (createMode.isSequential()) { path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
public String createChild( long sessionId, String childName, byte[] data, List<ACL> acl, CreateMode createMode ) throws KeeperException { // Append a sequence number to path if sequential if (createMode.isSequential()) { childName += String.format("%08d", nextSeqNum.addAndGet(1)); } ZNode zNode = new ZNode(sessionId, this, childName, data, acl, createMode); addChild(zNode); zNode.signalNodeEvent(EventType.NodeCreated); return childName; }
if (createMode.isSequential()) { path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
/** * Validate that this schema's create mode setting matches and that the data is valid * * @param mode CreateMode being used * @param path the znode full path * @param data data being set * @param acl the creation acls * @throws SchemaViolation if schema's create mode setting does not match or data is invalid */ public void validateCreate(CreateMode mode, String path, byte[] data, List<ACL> acl) { if ( mode.isEphemeral() && (ephemeral == Allowance.CANNOT) ) { throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl), "Cannot be ephemeral"); } if ( !mode.isEphemeral() && (ephemeral == Allowance.MUST) ) { throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl), "Must be ephemeral"); } if ( mode.isSequential() && (sequential == Allowance.CANNOT) ) { throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl), "Cannot be sequential"); } if ( !mode.isSequential() && (sequential == Allowance.MUST) ) { throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl), "Must be sequential"); } validateGeneral(path, data, acl); }
@Override public String forPath(final String givenPath, byte[] data) throws Exception { if ( compress ) { data = client.getCompressionProvider().compress(givenPath, data); } final String adjustedPath = adjustPath(client.fixForNamespace(givenPath, createMode.isSequential())); List<ACL> aclList = acling.getAclList(adjustedPath); client.getSchemaSet().getSchema(givenPath).validateCreate(createMode, givenPath, data, aclList); String returnPath = null; if ( backgrounding.inBackground() ) { pathInBackground(adjustedPath, data, givenPath); } else { String path = protectedPathInForeground(adjustedPath, data, aclList); returnPath = client.unfixForNamespace(path); } return returnPath; }
@Override protected void retry() { if (!createMode.isSequential()) { // it's always safe to retry create for non-sequential names _log.info("Retry create operation: path = " + path + " data length " + getDataLength(data)); zkCreate(path, data, acl, createMode, this, ctx); } else { _log.error("Connection lost during create operation of sequential node. " + "Consider using createUniqueSequential() instead"); } }
@Override public String forPath(final String givenPath, byte[] data) throws Exception { if ( compress ) { data = client.getCompressionProvider().compress(givenPath, data); } final String adjustedPath = adjustPath(client.fixForNamespace(givenPath, createMode.isSequential())); String returnPath = null; if ( backgrounding.inBackground() ) { pathInBackground(adjustedPath, data, givenPath); } else { String path = protectedPathInForeground(adjustedPath, data); returnPath = client.unfixForNamespace(path); } return returnPath; }
@Override public Schema schema() { if ( schema == null ) { schema = Schema.builder(path.toSchemaPathPattern()) .dataValidator(this) .ephemeral(createMode.isEphemeral() ? Schema.Allowance.MUST : Schema.Allowance.CANNOT) .canBeDeleted(true) .sequential(createMode.isSequential() ? Schema.Allowance.MUST : Schema.Allowance.CANNOT) .watched(Schema.Allowance.CAN) .build(); } return schema; }