Search in sources :

Example 11 with CreateMode

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);
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode)

Example 12 with CreateMode

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());
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) Test(org.junit.Test)

Example 13 with CreateMode

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));
}
Also used : CreateContainerTxn(org.apache.zookeeper.txn.CreateContainerTxn) CreateRequest(org.apache.zookeeper.proto.CreateRequest) ACL(org.apache.zookeeper.data.ACL) CreateTTLRequest(org.apache.zookeeper.proto.CreateTTLRequest) CreateTTLTxn(org.apache.zookeeper.txn.CreateTTLTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) CreateMode(org.apache.zookeeper.CreateMode) StatPersisted(org.apache.zookeeper.data.StatPersisted) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) KeeperException(org.apache.zookeeper.KeeperException)

Example 14 with CreateMode

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;
}
Also used : ACL(org.apache.zookeeper.data.ACL) CreateMode(org.apache.zookeeper.CreateMode) KeeperException(org.apache.zookeeper.KeeperException)

Example 15 with CreateMode

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));
}
Also used : CreateMode(org.apache.zookeeper.CreateMode)

Aggregations

CreateMode (org.apache.zookeeper.CreateMode)16 Test (org.junit.Test)4 KeeperException (org.apache.zookeeper.KeeperException)3 URI (java.net.URI)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 AsyncCallback (org.apache.zookeeper.AsyncCallback)2 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)2 Op (org.apache.zookeeper.Op)2 ACL (org.apache.zookeeper.data.ACL)2 Stat (org.apache.zookeeper.data.Stat)2 CreateRequest (org.apache.zookeeper.proto.CreateRequest)2 ZError (org.apache.zookeeper.server.jersey.jaxb.ZError)2 ZPath (org.apache.zookeeper.server.jersey.jaxb.ZPath)2 JSONWithPadding (com.sun.jersey.api.json.JSONWithPadding)1 LogExistsException (com.twitter.distributedlog.exceptions.LogExistsException)1 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)1 ZKException (com.twitter.distributedlog.exceptions.ZKException)1