private void register(String stringType, String value) { try { //can not switch the order of sysStat and appStat.because of the zk sequential consistency Stat sysStat = client.checkExists().creatingParentContainersIfNeeded().forPath(getSystemTypePath(stringType)); Stat appStringStat = client.checkExists().forPath(getAppBaseItemPath(stringType, value)); if(appStringStat != null) { return; } if(sysStat == null) { createNodeIfNotExists(getSystemTypePath(stringType)); } createNodeIfNotExists(getAppTypePath(stringType)); int numChildren = 0; if(sysStat != null) { numChildren = sysStat.getNumChildren(); } client.inTransaction() .create().forPath(getSystemBaseItemPath(stringType, numChildren + 1),value.getBytes()).and() .create().forPath(getAppBaseItemPath(stringType, value),ByteBuffer.allocate(4).putInt(numChildren + 1).array()).and() .commit(); } catch (Exception e) { LOG.warn("register string in zk failed", e); } }
SafeTransaction(List<ACL> fencingACL, String fencingNodePath) throws Exception { this.fencingNodePath = fencingNodePath; CuratorTransaction transaction = curator.inTransaction(); transactionFinal = transaction.create() .withMode(CreateMode.PERSISTENT).withACL(fencingACL) .forPath(fencingNodePath, new byte[0]).and(); }
@Test(timeout = 60_000L) public void testSessionKilled() throws Exception curator.start(); curator.blockUntilConnected(); Announcer announcer = new Announcer(curator, exec); try { curator.inTransaction().create().forPath("/somewhere").and().commit(); announcer.start();
try client.create().withMode(CreateMode.EPHEMERAL).forPath(lockNodePath); lockCreated = true; if ( type == ProcessType.NORMAL ) bytes = client.getData().forPath(itemPath); requeue = (processMessageBytes(itemNode, bytes) == ProcessMessageBytesCode.REQUEUE); client.inTransaction() .delete().forPath(itemPath) .and() .create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(makeRequeueItemPath(itemPath), bytes) .and() .commit();
if (curatorClient.checkExists().forPath(memberPath) != null) { if (curatorClient.getChildren().forPath(memberPath).size() == 0) { log.info("Deleting from " + baseDataPath); curatorClient.delete().deletingChildrenIfNeeded().forPath(baseDataPath); if (curatorClient.checkExists().forPath(baseDataPath) == null) { CuratorTransaction tx = curatorClient.inTransaction(); CuratorTransactionFinal tt = tx.create().forPath(baseDataPath).and(); tt = tt.create().forPath(statePath).and(); tt = tt.create().forPath(logPath).and(); for (int i = 0; i<logSize; i++) { tt = tt.create().forPath(logPath + "/" + i).and(); tt.commit();
public void addFinishedApplication(String appId, String queue, String yarnState, String yarnStatus, String user, String name) { String path = zkRoot + "/jobs/" + appId; try { if (curator.checkExists().forPath(path) != null) { curator.delete().deletingChildrenIfNeeded().forPath(path); } name = name.replace("/", "_"); if (name.length() > 50) { name = name.substring(0, 50); } CuratorTransactionBridge result = curator.inTransaction().create().withMode(CreateMode.PERSISTENT).forPath(path, ZKStateConstant.AppStatus.INIT.toString().getBytes("UTF-8")); result = result.and().create().withMode(CreateMode.PERSISTENT).forPath(path + "/info", String.format("%s/%s/%s/%s/%s", queue, yarnState, yarnStatus, user, name).getBytes("UTF-8")); result.and().commit(); } catch (Exception e) { LOG.error("fail adding finished application", e); throw new RuntimeException(e); } }
public CuratorTransactionFinal build( CuratorFramework client, String serviceRootPath ) throws Exception { // List of paths that are known to exist, or which are about to be created by the transaction // Includes "known to exist" in order to avoid repeated lookups for the same path Set<String> existingAndPendingCreatePaths = new HashSet<>(); CuratorTransactionFinal transaction = client .inTransaction() .check() .forPath(serviceRootPath) .and(); for (Map.Entry<String, byte[]> entry : pathBytesMap.entrySet()) { String path = entry.getKey(); if (!existingAndPendingCreatePaths.contains(path) && client.checkExists().forPath(path) == null) { // Path does not exist and is not being created: Create value (and any parents as needed). transaction = createParentsOf(client, path, transaction, existingAndPendingCreatePaths) .create().forPath(path, entry.getValue()).and(); existingAndPendingCreatePaths.add(path); } else { // Path exists (or will exist): Update existing value. transaction = transaction.setData().forPath(path, entry.getValue()).and(); } } return transaction; } }
@Override public void applyMigration() { try { for (String service : baragonStateDatastore.getServices()) { Collection<String> upstreams = curatorFramework.getChildren().forPath(ZKPaths.makePath(BaragonStateDatastore.SERVICES_FORMAT, service)); for (String upstream : upstreams) { Optional<UpstreamInfo> maybeUpstream = baragonStateDatastore.getUpstreamInfo(service, upstream); UpstreamInfo mergedInfo; if (maybeUpstream.isPresent()) { UpstreamInfo fromPath = UpstreamInfo.fromString(upstream); mergedInfo = new UpstreamInfo(fromPath.getUpstream(), maybeUpstream.get().getRequestId().or(fromPath.getRequestId()), maybeUpstream.get().getRackId().or(fromPath.getRackId())); } else { mergedInfo = UpstreamInfo.fromString(upstream); } curatorFramework.inTransaction() .delete().forPath(String.format(BaragonStateDatastore.UPSTREAM_FORMAT, service, upstream)).and() .create().forPath(String.format(BaragonStateDatastore.UPSTREAM_FORMAT, service, mergedInfo.toPath())).and() .commit(); } } } catch (Exception e) { Throwables.propagate(e); } } }
/** * Updates and returns a transaction which can be used to create missing parents of the provided * path, if any. */ private static CuratorTransactionFinal createParentsOf( CuratorFramework client, String path, CuratorTransactionFinal curatorTransactionFinal, Set<String> existingAndPendingCreatePaths) throws Exception { for (String parentPath : PersisterUtils.getParentPaths(path)) { if (!existingAndPendingCreatePaths.contains(parentPath) && client.checkExists().forPath(parentPath) == null) { // SUPPRESS CHECKSTYLE ParameterAssignment curatorTransactionFinal = curatorTransactionFinal.create().forPath(parentPath).and(); } existingAndPendingCreatePaths.add(parentPath); } return curatorTransactionFinal; }
CuratorTransactionFinal transaction; if (nodeExists(servicePath) && !request.isUpstreamUpdateOnly()) { transaction = curatorFramework.inTransaction().setData().forPath(servicePath, serialize(request.getLoadBalancerService())).and(); } else { transaction = curatorFramework.inTransaction().create().forPath(servicePath, serialize(request.getLoadBalancerService())).and(); LOG.info("Deleting {}", fullPath); pathsToDelete.add(fullPath); transaction.delete().forPath(fullPath).and(); String addPath = String.format(UPSTREAM_FORMAT, serviceId, upstreamInfo.toPath()); if (!nodeExists(addPath) || pathsToDelete.contains(addPath)) { transaction.create().forPath(addPath).and(); LOG.info("Deleting {}", fullPath); pathsToDelete.add(fullPath); transaction.delete().forPath(fullPath).and(); transaction.create().forPath(addPath).and();
@Override public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception { for (Map.Entry<JobInstance, List<Integer>> entry : shardingResults.entrySet()) { for (int shardingItem : entry.getValue()) { curatorTransactionFinal.create().forPath(jobNodePath.getFullPath(ShardingNode.getInstanceNode(shardingItem)), entry.getKey().getJobInstanceId().getBytes()).and(); } } curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.NECESSARY)).and(); curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.PROCESSING)).and(); } }
public static Collection<CuratorTransactionResult> transaction(CuratorFramework client) throws Exception { // this example shows how to use ZooKeeper's transactions CuratorOp createOp = client.transactionOp().create().forPath("/a/path", "some data".getBytes()); CuratorOp setDataOp = client.transactionOp().setData().forPath("/another/path", "other data".getBytes()); CuratorOp deleteOp = client.transactionOp().delete().forPath("/yet/another/path"); Collection<CuratorTransactionResult> results = client.transaction().forOperations(createOp, setDataOp, deleteOp); for ( CuratorTransactionResult result : results ) { System.out.println(result.getForPath() + " - " + result.getType()); } return results; } }
@Override public CuratorTransactionBridge forPath(String path, byte[] data) throws Exception { return new CuratorTransactionBridgeDecorator(inner.forPath(path, data)); }
@Override public ACLPathAndBytesable<CuratorTransactionBridge> withMode(CreateMode mode) { return new ACLPathAndBytesableDecorator<>(inner.withMode(mode)); }
@Override public PathAndBytesable<CuratorTransactionBridge> withACL(List<ACL> aclList) { return new PathAndBytesableDecorator<>(inner.withACL(aclList)); }
@Override public ACLCreateModePathAndBytesable<CuratorTransactionBridge> compressed() { return new ACLCreateModePathAndBytesableDecorator<>(inner.compressed()); }