Package co.cask.tephra

Examples of co.cask.tephra.TransactionContext


    this.queueClientFactory = queueClientFactory;
  }

  @Override
  public TransactionContext createTransactionManager() {
    return new TransactionContext(txSystemClient, Iterables.unmodifiableIterable(txAware));
  }
View Full Code Here


  public void close() throws IOException {
    try {
      if (consumer != null && consumer instanceof Closeable) {
        // Call close in a new transaction.
        // TODO (terence): Actually need to coordinates with other flowlets to drain the queue.
        TransactionContext txContext = dataFabricFacade.createTransactionManager();
        txContext.start();
        try {
          ((Closeable) consumer).close();
          txContext.finish();
        } catch (TransactionFailureException e) {
          LOG.warn("Fail to commit transaction when closing consumer.");
          txContext.abort();
        }
      }
    } catch (Exception e) {
      LOG.warn("Fail to close queue consumer.", e);
    }
View Full Code Here

    if (processMethod.needsInput()) {
      flowletContext.getProgramMetrics().gauge("process.tuples.attempt.read", 1);
    }

    // Begin transaction and dequeue
    final TransactionContext txContext = flowletContext.createTransactionContext();
    try {
      txContext.start();

      try {
        InputDatum<T> input = entry.getProcessSpec().getQueueReader().dequeue(0, TimeUnit.MILLISECONDS);
        if (!input.needProcess()) {
          entry.backOff();
          // End the transaction if nothing in the queue
          txContext.finish();
          return false;
        }
        // Resetting back-off time to minimum back-off time,
        // since an entry to process was de-queued and most likely more entries will follow.
        entry.resetBackOff();

        if (!entry.isRetry()) {
          // Only increment the inflight count for non-retry entries.
          // The inflight would get decrement when the transaction committed successfully or input get ignored.
          // See the processMethodCallback function.
          inflight.getAndIncrement();
        }

        try {
          // Call the process method and commit the transaction. The current process entry will put
          // back to queue in the postProcess method (either a retry copy or itself).
          ProcessMethod.ProcessResult<?> result = processMethod.invoke(input);
          postProcess(processMethodCallback(processQueue, entry, input), txContext, input, result);
          return true;
        } catch (Throwable t) {
          // If exception thrown from invoke or postProcess, the inflight count would not be touched.
          // hence need to decrements here
          if (!entry.isRetry()) {
            inflight.decrementAndGet();
          }
        }

      } catch (Throwable t) {
        LOG.error("System failure: {}", flowletContext, t);
        try {
          txContext.abort();
        } catch (Throwable e) {
          LOG.error("Fail to abort transaction: {}", flowletContext, e);
        }
      }
    } catch (Throwable t) {
View Full Code Here

  private InputAcknowledger createInputAcknowledger(final InputDatum input) {
    return new InputAcknowledger() {
      @Override
      public void ack() throws TransactionFailureException {
        TransactionContext txContext = dataFabricFacade.createTransactionManager();
        txContext.start();
        input.reclaim();
        txContext.finish();
      }
    };
  }
View Full Code Here

      }
    };
  }

  private void initFlowlet() throws InterruptedException {
    final TransactionContext txContext = flowletContext.createTransactionContext();
    try {
      txContext.start();
      try {
        LOG.info("Initializing flowlet: " + flowletContext);

        flowlet.initialize(flowletContext);

        LOG.info("Flowlet initialized: " + flowletContext);
      } catch (Throwable t) {
        LOG.error("User code exception. Aborting transaction.", t);
        txContext.abort(new TransactionFailureException("User code exception. Aborting transaction", t));
        throw Throwables.propagate(t);
      }
      txContext.finish();
    } catch (TransactionFailureException e) {
      LOG.error("Flowlet throws exception during flowlet initialize: " + flowletContext, e);
      throw Throwables.propagate(e);
    }
  }
