Search in sources :

Example 1 with ChainableSupplier

use of com.thinkbiganalytics.kylo.spark.job.tasks.ChainableSupplier in project kylo by Teradata.

the class DefaultSparkJobContext method create.

/**
 * Creates a {@code DefaultSparkJobContext} using the specified task to execute the Spark job.
 */
public static DefaultSparkJobContext create(@Nonnull final ChainableSupplier<SparkJobResponse> responseTask, @Nonnull final SparkJobCacheService cache, @Nonnull final ExecutorService executor) {
    // Create context
    final String id = UUID.randomUUID().toString();
    final Processor<SparkJobStatus, SparkJobStatus> processor = RingBufferProcessor.create(executor, false);
    final DefaultSparkJobContext context = new DefaultSparkJobContext(id, processor);
    // Start task
    final ChainableSupplier<SparkJobStatus> statusTask = responseTask.andThen(response -> {
        context.sparkJobId = response.getId();
        return response;
    }).andThen(new JobStatusFunction(cache));
    final CompletableFuture<SparkJobStatus> future = CompletableFuture.supplyAsync(statusTask, executor).whenComplete((response, error) -> {
        if (response != null) {
            processor.onNext(response);
            processor.onComplete();
        } else if (error != null) {
            processor.onError(error);
        } else {
            processor.onError(new NoSuchElementException());
        }
    });
    context.setFuture(future);
    return context;
}
Also used : JobStatusFunction(com.thinkbiganalytics.kylo.spark.job.tasks.JobStatusFunction) Processor(org.reactivestreams.Processor) Publisher(org.reactivestreams.Publisher) CompletableFuture(java.util.concurrent.CompletableFuture) RingBufferProcessor(reactor.core.processor.RingBufferProcessor) UUID(java.util.UUID) SparkJobResponse(com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse) Future(java.util.concurrent.Future) ChainableSupplier(com.thinkbiganalytics.kylo.spark.job.tasks.ChainableSupplier) NoSuchElementException(java.util.NoSuchElementException) Subscriber(org.reactivestreams.Subscriber) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) NoSuchElementException(java.util.NoSuchElementException) JobStatusFunction(com.thinkbiganalytics.kylo.spark.job.tasks.JobStatusFunction)

Aggregations

ChainableSupplier (com.thinkbiganalytics.kylo.spark.job.tasks.ChainableSupplier)1 JobStatusFunction (com.thinkbiganalytics.kylo.spark.job.tasks.JobStatusFunction)1 SparkJobResponse (com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse)1 NoSuchElementException (java.util.NoSuchElementException)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 Processor (org.reactivestreams.Processor)1 Publisher (org.reactivestreams.Publisher)1 Subscriber (org.reactivestreams.Subscriber)1 RingBufferProcessor (reactor.core.processor.RingBufferProcessor)1