org.apache.zookeeper.OpResult Java Examples

The following examples show how to use org.apache.zookeeper.OpResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestZKVersionedSetOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testAbortOpResult() throws Exception {
    final AtomicReference<Throwable> exception =
            new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    ZKVersionedSetOp versionedSetOp =
            new ZKVersionedSetOp(mock(Op.class), new Transaction.OpListener<Version>() {
                @Override
                public void onCommit(Version r) {
                    // no-op
                }

                @Override
                public void onAbort(Throwable t) {
                    exception.set(t);
                    latch.countDown();
                }
            });
    KeeperException ke = KeeperException.create(KeeperException.Code.SESSIONEXPIRED);
    OpResult opResult = new OpResult.ErrorResult(KeeperException.Code.NONODE.intValue());
    versionedSetOp.abortOpResult(ke, opResult);
    latch.await();
    assertTrue(exception.get() instanceof KeeperException.NoNodeException);
}
 
Example #2
Source File: ZKVersionedSetOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
protected void abortOpResult(Throwable t,
                             @Nullable OpResult opResult) {
    Throwable cause;
    if (null == opResult) {
        cause = t;
    } else {
        assert (opResult instanceof OpResult.ErrorResult);
        OpResult.ErrorResult errorResult = (OpResult.ErrorResult) opResult;
        if (KeeperException.Code.OK.intValue() == errorResult.getErr()) {
            cause = t;
        } else {
            cause = KeeperException.create(KeeperException.Code.get(errorResult.getErr()));
        }
    }
    listener.onAbort(cause);
}
 
Example #3
Source File: TestZKVersionedSetOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testAbortOpResult() throws Exception {
    final AtomicReference<Throwable> exception =
            new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    ZKVersionedSetOp versionedSetOp =
            new ZKVersionedSetOp(mock(Op.class), new Transaction.OpListener<Version>() {
                @Override
                public void onCommit(Version r) {
                    // no-op
                }

                @Override
                public void onAbort(Throwable t) {
                    exception.set(t);
                    latch.countDown();
                }
            });
    KeeperException ke = KeeperException.create(KeeperException.Code.SESSIONEXPIRED);
    OpResult opResult = new OpResult.ErrorResult(KeeperException.Code.NONODE.intValue());
    versionedSetOp.abortOpResult(ke, opResult);
    latch.await();
    assertTrue(exception.get() instanceof KeeperException.NoNodeException);
}
 
Example #4
Source File: RecoverableZooKeeper.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Run multiple operations in a transactional manner. Retry before throwing exception
 */
public List<OpResult> multi(Iterable<Op> ops)
  throws KeeperException, InterruptedException {
  try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.multi")) {
    RetryCounter retryCounter = retryCounterFactory.create();
    Iterable<Op> multiOps = prepareZKMulti(ops);
    while (true) {
      try {
        return checkZk().multi(multiOps);
      } catch (KeeperException e) {
        switch (e.code()) {
          case CONNECTIONLOSS:
            retryOrThrow(retryCounter, e, "multi");
            break;
          case OPERATIONTIMEOUT:
            retryOrThrow(retryCounter, e, "multi");
            break;

          default:
            throw e;
        }
      }
      retryCounter.sleepUntilNextRetry();
    }
  }
}
 
Example #5
Source File: CuratorMultiTransactionImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
@Override
public void performBackgroundOperation(final OperationAndData<CuratorMultiTransactionRecord> operationAndData) throws Exception
{
    try
    {
        final TimeTrace trace = client.getZookeeperClient().startTracer("CuratorMultiTransactionImpl-Background");
        AsyncCallback.MultiCallback callback = new AsyncCallback.MultiCallback()
        {
            @Override
            public void processResult(int rc, String path, Object ctx, List<OpResult> opResults)
            {
                trace.commit();
                List<CuratorTransactionResult> curatorResults = (opResults != null) ? CuratorTransactionImpl.wrapResults(client, opResults, operationAndData.getData()) : null;
                CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.TRANSACTION, rc, path, null, ctx, null, null, null, null, null, curatorResults);
                client.processBackgroundOperation(operationAndData, event);
            }
        };
        client.getZooKeeper().multi(operationAndData.getData(), callback, backgrounding.getContext());
    }
    catch ( Throwable e )
    {
        backgrounding.checkError(e, null);
    }
}
 
