Search in sources :

Example 1 with CuratorTransactionResult

use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.

the class TestTransactions method testBasic.

@Test
public void testBasic() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        Collection<CuratorTransactionResult> results = client.inTransaction().create().forPath("/foo").and().create().forPath("/foo/bar", "snafu".getBytes()).and().commit();
        Assert.assertTrue(client.checkExists().forPath("/foo/bar") != null);
        Assert.assertEquals(client.getData().forPath("/foo/bar"), "snafu".getBytes());
        CuratorTransactionResult fooResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/foo"));
        CuratorTransactionResult fooBarResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/foo/bar"));
        Assert.assertNotNull(fooResult);
        Assert.assertNotNull(fooBarResult);
        Assert.assertNotSame(fooResult, fooBarResult);
        Assert.assertEquals(fooResult.getResultPath(), "/foo");
        Assert.assertEquals(fooBarResult.getResultPath(), "/foo/bar");
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult) Test(org.testng.annotations.Test)

Example 2 with CuratorTransactionResult

use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.

the class TestTransactions method testWithCompression.

@Test
public void testWithCompression() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace("galt").build();
    client.start();
    try {
        Collection<CuratorTransactionResult> results = client.inTransaction().create().compressed().forPath("/foo", "one".getBytes()).and().create().compressed().withACL(ZooDefs.Ids.READ_ACL_UNSAFE).forPath("/bar", "two".getBytes()).and().create().compressed().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/test-", "three".getBytes()).and().create().compressed().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.READ_ACL_UNSAFE).forPath("/baz", "four".getBytes()).and().setData().compressed().withVersion(0).forPath("/foo", "five".getBytes()).and().commit();
        Assert.assertTrue(client.checkExists().forPath("/foo") != null);
        Assert.assertEquals(client.getData().decompressed().forPath("/foo"), "five".getBytes());
        Assert.assertTrue(client.checkExists().forPath("/bar") != null);
        Assert.assertEquals(client.getData().decompressed().forPath("/bar"), "two".getBytes());
        Assert.assertEquals(client.getACL().forPath("/bar"), ZooDefs.Ids.READ_ACL_UNSAFE);
        CuratorTransactionResult ephemeralResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
        Assert.assertNotNull(ephemeralResult);
        Assert.assertNotEquals(ephemeralResult.getResultPath(), "/test-");
        Assert.assertTrue(ephemeralResult.getResultPath().startsWith("/test-"));
        Assert.assertTrue(client.checkExists().forPath("/baz") != null);
        Assert.assertEquals(client.getData().decompressed().forPath("/baz"), "four".getBytes());
        Assert.assertEquals(client.getACL().forPath("/baz"), ZooDefs.Ids.READ_ACL_UNSAFE);
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult) Test(org.testng.annotations.Test)

Example 3 with CuratorTransactionResult

use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.

the class CuratorTransactionImpl method commit.

@Override
public Collection<CuratorTransactionResult> commit() throws Exception {
    Preconditions.checkState(!isCommitted, "transaction already committed");
    isCommitted = true;
    final AtomicBoolean firstTime = new AtomicBoolean(true);
    List<OpResult> resultList = RetryLoop.callWithRetry(client.getZookeeperClient(), new Callable<List<OpResult>>() {

        @Override
        public List<OpResult> call() throws Exception {
            return doOperation(firstTime);
        }
    });
    if (resultList.size() != transaction.metadataSize()) {
        throw new IllegalStateException(String.format("Result size (%d) doesn't match input size (%d)", resultList.size(), transaction.metadataSize()));
    }
    ImmutableList.Builder<CuratorTransactionResult> builder = ImmutableList.builder();
    for (int i = 0; i < resultList.size(); ++i) {
        OpResult opResult = resultList.get(i);
        CuratorMultiTransactionRecord.TypeAndPath metadata = transaction.getMetadata(i);
        CuratorTransactionResult curatorResult = makeCuratorResult(opResult, metadata);
        builder.add(curatorResult);
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) KeeperException(org.apache.zookeeper.KeeperException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OpResult(org.apache.zookeeper.OpResult) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult)

Example 4 with CuratorTransactionResult

use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.

the class TestTransactions method testWithNamespace.

@Test
public void testWithNamespace() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace("galt").build();
    client.start();
    try {
        Collection<CuratorTransactionResult> results = client.inTransaction().create().forPath("/foo", "one".getBytes()).and().create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/test-", "one".getBytes()).and().setData().forPath("/foo", "two".getBytes()).and().create().forPath("/foo/bar").and().delete().forPath("/foo/bar").and().commit();
        Assert.assertTrue(client.checkExists().forPath("/foo") != null);
        Assert.assertTrue(client.usingNamespace(null).checkExists().forPath("/galt/foo") != null);
        Assert.assertEquals(client.getData().forPath("/foo"), "two".getBytes());
        Assert.assertTrue(client.checkExists().forPath("/foo/bar") == null);
        CuratorTransactionResult ephemeralResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
        Assert.assertNotNull(ephemeralResult);
        Assert.assertNotEquals(ephemeralResult.getResultPath(), "/test-");
        Assert.assertTrue(ephemeralResult.getResultPath().startsWith("/test-"));
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult) Test(org.testng.annotations.Test)

Example 5 with CuratorTransactionResult

use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.

the class CuratorTransactionImpl method makeCuratorResult.

private CuratorTransactionResult makeCuratorResult(OpResult opResult, CuratorMultiTransactionRecord.TypeAndPath metadata) {
    String resultPath = null;
    Stat resultStat = null;
    switch(opResult.getType()) {
        default:
            {
                // NOP
                break;
            }
        case ZooDefs.OpCode.create:
            {
                OpResult.CreateResult createResult = (OpResult.CreateResult) opResult;
                resultPath = client.unfixForNamespace(createResult.getPath());
                break;
            }
        case ZooDefs.OpCode.setData:
            {
                OpResult.SetDataResult setDataResult = (OpResult.SetDataResult) opResult;
                resultStat = setDataResult.getStat();
                break;
            }
    }
    return new CuratorTransactionResult(metadata.type, metadata.forPath, resultPath, resultStat);
}
Also used : Stat(org.apache.zookeeper.data.Stat) OpResult(org.apache.zookeeper.OpResult) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult)

Aggregations

CuratorTransactionResult (org.apache.curator.framework.api.transaction.CuratorTransactionResult)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 RetryOneTime (org.apache.curator.retry.RetryOneTime)3 Test (org.testng.annotations.Test)3 OpResult (org.apache.zookeeper.OpResult)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)1 CuratorListener (org.apache.curator.framework.api.CuratorListener)1 CuratorOp (org.apache.curator.framework.api.transaction.CuratorOp)1 KeeperException (org.apache.zookeeper.KeeperException)1 Stat (org.apache.zookeeper.data.Stat)1 Test (org.junit.Test)1