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());
}
}
}
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);
}
}
use of org.apache.zookeeper.OpResult in project distributedlog by twitter.
the class BKLogWriteHandler method writeLogSegment.
// Transactional operations for logsegment
void writeLogSegment(final ZKTransaction txn, final List<ACL> acl, final String inprogressSegmentName, final LogSegmentMetadata metadata, final String path) {
byte[] finalisedData = metadata.getFinalisedData().getBytes(UTF_8);
Op zkOp = Op.create(path, finalisedData, acl, CreateMode.PERSISTENT);
txn.addOp(new ZKOp(zkOp) {
@Override
protected void commitOpResult(OpResult opResult) {
addLogSegmentToCache(inprogressSegmentName, metadata);
}
@Override
protected void abortOpResult(Throwable t, OpResult opResult) {
// no-op
}
});
}
use of org.apache.zookeeper.OpResult in project distributedlog by twitter.
the class BKLogWriteHandler method deleteLogSegment.
void deleteLogSegment(final ZKTransaction txn, final String logSegmentName, final String logSegmentPath) {
Op zkOp = Op.delete(logSegmentPath, -1);
txn.addOp(new ZKOp(zkOp) {
@Override
protected void commitOpResult(OpResult opResult) {
removeLogSegmentFromCache(logSegmentName);
}
@Override
protected void abortOpResult(Throwable t, OpResult opResult) {
// no-op
}
});
}
use of org.apache.zookeeper.OpResult in project distributedlog by twitter.
the class FederatedZKLogMetadataStore method createLogInNamespaceSync.
void createLogInNamespaceSync(URI uri, String logName) throws InterruptedException, IOException, KeeperException {
Transaction txn = zkc.get().transaction();
// we don't have the zk version yet. set it to 0 instead of -1, to prevent non CAS operation.
int zkVersion = null == zkSubnamespacesVersion.get() ? 0 : zkSubnamespacesVersion.get();
txn.setData(zkSubnamespacesPath, uri.getPath().getBytes(UTF_8), zkVersion);
String logPath = uri.getPath() + "/" + logName;
txn.create(logPath, new byte[0], zkc.getDefaultACL(), CreateMode.PERSISTENT);
try {
txn.commit();
// if the transaction succeed, the zk version is advanced
setZkSubnamespacesVersion(zkVersion + 1);
} catch (KeeperException ke) {
List<OpResult> opResults = ke.getResults();
OpResult createResult = opResults.get(1);
if (createResult instanceof OpResult.ErrorResult) {
OpResult.ErrorResult errorResult = (OpResult.ErrorResult) createResult;
if (Code.NODEEXISTS.intValue() == errorResult.getErr()) {
throw new LogExistsException("Log " + logName + " already exists");
}
}
OpResult setResult = opResults.get(0);
if (setResult instanceof OpResult.ErrorResult) {
OpResult.ErrorResult errorResult = (OpResult.ErrorResult) setResult;
if (Code.BADVERSION.intValue() == errorResult.getErr()) {
throw KeeperException.create(Code.BADVERSION);
}
}
throw new ZKException("ZK exception in creating log " + logName + " in " + uri, ke);
}
}
Aggregations