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); } }
@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();
.aclProvider(aclProvider.get()).authorization("digest", ("fabric:" + options.getZookeeperPassword()).getBytes()).sessionTimeoutMs(30000) .connectionTimeoutMs((int) options.getMigrationTimeout()).build(); dst.start(); try { LOGGER.info("Waiting for ensemble {} to become ready.", newClusterId); if (!dst.getZookeeperClient().blockUntilConnectedOrTimedOut()) { throw new EnsembleModificationFailed("Timed out connecting to new ensemble.", EnsembleModificationFailed.Reason.TIMEOUT); setData(dst, ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword())); setData(dst, ZkPath.CONFIG_ENSEMBLE_URL.getPath(), connectionUrl); obtainValid(curator).inTransaction() .setData().forPath(ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword()).getBytes(Charsets.UTF_8)) .and() .setData().forPath(ZkPath.CONFIG_ENSEMBLE_URL.getPath(), connectionUrl.getBytes(Charsets.UTF_8)) .and().commit();
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); } }
try { CuratorTransactionFinal transaction = client .inTransaction() .check() .forPath(serviceRootPath) .and(); Set<String> pendingDeletePaths = new HashSet<>(); for (String child : client.getChildren().forPath(serviceRootPath)) { .delete().forPath(childPath).and(); transaction.commit(); } catch (Exception e) { // SUPPRESS CHECKSTYLE IllegalCatch throw new PersisterException(Reason.STORAGE_ERROR, client.delete().deletingChildrenIfNeeded().forPath(path); } catch (KeeperException.NoNodeException e) { throw new PersisterException(
private List<String> getChildren() throws Exception { final Stat childrenStat = new Stat(); while (true) { final List<String> possibleChildren = curator.getChildren() .storingStatIn(childrenStat) .usingWatcher(childrenWatcher) .forPath(path); if (clusterId == null) { // Do not do any checks if the clusterId is not specified on the command line. return possibleChildren; } try { curator.inTransaction() .check().forPath(Paths.configId(clusterId)).and() .check().withVersion(childrenStat.getVersion()).forPath(path).and() .commit(); } catch (KeeperException.BadVersionException e) { // Jobs have somehow changed while we were creating the transaction, retry. continue; } return possibleChildren; } }
@Override public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit(); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON CuratorZookeeperExceptionHandler.handleException(ex); } }
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); } } }
public CuratorTransactionFinal build( CuratorFramework client, String serviceRootPath ) throws Exception { // List of paths which are about to be deleted by the transaction Set<String> pendingDeletePaths = new HashSet<>(); CuratorTransactionFinal transaction = client .inTransaction() .check() .forPath(serviceRootPath) .and(); for (String path : pathsToClear) { // if present, delete path and any children (unless already being deleted) if (!pendingDeletePaths.contains(path) && client.checkExists().forPath(path) != null) { transaction = deleteChildrenOf(client, path, transaction, pendingDeletePaths) .delete().forPath(path).and(); pendingDeletePaths.add(path); } } return transaction; } }
/** * 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; }
@Override public void deleteRecursive(final String path) throws KeeperException { assertClusterIdFlagTrue(); try { final List<String> nodes = listRecursive(path); if (nodes.isEmpty()) { return; } final CuratorTransactionFinal t = client.inTransaction().check().forPath(path).and(); for (final String node : reverse(nodes)) { t.delete().forPath(node).and(); } t.commit(); } catch (Exception e) { throwIfInstanceOf(e, KeeperException.class); throw new RuntimeException(e); } }
/** * Build a transaction to atomically delete a directory tree. Package private for testing. * * @param zkClient connection to ZooKeeper. * @param tx recursive transaction being built up. * @param path current directory to delete. * @return a transaction to delete the directory tree. * @throws Exception on unrecoverable ZooKeeper error. */ static CuratorTransactionFinal buildAtomicRecursiveDelete( final CuratorFramework zkClient, final CuratorTransaction tx, final String path ) throws Exception { final List<String> children = zkClient.getChildren().forPath(path); for (String child : children) { buildAtomicRecursiveDelete(zkClient, tx, path + "/" + child); } return tx.delete().forPath(path).and(); }
for (String child : client.getChildren().forPath(path)) { String childPath = PersisterUtils.joinPaths(path, child); curatorTransactionFinal = curatorTransactionFinal.delete().forPath(childPath).and(); pendingDeletePaths.add(childPath);
/** * Unannounces an announcement created at path. Note that if all announcements get removed, the Announcer * will continue to have ZK watches on paths because clearing them out is a source of ugly race conditions. * <p/> * If you need to completely clear all the state of what is being watched and announced, stop() the Announcer. * * @param path the path to unannounce */ public void unannounce(String path) { log.info("unannouncing [%s]", path); final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path); final String parentPath = pathAndNode.getPath(); final ConcurrentMap<String, byte[]> subPaths = announcements.get(parentPath); if (subPaths == null || subPaths.remove(pathAndNode.getNode()) == null) { log.error("Path[%s] not announced, cannot unannounce.", path); return; } try { curator.inTransaction().delete().forPath(path).and().commit(); } catch (KeeperException.NoNodeException e) { log.info("node[%s] didn't exist anyway...", path); } catch (Exception e) { throw Throwables.propagate(e); } }
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(); LOG.info("Deleting {}", fullPath); pathsToDelete.add(fullPath); transaction.delete().forPath(fullPath).and(); transaction.create().forPath(addPath).and();
private synchronized SchemaEntry createNewSchema(String schema) { try { // TODO: Make IDs String across the board (not Integer), // TODO: Add pluggable ID generation schemes List<Integer> allSchemaIds = getSchemaIds(); Integer newId = getLatestSchemaId(allSchemaIds) + 1; allSchemaIds.add(newId); byte[] newSchemaFile = schema.getBytes(); byte[] newSchemaIdsFile = serializeSchemaIds(allSchemaIds).getBytes(); // Create new schema and update schema IDs file in one ZK transaction zkClient.inTransaction(). create().forPath(getSchemaFilePath(newId.toString()), newSchemaFile). and(). setData().forPath(getSchemaIdsFilePath(), newSchemaIdsFile). and().commit(); // TODO: Keep new schema in a local cache return new SchemaEntry(String.valueOf(newId), schema); } catch (Exception e) { throw new RuntimeException( "An exception occurred while accessing ZK!", e); } }
@Override public void write(StateMachineContext<S,E> context, Stat stat) { byte[] data = serialize(context); CuratorTransaction tx = curatorClient.inTransaction(); try { CuratorTransactionFinal tt = tx.setData().withVersion(stat.getVersion()).forPath(path, data).and(); if (logPath != null) { tt = tt.setData().forPath(logPath + "/" + stat.getVersion() % logSize, data).and(); } Collection<CuratorTransactionResult> results = tt.commit(); int version = results.iterator().next().getResultStat().getVersion(); stat.setVersion(version); } catch (Exception e) { throw new StateMachineException("Error persisting data", e); } }