-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
590 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
core/src/main/java/io/kestra/core/models/flows/sla/ExecutionChangedSLA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.kestra.core.models.flows.sla; | ||
|
||
/** | ||
* Marker interface to denote an SLA as evaluating on execution change. | ||
* ExecutionChangedSLA will be evaluated on each execution change, a.k.a. at the beginning of the processing of the execution queue. | ||
*/ | ||
public interface ExecutionChangedSLA { | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/io/kestra/core/models/flows/sla/ExecutionMonitoringSLA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.kestra.core.models.flows.sla; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Marker interface to denote an SLA as evaluating using an {@link SLAMonitor}. | ||
* ExecutionMonitoringSLA will be evaluated on a deadline defined by the monitor; | ||
* the monitor is created when the execution is created. | ||
*/ | ||
public interface ExecutionMonitoringSLA { | ||
Duration getDuration(); | ||
} |
57 changes: 57 additions & 0 deletions
57
core/src/main/java/io/kestra/core/models/flows/sla/SLA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.kestra.core.models.flows.sla; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import io.kestra.core.exceptions.InternalException; | ||
import io.kestra.core.models.executions.Execution; | ||
import io.kestra.core.models.flows.sla.types.ExecutionConditionSLA; | ||
import io.kestra.core.models.flows.sla.types.MaxDurationSLA; | ||
import io.kestra.core.runners.RunContext; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@SuperBuilder | ||
@Getter | ||
@NoArgsConstructor | ||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true, include = JsonTypeInfo.As.EXISTING_PROPERTY) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = MaxDurationSLA.class, name = "MAX_DURATION"), | ||
@JsonSubTypes.Type(value = ExecutionConditionSLA.class, name = "EXECUTION_CONDITION"), | ||
}) | ||
public abstract class SLA { | ||
@NotNull | ||
@NotEmpty | ||
private String id; | ||
|
||
@NotNull | ||
private SLA.Type type; | ||
|
||
@NotNull | ||
private Behavior behavior; | ||
|
||
// TODO prevent system labels | ||
private Map<String, Object> labels; | ||
|
||
/** | ||
* Evaluate a flow SLA on an execution. | ||
* In case the SLA is exceeded, a violation will be returned. | ||
*/ | ||
public abstract Optional<Violation> evaluate(RunContext runContext, Execution execution) throws InternalException; | ||
|
||
public enum Type { | ||
MAX_DURATION, | ||
EXECUTION_CONDITION, | ||
} | ||
|
||
public enum Behavior { | ||
FAIL, | ||
CANCEL, | ||
NONE | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/src/main/java/io/kestra/core/models/flows/sla/SLAMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.kestra.core.models.flows.sla; | ||
|
||
import io.kestra.core.models.HasUID; | ||
import io.kestra.core.utils.IdUtils; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.Instant; | ||
|
||
@Builder | ||
@Getter | ||
public class SLAMonitor implements HasUID { | ||
String executionId; | ||
String slaId; | ||
Instant deadline; | ||
|
||
@Override | ||
public String uid() { | ||
return IdUtils.fromParts(executionId, slaId); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/io/kestra/core/models/flows/sla/SLAMonitorStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.kestra.core.models.flows.sla; | ||
|
||
import java.time.Instant; | ||
import java.util.function.Consumer; | ||
|
||
public interface SLAMonitorStorage { | ||
void save(SLAMonitor slaMonitor); | ||
|
||
void purge(String executionId); | ||
|
||
void processExpired(Instant now, Consumer<SLAMonitor> consumer); | ||
} |
6 changes: 6 additions & 0 deletions
6
core/src/main/java/io/kestra/core/models/flows/sla/Violation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.kestra.core.models.flows.sla; | ||
|
||
import java.util.Map; | ||
|
||
public record Violation(String slaId, SLA.Behavior behavior, Map<String, Object> labels, String reason) { | ||
} |
36 changes: 36 additions & 0 deletions
36
core/src/main/java/io/kestra/core/models/flows/sla/types/ExecutionConditionSLA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.kestra.core.models.flows.sla.types; | ||
|
||
import io.kestra.core.exceptions.InternalException; | ||
import io.kestra.core.models.executions.Execution; | ||
import io.kestra.core.models.flows.sla.ExecutionChangedSLA; | ||
import io.kestra.core.models.flows.sla.SLA; | ||
import io.kestra.core.models.flows.sla.Violation; | ||
import io.kestra.core.runners.RunContext; | ||
import io.kestra.core.utils.TruthUtils; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.Optional; | ||
|
||
@SuperBuilder | ||
@Getter | ||
@NoArgsConstructor | ||
public class ExecutionConditionSLA extends SLA implements ExecutionChangedSLA { | ||
@NotNull | ||
@NotEmpty | ||
private String condition; | ||
|
||
@Override | ||
public Optional<Violation> evaluate(RunContext runContext, Execution execution) throws InternalException { | ||
String result = runContext.render(this.getCondition()); | ||
if (TruthUtils.isTruthy(result)) { | ||
String reason = "execution condition violation: " + this.getCondition() + "."; | ||
return Optional.of(new Violation(this.getId(), this.getBehavior(), this.getLabels(), reason)); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
core/src/main/java/io/kestra/core/models/flows/sla/types/MaxDurationSLA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.kestra.core.models.flows.sla.types; | ||
|
||
import io.kestra.core.exceptions.InternalException; | ||
import io.kestra.core.models.executions.Execution; | ||
import io.kestra.core.models.flows.sla.ExecutionMonitoringSLA; | ||
import io.kestra.core.models.flows.sla.SLA; | ||
import io.kestra.core.models.flows.sla.Violation; | ||
import io.kestra.core.runners.RunContext; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Optional; | ||
|
||
@SuperBuilder | ||
@Getter | ||
@NoArgsConstructor | ||
public class MaxDurationSLA extends SLA implements ExecutionMonitoringSLA { | ||
@NotNull | ||
private Duration duration; | ||
|
||
@Override | ||
public Optional<Violation> evaluate(RunContext runContext, Execution execution) throws InternalException { | ||
Duration executionDuration = Duration.between(execution.getState().getStartDate(), Instant.now()); | ||
if (executionDuration.compareTo(this.getDuration()) > 0) { | ||
String reason = "execution duration of " + executionDuration.truncatedTo(ChronoUnit.MILLIS) + " exceed the maximum duration of " + this.getDuration() + "."; | ||
return Optional.of(new Violation(this.getId(), this.getBehavior(), this.getLabels(), reason)); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.