Search in sources :

Example 16 with OpResult

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

the class MultiOperationTest method commit.

private List<OpResult> commit(Transaction txn, boolean useAsync) 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 17 with OpResult

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

the class MultiOperationTest method testMultiRead.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testMultiRead(boolean useAsync) throws Exception {
    zk.create("/node1", "data1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/node2", "data2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    zk.create("/node1/node1", "data11".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/node1/node2", "data12".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    List<OpResult> multiRead = multi(zk, Arrays.asList(Op.getChildren("/node1"), Op.getData("/node1"), Op.getChildren("/node2"), Op.getData("/node2")), useAsync);
    assertEquals(multiRead.size(), 4);
    assertTrue(multiRead.get(0) instanceof OpResult.GetChildrenResult);
    List<String> childrenList = ((OpResult.GetChildrenResult) multiRead.get(0)).getChildren();
    assertEquals(childrenList.size(), 2);
    assertEquals(new TreeSet<String>(childrenList), new TreeSet<String>(Arrays.asList("node1", "node2")));
    assertArrayEquals(((OpResult.GetDataResult) multiRead.get(1)).getData(), "data1".getBytes());
    Stat stat = ((OpResult.GetDataResult) multiRead.get(1)).getStat();
    assertEquals(stat.getMzxid(), stat.getCzxid());
    assertEquals(stat.getCtime(), stat.getMtime());
    assertEquals(2, stat.getCversion());
    assertEquals(0, stat.getVersion());
    assertEquals(0, stat.getAversion());
    assertEquals(0, stat.getEphemeralOwner());
    assertEquals(5, stat.getDataLength());
    assertEquals(2, stat.getNumChildren());
    assertTrue(multiRead.get(2) instanceof OpResult.GetChildrenResult);
    childrenList = ((OpResult.GetChildrenResult) multiRead.get(2)).getChildren();
    assertTrue(childrenList.isEmpty());
    assertArrayEquals(((OpResult.GetDataResult) multiRead.get(3)).getData(), "data2".getBytes());
    stat = ((OpResult.GetDataResult) multiRead.get(3)).getStat();
    assertEquals(stat.getMzxid(), stat.getCzxid());
    assertEquals(stat.getMzxid(), stat.getPzxid());
    assertEquals(stat.getCtime(), stat.getMtime());
    assertEquals(0, stat.getCversion());
    assertEquals(0, stat.getVersion());
    assertEquals(0, stat.getAversion());
    assertEquals(zk.getSessionId(), stat.getEphemeralOwner());
    assertEquals(5, stat.getDataLength());
    assertEquals(0, stat.getNumChildren());
}
Also used : Stat(org.apache.zookeeper.data.Stat) OpResult(org.apache.zookeeper.OpResult) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with OpResult

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

the class MultiOperationTest method testMultiGetChildrenAuthentication.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testMultiGetChildrenAuthentication(boolean useAsync) throws KeeperException, InterruptedException {
    List<ACL> writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone")));
    zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT);
    // Check for normal behaviour.
    List<OpResult> multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_auth")), useAsync);
    assertEquals(multiChildrenList.size(), 1);
    assertTrue(multiChildrenList.get(0) instanceof OpResult.GetChildrenResult);
    List<String> childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(0)).getChildren();
    assertEquals(childrenList.size(), 1);
    assertEquals(childrenList.get(0), "bar");
    // Check for authentication violation.
    multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_no_auth")), useAsync);
    assertEquals(multiChildrenList.size(), 1);
    assertTrue(multiChildrenList.get(0) instanceof OpResult.ErrorResult);
    assertEquals(((OpResult.ErrorResult) multiChildrenList.get(0)).getErr(), KeeperException.Code.NOAUTH.intValue(), "Expected NoAuthException for getting the children of a write only node");
}
Also used : OpResult(org.apache.zookeeper.OpResult) ErrorResult(org.apache.zookeeper.OpResult.ErrorResult) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with OpResult

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

the class MultiOperationTest method testMultiGetChildrenMixedAuthenticationCorrectFirst.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testMultiGetChildrenMixedAuthenticationCorrectFirst(boolean useAsync) throws KeeperException, InterruptedException {
    List<ACL> writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone")));
    zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT);
    // Check for getting the children of the nodes with mixed authentication.
    // The getChildren operation returns GetChildrenResult if it happened before the error.
    List<OpResult> multiChildrenList;
    multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_auth"), Op.getChildren("/foo_no_auth")), useAsync);
    assertSame(multiChildrenList.size(), 2);
    assertTrue(multiChildrenList.get(0) instanceof OpResult.GetChildrenResult);
    List<String> childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(0)).getChildren();
    assertEquals(childrenList.size(), 1);
    assertEquals(childrenList.get(0), "bar");
    assertTrue(multiChildrenList.get(1) instanceof OpResult.ErrorResult);
    assertEquals(((OpResult.ErrorResult) multiChildrenList.get(1)).getErr(), KeeperException.Code.NOAUTH.intValue(), "Expected NoAuthException for getting the children of a write only node");
}
Also used : OpResult(org.apache.zookeeper.OpResult) ErrorResult(org.apache.zookeeper.OpResult.ErrorResult) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with OpResult

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

the class MultiOperationTest method multiHavingErrors.

private void multiHavingErrors(ZooKeeper zk, Iterable<Op> ops, List<Integer> expectedResultCodes, String expectedErr, boolean useAsync) 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);
            assertTrue(opResult instanceof ErrorResult, "Did't receive proper error response");
            ErrorResult errRes = (ErrorResult) opResult;
            assertEquals(expectedResultCodes.get(i).intValue(), errRes.getErr(), "Did't receive proper error code");
        }
    } else {
        try {
            zk.multi(ops);
            fail("Shouldn't have validated in ZooKeeper client!");
        } catch (KeeperException e) {
            assertEquals(expectedErr, e.code().name(), "Wrong exception");
        } catch (IllegalArgumentException e) {
            assertEquals(expectedErr, e.getMessage(), "Wrong exception");
        }
    }
}
Also used : OpResult(org.apache.zookeeper.OpResult) ErrorResult(org.apache.zookeeper.OpResult.ErrorResult) 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