Example usage for org.apache.zookeeper ZooKeeper sync

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

Introduction

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

Prototype

public void sync(final String path, VoidCallback cb, Object ctx) 

Source Link

Document

Asynchronous sync.

Usage

From source file:com.github.mosuka.zookeeper.nicli.command.SyncCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {/*from ww  w  .  ja  v a 2 s . co m*/
        String path = (String) parameters.get("path");

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        zk.sync(path, new AsyncCallback.VoidCallback() {
            public void processResult(int rc, String path, Object ctx) {
                // System.out.println("Sync returned " + rc);
            }
        }, null);

        setStatus(Command.STATUS_SUCCESS);
        setMessage(Command.SUCCESS_MESSAGE);
    } catch (IllegalArgumentException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (ClassCastException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (NullPointerException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    }
}

From source file:com.greplin.zookeeper.RobustZooKeeper.java

License:Apache License

public void sync() throws IOException, InterruptedException {

    log.info("Called sync() on client " + clientNumber);
    final CountDownLatch waitForSync = new CountDownLatch(1);
    final ZooKeeper c = getClient();
    assert c.getState().isAlive();

    c.sync("/", new AsyncCallback.VoidCallback() {
        @Override/* w w  w  . j  av a  2  s.c  o m*/
        public void processResult(int rc, String path, Object ctx) {
            log.info("Sync callback triggered on client " + clientNumber);
            waitForSync.countDown();
        }
    }, null);

    log.info("Waitng for sync callback on client " + clientNumber);
    waitForSync.await();

    log.info("sync() finished on " + clientNumber);
    return;
}

From source file:com.iquanwai.confucius.biz.util.zk.RobustZooKeeper.java

License:Apache License

public void sync() throws IOException, InterruptedException {
    log.info("Called sync() on client " + this.clientNumber);
    final CountDownLatch waitForSync = new CountDownLatch(1);
    final ZooKeeper c = getClient();
    assert c.getState().isAlive();

    c.sync("/", new AsyncCallback.VoidCallback() {
        @Override//from w  ww .j  av  a  2s .  co  m
        public void processResult(int rc, String path, Object ctx) {
            log.info("Sync callback triggered on client " + RobustZooKeeper.this.clientNumber);
            waitForSync.countDown();
        }
    }, null);

    log.info("Waitng for sync callback on client " + this.clientNumber);
    waitForSync.await();

    log.info("sync() finished on " + this.clientNumber);
}

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

License:Apache License

@Override
public void sync(final String path) throws Exception {
    preconditionNotWatched();// ww w . jav  a 2  s.  c  o  m
    preconditionInBackground();

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

From source file:com.twitter.distributedlog.BKLogHandler.java

License:Apache License

Future<Void> checkLogStreamExistsAsync() {
    final Promise<Void> promise = new Promise<Void>();
    try {// w w  w. j a va 2 s .com
        final ZooKeeper zk = zooKeeperClient.get();
        zk.sync(logMetadata.getLogSegmentsPath(), new AsyncCallback.VoidCallback() {
            @Override
            public void processResult(int syncRc, String path, Object syncCtx) {
                if (KeeperException.Code.NONODE.intValue() == syncRc) {
                    promise.setException(new LogNotFoundException(String
                            .format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                    return;
                } else if (KeeperException.Code.OK.intValue() != syncRc) {
                    promise.setException(
                            new ZKException("Error on checking log existence for " + getFullyQualifiedName(),
                                    KeeperException.create(KeeperException.Code.get(syncRc))));
                    return;
                }
                zk.exists(logMetadata.getLogSegmentsPath(), false, new AsyncCallback.StatCallback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx, Stat stat) {
                        if (KeeperException.Code.OK.intValue() == rc) {
                            promise.setValue(null);
                        } else if (KeeperException.Code.NONODE.intValue() == rc) {
                            promise.setException(new LogNotFoundException(String.format(
                                    "Log %s does not exist or has been deleted", getFullyQualifiedName())));
                        } else {
                            promise.setException(new ZKException(
                                    "Error on checking log existence for " + getFullyQualifiedName(),
                                    KeeperException.create(KeeperException.Code.get(rc))));
                        }
                    }
                }, null);
            }
        }, null);

    } catch (InterruptedException ie) {
        LOG.error("Interrupted while reading {}", logMetadata.getLogSegmentsPath(), ie);
        promise.setException(new DLInterruptedException(
                "Interrupted while checking " + logMetadata.getLogSegmentsPath(), ie));
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    }
    return promise;
}

From source file:com.twitter.distributedlog.impl.ZKLogMetadataStore.java

License:Apache License

@Override
public Future<Iterator<String>> getLogs() {
    final Promise<Iterator<String>> promise = new Promise<Iterator<String>>();
    final String nsRootPath = namespace.getPath();
    try {//from  ww w .j a  va2s.co  m
        final ZooKeeper zk = zkc.get();
        zk.sync(nsRootPath, new AsyncCallback.VoidCallback() {
            @Override
            public void processResult(int syncRc, String syncPath, Object ctx) {
                if (KeeperException.Code.OK.intValue() == syncRc) {
                    zk.getChildren(nsRootPath, false, new AsyncCallback.Children2Callback() {
                        @Override
                        public void processResult(int rc, String path, Object ctx, List<String> children,
                                Stat stat) {
                            if (KeeperException.Code.OK.intValue() == rc) {
                                List<String> results = Lists.newArrayListWithExpectedSize(children.size());
                                for (String child : children) {
                                    if (!isReservedStreamName(child)) {
                                        results.add(child);
                                    }
                                }
                                promise.setValue(results.iterator());
                            } else if (KeeperException.Code.NONODE.intValue() == rc) {
                                List<String> streams = Lists.newLinkedList();
                                promise.setValue(streams.iterator());
                            } else {
                                promise.setException(new ZKException("Error reading namespace " + nsRootPath,
                                        KeeperException.Code.get(rc)));
                            }
                        }
                    }, null);
                } else if (KeeperException.Code.NONODE.intValue() == syncRc) {
                    List<String> streams = Lists.newLinkedList();
                    promise.setValue(streams.iterator());
                } else {
                    promise.setException(new ZKException("Error reading namespace " + nsRootPath,
                            KeeperException.Code.get(syncRc)));
                }
            }
        }, null);
        zkc.get();
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    }
    return promise;
}

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

License:Apache License

/**
 * Sync zookeeper client on given <i>path</i>.
 *
 * @param zkc//www  .ja v a 2  s. c  o m
 *          zookeeper client
 * @param path
 *          path to sync
 * @return zookeeper client after sync
 * @throws IOException
 */
public static ZooKeeper sync(ZooKeeperClient zkc, String path) throws IOException {
    ZooKeeper zk;
    try {
        zk = zkc.get();
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on checking if log " + path + " exists", e);
    }
    final CountDownLatch syncLatch = new CountDownLatch(1);
    final AtomicInteger syncResult = new AtomicInteger(0);
    zk.sync(path, new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            syncResult.set(rc);
            syncLatch.countDown();
        }
    }, null);
    try {
        syncLatch.await();
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on syncing zookeeper connection", e);
    }
    if (KeeperException.Code.OK.intValue() != syncResult.get()) {
        throw new ZKException("Error syncing zookeeper connection ",
                KeeperException.Code.get(syncResult.get()));
    }
    return zk;
}

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

