Search in sources :

Example 6 with OpResult

use of org.apache.zookeeper.OpResult in project zookeeper by apache.

the class MultiTransactionTest method multiHavingErrors.

private void multiHavingErrors(ZooKeeper zk, Iterable<Op> ops, List<Integer> expectedResultCodes, String expectedErr) throws KeeperException, InterruptedException {
    if (useAsync) {
        final MultiResult res = new MultiResult();
        zk.multi(ops, new MultiCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, List<OpResult> opResults) {
                synchronized (res) {
                    res.rc = rc;
                    res.results = opResults;
                    res.finished = true;
                    res.notifyAll();
                }
            }
        }, null);
        synchronized (res) {
            while (!res.finished) {
                res.wait();
            }
        }
        for (int i = 0; i < res.results.size(); i++) {
            OpResult opResult = res.results.get(i);
            Assert.assertTrue("Did't recieve proper error response", opResult instanceof ErrorResult);
            ErrorResult errRes = (ErrorResult) opResult;
            Assert.assertEquals("Did't recieve proper error code", expectedResultCodes.get(i).intValue(), errRes.getErr());
        }
    } else {
        try {
            zk.multi(ops);
            Assert.fail("Shouldn't have validated in ZooKeeper client!");
        } catch (KeeperException e) {
            Assert.assertEquals("Wrong exception", expectedErr, e.code().name());
        } catch (IllegalArgumentException e) {
            Assert.assertEquals("Wrong exception", expectedErr, e.getMessage());
        }
    }
}
Also used : OpResult(org.apache.zookeeper.OpResult) ErrorResult(org.apache.zookeeper.OpResult.ErrorResult) MultiCallback(org.apache.zookeeper.AsyncCallback.MultiCallback) KeeperException(org.apache.zookeeper.KeeperException)

Example 7 with OpResult

use of org.apache.zookeeper.OpResult in project zookeeper by apache.

the class MultiTransactionTest method multi.

private List<OpResult> multi(ZooKeeper zk, Iterable<Op> ops) throws KeeperException, InterruptedException {
    if (useAsync) {
        final MultiResult res = new MultiResult();
        zk.multi(ops, new MultiCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, List<OpResult> opResults) {
                synchronized (res) {
                    res.rc = rc;
                    res.results = opResults;
                    res.finished = true;
                    res.notifyAll();
                }
            }
        }, null);
        synchronized (res) {
            while (!res.finished) {
                res.wait();
            }
        }
        if (KeeperException.Code.OK.intValue() != res.rc) {
            KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
            throw ke;
        }
        return res.results;
    } else {
        return zk.multi(ops);
    }
}
Also used : OpResult(org.apache.zookeeper.OpResult) MultiCallback(org.apache.zookeeper.AsyncCallback.MultiCallback) KeeperException(org.apache.zookeeper.KeeperException)

Example 8 with OpResult

use of org.apache.zookeeper.OpResult in project distributedlog by twitter.

the class BKLogWriteHandler method writeLogSegment.

// Transactional operations for logsegment
void writeLogSegment(final ZKTransaction txn, final List<ACL> acl, final String inprogressSegmentName, final LogSegmentMetadata metadata, final String path) {
    byte[] finalisedData = metadata.getFinalisedData().getBytes(UTF_8);
    Op zkOp = Op.create(path, finalisedData, acl, CreateMode.PERSISTENT);
    txn.addOp(new ZKOp(zkOp) {

        @Override
        protected void commitOpResult(OpResult opResult) {
            addLogSegmentToCache(inprogressSegmentName, metadata);
        }

        @Override
        protected void abortOpResult(Throwable t, OpResult opResult) {
        // no-op
        }
    });
}
Also used : Op(org.apache.zookeeper.Op) ZKVersionedSetOp(com.twitter.distributedlog.zk.ZKVersionedSetOp) ZKOp(com.twitter.distributedlog.zk.ZKOp) OpResult(org.apache.zookeeper.OpResult) ZKOp(com.twitter.distributedlog.zk.ZKOp)

Example 9 with OpResult

use of org.apache.zookeeper.OpResult in project distributedlog by twitter.

the class BKLogWriteHandler method deleteLogSegment.

void deleteLogSegment(final ZKTransaction txn, final String logSegmentName, final String logSegmentPath) {
    Op zkOp = Op.delete(logSegmentPath, -1);
    txn.addOp(new ZKOp(zkOp) {

        @Override
        protected void commitOpResult(OpResult opResult) {
            removeLogSegmentFromCache(logSegmentName);
        }

        @Override
        protected void abortOpResult(Throwable t, OpResult opResult) {
        // no-op
        }
    });
}
Also used : Op(org.apache.zookeeper.Op) ZKVersionedSetOp(com.twitter.distributedlog.zk.ZKVersionedSetOp) ZKOp(com.twitter.distributedlog.zk.ZKOp) OpResult(org.apache.zookeeper.OpResult) ZKOp(com.twitter.distributedlog.zk.ZKOp)

Example 10 with OpResult

use of org.apache.zookeeper.OpResult in project distributedlog by twitter.

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(com.twitter.distributedlog.exceptions.ZKException) LogExistsException(com.twitter.distributedlog.exceptions.LogExistsException) Transaction(org.apache.zookeeper.Transaction) OpResult(org.apache.zookeeper.OpResult) List(java.util.List) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

OpResult (org.apache.zookeeper.OpResult)14 KeeperException (org.apache.zookeeper.KeeperException)8 Op (org.apache.zookeeper.Op)7 MultiCallback (org.apache.zookeeper.AsyncCallback.MultiCallback)5 Test (org.junit.Test)4 List (java.util.List)3 CreateResult (org.apache.zookeeper.OpResult.CreateResult)3 ErrorResult (org.apache.zookeeper.OpResult.ErrorResult)3 LogExistsException (com.twitter.distributedlog.exceptions.LogExistsException)2 ZKException (com.twitter.distributedlog.exceptions.ZKException)2 ZKOp (com.twitter.distributedlog.zk.ZKOp)2 ZKVersionedSetOp (com.twitter.distributedlog.zk.ZKVersionedSetOp)2 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)2 CheckResult (org.apache.zookeeper.OpResult.CheckResult)2 DeleteResult (org.apache.zookeeper.OpResult.DeleteResult)2 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)1 Transaction (com.twitter.distributedlog.util.Transaction)1 File (java.io.File)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1