View Full Code Here

      throw Throwables.propagate(e);
    }
  }

  private void destroyFlowlet() {
    final TransactionContext txContext = flowletContext.createTransactionContext();
    try {
      txContext.start();
      try {
        LOG.info("Destroying flowlet: " + flowletContext);
        flowlet.destroy();
        LOG.info("Flowlet destroyed: " + flowletContext);
      } catch (Throwable t) {
        LOG.error("User code exception. Aborting transaction.", t);
        txContext.abort(new TransactionFailureException("User code exception. Aborting transaction", t));
        // No need to propagate, as it is shutting down.
      }
      txContext.finish();
    } catch (TransactionFailureException e) {
      LOG.error("Flowlet throws exception during flowlet destroy: " + flowletContext, e);
      // No need to propagate, as it is shutting down.
    }
  }
View Full Code Here

                                 DiscoveryServiceClient discoveryServiceClient, TransactionSystemClient txClient) {
    super(program, runId, datasets, metricsContext, metricsCollectionService, dsFramework, conf,
          discoveryServiceClient);
    this.spec = spec;
    this.runtimeArgs = ImmutableMap.copyOf(RuntimeArguments.fromPosixArray(runtimeArgs));
    this.txContext = new TransactionContext(txClient, getDatasetInstantiator().getTransactionAware());
    this.serviceRunnableMetrics = new ServiceRunnableMetrics(metricsCollectionService, metricsContext);
  }
View Full Code Here

                         program.getName(), runnableName, instanceId);
  }

  @Override
  public void execute(TxRunnable runnable) {
    final TransactionContext context = new TransactionContext(transactionSystemClient);
    try {
      context.start();
      runnable.run(new ServiceWorkerDatasetContext(context));
      context.finish();
    } catch (TransactionFailureException e) {
      abortTransaction(e, "Failed to commit. Aborting transaction.", context);
    } catch (Exception e) {
      abortTransaction(e, "Exception occurred running user code. Aborting transaction.", context);
    }
View Full Code Here

    if (processMethod.needsInput()) {
      flowletContext.getProgramMetrics().increment("process.tuples.attempt.read", 1);
    }

    // Begin transaction and dequeue
    TransactionContext txContext = dataFabricFacade.createTransactionManager();
    try {
      txContext.start();

      try {
        InputDatum<T> input = entry.getProcessSpec().getQueueReader().dequeue(0, TimeUnit.MILLISECONDS);
        if (!input.needProcess()) {
          entry.backOff();
          // End the transaction if nothing in the queue
          txContext.finish();
          return false;
        }
        // Resetting back-off time to minimum back-off time,
        // since an entry to process was de-queued and most likely more entries will follow.
        entry.resetBackOff();

        if (!entry.isRetry()) {
          // Only increment the inflight count for non-retry entries.
          // The inflight would get decrement when the transaction committed successfully or input get ignored.
          // See the processMethodCallback function.
          inflight.getAndIncrement();
        }

        try {
          // Call the process method and commit the transaction. The current process entry will put
          // back to queue in the postProcess method (either a retry copy or itself).
          ProcessMethod.ProcessResult<?> result = processMethod.invoke(input);
          postProcess(processMethodCallback(processQueue, entry, input), txContext, input, result);
          return true;
        } catch (Throwable t) {
          // If exception thrown from invoke or postProcess, the inflight count would not be touched.
          // hence need to decrements here
          if (!entry.isRetry()) {
            inflight.decrementAndGet();
          }
        }

      } catch (Throwable t) {
        LOG.error("System failure: {}", flowletContext, t);
        try {
          txContext.abort();
        } catch (Throwable e) {
          LOG.error("Fail to abort transaction: {}", flowletContext, e);
        }
      }
    } catch (Throwable t) {
View Full Code Here

  private InputAcknowledger createInputAcknowledger(final InputDatum input) {
    return new InputAcknowledger() {
      @Override
      public void ack() throws TransactionFailureException {
        TransactionContext txContext = dataFabricFacade.createTransactionManager();
        txContext.start();
        input.reclaim();
        txContext.finish();
      }
    };
  }
View Full Code Here

TOP

Related Classes of co.cask.tephra.TransactionContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.