Search in sources :

Example 1 with Executor

use of io.kestra.core.runners.Executor in project kestra by kestra-io.

the class ExecutorTriggerCleaner method topology.

public StreamsBuilder topology() {
    StreamsBuilder builder = new KafkaStreamsBuilder();
    KStream<String, Executor> executorKStream = kafkaStreamSourceService.executorKStream(builder);
    KStream<String, Executor> executionWithFlowKStream = kafkaStreamSourceService.executorWithFlow(executorKStream, false);
    GlobalKTable<String, Trigger> triggerGlobalKTable = kafkaStreamSourceService.triggerGlobalKTable(builder);
    executionWithFlowKStream.filter((key, value) -> value.getExecution().getTrigger() != null, Named.as("cleanTrigger-hasTrigger-filter")).filter((key, value) -> conditionService.isTerminatedWithListeners(value.getFlow(), value.getExecution()), Named.as("cleanTrigger-terminated-filter")).join(triggerGlobalKTable, (key, executionWithFlow) -> Trigger.uid(executionWithFlow.getExecution()), (execution, trigger) -> trigger.resetExecution(), Named.as("cleanTrigger-join")).selectKey((key, value) -> queueService.key(value)).to(kafkaAdminService.getTopicName(Trigger.class), Produced.with(Serdes.String(), JsonSerde.of(Trigger.class)));
    return builder;
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaAdminService(io.kestra.runner.kafka.services.KafkaAdminService) Produced(org.apache.kafka.streams.kstream.Produced) ConditionService(io.kestra.core.services.ConditionService) Singleton(jakarta.inject.Singleton) Trigger(io.kestra.core.models.triggers.Trigger) QueueService(io.kestra.core.queues.QueueService) KStream(org.apache.kafka.streams.kstream.KStream) JsonSerde(io.kestra.runner.kafka.serializers.JsonSerde) Slf4j(lombok.extern.slf4j.Slf4j) GlobalKTable(org.apache.kafka.streams.kstream.GlobalKTable) Named(org.apache.kafka.streams.kstream.Named) Executor(io.kestra.core.runners.Executor) Serdes(org.apache.kafka.common.serialization.Serdes) Inject(jakarta.inject.Inject) KafkaStreamSourceService(io.kestra.runner.kafka.services.KafkaStreamSourceService) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder) KafkaQueueEnabled(io.kestra.runner.kafka.KafkaQueueEnabled) Executor(io.kestra.core.runners.Executor) Trigger(io.kestra.core.models.triggers.Trigger) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder)

Example 2 with Executor

use of io.kestra.core.runners.Executor in project kestra by kestra-io.

the class ExecutorFromExecutionTransformer method transform.

@Override
public Executor transform(final String key, final Execution value) {
    if (value == null) {
        return null;
    }
    Executor executor = new Executor(value, this.context.offset());
    // restart need to be saved on state store for future join
    if (executor.getExecution().getState().getCurrent() == State.Type.RESTARTED) {
        store.put(key, executor.serialize());
    }
    this.context.headers().remove("from");
    this.context.headers().remove("offset");
    return executor;
}
Also used : Executor(io.kestra.core.runners.Executor)

Example 3 with Executor

use of io.kestra.core.runners.Executor in project kestra by kestra-io.

the class ExecutorNextTransformer method transform.

@Override
public Executor transform(final String key, final Executor value) {
    Executor executor = executorService.process(value);
    if (executor.getNexts().size() == 0) {
        return value;
    }
    Store store = this.store.get(key) == null ? new Store() : this.store.get(key);
    Map<Boolean, List<String>> groups = executor.getNexts().stream().map(taskRun -> taskRun.getParentTaskRunId() + "-" + taskRun.getTaskId() + "-" + taskRun.getValue()).collect(Collectors.partitioningBy(store::contains));
    if (groups.get(true).size() > 0) {
        groups.get(true).forEach(s -> log.trace("Duplicate next taskRun for execution '{}', value '{}'", key, s));
        return value;
    }
    store.addAll(groups.get(false));
    this.store.put(key, store);
    Execution newExecution = executorService.onNexts(value.getFlow(), value.getExecution(), executor.getNexts());
    return value.withExecution(newExecution, "onNexts");
}
Also used : Getter(lombok.Getter) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Map(java.util.Map) Executor(io.kestra.core.runners.Executor) ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) ExecutorService(io.kestra.core.runners.ExecutorService) NoArgsConstructor(lombok.NoArgsConstructor) Executor(io.kestra.core.runners.Executor) Execution(io.kestra.core.models.executions.Execution) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Executor

use of io.kestra.core.runners.Executor in project kestra by kestra-io.

the class ExecutorJoinerTransformer method transform.

