@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); } }
@Override public void register(final CuratorTransaction transaction) throws Exception { transaction .create().forPath(path).and() .setData().withVersion(version).forPath(path, data); }
.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();
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; } }
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();
@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); } }
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); } }
public void setData(String path, byte[] data, int version) throws Exception { transactionFinal = transactionFinal.setData() .withVersion(version).forPath(path, data).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 PathAndBytesable<CuratorTransactionBridge> withVersion(int version) { return new PathAndBytesableCuratorTransactionBridgeDecorator(inner.withVersion(version)); } }
@Override public VersionPathAndBytesable<CuratorTransactionBridge> compressed() { return new VersionPathAndBytesableCuratorTransactionBridgeDecorator(inner.compressed()); }
public static Collection<CuratorTransactionResult> transaction(CuratorFramework client) throws Exception { // // this example shows how to use ZooKeeper's new transactions // Collection<CuratorTransactionResult> results = client.inTransaction().create().forPath("/a/path", "some data".getBytes()) // .and().setData().forPath("/another/path", "other data".getBytes()) // .and().delete().forPath("/yet/another/path") // .and().commit(); // IMPORTANT! //inTransaction is deprecated. use transaction() instead List<CuratorTransactionResult> results = client.transaction().forOperations( client.transactionOp().create().forPath("/a/path", "some data".getBytes()), client.transactionOp().setData().forPath("/another/path", "other data".getBytes()), client.transactionOp().delete().forPath("/yet/another/path")); // called for (CuratorTransactionResult result : results) { System.out.println(result.getForPath() + " - " + result.getType()); } return results; } }
@Override public CuratorTransactionBridge forPath(String path) throws Exception { return new CuratorTransactionBridgeDecorator(inner.forPath(path)); }
private CuratorOp internalForPath(String path, byte[] data, boolean useData) { TransactionSetDataBuilder<CuratorOp> builder1 = client.transactionOp().setData(); VersionPathAndBytesable<CuratorOp> builder2 = compressed ? builder1.compressed() : builder1; PathAndBytesable<CuratorOp> builder3 = builder2.withVersion(version); try { return useData ? builder3.forPath(path, data) : builder3.forPath(path); } catch ( Exception e ) { throw new RuntimeException(e); // should never happen } } };
@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 RegExceptionHandler.handleException(ex); } }
@Override public void register(final CuratorTransaction transaction) throws Exception { transaction .create().forPath(path).and() .setData().withVersion(version).forPath(path, data); }