Search in sources :

Example 1 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class DLMetadata method create.

public void create(URI uri) throws IOException {
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder().sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds()).retryThreadCount(conf.getZKClientNumberRetryThreads()).requestRateLimit(conf.getZKRequestRateLimit()).zkAclId(conf.getZkAclId()).uri(uri).build();
    byte[] data = serialize();
    try {
        Utils.zkCreateFullPathOptimistic(zkc, uri.getPath(), data, zkc.getDefaultACL(), CreateMode.PERSISTENT);
    } catch (KeeperException ke) {
        throw new ZKException("Encountered zookeeper exception on creating dl metadata", ke);
    } finally {
        zkc.close();
    }
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) ZKException(org.apache.distributedlog.exceptions.ZKException) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method getLogSegments.

@VisibleForTesting
static CompletableFuture<List<LogSegmentMetadata>> getLogSegments(ZooKeeperClient zk, String logSegmentsPath) {
    CompletableFuture<List<LogSegmentMetadata>> future = FutureUtils.createFuture();
    try {
        zk.get().getChildren(logSegmentsPath, false, (rc, path, ctx, children, stat) -> {
            if (Code.OK.intValue() != rc) {
                if (Code.NONODE.intValue() == rc) {
                    future.completeExceptionally(new LogNotFoundException("Log " + path + " not found"));
                } else {
                    future.completeExceptionally(new ZKException("Failed to get log segments from " + path, Code.get(rc)));
                }
                return;
            }
            // get all the segments
            List<CompletableFuture<LogSegmentMetadata>> futures = Lists.newArrayListWithExpectedSize(children.size());
            for (String child : children) {
                futures.add(LogSegmentMetadata.read(zk, logSegmentsPath + "/" + child));
            }
            FutureUtils.proxyTo(FutureUtils.collect(futures), future);
        }, null);
    } catch (ZooKeeperConnectionException e) {
        future.completeExceptionally(e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        future.completeExceptionally(e);
    }
    return future;
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) LinkedList(java.util.LinkedList) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method executeCreateMissingPathTxn.

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);
}
Also used : LogExistsException(org.apache.distributedlog.exceptions.LogExistsException) Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZKException(org.apache.distributedlog.exceptions.ZKException) OpResult(org.apache.zookeeper.OpResult) LongVersion(org.apache.bookkeeper.versioning.LongVersion) List(java.util.List) LinkedList(java.util.LinkedList)

Example 4 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method executeRenameTxn.

private CompletableFuture<Void> executeRenameTxn(String oldLogPath, String newLogPath, LinkedList<Op> createOps, LinkedList<Op> deleteOps) {
    CompletableFuture<Void> future = FutureUtils.createFuture();
    List<Op> zkOps = Lists.newArrayListWithExpectedSize(createOps.size() + deleteOps.size());
    zkOps.addAll(createOps);
    zkOps.addAll(deleteOps);
    if (LOG.isDebugEnabled()) {
        for (Op op : zkOps) {
            if (op instanceof Create) {
                Create create = (Create) op;
                LOG.debug("op : create {}", create.getPath());
            } else if (op instanceof Delete) {
                Delete delete = (Delete) op;
                LOG.debug("op : delete {}, record = {}", delete.getPath(), op.toRequestRecord());
            } else {
                LOG.debug("op : {}", op);
            }
        }
    }
    try {
        zooKeeperClient.get().multi(zkOps, (rc, path, ctx, opResults) -> {
            if (Code.OK.intValue() == rc) {
                future.complete(null);
            } else if (Code.NODEEXISTS.intValue() == rc) {
                future.completeExceptionally(new LogExistsException("Someone just created new log " + newLogPath));
            } else if (Code.NOTEMPTY.intValue() == rc) {
                future.completeExceptionally(new LockingException(oldLogPath + LOCK_PATH, "Someone is holding a lock on log " + oldLogPath));
            } else {
                future.completeExceptionally(new ZKException("Failed to rename log " + oldLogPath + " to " + newLogPath + " at path " + path, Code.get(rc)));
            }
        }, null);
    } catch (ZooKeeperConnectionException e) {
        future.completeExceptionally(e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        future.completeExceptionally(e);
    }
    return future;
}
Also used : Delete(org.apache.zookeeper.Op.Delete) Op(org.apache.zookeeper.Op) LockingException(org.apache.distributedlog.exceptions.LockingException) ZKException(org.apache.distributedlog.exceptions.ZKException) LogExistsException(org.apache.distributedlog.exceptions.LogExistsException) Create(org.apache.zookeeper.Op.Create) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException)

Example 5 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method logExists.

@Override
public CompletableFuture<Void> logExists(URI uri, final String logName) {
    final String logSegmentsPath = LogMetadata.getLogSegmentsPath(uri, logName, conf.getUnpartitionedStreamName());
    final CompletableFuture<Void> promise = new CompletableFuture<Void>();
    try {
        final ZooKeeper zk = zooKeeperClient.get();
        zk.sync(logSegmentsPath, new AsyncCallback.VoidCallback() {

            @Override
            public void processResult(int syncRc, String path, Object syncCtx) {
                if (KeeperException.Code.NONODE.intValue() == syncRc) {
                    promise.completeExceptionally(new LogNotFoundException(String.format("Log %s does not exist or has been deleted", logName)));
                    return;
                } else if (KeeperException.Code.OK.intValue() != syncRc) {
                    promise.completeExceptionally(new ZKException("Error on checking log existence for " + logName, KeeperException.create(KeeperException.Code.get(syncRc))));
                    return;
                }
                zk.exists(logSegmentsPath, false, new AsyncCallback.StatCallback() {

                    @Override
                    public void processResult(int rc, String path, Object ctx, Stat stat) {
                        if (KeeperException.Code.OK.intValue() == rc) {
                            promise.complete(null);
                        } else if (KeeperException.Code.NONODE.intValue() == rc) {
                            promise.completeExceptionally(new LogNotFoundException(String.format("Log %s does not exist or has been deleted", logName)));
                        } else {
                            promise.completeExceptionally(new ZKException("Error on checking log existence for " + logName, KeeperException.create(KeeperException.Code.get(rc))));
                        }
                    }
                }, null);
            }
        }, null);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        LOG.error("Interrupted while reading {}", logSegmentsPath, ie);
        promise.completeExceptionally(new DLInterruptedException("Interrupted while checking " + logSegmentsPath, ie));
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.completeExceptionally(e);
    }
    return promise;
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) CompletableFuture(java.util.concurrent.CompletableFuture) ZKException(org.apache.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Aggregations

ZKException (org.apache.distributedlog.exceptions.ZKException)31 Test (org.junit.Test)15 CompletableFuture (java.util.concurrent.CompletableFuture)8 LongVersion (org.apache.bookkeeper.versioning.LongVersion)8 Versioned (org.apache.bookkeeper.versioning.Versioned)8 Stat (org.apache.zookeeper.data.Stat)7 List (java.util.List)6 ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)5 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)4 Version (org.apache.bookkeeper.versioning.Version)4 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)4 ZKTransaction (org.apache.distributedlog.zk.ZKTransaction)4 KeeperException (org.apache.zookeeper.KeeperException)4 ZooKeeperConnectionException (org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException)3 LogExistsException (org.apache.distributedlog.exceptions.LogExistsException)3 LogNotFoundException (org.apache.distributedlog.exceptions.LogNotFoundException)3 Transaction (org.apache.distributedlog.util.Transaction)3 AsyncCallback (org.apache.zookeeper.AsyncCallback)3