Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jdbc): avoid consuming the execution queue inside the Scheduler #7369

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions jdbc/src/main/java/io/kestra/jdbc/runner/JdbcExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.repositories.FlowRepositoryInterface;
import io.kestra.core.repositories.TriggerRepositoryInterface;
import io.kestra.core.runners.Executor;
import io.kestra.core.runners.ExecutorService;
import io.kestra.core.runners.*;
import io.kestra.core.schedulers.SchedulerExecutionStateInterface;
import io.kestra.core.schedulers.SchedulerTriggerStateInterface;
import io.kestra.core.server.ClusterEvent;
import io.kestra.core.server.Service;
import io.kestra.core.server.ServiceStateChangeEvent;
Expand Down Expand Up @@ -173,6 +176,12 @@ public class JdbcExecutor implements ExecutorInterface, Service {
@Inject
private SLAService slaService;

@Inject
private TriggerRepositoryInterface triggerRepository;

@Inject
private SchedulerTriggerStateInterface triggerState;

private final Tracer tracer;

private final FlowRepositoryInterface flowRepository;
Expand Down Expand Up @@ -999,6 +1008,15 @@ private void toExecution(Executor executor, boolean ignoreFailure) {
);
}

// purge the trigger: reset scheduler trigger at end
if (execution.getTrigger() != null) {
triggerRepository
.findByExecution(execution)
.ifPresent(trigger -> {
this.triggerState.update(trigger.resetExecution(execution.getState().getCurrent()));
});
}

// Purge the workerTaskResultQueue and the workerJobQueue
// IMPORTANT: this is safe as only the executor is listening to WorkerTaskResult,
// and we are sure at this stage that all WorkerJob has been listened and processed by the Worker.
Expand Down
30 changes: 0 additions & 30 deletions jdbc/src/main/java/io/kestra/jdbc/runner/JdbcScheduler.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package io.kestra.jdbc.runner;

import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.flows.FlowWithSource;
import io.kestra.core.models.triggers.Trigger;
import io.kestra.core.repositories.FlowRepositoryInterface;
import io.kestra.core.repositories.TriggerRepositoryInterface;
import io.kestra.core.schedulers.*;
import io.kestra.core.services.ConditionService;
import io.kestra.core.services.FlowListenersInterface;
import io.kestra.core.services.FlowService;
import io.kestra.core.utils.ListUtils;
Expand All @@ -26,9 +23,7 @@
@Slf4j
public class JdbcScheduler extends AbstractScheduler {
private final TriggerRepositoryInterface triggerRepository;
private final FlowRepositoryInterface flowRepository;
private final JooqDSLContextWrapper dslContextWrapper;
private final ConditionService conditionService;


@Inject
Expand All @@ -41,38 +36,13 @@ public JdbcScheduler(
triggerRepository = applicationContext.getBean(AbstractJdbcTriggerRepository.class);
triggerState = applicationContext.getBean(SchedulerTriggerStateInterface.class);
executionState = applicationContext.getBean(SchedulerExecutionState.class);
conditionService = applicationContext.getBean(ConditionService.class);
flowRepository = applicationContext.getBean(FlowRepositoryInterface.class);
dslContextWrapper = applicationContext.getBean(JooqDSLContextWrapper.class);
}

@Override
public void run() {
super.run();

this.receiveCancellations.addFirst(executionQueue.receive(
Scheduler.class,
either -> {
if (either.isRight()) {
log.error("Unable to deserialize an execution: {}", either.getRight().getMessage());
return;
}

Execution execution = either.getLeft();
if (execution.getTrigger() != null) {
var flow = flowRepository.findById(execution.getTenantId(), execution.getNamespace(), execution.getFlowId()).orElse(null);
if (execution.isDeleted() || conditionService.isTerminatedWithListeners(flow, execution)) {
// reset scheduler trigger at end
triggerRepository
.findByExecution(execution)
.ifPresent(trigger -> {
this.triggerState.update(trigger.resetExecution(execution.getState().getCurrent()));
});
}
}
}
));

// remove trigger on flow update
this.flowListeners.listen((flow, previous) -> {
if (flow.isDeleted()) {
Expand Down
Loading