Example #6
Source File: CuratorMultiTransactionImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
private List<CuratorTransactionResult> forOperationsInForeground(final CuratorMultiTransactionRecord record) throws Exception
{
    TimeTrace trace = client.getZookeeperClient().startTracer("CuratorMultiTransactionImpl-Foreground");
    List<OpResult> responseData = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<List<OpResult>>()
        {
            @Override
            public List<OpResult> call() throws Exception
            {
                return client.getZooKeeper().multi(record);
            }
        }
    );
    trace.commit();

    return CuratorTransactionImpl.wrapResults(client, responseData, record);
}
 
Example #7
Source File: CuratorTransactionImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<CuratorTransactionResult> commit() throws Exception
{
    Preconditions.checkState(!isCommitted, "transaction already committed");
    isCommitted = true;

    List<OpResult> resultList = RetryLoop.callWithRetry
        (
            client.getZookeeperClient(),
            new Callable<List<OpResult>>()
            {
                @Override
                public List<OpResult> call() throws Exception
                {
                    return doOperation();
                }
            }
        );

    if ( resultList.size() != transaction.metadataSize() )
    {
        throw new IllegalStateException(String.format("Result size (%d) doesn't match input size (%d)", resultList.size(), transaction.metadataSize()));
    }

    return wrapResults(client, resultList, transaction);
}
 
Example #8
Source File: ZKVersionedSetOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
protected void abortOpResult(Throwable t,
                             @Nullable OpResult opResult) {
    Throwable cause;
    if (null == opResult) {
        cause = t;
    } else {
        assert (opResult instanceof OpResult.ErrorResult);
        OpResult.ErrorResult errorResult = (OpResult.ErrorResult) opResult;
        if (KeeperException.Code.OK.intValue() == errorResult.getErr()) {
            cause = t;
        } else {
            cause = KeeperException.create(KeeperException.Code.get(errorResult.getErr()));
        }
    }
    if (null != listener) {
        listener.onAbort(cause);
    }
}
 
Example #9
Source File: ZKTransaction.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> results) {
    if (KeeperException.Code.OK.intValue() == rc) { // transaction succeed
        for (int i = 0; i < ops.size(); i++) {
            ops.get(i).commitOpResult(results.get(i));
        }
        FutureUtils.complete(result, null);
    } else {
        KeeperException ke = KeeperException.create(KeeperException.Code.get(rc));
        for (int i = 0; i < ops.size(); i++) {
            ops.get(i).abortOpResult(ke, null != results ? results.get(i) : null);
        }
        FutureUtils.completeExceptionally(result, ke);
    }
}
 
Example #10
Source File: ZKTransaction.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> results) {
    if (KeeperException.Code.OK.intValue() == rc) { // transaction succeed
        for (int i = 0; i < ops.size(); i++) {
            ops.get(i).commitOpResult(results.get(i));
        }
        FutureUtils.setValue(result, null);
    } else {
        KeeperException ke = KeeperException.create(KeeperException.Code.get(rc));
        for (int i = 0; i < ops.size(); i++) {
            ops.get(i).abortOpResult(ke, null != results ? results.get(i) : null);
        }
        FutureUtils.setException(result, ke);
    }
}
 
Example #11
Source File: CuratorTransactionImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
static List<CuratorTransactionResult> wrapResults(CuratorFrameworkImpl client, List<OpResult> resultList, CuratorMultiTransactionRecord transaction)
{
    ImmutableList.Builder<CuratorTransactionResult> builder = ImmutableList.builder();
    for ( int i = 0; i < resultList.size(); ++i )
    {
        OpResult opResult = resultList.get(i);
        TypeAndPath metadata = transaction.getMetadata(i);
        CuratorTransactionResult curatorResult = makeCuratorResult(client, opResult, metadata);
        builder.add(curatorResult);
    }

    return builder.build();
}
 
Example #12
Source File: CuratorTransactionImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
static CuratorTransactionResult makeCuratorResult(CuratorFrameworkImpl client, OpResult opResult, TypeAndPath metadata)
{
    String resultPath = null;
    Stat resultStat = null;
    int error = 0;
    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;
        }

        case ZooDefs.OpCode.error:
        {
            OpResult.ErrorResult errorResult = (OpResult.ErrorResult)opResult;
            error = errorResult.getErr();
            break;
        }
    }

    return new CuratorTransactionResult(metadata.getType(), metadata.getForPath(), resultPath, resultStat, error);
}
 
Example #13
Source File: ZkClient.java    From helix with Apache License 2.0 5 votes vote down vote up
public List<OpResult> multi(final Iterable<Op> ops) throws ZkException {
  if (ops == null) {
    throw new NullPointerException("ops must not be null.");
  }

  return retryUntilConnected(new Callable<List<OpResult>>() {

    @Override
    public List<OpResult> call() throws Exception {
      return getConnection().multi(ops);
    }
  });
}
 
