use of org.apache.zookeeper.Transaction in project zookeeper by apache.
the class MultiTransactionTest method testChRootTransaction.
@Test
public void testChRootTransaction() throws Exception {
// creating the subtree for chRoot clients.
String chRoot = createNameSpace();
// checking the child version using chRoot client.
zk_chroot = createClient(this.hostPort + chRoot);
String childPath = "/myid";
Transaction transaction = zk_chroot.transaction();
transaction.create(childPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
transaction.check(childPath, 0);
transaction.setData(childPath, childPath.getBytes(), 0);
commit(transaction);
Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk.exists(chRoot + childPath, false));
Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk_chroot.exists(childPath, false));
Assert.assertNull("zNode is created directly under '/', ignored configured chroot", zk.exists(childPath, false));
Assert.assertArrayEquals("zNode data not matching", childPath.getBytes(), zk_chroot.getData(childPath, false, null));
transaction = zk_chroot.transaction();
// Deleting child using chRoot client.
transaction.delete(childPath, 1);
commit(transaction);
Assert.assertNull("chroot:" + chRoot + " exists after delete", zk.exists(chRoot + "/myid", false));
Assert.assertNull("chroot:" + chRoot + " exists after delete", zk_chroot.exists("/myid", false));
}
use of org.apache.zookeeper.Transaction 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);
}
}
use of org.apache.zookeeper.Transaction in project zookeeper by apache.
the class ReadOnlyModeTest method testMultiTransaction.
/**
* Test write operations using multi request.
*/
@Test(timeout = 90000)
public void testMultiTransaction() throws Exception {
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
// ensure zk got connected
watcher.waitForConnected(CONNECTION_TIMEOUT);
final String data = "Data to be read in RO mode";
final String node1 = "/tnode1";
final String node2 = "/tnode2";
zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
watcher.reset();
qu.shutdown(2);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY, zk.getState());
// read operation during r/o mode
String remoteData = new String(zk.getData(node1, false, null));
Assert.assertEquals("Failed to read data in r-o mode", data, remoteData);
try {
Transaction transaction = zk.transaction();
transaction.setData(node1, "no way".getBytes(), -1);
transaction.create(node2, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
transaction.commit();
Assert.fail("Write operation using multi-transaction" + " api has succeeded during RO mode");
} catch (NotReadOnlyException e) {
// ok
}
Assert.assertNull("Should have created the znode:" + node2, zk.exists(node2, false));
}
use of org.apache.zookeeper.Transaction in project distributedlog by twitter.
the class TestZKLogMetadataForWriter method createLog.
private static void createLog(ZooKeeperClient zk, URI uri, String logName, String logIdentifier) throws Exception {
final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
final String lockPath = logRootPath + LOCK_PATH;
final String readLockPath = logRootPath + READ_LOCK_PATH;
final String versionPath = logRootPath + VERSION_PATH;
final String allocationPath = logRootPath + ALLOCATION_PATH;
Utils.zkCreateFullPathOptimistic(zk, logRootPath, new byte[0], zk.getDefaultACL(), CreateMode.PERSISTENT);
Transaction txn = zk.get().transaction();
txn.create(logSegmentsPath, DLUtils.serializeLogSegmentSequenceNumber(DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO), zk.getDefaultACL(), CreateMode.PERSISTENT);
txn.create(maxTxIdPath, DLUtils.serializeTransactionId(0L), zk.getDefaultACL(), CreateMode.PERSISTENT);
txn.create(lockPath, DistributedLogConstants.EMPTY_BYTES, zk.getDefaultACL(), CreateMode.PERSISTENT);
txn.create(readLockPath, DistributedLogConstants.EMPTY_BYTES, zk.getDefaultACL(), CreateMode.PERSISTENT);
txn.create(versionPath, ZKLogMetadataForWriter.intToBytes(LAYOUT_VERSION), zk.getDefaultACL(), CreateMode.PERSISTENT);
txn.create(allocationPath, DistributedLogConstants.EMPTY_BYTES, zk.getDefaultACL(), CreateMode.PERSISTENT);
txn.commit();
}
Aggregations