Example usage for org.apache.zookeeper ZooKeeper create

List of usage examples for org.apache.zookeeper ZooKeeper create

Introduction

In this page you can find the example usage for org.apache.zookeeper ZooKeeper create.

Prototype

public void create(final String path, byte[] data, List<ACL> acl, CreateMode createMode, Create2Callback cb,
        Object ctx) 

Source Link

Document

The asynchronous version of create.

Usage

From source file:com.linkedin.d2.discovery.stores.zk.ZKConnection.java

License:Apache License

/**
 * checks if the path in zk exist or not. If it doesn't exist, will create the node.
 *
 * @param path//  w ww . j av  a 2s. c o m
 * @param callback
 */
public void ensurePersistentNodeExists(String path, final Callback<None> callback) {
    final ZooKeeper zk = zk();
    // Remove any trailing slash except for when we just want the root
    while (path.endsWith("/") && path.length() > 1) {
        path = path.substring(0, path.length() - 1);
    }
    final String normalizedPath = path;
    AsyncCallback.StringCallback createCallback = new AsyncCallback.StringCallback() {
        @Override
        public void processResult(int rc, String unused, Object ctx, String name) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
            case NODEEXISTS:
                callback.onSuccess(None.none());
                break;
            case NONODE:
                // create parent and retry
                String parent = normalizedPath.substring(0, normalizedPath.lastIndexOf('/'));
                ensurePersistentNodeExists(parent, new Callback<None>() {
                    @Override
                    public void onSuccess(None none) {
                        ensurePersistentNodeExists(normalizedPath, callback);
                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onError(e);
                    }
                });
                break;
            default:
                callback.onError(KeeperException.create(code));
                break;
            }
        }
    };
    try {
        zk.create(normalizedPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, createCallback,
                null);
    } catch (Exception e) {
        callback.onError(e);
    }
}

From source file:com.proofpoint.zookeeper.ZookeeperClient.java

License:Apache License

@Override
public String create(final String path, final byte data[]) throws Exception {
    preconditionNotWatched();/*w ww  . j ava 2  s.c  om*/

    if (inBackground) {
        RetryHandler.Call<Void> backgroundCall = new RetryHandler.Call<Void>() {
            @Override
            public Void call(ZooKeeper client, final RetryHandler retryHandler) {
                client.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode,
                        new RetryStringCallback(retryHandler), context);
                return null;
            }
        };
        RetryHandler.makeAndStart(this, retryPolicy, backgroundCall);

        return null;
    }

    RetryHandler.Call<String> backgroundCall = new RetryHandler.Call<String>() {
        @Override
        public String call(ZooKeeper client, RetryHandler<String> stringRetryHandler) throws Exception {
            return client.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
        }
    };
    return RetryHandler.makeAndStart(this, retryPolicy, backgroundCall);
}

From source file:org.apache.bookkeeper.util.ZkUtils.java

License:Apache License

/**
 * Asynchronously create zookeeper path recursively and optimistically.
 *
 * @see #createFullPathOptimistic(ZooKeeper,String,byte[],List<ACL>,CreateMode)
 *
 * @param zk/*from ww w . ja  va  2  s .  c  o  m*/
 *          Zookeeper client
 * @param originalPath
 *          Zookeeper full path
 * @param data
 *          Zookeeper data
 * @param acl
 *          Acl of the zk path
 * @param createMode
 *          Create mode of zk path
 * @param callback
 *          Callback
 * @param ctx
 *          Context object
 */
public static void asyncCreateFullPathOptimistic(final ZooKeeper zk, final String originalPath,
        final byte[] data, final List<ACL> acl, final CreateMode createMode,
        final AsyncCallback.StringCallback callback, final Object ctx) {

    zk.create(originalPath, data, acl, createMode, new StringCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, String name) {

            if (rc != Code.NONODE.intValue()) {
                callback.processResult(rc, path, ctx, name);
                return;
            }

            // Since I got a nonode, it means that my parents don't exist
            // create mode is persistent since ephemeral nodes can't be
            // parents
            String parent = new File(originalPath).getParent().replace("\\", "/");
            asyncCreateFullPathOptimistic(zk, parent, new byte[0], acl, CreateMode.PERSISTENT,
                    new StringCallback() {

                        @Override
                        public void processResult(int rc, String path, Object ctx, String name) {
                            if (rc == Code.OK.intValue() || rc == Code.NODEEXISTS.intValue()) {
                                // succeeded in creating the parent, now
                                // create the original path
                                asyncCreateFullPathOptimistic(zk, originalPath, data, acl, createMode, callback,
                                        ctx);
                            } else {
                                callback.processResult(rc, path, ctx, name);
                            }
                        }
                    }, ctx);
        }
    }, ctx);
}

From source file:org.apache.bookkeeper.zookeeper.BkZooKeeperClient.java

License:Apache License