@Override
public Executor transform(final String key, final Executor value) {
    if (value.getExecution() != null) {
        return value;
    }
    WorkerTaskResult workerTaskResult = value.getJoined();
    if (log.isDebugEnabled()) {
        log.debug("<< IN  WorkerTaskResult [key='{}', partition='{}, offset='{}'] : {}", key, context.partition(), context.offset(), workerTaskResult.getTaskRun().toStringState());
    }
    Executor executor = this.store.get(key);
    // already purge execution ?
    if (executor == null) {
        log.warn("Unable to find Executor with key '" + key + "' for WorkerTaskResult id '" + workerTaskResult.getTaskRun().getId() + "' '" + workerTaskResult.getTaskRun().toStringState() + "'");
        return null;
    }
    if (!executor.getExecution().hasTaskRunJoinable(value.getJoined().getTaskRun())) {
        return executor;
    }
    try {
        Execution newExecution = executor.getExecution().withTaskRun(workerTaskResult.getTaskRun());
        executor = executor.withExecution(newExecution, "joinWorkerResult");
    } catch (Exception e) {
        return executor.withException(e, "joinWorkerResult");
    }
    // send metrics
    metricRegistry.counter(MetricRegistry.KESTRA_EXECUTOR_TASKRUN_ENDED_COUNT, metricRegistry.tags(workerTaskResult)).increment();
    metricRegistry.timer(MetricRegistry.KESTRA_EXECUTOR_TASKRUN_ENDED_DURATION, metricRegistry.tags(workerTaskResult)).record(workerTaskResult.getTaskRun().getState().getDuration());
    return executor;
}
Also used : Executor(io.kestra.core.runners.Executor) Execution(io.kestra.core.models.executions.Execution) WorkerTaskResult(io.kestra.core.runners.WorkerTaskResult)

Example 5 with Executor

use of io.kestra.core.runners.Executor in project kestra by kestra-io.

the class FlowJoinerTransformer method transform.

@Override
public Executor transform(String key, Executor executor) {
    Flow flow;
    try {
        // pooling of new flow can be delayed on ExecutorStore, we maybe need to wait that the flow is updated
        flow = Await.until(() -> flowExecutorInterface.findByExecution(executor.getExecution()).orElse(null), Duration.ofMillis(100), Duration.ofMinutes(5));
    } catch (TimeoutException e) {
        return executor.withException(new Exception("Unable to find flow with namespace: '" + executor.getExecution().getNamespace() + "'" + ", id: '" + executor.getExecution().getFlowId() + "', " + "revision '" + executor.getExecution().getFlowRevision() + "'"), "joinFlow");
    }
    if (!withDefaults) {
        return executor.withFlow(flow);
    }
    try {
        flow = Template.injectTemplate(flow, executor.getExecution(), (namespace, id) -> this.templateExecutorInterface.findById(namespace, id).orElse(null));
    } catch (InternalException e) {
        log.warn("Failed to inject template", e);
    }
    Flow flowWithDefaults = taskDefaultService.injectDefaults(flow, executor.getExecution());
    return executor.withFlow(flowWithDefaults);
}
Also used : ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) FlowExecutorInterface(io.kestra.core.runners.FlowExecutorInterface) Slf4j(lombok.extern.slf4j.Slf4j) Await(io.kestra.core.utils.Await) TaskDefaultService(io.kestra.core.services.TaskDefaultService) Duration(java.time.Duration) Executor(io.kestra.core.runners.Executor) ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) TimeoutException(java.util.concurrent.TimeoutException) Flow(io.kestra.core.models.flows.Flow) Template(io.kestra.core.tasks.flows.Template) InternalException(io.kestra.core.exceptions.InternalException) TimeoutException(java.util.concurrent.TimeoutException) InternalException(io.kestra.core.exceptions.InternalException) Flow(io.kestra.core.models.flows.Flow) TimeoutException(java.util.concurrent.TimeoutException) InternalException(io.kestra.core.exceptions.InternalException)

Aggregations

Executor (io.kestra.core.runners.Executor)5 Slf4j (lombok.extern.slf4j.Slf4j)3 Execution (io.kestra.core.models.executions.Execution)2 ValueTransformerWithKey (org.apache.kafka.streams.kstream.ValueTransformerWithKey)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2 InternalException (io.kestra.core.exceptions.InternalException)1 Flow (io.kestra.core.models.flows.Flow)1 Trigger (io.kestra.core.models.triggers.Trigger)1 QueueService (io.kestra.core.queues.QueueService)1 ExecutorService (io.kestra.core.runners.ExecutorService)1 FlowExecutorInterface (io.kestra.core.runners.FlowExecutorInterface)1 WorkerTaskResult (io.kestra.core.runners.WorkerTaskResult)1 ConditionService (io.kestra.core.services.ConditionService)1 TaskDefaultService (io.kestra.core.services.TaskDefaultService)1 Template (io.kestra.core.tasks.flows.Template)1 Await (io.kestra.core.utils.Await)1 KafkaQueueEnabled (io.kestra.runner.kafka.KafkaQueueEnabled)1 JsonSerde (io.kestra.runner.kafka.serializers.JsonSerde)1 KafkaAdminService (io.kestra.runner.kafka.services.KafkaAdminService)1 KafkaStreamSourceService (io.kestra.runner.kafka.services.KafkaStreamSourceService)1