Example usage for org.apache.zookeeper ZooKeeper setData

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

Introduction

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

Prototype

public void setData(final String path, byte[] data, int version, StatCallback cb, Object ctx) 

Source Link

Document

The asynchronous version of setData.

Usage

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

License:Apache License

private void setDataUnsafe(final String path, final byte[] data, final Callback<None> callback,
        final int count) {
    final ZooKeeper zk = zk();
    final AsyncCallback.StatCallback dataCallback = new AsyncCallback.StatCallback() {
        @Override// www .ja v  a2s.  com
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
                callback.onSuccess(None.none());
                break;
            case BADVERSION:
                if (count < MAX_RETRIES) {
                    LOG.info("setDataUnsafe: ignored BADVERSION for {}", path);
                    setDataUnsafe(path, data, callback, count + 1);
                } else {
                    callback.onError(KeeperException.create(code));
                }
                break;
            default:
                callback.onError(KeeperException.create(code));
                break;
            }
        }
    };
    final AsyncCallback.StatCallback statCallback = new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
                zk.setData(path, data, stat.getVersion(), dataCallback, null);
                break;
            default:
                callback.onError(KeeperException.create(code));
                break;
            }
        }
    };
    try {
        zk.exists(path, false, statCallback, null);
    } catch (Exception e) {
        callback.onError(e);
    }
}

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

License:Apache License

@Override
public Stat setData(final String path, final byte data[]) throws Exception {
    preconditionNotWatched();//from www  . j  a  v  a  2s  . co m

    if (inBackground) {
        RetryHandler.Call<Void> backgroundCall = new RetryHandler.Call<Void>() {
            @Override
            public Void call(ZooKeeper client, final RetryHandler retryHandler) {
                client.setData(path, data, dataVersion, new AsyncCallback.StatCallback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx, Stat stat) {
                        if (retryHandler.okToContinue(rc)) {
                            eventQueue.postEvent(new ZookeeperEvent(ZookeeperEvent.Type.SET_DATA, rc, path, ctx,
                                    null, stat, null, null, key));
                        }
                    }
                }, context);
                return null;
            }
        };
        RetryHandler.makeAndStart(this, retryPolicy, backgroundCall);
        return null;
    }

    RetryHandler.Call<Stat> backgroundCall = new RetryHandler.Call<Stat>() {
        @Override
        public Stat call(ZooKeeper client, RetryHandler<Stat> statRetryHandler) throws Exception {
            return client.setData(path, data, dataVersion);
        }
    };
    return RetryHandler.makeAndStart(this, retryPolicy, backgroundCall);
}

From source file:com.twitter.distributedlog.util.Utils.java

License:Apache License

/**
 * Set <code>data</code> to zookeeper <code>path</code>.
 *
 * @param zk/*from  w  w w .  j  ava  2 s.  com*/
 *          zookeeper client
 * @param path
 *          path to set data
 * @param data
 *          data to set
 * @param version
 *          version used to set data
 * @return future representing the version after this operation.
 */
public static Future<ZkVersion> zkSetData(ZooKeeper zk, String path, byte[] data, ZkVersion version) {
    final Promise<ZkVersion> promise = new Promise<ZkVersion>();
    zk.setData(path, data, version.getZnodeVersion(), new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.updateIfEmpty(new Return<ZkVersion>(new ZkVersion(stat.getVersion())));
                return;
            }
            promise.updateIfEmpty(new Throw<ZkVersion>(KeeperException.create(KeeperException.Code.get(rc))));
            return;
        }
    }, null);
    return promise;
}

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

License:Apache License

@Override
public void setData(final String path, final byte[] data, final int version, final StatCallback cb,
        final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, setStats) {

        final StatCallback stCb = new StatCallback() {

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

        };

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                BkZooKeeperClient.super.setData(path, data, version, stCb, worker);
            } else {
                zkHandle.setData(path, data, version, stCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("setData (%s, version = %d)", path, version);
        }
    };
    // execute it immediately
    proc.run();
}

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

License:Apache License

@Override
public void setData(final String path, final byte[] data, final int version, final StatCallback cb,
        final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, setStats) {

        final StatCallback stCb = new StatCallback() {

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

        };

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.setData(path, data, version, stCb, worker);
            } else {
                zkHandle.setData(path, data, version, stCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("setData (%s, version = %d)", path, version);
        }
    };
    // execute it immediately
    proc.run();
}

From source file:org.apache.camel.component.zookeeper.ZookeeperProducer.java

License:Apache License

private void asynchronouslySetDataOnNode(ZooKeeper connection, ProductionContext context) {
    if (log.isDebugEnabled()) {
        log.debug(format("Storing data to node '%s', not waiting for confirmation", context.node));
    }/*ww  w .  j a v  a  2  s . c o m*/
    connection.setData(context.node, context.payload, context.version, new AsyncSetDataCallback(), context);
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

/**
 * Set <code>data</code> to zookeeper <code>path</code>.
 *
 * @param zk/*  ww  w .  j a  va 2  s. com*/
 *          zookeeper client
 * @param path
 *          path to set data
 * @param data
 *          data to set
 * @param version
 *          version used to set data
 * @return future representing the version after this operation.
 */
public static CompletableFuture<LongVersion> zkSetData(ZooKeeper zk, String path, byte[] data,
        LongVersion version) {
    final CompletableFuture<LongVersion> promise = new CompletableFuture<LongVersion>();
    zk.setData(path, data, (int) version.getLongVersion(), new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.complete(new LongVersion(stat.getVersion()));
                return;
            }
            promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            return;
        }
    }, null);
    return promise;
}