@Override
public void create(final String path, final byte[] data, final List<ACL> acl, final CreateMode createMode,
        final StringCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, createStats) {

        final StringCallback createCb = new StringCallback() {

            @Override/*  w  ww.  ja  v a2s. c om*/
            public void processResult(int rc, String path, Object ctx, String name) {
                BkZooWorker worker = (BkZooWorker) ctx;
                if (allowRetry(worker, rc)) {
                    backOffAndRetry(that, worker.nextRetryWaitTime());
                } else {
                    cb.processResult(rc, path, context, name);
                }
            }

        };

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                BkZooKeeperClient.super.create(path, data, acl, createMode, createCb, worker);
            } else {
                zkHandle.create(path, data, acl, createMode, createCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("create (%s, acl = %s, mode = %s)", path, acl, createMode);
        }
    };
    // execute it immediately
    proc.run();
}

From source file:org.apache.bookkeeper.zookeeper.ZooKeeperClient.java

License:Apache License

@Override
public void create(final String path, final byte[] data, final List<ACL> acl, final CreateMode createMode,
        final StringCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, createStats) {

        final StringCallback createCb = new StringCallback() {

            @Override/*from w w  w .j  a  v  a  2 s  .  c  om*/
            public void processResult(int rc, String path, Object ctx, String name) {
                ZooWorker worker = (ZooWorker) ctx;
                if (allowRetry(worker, rc)) {
                    backOffAndRetry(that, worker.nextRetryWaitTime());
                } else {
                    cb.processResult(rc, path, context, name);
                }
            }

        };

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.create(path, data, acl, createMode, createCb, worker);
            } else {
                zkHandle.create(path, data, acl, createMode, createCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("create (%s, acl = %s, mode = %s)", path, acl, createMode);
        }
    };
    // execute it immediately
    proc.run();
}

From source file:org.apache.bookkeeper.zookeeper.ZooKeeperClient.java

License:Apache License

@Override
public void create(final String path, final byte[] data, final List<ACL> acl, final CreateMode createMode,
        final Create2Callback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, createStats) {

        final Create2Callback createCb = new Create2Callback() {

            @Override/*  w ww .jav  a  2s. c o  m*/
            public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
                ZooWorker worker = (ZooWorker) ctx;
                if (allowRetry(worker, rc)) {
                    backOffAndRetry(that, worker.nextRetryWaitTime());
                } else {
                    cb.processResult(rc, path, context, name, stat);
                }
            }

        };

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.create(path, data, acl, createMode, createCb, worker);
            } else {
                zkHandle.create(path, data, acl, createMode, createCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("create (%s, acl = %s, mode = %s)", path, acl, createMode);
        }
    };
    // execute it immediately
    proc.run();
}

From source file:org.apache.hedwig.zookeeper.ZkUtils.java

License:Apache License

public static void createFullPathOptimistic(final ZooKeeper zk, final String originalPath, final byte[] data,
        final List<ACL> acl, final CreateMode createMode, final AsyncCallback.StringCallback callback,
        final Object ctx) {

    zk.create(originalPath, data, acl, createMode, new SafeAsyncZKCallback.StringCallback() {
        @Override/*w  w  w.  j  a v  a  2  s . c o  m*/
        public void safeProcessResult(int rc, String path, Object ctx, String name) {

            if (rc != Code.NONODE.intValue()) {
                callback.processResult(rc, path, ctx, name);
                return;
            }

            // Since I got a nonode, it means that my parents don't exist
            // create mode is persistent since ephemeral nodes can't be
            // parents
            ZkUtils.createFullPathOptimistic(zk, PathUtils.parent(originalPath), new byte[0], acl,
                    CreateMode.PERSISTENT, new SafeAsyncZKCallback.StringCallback() {

                        @Override
                        public void safeProcessResult(int rc, String path, Object ctx, String name) {
                            if (rc == Code.OK.intValue() || rc == Code.NODEEXISTS.intValue()) {
                                // succeeded in creating the parent, now
                                // create the original path
                                ZkUtils.createFullPathOptimistic(zk, originalPath, data, acl, createMode,
                                        callback, ctx);
                            } else {
                                callback.processResult(rc, path, ctx, name);
                            }
                        }
                    }, ctx);
        }
    }, ctx);

}

From source file:yangqi.zookeeper.example.masterworker.AsynCreate.java

License:Open Source License

/**
 * @param args// w ww  .j  a v  a2s  . c om
 * @throws IOException
 * @throws InterruptedException
 */
public static void main(String[] args) throws IOException, InterruptedException {
    ZooKeeper zookeeper = new ZooKeeper("localhost:2181", 200000, null);

    zookeeper.create("/mas", "sid-o2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,
            new StringCallback() {

                @Override
                public void processResult(int rc, String path, Object ctx, String name) {
                    Code code = Code.get(rc);
                    switch (code) {
                    case OK:
                        System.out.println(code);
                        break;
                    case NODEEXISTS:
                        System.out.println(code);
                        break;
                    case SESSIONEXPIRED:
                        System.out.println(code);
                        break;
                    default:
                        System.out.println("unknow " + code);
                    }

                }
            }, null);

    DataCallback callback = new DataCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            Code code = Code.get(rc);
            System.out.println("code for check " + code);
            switch (code) {
            case OK:
                break;
            case NONODE:
                break;
            case NODEEXISTS:
                break;
            case SESSIONEXPIRED:
                break;
            default:
            }

        }

    };

    zookeeper.getData("/mas", true, callback, null);

    Thread.sleep(200000);

}