Java Code Examples for org.apache.zookeeper.ZooKeeper#multi()

The following examples show how to use org.apache.zookeeper.ZooKeeper#multi() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZKLogStreamMetadataStore.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private static void executeCreateMissingPathTxn(ZooKeeper zk,
                                                List<Op> zkOps,
                                                List<byte[]> pathsToCreate,
                                                List<Versioned<byte[]>> metadatas,
                                                String logRootPath,
                                                CompletableFuture<List<Versioned<byte[]>>> promise) {

    zk.multi(zkOps, new AsyncCallback.MultiCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, List<OpResult> resultList) {
            if (KeeperException.Code.OK.intValue() == rc) {
                List<Versioned<byte[]>> finalMetadatas =
                        Lists.newArrayListWithExpectedSize(metadatas.size());
                for (int i = 0; i < pathsToCreate.size(); i++) {
                    byte[] dataCreated = pathsToCreate.get(i);
                    if (null == dataCreated) {
                        finalMetadatas.add(metadatas.get(i));
                    } else {
                        finalMetadatas.add(new Versioned<byte[]>(dataCreated, new LongVersion(0)));
                    }
                }
                promise.complete(finalMetadatas);
            } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                promise.completeExceptionally(new LogExistsException("Someone just created log "
                        + logRootPath));
            } else {
                if (LOG.isDebugEnabled()) {
                    StringBuilder builder = new StringBuilder();
                    for (OpResult result : resultList) {
                        if (result instanceof OpResult.ErrorResult) {
                            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) result;
                            builder.append(errorResult.getErr()).append(",");
                        } else {
                            builder.append(0).append(",");
                        }
                    }
                    String resultCodeList = builder.substring(0, builder.length() - 1);
                    LOG.debug("Failed to create log, full rc list = {}", resultCodeList);
                }

                promise.completeExceptionally(new ZKException("Failed to create log " + logRootPath,
                        KeeperException.Code.get(rc)));
            }
        }
    }, null);
}