Example #14
Source File: ZKVersionedSetOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected void commitOpResult(OpResult opResult) {
    assert(opResult instanceof OpResult.SetDataResult);
    OpResult.SetDataResult setDataResult = (OpResult.SetDataResult) opResult;
    if (null != listener) {
        listener.onCommit(new LongVersion(setDataResult.getStat().getVersion()));
    }
}
 
Example #15
Source File: SolrZkClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public List<OpResult> multi(final Iterable<Op> ops, boolean retryOnConnLoss) throws InterruptedException, KeeperException  {
  if (retryOnConnLoss) {
    return zkCmdExecutor.retryOperation(() -> keeper.multi(ops));
  } else {
    return keeper.multi(ops);
  }
}
 
Example #16
Source File: ZooKeeperClient.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Override
public List<OpResult> multi(final Iterable<Op> ops) throws InterruptedException, KeeperException {
  return execute(new ZKExecutor<List<OpResult>>("multi") {
    @Override
    List<OpResult> execute() throws KeeperException, InterruptedException {
      return ZooKeeperClient.super.multi(ops);
    }
  });
}
 
Example #17
Source File: CuratorTransactionImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #18
Source File: CuratorTransactionImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<CuratorTransactionResult> commit() throws Exception
{
    Preconditions.checkState(!isCommitted, "transaction already committed");
    isCommitted = true;

    final AtomicBoolean firstTime = new AtomicBoolean(true);
    List<OpResult>      resultList = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<List<OpResult>>()
        {
            @Override
            public List<OpResult> call() throws Exception
            {
                return doOperation(firstTime);
            }
        }
    );
    
    if ( resultList.size() != transaction.metadataSize() )
    {
        throw new IllegalStateException(String.format("Result size (%d) doesn't match input size (%d)", resultList.size(), transaction.metadataSize()));
    }

    ImmutableList.Builder<CuratorTransactionResult>     builder = ImmutableList.builder();
    for ( int i = 0; i < resultList.size(); ++i )
    {
        OpResult                                    opResult = resultList.get(i);
        CuratorMultiTransactionRecord.TypeAndPath   metadata = transaction.getMetadata(i);
        CuratorTransactionResult                    curatorResult = makeCuratorResult(opResult, metadata);
        builder.add(curatorResult);
    }

    return builder.build();
}
 
Example #19
Source File: TransactionImpl.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void commit() throws RegistryException {
  try {
    List<OpResult> results = zkTransaction.commit();
  } catch (InterruptedException | KeeperException e) {
    throw new RegistryException(ErrorCode.Unknown, e);
  }
}
 
Example #20
Source File: ZkConnection.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public List<OpResult> multi(Iterable<Op> ops) throws KeeperException, InterruptedException {
  return _zk.multi(ops);
}
 
Example #21
Source File: ZooKeeperTransaction.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Override
public List<OpResult> commit() throws InterruptedException, KeeperException {
    return transaction.commit();
}
 
Example #22
Source File: SharedZkClient.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public List<OpResult> multi(Iterable<Op> ops) {
  return _innerSharedZkClient.multi(ops);
}
 
Example #23
Source File: FederatedZkClient.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public List<OpResult> multi(Iterable<Op> ops) {
  throwUnsupportedOperationException();
  return null;
}
 
Example #24
Source File: DedicatedZkClient.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public List<OpResult> multi(Iterable<Op> ops) {
  return _rawZkClient.multi(ops);
}
 
Example #25
Source File: AutoCommitTransactionWrapper.java    From zkcopy with Apache License 2.0 4 votes vote down vote up
@Override
public List<OpResult> commit() throws InterruptedException, KeeperException {
    return transaction.commit();
}
 
Example #26
Source File: TestZKTransaction.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
protected void abortOpResult(Throwable t, @Nullable OpResult opResult) {
    this.abortLatch.countDown();
}
 
Example #27
Source File: TestZKTransaction.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
protected void commitOpResult(OpResult opResult) {
    this.commitLatch.countDown();
}
 
Example #28
Source File: DefaultZKOp.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
protected void abortOpResult(Throwable t, OpResult opResult) {
    // no-op
}
 
Example #29
Source File: DefaultZKOp.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
protected void commitOpResult(OpResult opResult) {
    // no-op
}
 
Example #30
Source File: ZKOp.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void abort(Throwable t, Object r) {
    assert(r instanceof OpResult);
    abortOpResult(t, (OpResult) r);
}