mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 15:49:33 -05:00
fix(ecs): end a deployment the circuit breaker detects as failed (#4767)
* feat(ecs): end a deployment the circuit breaker detects as failed A service deployment whose tasks never start stayed IN_PROGRESS forever. With deploymentCircuitBreaker enabled and rollback off, a service task that stops without reaching RUNNING now counts toward the breaker's threshold. Once the count reaches the threshold, the deployment is STOPPED with a statusReason, finishedAt and stoppedAt. The service's deployment reports rolloutState FAILED, and the reconciler launches nothing more for it. The threshold follows the circuit breaker documentation: BOUNDED_PERCENT (default 50) of the desired count rounded up and clamped to 3..200, UNBOUNDED_PERCENT without the clamp, COUNT as given. resetOnHealthyTask (default true) zeroes the count when a task starts. With the breaker off, ECS keeps retrying, and so does this. With rollback on, AWS rolls back instead of stopping; that is not modelled, so such a deployment is left in progress rather than reported STOPPED. The reconciler now settles a deployment on the tick its tasks come up, so a later launch failure cannot fail a deployment that already completed. * fix(ecs): settle a converged deployment on every reconciler tick The tick settle ran only when the tick launched tasks. A deployment that converged without a launch was left unsettled. Scaling in to the tasks already running is one way that happens. If a task then died and its replacements failed to start, the circuit breaker stopped a deployment that had already completed. * fix(ecs): read a deployment's breaker state under its lock deploymentsFor runs on request threads and read the failure count and the triggered flag straight off the live record. It now reads them from a copy taken under the deployment's lock, like every other read path. The reconciler's halt check does the same. * test(ecs): pin the circuit breaker threshold's rounding, cap and unbounded type Three cases: 50% of 7 rounds up to 4, 50% of 402 is capped at 200, and UNBOUNDED_PERCENT at 100% of 201 goes past the cap to 201. * fix(ecs): count only tasks that failed to start against the circuit breaker Any launch that came back short of RUNNING used to count, including a task a user stopped with StopTask while its image was still pulling. Only a STOPPED task with stopCode TaskFailedToStart counts now. The Docker start-failure path always sets that code. * fix(ecs): use a neutral reason when the circuit breaker ends a deployment The previous wording read like an AWS message but does not appear in the AWS documentation. The ServiceDeployment reference describes statusReason only by example: the circuit breaker detected a failure. Both statusReason and rolloutStateReason now say that. * fix(ecs): keep a circuit-breaker-failed deployment halted across a restart Deployment records live only in memory, while services persist. After a restart the failed deployment had no record, so the reconciler launched its tasks again and nothing could count their failures. The failed deployment id is now kept on the persisted service model, as lastCompletedDeploymentId already is, and both the reconciler's halt and the FAILED rolloutState read it from there.
This commit is contained in:
1 parent
95b5acb1db
commit
4716ac9992
4 files changed
+635
-5
No files matched your search
@@ -166,6 +166,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
public static final String DEPLOYMENT_STATUS_SUCCESSFUL = "SUCCESSFUL";
|
||||
/** A service deployment that ended without completing. */
|
||||
public static final String DEPLOYMENT_STATUS_STOPPED = "STOPPED";
|
||||
/** Why a deployment the circuit breaker ended stopped, and why its rollout FAILED. */
|
||||
public static final String CIRCUIT_BREAKER_REASON = "The deployment circuit breaker detected a failure.";
|
||||
/** RunTask places at most ten tasks in one call, and StartTask at most ten instances. */
|
||||
public static final int MAX_TASKS_PER_RUN = 10;
|
||||
/** A listing returns at most a hundred ARNs per page. */
|
||||
@@ -3885,6 +3887,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
// deployment would report a task-definition change COMPLETED as soon as it was made.
|
||||
long running = runningOnCurrentDeployment(svc);
|
||||
boolean converged = running >= svc.getDesiredCount();
|
||||
ServiceDeployment record = lockedCopyOf(deploymentRecordOf(svc));
|
||||
boolean failed = deploymentId.equals(svc.getFailedDeploymentId());
|
||||
|
||||
Deployment d = new Deployment();
|
||||
d.setId(deploymentId);
|
||||
@@ -3893,10 +3897,15 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
d.setDesiredCount(svc.getDesiredCount());
|
||||
d.setPendingCount(svc.getPendingCount());
|
||||
d.setRunningCount((int) running);
|
||||
d.setFailedTasks(0);
|
||||
d.setRolloutState(converged ? "COMPLETED" : "IN_PROGRESS");
|
||||
d.setRolloutStateReason("ECS deployment " + deploymentId
|
||||
+ (converged ? " completed." : " in progress."));
|
||||
d.setFailedTasks(record != null ? record.getFailedTasks() : 0);
|
||||
if (failed) {
|
||||
d.setRolloutState("FAILED");
|
||||
d.setRolloutStateReason(CIRCUIT_BREAKER_REASON);
|
||||
} else {
|
||||
d.setRolloutState(converged ? "COMPLETED" : "IN_PROGRESS");
|
||||
d.setRolloutStateReason("ECS deployment " + deploymentId
|
||||
+ (converged ? " completed." : " in progress."));
|
||||
}
|
||||
// A deployment reports the placement it runs under the same way the service does: a
|
||||
// capacity provider strategy when there is one, a launch type otherwise, never both.
|
||||
if (svc.getCapacityProviderStrategy() != null && !svc.getCapacityProviderStrategy().isEmpty()) {
|
||||
@@ -4105,6 +4114,97 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
/** The live record of the deployment the service is on, or null if none was recorded. */
|
||||
private ServiceDeployment deploymentRecordOf(EcsServiceModel svc) {
|
||||
if (svc.getServiceArn() == null) {
|
||||
return null;
|
||||
}
|
||||
String suffix = "/" + taskSetId(svc);
|
||||
return serviceDeployments.values().stream()
|
||||
.filter(d -> svc.getServiceArn().equals(d.getServiceArn()))
|
||||
.filter(d -> d.getTargetServiceRevisionArn() != null
|
||||
&& d.getTargetServiceRevisionArn().endsWith(suffix))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts a service task launch against the deployment's circuit breaker, and ends the
|
||||
* deployment once the failures reach the threshold. Returns whether it ended.
|
||||
*/
|
||||
private boolean countLaunch(EcsServiceModel svc, ServiceDeployment deployment, EcsTask task) {
|
||||
if (deployment == null) {
|
||||
return false;
|
||||
}
|
||||
Map<String, Object> breaker = circuitBreakerOf(svc);
|
||||
synchronized (deployment) {
|
||||
if (!DEPLOYMENT_STATUS_IN_PROGRESS.equals(deployment.getStatus())) {
|
||||
return false;
|
||||
}
|
||||
if (TaskStatus.RUNNING.name().equals(task.getLastStatus())) {
|
||||
if (flag(breaker.get("resetOnHealthyTask"), true)) {
|
||||
deployment.setFailedTasks(0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// Only a task that failed to start counts, not one a user stopped during its pull.
|
||||
if (!TaskStatus.STOPPED.name().equals(task.getLastStatus())
|
||||
|| !STOP_CODE_TASK_FAILED_TO_START.equals(task.getStopCode())) {
|
||||
return false;
|
||||
}
|
||||
deployment.setFailedTasks(deployment.getFailedTasks() + 1);
|
||||
// With rollback on, AWS rolls back instead of stopping; that is not modelled.
|
||||
if (!flag(breaker.get("enable"), false) || flag(breaker.get("rollback"), false)
|
||||
|| deployment.getFailedTasks() < circuitBreakerThreshold(breaker, svc.getDesiredCount())) {
|
||||
return false;
|
||||
}
|
||||
Instant now = Instant.now();
|
||||
deployment.setStatus(DEPLOYMENT_STATUS_STOPPED);
|
||||
deployment.setStatusReason(CIRCUIT_BREAKER_REASON);
|
||||
deployment.setFinishedAt(now);
|
||||
deployment.setStoppedAt(now);
|
||||
deployment.setUpdatedAt(now);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, Object> circuitBreakerOf(EcsServiceModel svc) {
|
||||
Map<String, Object> config = svc.getDeploymentConfiguration();
|
||||
Object breaker = config == null ? null : config.get("deploymentCircuitBreaker");
|
||||
return breaker instanceof Map<?, ?> map ? (Map<String, Object>) map : Map.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* The failure count that trips the circuit breaker. BOUNDED_PERCENT (the default, at 50) is
|
||||
* the percentage of the desired count rounded up and clamped to 3..200, UNBOUNDED_PERCENT the
|
||||
* same without the clamp, and COUNT the value itself.
|
||||
*/
|
||||
private static int circuitBreakerThreshold(Map<String, Object> breaker, int desiredCount) {
|
||||
Object raw = breaker.get("thresholdConfiguration");
|
||||
Map<?, ?> threshold = raw instanceof Map<?, ?> map ? map : Map.of();
|
||||
String type = threshold.get("type") != null ? threshold.get("type").toString() : "BOUNDED_PERCENT";
|
||||
int value = threshold.get("value") instanceof Number n ? n.intValue() : 50;
|
||||
if ("COUNT".equals(type)) {
|
||||
return value;
|
||||
}
|
||||
int percent = (value * desiredCount + 99) / 100;
|
||||
return "UNBOUNDED_PERCENT".equals(type) ? percent : Math.min(200, Math.max(3, percent));
|
||||
}
|
||||
|
||||
private static boolean flag(Object value, boolean fallback) {
|
||||
return value == null ? fallback : Boolean.parseBoolean(value.toString());
|
||||
}
|
||||
|
||||
/** {@link #copyOf} under the deployment's lock, or null for no deployment. */
|
||||
private static ServiceDeployment lockedCopyOf(ServiceDeployment deployment) {
|
||||
if (deployment == null) {
|
||||
return null;
|
||||
}
|
||||
synchronized (deployment) {
|
||||
return copyOf(deployment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A copy taken under the deployment's lock, so a response never sees a half-applied
|
||||
* transition.
|
||||
@@ -4124,6 +4224,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
copy.setUpdatedAt(source.getUpdatedAt());
|
||||
copy.setTargetServiceRevisionArn(source.getTargetServiceRevisionArn());
|
||||
copy.setSourceServiceRevisionArns(source.getSourceServiceRevisionArns());
|
||||
copy.setFailedTasks(source.getFailedTasks());
|
||||
return copy;
|
||||
}
|
||||
|
||||
@@ -4716,12 +4817,23 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (!leftoverContainersCleared()) {
|
||||
return;
|
||||
}
|
||||
// A deployment the circuit breaker failed launches no new tasks. That is read off the
|
||||
// persisted service, not the in-memory deployment record, so it survives a restart.
|
||||
if (currentDeploymentId.equals(svc.getFailedDeploymentId())) {
|
||||
return;
|
||||
}
|
||||
ServiceDeployment deployment = deploymentRecordOf(svc);
|
||||
int toStart = svc.getDesiredCount() - (int) current;
|
||||
for (int i = 0; i < toStart; i++) {
|
||||
try {
|
||||
EcsTask launched = launchServiceTask(cluster, svc, svc.getLaunchType(), null, region);
|
||||
LOG.infov("Service reconciler started task {0} for service {1}",
|
||||
launched.getTaskArn(), svc.getServiceName());
|
||||
if (countLaunch(svc, deployment, launched)) {
|
||||
svc.setFailedDeploymentId(currentDeploymentId);
|
||||
services.put(key, svc);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Service reconciler failed to start task for {0}: {1}",
|
||||
svc.getServiceName(), e.getMessage());
|
||||
@@ -4747,6 +4859,12 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
});
|
||||
}
|
||||
// Settle on every tick, not only on a read: a deployment that has converged is finished,
|
||||
// so a task dying afterwards cannot count against it.
|
||||
ServiceDeployment settled = deploymentRecordOf(svc);
|
||||
if (settled != null) {
|
||||
settleStatus(settled);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Resolution helpers ────────────────────────────────────────────────────
|
||||
|
||||
@@ -28,6 +28,8 @@ public class EcsServiceModel {
|
||||
private String deploymentId;
|
||||
/** The deploymentId last observed to reach steady state; guards against re-emitting COMPLETED. */
|
||||
private String lastCompletedDeploymentId;
|
||||
/** The deploymentId the circuit breaker failed; persisted so a restart does not resume it. */
|
||||
private String failedDeploymentId;
|
||||
private String namespace;
|
||||
private String deploymentController;
|
||||
private String schedulingStrategy;
|
||||
@@ -49,7 +51,7 @@ public class EcsServiceModel {
|
||||
private String propagateTags;
|
||||
private Integer healthCheckGracePeriodSeconds;
|
||||
private String roleArn;
|
||||
/** {@code deploymentConfiguration}, kept raw: Floci reports it but runs no rollout against it. */
|
||||
/** {@code deploymentConfiguration}, kept raw; only its {@code deploymentCircuitBreaker} is acted on. */
|
||||
private Map<String, Object> deploymentConfiguration;
|
||||
/**
|
||||
* {@code serviceRegistries}, kept raw. {@link EcsServiceDiscoveryRegistrar}
|
||||
@@ -99,6 +101,9 @@ public class EcsServiceModel {
|
||||
this.lastCompletedDeploymentId = lastCompletedDeploymentId;
|
||||
}
|
||||
|
||||
public String getFailedDeploymentId() { return failedDeploymentId; }
|
||||
public void setFailedDeploymentId(String failedDeploymentId) { this.failedDeploymentId = failedDeploymentId; }
|
||||
|
||||
public String getNamespace() { return namespace; }
|
||||
public void setNamespace(String namespace) { this.namespace = namespace; }
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ public class ServiceDeployment {
|
||||
private Instant updatedAt;
|
||||
private String targetServiceRevisionArn;
|
||||
private List<String> sourceServiceRevisionArns;
|
||||
/** Tasks the circuit breaker has counted as failing to start. */
|
||||
private int failedTasks;
|
||||
|
||||
public Instant getStartedAt() { return startedAt; }
|
||||
public void setStartedAt(Instant startedAt) { this.startedAt = startedAt; }
|
||||
@@ -51,6 +53,9 @@ public class ServiceDeployment {
|
||||
this.sourceServiceRevisionArns = sourceServiceRevisionArns;
|
||||
}
|
||||
|
||||
public int getFailedTasks() { return failedTasks; }
|
||||
public void setFailedTasks(int failedTasks) { this.failedTasks = failedTasks; }
|
||||
|
||||
public String getServiceDeploymentArn() { return serviceDeploymentArn; }
|
||||
public void setServiceDeploymentArn(String serviceDeploymentArn) { this.serviceDeploymentArn = serviceDeploymentArn; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user