Search in sources :

Example 26 with CreateMode

use of org.apache.flink.shaded.zookeeper3.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 27 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project zookeeper by apache.

the class CreateTTLTest method testBadUsage.

@Test
public void testBadUsage() throws KeeperException, InterruptedException {
    for (CreateMode createMode : CreateMode.values()) {
        try {
            zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, new Stat(), createMode.isTTL() ? 0 : 100);
            fail("should have thrown IllegalArgumentException");
        } catch (IllegalArgumentException dummy) {
        // correct
        }
    }
    for (CreateMode createMode : CreateMode.values()) {
        AsyncCallback.Create2Callback callback = (rc, path, ctx, name, stat) -> {
        // NOP
        };
        try {
            zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, callback, null, createMode.isTTL() ? 0 : 100);
            fail("should have thrown IllegalArgumentException");
        } catch (IllegalArgumentException dummy) {
        // correct
        }
    }
    try {
        Op op = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, 0);
        zk.multi(Collections.singleton(op));
        fail("should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException dummy) {
    // correct
    }
    try {
        Op op = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 0);
        zk.multi(Collections.singleton(op));
        fail("should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException dummy) {
    // correct
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CreateMode(org.apache.zookeeper.CreateMode) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Stat(org.apache.zookeeper.data.Stat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OpResult(org.apache.zookeeper.OpResult) Op(org.apache.zookeeper.Op) KeeperException(org.apache.zookeeper.KeeperException) Collection(java.util.Collection) TestInfo(org.junit.jupiter.api.TestInfo) RequestHeader(org.apache.zookeeper.proto.RequestHeader) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) ZooDefs(org.apache.zookeeper.ZooDefs) AsyncCallback(org.apache.zookeeper.AsyncCallback) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) CreateTTLRequest(org.apache.zookeeper.proto.CreateTTLRequest) Collections(java.util.Collections) ClientBase(org.apache.zookeeper.test.ClientBase) Code(org.apache.zookeeper.KeeperException.Code) CreateResponse(org.apache.zookeeper.proto.CreateResponse) Op(org.apache.zookeeper.Op) Stat(org.apache.zookeeper.data.Stat) CreateMode(org.apache.zookeeper.CreateMode) AsyncCallback(org.apache.zookeeper.AsyncCallback) Test(org.junit.jupiter.api.Test)

Example 28 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project zookeeper by apache.

the class ZNodeResource method createZNodeAsOctet.

@POST
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response createZNodeAsOctet(@PathParam("path") String path, @DefaultValue("create") @QueryParam("op") String op, @QueryParam("name") String name, @DefaultValue("false") @QueryParam("null") String setNull, @DefaultValue("false") @QueryParam("sequence") String sequence, @Context UriInfo ui, byte[] data) throws InterruptedException, KeeperException {
    ensurePathNotNull(path);
    if (path.equals("/")) {
        path += name;
    } else {
        path += "/" + name;
    }
    if (!op.equals("create")) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), path + " bad operaton " + op)).build());
    }
    if (setNull.equals("true")) {
        data = null;
    }
    CreateMode createMode;
    if (sequence.equals("true")) {
        createMode = CreateMode.PERSISTENT_SEQUENTIAL;
    } else {
        createMode = CreateMode.PERSISTENT;
    }
    String newPath = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
    URI uri = ui.getAbsolutePathBuilder().path(newPath).build();
    return Response.created(uri).entity(new ZPath(newPath, ui.getAbsolutePath().toString())).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) CreateMode(org.apache.zookeeper.CreateMode) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) URI(java.net.URI) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 29 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project zookeeper by apache.

the class ZNodeResource method createZNode.

@POST
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response createZNode(@PathParam("path") String path, @QueryParam("callback") String callback, @DefaultValue("create") @QueryParam("op") String op, @QueryParam("name") String name, @DefaultValue("base64") @QueryParam("dataformat") String dataformat, @DefaultValue("false") @QueryParam("null") String setNull, @DefaultValue("false") @QueryParam("sequence") String sequence, @DefaultValue("false") @QueryParam("ephemeral") String ephemeral, @Context UriInfo ui, byte[] data) throws InterruptedException, KeeperException {
    ensurePathNotNull(path);
    if (path.equals("/")) {
        path += name;
    } else {
        path += "/" + name;
    }
    if (!op.equals("create")) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), path + " bad operaton " + op)).build());
    }
    if (setNull.equals("true")) {
        data = null;
    }
    CreateMode createMode;
    if (sequence.equals("true")) {
        if (ephemeral.equals("false")) {
            createMode = CreateMode.PERSISTENT_SEQUENTIAL;
        } else {
            createMode = CreateMode.EPHEMERAL_SEQUENTIAL;
        }
    } else if (ephemeral.equals("false")) {
        createMode = CreateMode.PERSISTENT;
    } else {
        createMode = CreateMode.EPHEMERAL;
    }
    String newPath = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
    URI uri = ui.getAbsolutePathBuilder().path(newPath).build();
    return Response.created(uri).entity(new JSONWithPadding(new ZPath(newPath, ui.getAbsolutePath().toString()))).build();
}
Also used : JSONWithPadding(com.sun.jersey.api.json.JSONWithPadding) WebApplicationException(javax.ws.rs.WebApplicationException) CreateMode(org.apache.zookeeper.CreateMode) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) ZError(org.apache.zookeeper.server.jersey.jaxb.ZError) URI(java.net.URI) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 30 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project tesb-rt-se by Talend.

the class EndpointNodeImpl method setExpiryTime.

@Override
public void setExpiryTime(Date expiryTime, boolean persistent) throws ServiceLocatorException, InterruptedException {
    NodePath expNodePath = child(TIMETOLIVE);
    String strTime = Long.toString(expiryTime.getTime());
    byte[] content = strTime.getBytes(UTF8);
    CreateMode mode = persistent ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
    zkBackend.ensurePathExists(expNodePath, mode, content);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) NodePath(org.talend.esb.servicelocator.client.internal.NodePath)

Aggregations

CreateMode (org.apache.zookeeper.CreateMode)35 KeeperException (org.apache.zookeeper.KeeperException)8 HelixException (org.apache.helix.HelixException)7 Stat (org.apache.zookeeper.data.Stat)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Op (org.apache.zookeeper.Op)4 ACL (org.apache.zookeeper.data.ACL)4 DataUpdater (org.I0Itec.zkclient.DataUpdater)3 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)3 ZkException (org.I0Itec.zkclient.exception.ZkException)3 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)3 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)3 ZNRecord (org.apache.helix.ZNRecord)3 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)3 CreateCallbackHandler (org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler)3 Code (org.apache.zookeeper.KeeperException.Code)3 CreateRequest (org.apache.zookeeper.proto.CreateRequest)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 URI (java.net.URI)2