Search in sources :

Example 16 with CreateMode

use of org.apache.zookeeper.CreateMode in project disgear by yangbutao.

the class ZookeeperClient method makePath.

public void makePath(String path, byte[] data, CreateMode createMode) throws Exception {
    boolean failOnExists = true;
    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++) {
        byte[] bytes = null;
        String pathPiece = paths[i];
        sbPath.append("/" + pathPiece);
        final String currentPath = sbPath.toString();
        Object exists = zkClient.exists(currentPath, null);
        if (exists == null || ((i == paths.length - 1) && failOnExists)) {
            CreateMode mode = CreateMode.PERSISTENT;
            if (i == paths.length - 1) {
                mode = createMode;
                bytes = data;
            }
            try {
                zkClient.create(currentPath, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
            } catch (NodeExistsException e) {
                if (!failOnExists) {
                    // TODO:
                    zkClient.setData(currentPath, data, -1);
                    zkClient.exists(currentPath, null);
                    return;
                }
                if (i == paths.length - 1) {
                    throw e;
                }
            }
            if (i == paths.length - 1) {
                zkClient.exists(currentPath, null);
            }
        } else if (i == paths.length - 1) {
            zkClient.setData(currentPath, data, -1);
            zkClient.exists(currentPath, null);
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

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