Search in sources :

Example 1 with MultiCallback

use of org.apache.zookeeper.AsyncCallback.MultiCallback in project zookeeper by apache.

the class MultiAsyncTransactionTest method testSequentialNodeCreateInAsyncMulti.

/**
     * ZOOKEEPER-1624: PendingChanges of create sequential node request didn't
     * get rollbacked correctly when multi-op failed. This cause
     * create sequential node request in subsequent multi-op to failed because
     * sequential node name generation is incorrect.
     *
     * The check is to make sure that each request in multi-op failed with
     * the correct reason.
     */
@Test
public void testSequentialNodeCreateInAsyncMulti() throws Exception {
    final int iteration = 4;
    final List<MultiResult> results = new ArrayList<MultiResult>();
    pendingOps.set(iteration);
    List<Op> ops = Arrays.asList(Op.create("/node-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL), Op.create("/dup", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
    for (int i = 0; i < iteration; ++i) {
        zk.multi(ops, new MultiCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, List<OpResult> opResults) {
                MultiResult result = new MultiResult();
                result.results = opResults;
                result.rc = rc;
                results.add(result);
                finishPendingOps();
            }
        }, null);
    }
    waitForPendingOps(CONNECTION_TIMEOUT);
    // Check that return code of all request are correct
    assertEquals(KeeperException.Code.OK.intValue(), results.get(0).rc);
    assertEquals(KeeperException.Code.NODEEXISTS.intValue(), results.get(1).rc);
    assertEquals(KeeperException.Code.NODEEXISTS.intValue(), results.get(2).rc);
    assertEquals(KeeperException.Code.NODEEXISTS.intValue(), results.get(3).rc);
    // Check that the first operation is successful in all request
    assertTrue(results.get(0).results.get(0) instanceof CreateResult);
    assertEquals(KeeperException.Code.OK.intValue(), ((ErrorResult) results.get(1).results.get(0)).getErr());
    assertEquals(KeeperException.Code.OK.intValue(), ((ErrorResult) results.get(2).results.get(0)).getErr());
    assertEquals(KeeperException.Code.OK.intValue(), ((ErrorResult) results.get(3).results.get(0)).getErr());
    // Check that the second operation failed after the first request
    assertEquals(KeeperException.Code.NODEEXISTS.intValue(), ((ErrorResult) results.get(1).results.get(1)).getErr());
    assertEquals(KeeperException.Code.NODEEXISTS.intValue(), ((ErrorResult) results.get(2).results.get(1)).getErr());
    assertEquals(KeeperException.Code.NODEEXISTS.intValue(), ((ErrorResult) results.get(3).results.get(1)).getErr());
}
Also used : Op(org.apache.zookeeper.Op) ArrayList(java.util.ArrayList) CreateResult(org.apache.zookeeper.OpResult.CreateResult) OpResult(org.apache.zookeeper.OpResult) MultiCallback(org.apache.zookeeper.AsyncCallback.MultiCallback) Test(org.junit.Test)

Example 2 with MultiCallback

use of org.apache.zookeeper.AsyncCallback.MultiCallback in project zookeeper by apache.

the class MultiTransactionTest method commit.

private List<OpResult> commit(Transaction txn) throws KeeperException, InterruptedException {
    if (useAsync) {
        final MultiResult res = new MultiResult();
        txn.commit(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 txn.commit();
    }
}
Also used : OpResult(org.apache.zookeeper.OpResult) MultiCallback(org.apache.zookeeper.AsyncCallback.MultiCallback) KeeperException(org.apache.zookeeper.KeeperException)

Example 3 with MultiCallback

use of org.apache.zookeeper.AsyncCallback.MultiCallback in project zookeeper by apache.

the class MultiTransactionTest method testGetResults.

@Test
public void testGetResults() throws Exception {
    /* Delete of a node folowed by an update of the (now) deleted node */
    Iterable<Op> ops = Arrays.asList(Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.delete("/multi", 0), Op.setData("/multi", "Y".getBytes(), 0), Op.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
    List<OpResult> results = null;
    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();
            }
        }
        Assert.assertFalse("/multi should have been deleted so setData should have failed", KeeperException.Code.OK.intValue() == res.rc);
        Assert.assertNull(zk.exists("/multi", null));
        results = res.results;
    } else {
        try {
            zk.multi(ops);
            Assert.fail("/multi should have been deleted so setData should have failed");
        } catch (KeeperException e) {
            // '/multi' should never have been created as entire op should fail
            Assert.assertNull(zk.exists("/multi", null));
            results = e.getResults();
        }
    }
    Assert.assertNotNull(results);
    for (OpResult r : results) {
        LOG.info("RESULT==> {}", r);
        if (r instanceof ErrorResult) {
            ErrorResult er = (ErrorResult) r;
            LOG.info("ERROR RESULT: {} ERR=>{}", er, KeeperException.Code.get(er.getErr()));
        }
    }
}
Also used : Op(org.apache.zookeeper.Op) OpResult(org.apache.zookeeper.OpResult) ErrorResult(org.apache.zookeeper.OpResult.ErrorResult) MultiCallback(org.apache.zookeeper.AsyncCallback.MultiCallback) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 4 with MultiCallback

use of org.apache.zookeeper.AsyncCallback.MultiCallback 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 5 with MultiCallback

use of org.apache.zookeeper.AsyncCallback.MultiCallback 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)

Aggregations

MultiCallback (org.apache.zookeeper.AsyncCallback.MultiCallback)5 OpResult (org.apache.zookeeper.OpResult)5 KeeperException (org.apache.zookeeper.KeeperException)4 Op (org.apache.zookeeper.Op)2 ErrorResult (org.apache.zookeeper.OpResult.ErrorResult)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 CreateResult (org.apache.zookeeper.OpResult.CreateResult)1