License:Apache License

/**
 * Async get direct children under single node
 *
 * @param zk/*from  w w  w .  j  ava  2 s . c  o  m*/
 *          zookeeper client
 * @param node
 *          node path
 * @param cb
 *          callback function
 */
public static void getChildrenInSingleNode(final ZooKeeper zk, final String node,
        final GenericCallback<List<String>> cb) {
    zk.sync(node, new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (rc != Code.OK.intValue()) {
                LOG.error("ZK error syncing nodes when getting children: ",
                        KeeperException.create(KeeperException.Code.get(rc), path));
                cb.operationComplete(rc, null);
                return;
            }
            zk.getChildren(node, false, new AsyncCallback.ChildrenCallback() {
                @Override
                public void processResult(int rc, String path, Object ctx, List<String> nodes) {
                    if (rc != Code.OK.intValue()) {
                        LOG.error("Error polling ZK for the available nodes: ",
                                KeeperException.create(KeeperException.Code.get(rc), path));
                        cb.operationComplete(rc, null);
                        return;
                    }

                    cb.operationComplete(rc, nodes);

                }
            }, null);
        }
    }, null);
}

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

License:Apache License

@Override
public void sync(final String path, final VoidCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, syncStats) {

        final VoidCallback vCb = new VoidCallback() {

            @Override//  w ww  .j  ava2s .co  m
            public void processResult(int rc, String path, Object ctx) {
                BkZooWorker worker = (BkZooWorker) ctx;
                if (allowRetry(worker, rc)) {
                    backOffAndRetry(that, worker.nextRetryWaitTime());
                } else {
                    cb.processResult(rc, path, context);
                }
            }

        };

        @Override
        public String toString() {
            return String.format("sync (%s)", path);
        }

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                BkZooKeeperClient.super.sync(path, vCb, worker);
            } else {
                zkHandle.sync(path, vCb, worker);
            }
        }
    };
    // execute it immediately
    proc.run();
}

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

License:Apache License

@Override
public void sync(final String path, final VoidCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, syncStats) {

        final VoidCallback vCb = new VoidCallback() {

            @Override//from   ww  w.java2 s .  co m
            public void processResult(int rc, String path, Object ctx) {
                ZooWorker worker = (ZooWorker) ctx;
                if (allowRetry(worker, rc)) {
                    backOffAndRetry(that, worker.nextRetryWaitTime());
                } else {
                    cb.processResult(rc, path, context);
                }
            }

        };

        @Override
        public String toString() {
            return String.format("sync (%s)", path);
        }

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.sync(path, vCb, worker);
            } else {
                zkHandle.sync(path, vCb, worker);
            }
        }
    };
    // execute it immediately
    proc.run();
}