fix(codedeploy): use service clock for lifecycle timestamps (#8557)

* fix(codedeploy): use service clock for lifecycle timestamps
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-16 07:10:06 -04:00
1 parent 93da7a6c26
commit e47747845e
2 files changed
+78 -28

No files matched your search

@@ -35,7 +35,6 @@ import org.jboss.logging.Logger;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -176,7 +175,7 @@ public class CodeDeployService {
if (store == null) {
continue;
}
double now = EpochSeconds.fromInstant(Instant.now());
double now = EpochSeconds.fromInstant(clock.instant());
boolean changed = false;
for (String name : BUILT_IN_CONFIG_NAMES) {
if (!store.containsKey(name)) {
@@ -266,7 +265,7 @@ public class CodeDeployService {
private Map<String, DeploymentConfig> deploymentConfigsFor(String region) {
return deploymentConfigs.computeIfAbsent(region, r -> {
ConcurrentHashMap<String, DeploymentConfig> store = new ConcurrentHashMap<>();
double now = EpochSeconds.fromInstant(Instant.now());
double now = EpochSeconds.fromInstant(clock.instant());
for (String name : BUILT_IN_CONFIG_NAMES) {
store.put(name, buildBuiltInConfig(name, now));
}
@@ -341,7 +340,7 @@ public class CodeDeployService {
Application app = new Application();
app.setApplicationId(UUID.randomUUID().toString());
app.setApplicationName(name);
app.setCreateTime(EpochSeconds.fromInstant(Instant.now()));
app.setCreateTime(EpochSeconds.fromInstant(clock.instant()));
app.setLinkedToGitHub(false);
app.setComputePlatform(computePlatform != null ? computePlatform : "Server");
store.put(name, app);
@@ -546,7 +545,7 @@ public class CodeDeployService {
cfg.setDeploymentConfigId(UUID.randomUUID().toString());
cfg.setDeploymentConfigName(name);
cfg.setMinimumHealthyHosts(minimumHealthyHosts);
cfg.setCreateTime(EpochSeconds.fromInstant(Instant.now()));
cfg.setCreateTime(EpochSeconds.fromInstant(clock.instant()));
cfg.setComputePlatform(computePlatform != null ? computePlatform : "Server");
cfg.setTrafficRoutingConfig(trafficRoutingConfig);
cfg.setZonalConfig(zonalConfig);
@@ -616,7 +615,7 @@ public class CodeDeployService {
String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName();
String deploymentId = generateDeploymentId();
double now = EpochSeconds.fromInstant(Instant.now());
double now = EpochSeconds.fromInstant(clock.instant());
Deployment deployment = new Deployment();
deployment.setDeploymentId(deploymentId);
@@ -749,7 +748,7 @@ public class CodeDeployService {
"instance/" + instanceName).toString());
inst.setIamSessionArn(iamSessionArn);
inst.setIamUserArn(iamUserArn);
inst.setRegisterTime(EpochSeconds.fromInstant(Instant.now()));
inst.setRegisterTime(EpochSeconds.fromInstant(clock.instant()));
inst.setRegistrationStatus("Registered");
store.put(instanceName, inst);
persistRegion(onPremisesInstances, region);
@@ -758,7 +757,7 @@ public class CodeDeployService {
public void deregisterOnPremisesInstance(String region, String instanceName) {
OnPremisesInstance inst = requireOnPremisesInstance(region, instanceName);
inst.setDeregisterTime(EpochSeconds.fromInstant(Instant.now()));
inst.setDeregisterTime(EpochSeconds.fromInstant(clock.instant()));
inst.setRegistrationStatus("Deregistered");
persistRegion(onPremisesInstances, region);
}
@@ -820,7 +819,7 @@ public class CodeDeployService {
String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName();
String deploymentId = generateDeploymentId();
double now = EpochSeconds.fromInstant(Instant.now());
double now = EpochSeconds.fromInstant(clock.instant());
Deployment deployment = new Deployment();
deployment.setDeploymentId(deploymentId);
@@ -880,7 +879,7 @@ public class CodeDeployService {
String deploymentId = deployment.getDeploymentId();
try {
deployment.setStatus("InProgress");
deployment.setStartTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setStartTime(EpochSeconds.fromInstant(clock.instant()));
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "InProgress"));
boolean anyFailed = false;
@@ -888,7 +887,7 @@ public class CodeDeployService {
if (stopFlag.get()) {
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Skipped"));
deployment.setStatus("Stopped");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
return;
}
String instanceId = instanceIds.get(i);
@@ -908,18 +907,18 @@ public class CodeDeployService {
} else {
deployment.setStatus("Succeeded");
}
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Skipped"));
deployment.setStatus("Stopped");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
} catch (Exception e) {
LOG.warnv("Server deployment {0} failed: {1}", deploymentId, e.getMessage());
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Failed"));
deployment.setStatus("Failed");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
deployment.setErrorInformation(Map.of("code", "DeploymentFailed",
"message", e.getMessage() != null ? e.getMessage() : "Unknown error"));
} finally {
@@ -1287,7 +1286,7 @@ public class CodeDeployService {
String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName();
String deploymentId = generateDeploymentId();
double now = EpochSeconds.fromInstant(Instant.now());
double now = EpochSeconds.fromInstant(clock.instant());
Deployment deployment = new Deployment();
deployment.setDeploymentId(deploymentId);
@@ -1387,7 +1386,7 @@ public class CodeDeployService {
TaskSet blueTaskSet = null;
try {
deployment.setStatus("InProgress");
deployment.setStartTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setStartTime(EpochSeconds.fromInstant(clock.instant()));
updateEcsTargetStatus(ecsTargetMap, "InProgress");
if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; }
@@ -1504,7 +1503,7 @@ public class CodeDeployService {
updateEcsTargetStatus(ecsTargetMap, "Succeeded");
deployment.setStatus("Succeeded");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
@@ -1595,20 +1594,20 @@ public class CodeDeployService {
private void updateEcsTargetStatus(Map<String, Object> ecsTargetMap, String status) {
ecsTargetMap.put("status", status);
ecsTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now()));
ecsTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(clock.instant()));
}
private void finishEcsStopped(Deployment deployment, Map<String, Object> ecsTargetMap) {
updateEcsTargetStatus(ecsTargetMap, "Skipped");
deployment.setStatus("Stopped");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
}
private void finishEcsFailed(Deployment deployment, Map<String, Object> ecsTargetMap,
String errorCode, String message) {
updateEcsTargetStatus(ecsTargetMap, "Failed");
deployment.setStatus("Failed");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
deployment.setErrorInformation(Map.of("code", errorCode, "message", message != null ? message : ""));
}
@@ -1647,7 +1646,7 @@ public class CodeDeployService {
String deploymentId = deployment.getDeploymentId();
try {
deployment.setStatus("InProgress");
deployment.setStartTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setStartTime(EpochSeconds.fromInstant(clock.instant()));
updateTargetStatus(lambdaTargetMap, "InProgress");
if (stopFlag.get()) { finishStopped(deployment, lambdaTargetMap); return; }
@@ -1676,7 +1675,7 @@ public class CodeDeployService {
updateTargetStatus(lambdaTargetMap, "Succeeded");
deployment.setStatus("Succeeded");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
@@ -1822,18 +1821,18 @@ public class CodeDeployService {
private Map<String, Object> addLifecycleEvent(Map<String, Object> lambdaTargetMap, String name) {
Map<String, Object> event = new ConcurrentHashMap<>();
event.put("lifecycleEventName", name);
event.put("startTime", EpochSeconds.fromInstant(Instant.now()));
event.put("startTime", EpochSeconds.fromInstant(clock.instant()));
event.put("status", "InProgress");
List<Map<String, Object>> events = (List<Map<String, Object>>) lambdaTargetMap.get("lifecycleEvents");
if (events != null) {
events.add(event);
}
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now()));
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(clock.instant()));
return event;
}
private void finishLifecycleEvent(Map<String, Object> event, String status) {
event.put("endTime", EpochSeconds.fromInstant(Instant.now()));
event.put("endTime", EpochSeconds.fromInstant(clock.instant()));
event.put("status", status);
}
@@ -1845,20 +1844,20 @@ public class CodeDeployService {
private void updateTargetStatus(Map<String, Object> lambdaTargetMap, String status) {
lambdaTargetMap.put("status", status);
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now()));
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(clock.instant()));
}
private void finishStopped(Deployment deployment, Map<String, Object> lambdaTargetMap) {
updateTargetStatus(lambdaTargetMap, "Skipped");
deployment.setStatus("Stopped");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
}
private void finishFailed(Deployment deployment, Map<String, Object> lambdaTargetMap,
String errorCode, String message) {
updateTargetStatus(lambdaTargetMap, "Failed");
deployment.setStatus("Failed");
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
deployment.setErrorInformation(Map.of("code", errorCode, "message", message != null ? message : ""));
}