use of org.apache.zookeeper.OpResult.ErrorResult in project zookeeper by apache.
the class ZooKeeper method multiInternal.
protected List<OpResult> multiInternal(MultiTransactionRecord request) throws InterruptedException, KeeperException {
RequestHeader h = new RequestHeader();
h.setType(ZooDefs.OpCode.multi);
MultiResponse response = new MultiResponse();
ReplyHeader r = cnxn.submitRequest(h, request, response, null);
if (r.getErr() != 0) {
throw KeeperException.create(KeeperException.Code.get(r.getErr()));
}
List<OpResult> results = response.getResultList();
ErrorResult fatalError = null;
for (OpResult result : results) {
if (result instanceof ErrorResult && ((ErrorResult) result).getErr() != KeeperException.Code.OK.intValue()) {
fatalError = (ErrorResult) result;
break;
}
}
if (fatalError != null) {
KeeperException ex = KeeperException.create(KeeperException.Code.get(fatalError.getErr()));
ex.setMultiResults(results);
throw ex;
}
return results;
}
use of org.apache.zookeeper.OpResult.ErrorResult 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()));
}
}
}
use of org.apache.zookeeper.OpResult.ErrorResult 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());
}
}
}
use of org.apache.zookeeper.OpResult.ErrorResult in project zookeeper by apache.
the class MultiTransactionTest method testOpResultEquals.
/**
* Exercise the equals methods of OpResult classes.
*/
@Test
public void testOpResultEquals() {
opEquals(new CreateResult("/foo"), new CreateResult("/foo"), new CreateResult("nope"));
opEquals(new CreateResult("/foo"), new CreateResult("/foo"), new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)));
opEquals(new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("nope", new Stat(11, 12, 13, 14, 15, 16, 17, 18, 19, 110, 111)));
opEquals(new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("/foo"));
opEquals(new CheckResult(), new CheckResult(), null);
opEquals(new SetDataResult(new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new SetDataResult(new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new SetDataResult(new Stat(11, 12, 13, 14, 15, 16, 17, 18, 19, 110, 111)));
opEquals(new ErrorResult(1), new ErrorResult(1), new ErrorResult(2));
opEquals(new DeleteResult(), new DeleteResult(), null);
opEquals(new ErrorResult(1), new ErrorResult(1), new ErrorResult(2));
}
use of org.apache.zookeeper.OpResult.ErrorResult 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");
}
}
}
Aggregations