@Override public boolean apply(CuratorTransactionResult result) { return (result.getType() == type) && result.getForPath().equals(forPath); } };
Assert.assertEquals(1, results.size()); final CuratorTransactionResult result = results.iterator().next(); Assert.assertEquals(Code.OK.intValue(), result.getError()); // assert delete
return new CuratorTransactionResult(metadata.getType(), metadata.getForPath(), resultPath, resultStat, error);
@Timed public QueuedRequestId enqueueRequest(BaragonRequest request, InternalRequestStates state) throws NodeExistsException { final long start = System.currentTimeMillis(); final String queuedRequestPath = String.format(REQUEST_ENQUEUE_FORMAT, request.getLoadBalancerService().getServiceId(), request.getLoadBalancerRequestId()); final String requestPath = String.format(REQUEST_FORMAT, request.getLoadBalancerRequestId()); final String requestStatePath = String.format(REQUEST_STATE_FORMAT, request.getLoadBalancerRequestId()); try { if (!nodeExists(REQUESTS_FORMAT)) { createNode(REQUESTS_FORMAT); } if (!nodeExists(REQUEST_QUEUE_FORMAT)) { createNode(REQUEST_QUEUE_FORMAT); } byte[] requestBytes = objectMapper.writeValueAsBytes(request); byte[] stateBytes = objectMapper.writeValueAsBytes(state); Collection<CuratorTransactionResult> results = curatorFramework.inTransaction() .create().forPath(requestPath, requestBytes).and() .create().forPath(requestStatePath, stateBytes).and() .create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(queuedRequestPath) .and().commit(); log(OperationType.WRITE, Optional.of(3), Optional.of(requestBytes.length + stateBytes.length), start, String.format("Transaction Paths [%s + %s + %s]", requestPath, requestStatePath, queuedRequestPath)); return QueuedRequestId.fromString(ZKPaths.getNodeFromPath(Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(org.apache.curator.framework.api.transaction.OperationType.CREATE, queuedRequestPath)) .getResultPath())); } catch (NodeExistsException nee) { throw nee; } catch (Exception e) { throw new RuntimeException(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); } }
private CuratorTransactionResult makeCuratorResult(OpResult opResult, CuratorMultiTransactionRecord.TypeAndPath metadata) { String resultPath = null; Stat resultStat = null; switch ( opResult.getType() ) { default: { // NOP break; } case ZooDefs.OpCode.create: { OpResult.CreateResult createResult = (OpResult.CreateResult)opResult; resultPath = client.unfixForNamespace(createResult.getPath()); break; } case ZooDefs.OpCode.setData: { OpResult.SetDataResult setDataResult = (OpResult.SetDataResult)opResult; resultStat = setDataResult.getStat(); break; } } return new CuratorTransactionResult(metadata.type, metadata.forPath, resultPath, resultStat); } }
@Override public boolean apply(CuratorTransactionResult result) { return (result.getType() == type) && result.getForPath().equals(forPath); } };
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; } }
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; } }