use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class CreateTTLTest method testMulti.
@Test
public void testMulti() throws KeeperException, InterruptedException {
Op createTtl = Op.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, 100);
Op createTtlSequential = Op.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 200);
Op createNonTtl = Op.create("/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
List<OpResult> results = zk.multi(Arrays.asList(createTtl, createTtlSequential, createNonTtl));
String sequentialPath = ((OpResult.CreateResult) results.get(1)).getPath();
final AtomicLong fakeElapsed = new AtomicLong(0);
ContainerManager containerManager = newContainerManager(fakeElapsed);
containerManager.checkContainers();
assertNotNull(zk.exists("/a", false), "node should not have been deleted yet");
assertNotNull(zk.exists(sequentialPath, false), "node should not have been deleted yet");
assertNotNull(zk.exists("/c", false), "node should never be deleted");
fakeElapsed.set(110);
containerManager.checkContainers();
assertNull(zk.exists("/a", false), "node should have been deleted");
assertNotNull(zk.exists(sequentialPath, false), "node should not have been deleted yet");
assertNotNull(zk.exists("/c", false), "node should never be deleted");
fakeElapsed.set(210);
containerManager.checkContainers();
assertNull(zk.exists("/a", false), "node should have been deleted");
assertNull(zk.exists(sequentialPath, false), "node should have been deleted");
assertNotNull(zk.exists("/c", false), "node should never be deleted");
}
use of org.apache.zookeeper.OpResult in project xian by happyyangyuan.
the class CuratorTransactionImpl method doOperation.
private List<OpResult> doOperation(AtomicBoolean firstTime) throws Exception {
boolean localFirstTime = firstTime.getAndSet(false);
if (!localFirstTime) {
}
List<OpResult> opResults = client.getZooKeeper().multi(transaction);
if (opResults.size() > 0) {
OpResult firstResult = opResults.get(0);
if (firstResult.getType() == ZooDefs.OpCode.error) {
OpResult.ErrorResult error = (OpResult.ErrorResult) firstResult;
KeeperException.Code code = KeeperException.Code.get(error.getErr());
if (code == null) {
code = KeeperException.Code.UNIMPLEMENTED;
}
throw KeeperException.create(code);
}
}
return opResults;
}
use of org.apache.zookeeper.OpResult 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);
}
use of org.apache.zookeeper.OpResult in project bookkeeper by apache.
the class TestZKVersionedSetOp method testAbortOpResult.
@Test(timeout = 60000)
public void testAbortOpResult() throws Exception {
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1);
ZKVersionedSetOp versionedSetOp = new ZKVersionedSetOp(mock(Op.class), new Transaction.OpListener<Version>() {
@Override
public void onCommit(Version r) {
// no-op
}
@Override
public void onAbort(Throwable t) {
exception.set(t);
latch.countDown();
}
});
KeeperException ke = KeeperException.create(KeeperException.Code.SESSIONEXPIRED);
OpResult opResult = new OpResult.ErrorResult(KeeperException.Code.NONODE.intValue());
versionedSetOp.abortOpResult(ke, opResult);
latch.await();
assertTrue(exception.get() instanceof KeeperException.NoNodeException);
}
use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class MultiOperationTest method multi.
private List<OpResult> multi(ZooKeeper zk, Iterable<Op> ops, 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) {
if (!ClientCnxn.isInEventThread()) {
throw new RuntimeException("not in event thread");
}
synchronized (res) {
res.rc = rc;
res.results = opResults;
res.finished = true;
res.notifyAll();
}
}
}, null);
synchronized (res) {
while (!res.finished) {
res.wait();
}
}
// In case of only OpKind.READ operations, no exception is thrown. Errors only marked in form of ErrorResults.
if (KeeperException.Code.OK.intValue() != res.rc && ops.iterator().next().getKind() != Op.OpKind.READ) {
KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
throw ke;
}
return res.results;
} else {
return zk.multi(ops);
}
}
Aggregations