List of usage examples for org.apache.zookeeper Transaction commit
public List<OpResult> commit() throws InterruptedException, KeeperException
From source file:com.twitter.distributedlog.impl.federated.FederatedZKLogMetadataStore.java
License:Apache License
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 {/*from w ww. j a v a 2 s .com*/ 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); } }
From source file:com.twitter.distributedlog.impl.metadata.TestZKLogMetadataForWriter.java
License:Apache License
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,/*from ww w . ja v a2 s. c o m*/ 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(); }
From source file:org.apache.bookkeeper.test.ZooKeeperCluster.java
License:Apache License
default void createBKEnsemble(String ledgersPath) throws KeeperException, InterruptedException { Transaction txn = getZooKeeperClient().transaction(); txn.create(ledgersPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); txn.create(ledgersPath + "/" + AVAILABLE_NODE, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); txn.create(ledgersPath + "/" + AVAILABLE_NODE + "/" + READONLY, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//from w w w . ja v a 2 s. c o m txn.commit(); }
From source file:org.apache.distributedlog.impl.metadata.TestZKLogStreamMetadataStore.java
License:Apache License
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,//from w w w . j av a2 s . c o m 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, intToBytes(LAYOUT_VERSION), zk.getDefaultACL(), CreateMode.PERSISTENT); txn.create(allocationPath, DistributedLogConstants.EMPTY_BYTES, zk.getDefaultACL(), CreateMode.PERSISTENT); txn.commit(); }
From source file:org.apache.distributedlog.service.placement.ZKPlacementStateManager.java
License:Apache License
@Override public void saveOwnership(TreeSet<ServerLoad> serverLoads) throws StateManagerSaveException { logger.info("saving ownership"); try {// w ww . j ava 2 s . com ZooKeeper zk = zkClient.get(); // use timestamp as data so watchers will see any changes byte[] timestamp = ByteBuffer.allocate(8).putLong(System.currentTimeMillis()).array(); if (zk.exists(serverLoadPath, false) == null) { //create path to rootnode if it does not yet exist createServerLoadPathIfNoExists(timestamp); } Transaction tx = zk.transaction(); List<String> children = zk.getChildren(serverLoadPath, false); HashSet<String> servers = new HashSet<String>(children); tx.setData(serverLoadPath, timestamp, -1); // trigger the watcher that data has been updated for (ServerLoad serverLoad : serverLoads) { String server = serverToZkFormat(serverLoad.getServer()); String serverPath = serverPath(server); if (servers.contains(server)) { servers.remove(server); tx.setData(serverPath, serverLoad.serialize(), -1); } else { tx.create(serverPath, serverLoad.serialize(), zkClient.getDefaultACL(), CreateMode.PERSISTENT); } } for (String server : servers) { tx.delete(serverPath(server), -1); } tx.commit(); } catch (InterruptedException | IOException | KeeperException e) { throw new StateManagerSaveException(e); } }
From source file:org.jhk.pulsing.zookeeper.taskdistribution.MasterMimic.java
License:Apache License
/** * @param path /assign/worker-1/task-1-1 * @param data run xxx/*from ww w .j a v a2 s. c om*/ * @throws KeeperException * @throws InterruptedException */ private void assignTaskToWorker(String path, byte[] data) { // can use Transaction or zookeeper.multi with Op commands; however in nutshell needs it to be a single transaction // since assigning the task to a worker, must mean one creates the path and deletes the task from the task pool Transaction transaction = zookeeper.transaction(); transaction.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); transaction.delete(TASKS_PATH + "/" + path.substring(path.lastIndexOf("/") + 1), -1); try { _LOGGER.info("Task assign and delete transaction for path {}, result {}", path, transaction.commit()); } catch (InterruptedException | KeeperException exception) { _LOGGER.error("Error while assign and delete transaction ", exception); } /* zookeeper.multi(Arrays.asList( Op.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.delete(TASKS_PATH + "/" + path.substring( path.lastIndexOf("/") + 1), -1) ), ASSIGN_DELETE_TASK_CALLBACK, data); */ }