Search in sources :

Example 1 with LogExistsException

use of org.apache.distributedlog.exceptions.LogExistsException 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 2 with LogExistsException

use of org.apache.distributedlog.exceptions.LogExistsException 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 3 with LogExistsException

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

the class FederatedZKLogMetadataStore method createLogInNamespaceSync.

void createLogInNamespaceSync(URI uri, String logName) throws InterruptedException, IOException, KeeperException {
    Transaction txn = zkc.get().transaction();
    // we don't have the zk version yet. set it to 0 instead of -1, to prevent non CAS operation.
    int zkVersion = null == zkSubnamespacesVersion.get() ? 0 : zkSubnamespacesVersion.get();
    txn.setData(zkSubnamespacesPath, uri.getPath().getBytes(UTF_8), zkVersion);
    String logPath = uri.getPath() + "/" + logName;
    txn.create(logPath, new byte[0], zkc.getDefaultACL(), CreateMode.PERSISTENT);
    try {
        txn.commit();
        // if the transaction succeed, the zk version is advanced
        setZkSubnamespacesVersion(zkVersion + 1);
    } catch (KeeperException ke) {
        List<OpResult> opResults = ke.getResults();
        OpResult createResult = opResults.get(1);
        if (createResult instanceof OpResult.ErrorResult) {
            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) createResult;
            if (Code.NODEEXISTS.intValue() == errorResult.getErr()) {
                throw new LogExistsException("Log " + logName + " already exists");
            }
        }
        OpResult setResult = opResults.get(0);
        if (setResult instanceof OpResult.ErrorResult) {
            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) setResult;
            if (Code.BADVERSION.intValue() == errorResult.getErr()) {
                throw KeeperException.create(Code.BADVERSION);
            }
        }
        throw new ZKException("ZK exception in creating log " + logName + " in " + uri, ke);
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogExistsException(org.apache.distributedlog.exceptions.LogExistsException) Transaction(org.apache.zookeeper.Transaction) OpResult(org.apache.zookeeper.OpResult) List(java.util.List) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

LogExistsException (org.apache.distributedlog.exceptions.LogExistsException)3 ZKException (org.apache.distributedlog.exceptions.ZKException)3 List (java.util.List)2 OpResult (org.apache.zookeeper.OpResult)2 LinkedList (java.util.LinkedList)1 LongVersion (org.apache.bookkeeper.versioning.LongVersion)1 Versioned (org.apache.bookkeeper.versioning.Versioned)1 ZooKeeperConnectionException (org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException)1 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)1 LockingException (org.apache.distributedlog.exceptions.LockingException)1 AsyncCallback (org.apache.zookeeper.AsyncCallback)1 KeeperException (org.apache.zookeeper.KeeperException)1 Op (org.apache.zookeeper.Op)1 Create (org.apache.zookeeper.Op.Create)1 Delete (org.apache.zookeeper.Op.Delete)1 Transaction (org.apache.zookeeper.Transaction)1