Search in sources :

Example 1 with OpResult

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

the class MultiTransactionTest method testTransactionBuilder.

@Test
public void testTransactionBuilder() throws Exception {
    List<OpResult> results = commit(zk.transaction().create("/t1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).create("/t1/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).create("/t2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
    assertEquals(3, results.size());
    for (OpResult r : results) {
        CreateResult c = (CreateResult) r;
        assertTrue(c.getPath().startsWith("/t"));
        assertNotNull(c.toString());
    }
    assertNotNull(zk.exists("/t1", false));
    assertNotNull(zk.exists("/t1/child", false));
    assertNotNull(zk.exists("/t2", false));
    results = commit(zk.transaction().check("/t1", 0).check("/t1/child", 0).check("/t2", 0));
    assertEquals(3, results.size());
    for (OpResult r : results) {
        CheckResult c = (CheckResult) r;
        assertNotNull(c.toString());
    }
    try {
        results = commit(zk.transaction().check("/t1", 0).check("/t1/child", 0).check("/t2", 1));
        fail();
    } catch (KeeperException.BadVersionException e) {
    // expected
    }
    results = commit(zk.transaction().check("/t1", 0).setData("/t1", new byte[0], 0));
    assertEquals(2, results.size());
    for (OpResult r : results) {
        assertNotNull(r.toString());
    }
    try {
        results = commit(zk.transaction().check("/t1", 1).setData("/t1", new byte[0], 2));
        fail();
    } catch (KeeperException.BadVersionException e) {
    // expected
    }
    results = commit(zk.transaction().check("/t1", 1).check("/t1/child", 0).check("/t2", 0));
    assertEquals(3, results.size());
    results = commit(zk.transaction().delete("/t2", -1).delete("/t1/child", -1));
    assertEquals(2, results.size());
    for (OpResult r : results) {
        DeleteResult d = (DeleteResult) r;
        assertNotNull(d.toString());
    }
    assertNotNull(zk.exists("/t1", false));
    assertNull(zk.exists("/t1/child", false));
    assertNull(zk.exists("/t2", false));
}
Also used : CreateResult(org.apache.zookeeper.OpResult.CreateResult) OpResult(org.apache.zookeeper.OpResult) CheckResult(org.apache.zookeeper.OpResult.CheckResult) KeeperException(org.apache.zookeeper.KeeperException) DeleteResult(org.apache.zookeeper.OpResult.DeleteResult) Test(org.junit.Test)

Example 2 with OpResult

use of org.apache.zookeeper.OpResult 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 OpResult

use of org.apache.zookeeper.OpResult 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 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 5 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)

Aggregations

OpResult (org.apache.zookeeper.OpResult)34 KeeperException (org.apache.zookeeper.KeeperException)21 Op (org.apache.zookeeper.Op)13 MultiCallback (org.apache.zookeeper.AsyncCallback.MultiCallback)10 ErrorResult (org.apache.zookeeper.OpResult.ErrorResult)8 List (java.util.List)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 ValueSource (org.junit.jupiter.params.provider.ValueSource)6 IOException (java.io.IOException)4 CreateResult (org.apache.zookeeper.OpResult.CreateResult)4 ACL (org.apache.zookeeper.data.ACL)4 Id (org.apache.zookeeper.data.Id)4 Test (org.junit.Test)4 DataStorageManagerException (herddb.storage.DataStorageManagerException)3 CheckResult (org.apache.zookeeper.OpResult.CheckResult)3 DeleteResult (org.apache.zookeeper.OpResult.DeleteResult)3 Stat (org.apache.zookeeper.data.Stat)3 LogExistsException (com.twitter.distributedlog.exceptions.LogExistsException)2 ZKException (com.twitter.distributedlog.exceptions.ZKException)2 ZKOp (com.twitter.distributedlog.zk.ZKOp)2