diff --git a/docs/services/emr.md b/docs/services/emr.md index cdbb347cbc..6d575c82d1 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -416,8 +416,14 @@ holding the driver and its executors, or a command or a script. The cluster itse goes to `stdout.gz` and `stderr.gz` under `//node//bootstrap-actions//`, as the Management Guide's bootstrap failure pages give the folder. The actions run once, as on the master, and every node of the cluster runs from the image they leave, so each holds what they installed. Shutdown - actions do not run, and - `Status.ErrorDetails` is not given. + actions do not run. A cluster a failed action ended has a `Status.ErrorDetails` entry in `DescribeCluster` and + `ListClusters`, with the code and `ErrorData` keys of the Management Guide's error code pages: + `BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE` with `primary-instance-id`, `bootstrap-action`, `return-code`, + `amazon-s3-path` and `public-doc` for an action that exits with another code than 0, and + `BOOTSTRAP_FAILURE_BA_DOWNLOAD_FAILED_PRIMARY` with the same but `return-code` for a script that cannot be read. + `ErrorData` is a list of maps, and no source shows how AWS spreads the keys over them: Floci gives one map holding + them all. `ErrorMessage` is the reason's message and the error code's page, in Floci's words, as AWS advises + against parsing that text. A cluster that ends with `INTERNAL_ERROR` has no `ErrorDetails`. * **States**: a step is `RUNNING` from when it starts until its driver or command exits; `step-duration-seconds` does not apply. The first step of a release also waits while its image is pulled (3.5 to 4.3 GB unpacked; see [`floci.docker.image-pull-timeout-seconds`](../configuration/docker.md)). It is then diff --git a/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java b/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java index 39d6c33de6..3eeb3aa666 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java +++ b/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java @@ -1,6 +1,7 @@ package io.floci.aws.services.emr; import io.floci.aws.services.emr.model.EmrCluster; +import io.floci.aws.services.emr.model.EmrErrorDetail; import io.floci.aws.services.emr.model.EmrInstanceFleet; import io.floci.aws.services.emr.model.EmrInstanceGroup; import io.floci.aws.services.emr.model.EmrStep; @@ -242,12 +243,14 @@ final class EmrClusterLifecycle { /** * The bootstrap actions a cluster ran for real have ended: when {@code failureCode} is null they all * succeeded and left {@code image}, which its steps run in, and the cluster is ready to run them; - * otherwise the cluster ends, for {@code failureCode} and {@code message}. + * otherwise the cluster ends, for {@code failureCode} and {@code message}, with {@code errorDetails} as + * its Status.ErrorDetails. */ - void bootstrapEnded(EmrCluster cluster, String image, String failureCode, String message, Instant at, - String region) { + void bootstrapEnded(EmrCluster cluster, String image, String failureCode, String message, + List errorDetails, Instant at, String region) { cluster.setBootstrapInContainer(false); if (failureCode != null) { + cluster.setErrorDetails(new ArrayList<>(errorDetails)); terminate(cluster, failureCode, message, at, region); return; } diff --git a/src/main/java/io/floci/aws/services/emr/EmrHandler.java b/src/main/java/io/floci/aws/services/emr/EmrHandler.java index 0646080d10..8aa74d8015 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrHandler.java +++ b/src/main/java/io/floci/aws/services/emr/EmrHandler.java @@ -15,6 +15,7 @@ import io.floci.aws.core.common.PaginatedResult; import io.floci.aws.core.common.SmithyValidationErrors; import io.floci.aws.services.emr.model.EmrBlockPublicAccess; import io.floci.aws.services.emr.model.EmrCluster; +import io.floci.aws.services.emr.model.EmrErrorDetail; import io.floci.aws.services.emr.model.EmrInstanceFleet; import io.floci.aws.services.emr.model.EmrInstanceGroup; import io.floci.aws.services.emr.model.EmrStep; @@ -741,6 +742,19 @@ public class EmrHandler { putEpoch(timeline, "CreationDateTime", c.getCreationDateTime()); putEpoch(timeline, "ReadyDateTime", c.getReadyDateTime()); putEpoch(timeline, "EndDateTime", c.getEndDateTime()); + if (!c.getErrorDetails().isEmpty()) { + ArrayNode details = status.putArray("ErrorDetails"); + for (EmrErrorDetail detail : c.getErrorDetails()) { + ObjectNode node = details.addObject(); + node.put("ErrorCode", detail.getErrorCode()); + ArrayNode data = node.putArray("ErrorData"); + for (Map pairs : detail.getErrorData()) { + ObjectNode map = data.addObject(); + pairs.forEach(map::put); + } + node.put("ErrorMessage", detail.getErrorMessage()); + } + } return status; } diff --git a/src/main/java/io/floci/aws/services/emr/EmrService.java b/src/main/java/io/floci/aws/services/emr/EmrService.java index 0a2431c4b6..67b221216c 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrService.java @@ -21,6 +21,7 @@ import io.floci.aws.core.storage.StorageBackend; import io.floci.aws.core.storage.StorageFactory; import io.floci.aws.services.emr.model.EmrBlockPublicAccess; import io.floci.aws.services.emr.model.EmrCluster; +import io.floci.aws.services.emr.model.EmrErrorDetail; import io.floci.aws.services.emr.model.EmrInstanceFleet; import io.floci.aws.services.emr.model.EmrInstanceGroup; import io.floci.aws.services.emr.model.EmrStep; @@ -103,6 +104,11 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear "On the master instance (%s), bootstrap action %d could not be read from %s: %s"; private static final String BOOTSTRAP_NOT_RUN = "Floci could not run the bootstrap actions: %s"; static final String BOOTSTRAP_INTERRUPTED = "Floci stopped while the cluster's bootstrap actions were running."; + // The ErrorDetail codes of an action that failed on the primary node and of one whose script it could not download, + // each with a Management Guide page of its own name. + static final String BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE = "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE"; + static final String BOOTSTRAP_FAILURE_BA_DOWNLOAD_FAILED_PRIMARY = "BOOTSTRAP_FAILURE_BA_DOWNLOAD_FAILED_PRIMARY"; + private static final String ERROR_CODE_PAGE = "https://docs.aws.amazon.com/emr/latest/ManagementGuide/%s.html"; // The release whose EMR Serverless Hive image runs the HDFS of a cluster of the AWS runtime for Apache Spark, // which publishes none. Hadoop keeps its wire protocols compatible across a major version's minor releases, so // a newer client, such as the runtime's, reaches its NameNode. @@ -1307,8 +1313,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear || bootstrapping.contains(clusterKey(regionResolver.getAccountId(), cluster.getId()))) { return false; } - lifecycle.bootstrapEnded(cluster, null, EmrClusterLifecycle.INTERNAL_ERROR, BOOTSTRAP_INTERRUPTED, now(), - regionOf(cluster)); + lifecycle.bootstrapEnded(cluster, null, EmrClusterLifecycle.INTERNAL_ERROR, BOOTSTRAP_INTERRUPTED, List.of(), + now(), regionOf(cluster)); return true; } @@ -1939,6 +1945,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear String master = masterInstanceId(cluster); String code = EmrClusterLifecycle.BOOTSTRAP_FAILURE; String message; + List errorDetails = List.of(); if (outcome.image() != null) { code = null; message = null; @@ -1946,18 +1953,46 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear cluster.setBootstrapImageRunsYarn(runsYarn(cluster)); } else if (outcome.exitCode() != null) { message = BOOTSTRAP_ACTION_FAILED.formatted(master, outcome.failedAction()); + errorDetails = List.of(bootstrapErrorDetail(BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE, master, + outcome.failedAction(), outcome.exitCode(), bootstrap, message)); } else if (outcome.failedAction() != null) { message = BOOTSTRAP_ACTION_NOT_READ.formatted(master, outcome.failedAction(), bootstrap.actions().get(outcome.failedAction() - 1).path(), outcome.failure()); + errorDetails = List.of(bootstrapErrorDetail(BOOTSTRAP_FAILURE_BA_DOWNLOAD_FAILED_PRIMARY, master, + outcome.failedAction(), null, bootstrap, message)); } else { code = EmrClusterLifecycle.INTERNAL_ERROR; message = BOOTSTRAP_NOT_RUN.formatted(outcome.failure()); } - lifecycle.bootstrapEnded(cluster, outcome.image(), code, message, now(), regionOf(cluster)); + lifecycle.bootstrapEnded(cluster, outcome.image(), code, message, errorDetails, now(), regionOf(cluster)); store(cluster); LOG.infov("The bootstrap actions of EMR cluster {0} ended; it is {1}", cluster.getId(), cluster.getState()); } + /** + * The ErrorDetail of bootstrap action {@code action}, counted from 1, that failed on the primary node + * {@code master} for {@code code}, with the ErrorData its Management Guide page gives: the primary node, the + * action, its return code when it ran ({@code returnCode}, null when it did not), its script and the page itself. + * The model makes ErrorData a list of maps, and no source shows how the keys spread over them: Floci puts them all + * in one, which a client that reads the first map and one that merges them all read alike. ErrorMessage is + * "Description of the error with a link to more information in the Amazon EMR documentation", whose text AWS does + * not give: Floci's is the cluster's reason, {@code message}, and the page. + */ + private static EmrErrorDetail bootstrapErrorDetail(String code, String master, int action, Integer returnCode, + BootstrapRunner.Bootstrap bootstrap, String message) { + String page = ERROR_CODE_PAGE.formatted(code); + Map data = new LinkedHashMap<>(); + data.put("primary-instance-id", master); + data.put("bootstrap-action", String.valueOf(action)); + if (returnCode != null) { + data.put("return-code", String.valueOf(returnCode)); + } + data.put("amazon-s3-path", bootstrap.actions().get(action - 1).path()); + data.put("public-doc", page); + String sentence = message.endsWith(".") ? message : message + "."; + return new EmrErrorDetail(code, List.of(data), sentence + " For more information, see " + page); + } + /** The EC2 instance id ListInstances gives a cluster's instance {@code index}, counted from 1. */ static String ec2InstanceId(long index) { return "i-" + String.format("%017d", index); diff --git a/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java b/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java index 95f6914c36..026b879a43 100644 --- a/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java +++ b/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java @@ -68,6 +68,8 @@ public class EmrCluster { private Instant endDateTime; // When the cluster began TERMINATING; Floci's own, as the API's Timeline has no such time. private Instant terminatingDateTime; + // Status.ErrorDetails: the errors that ended the cluster, empty unless one of those with an ErrorDetail code did. + private List errorDetails = new ArrayList<>(); private Map tags = new LinkedHashMap<>(); private List steps = new ArrayList<>(); private List instanceGroups = new ArrayList<>(); @@ -217,6 +219,9 @@ public class EmrCluster { public Instant getTerminatingDateTime() { return terminatingDateTime; } public void setTerminatingDateTime(Instant terminatingDateTime) { this.terminatingDateTime = terminatingDateTime; } + public List getErrorDetails() { return errorDetails; } + public void setErrorDetails(List errorDetails) { this.errorDetails = errorDetails; } + public Map getTags() { return tags; } public void setTags(Map tags) { this.tags = tags; } diff --git a/src/main/java/io/floci/aws/services/emr/model/EmrErrorDetail.java b/src/main/java/io/floci/aws/services/emr/model/EmrErrorDetail.java new file mode 100644 index 0000000000..5a13d8a09f --- /dev/null +++ b/src/main/java/io/floci/aws/services/emr/model/EmrErrorDetail.java @@ -0,0 +1,36 @@ +package io.floci.aws.services.emr.model; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * One of a cluster's ErrorDetails, "A tuple that provides information about an error that caused a cluster to + * terminate": its ErrorCode, its ErrorData, a list of key-value maps, and its ErrorMessage. + */ +@RegisterForReflection +public class EmrErrorDetail { + + private String errorCode; + private List> errorData = new ArrayList<>(); + private String errorMessage; + + public EmrErrorDetail() {} + + public EmrErrorDetail(String errorCode, List> errorData, String errorMessage) { + this.errorCode = errorCode; + this.errorData = errorData; + this.errorMessage = errorMessage; + } + + public String getErrorCode() { return errorCode; } + public void setErrorCode(String errorCode) { this.errorCode = errorCode; } + + public List> getErrorData() { return errorData; } + public void setErrorData(List> errorData) { this.errorData = errorData; } + + public String getErrorMessage() { return errorMessage; } + public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } +} diff --git a/src/test/java/io/floci/aws/services/emr/EmrServiceContainerStepsTest.java b/src/test/java/io/floci/aws/services/emr/EmrServiceContainerStepsTest.java index 0af63eacbf..74cde8386f 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrServiceContainerStepsTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrServiceContainerStepsTest.java @@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.ContainerTeardown; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.services.emr.model.EmrCluster; +import io.floci.aws.services.emr.model.EmrErrorDetail; import io.floci.aws.services.emr.model.EmrInstanceFleet; import io.floci.aws.services.emr.model.EmrInstanceGroup; import io.floci.aws.services.emr.model.EmrStep; @@ -59,6 +60,7 @@ class EmrServiceContainerStepsTest { private static final String REPORT = "s3://bucket/jobs/report.py"; private static final String DRIVER_FAILED = "The Spark driver exited with code 1."; private static final String INSTALL = "s3://bucket/bootstrap/install.sh"; + private static final String ERROR_CODE_PAGES = "https://docs.aws.amazon.com/emr/latest/ManagementGuide/"; // The id of the image a bootstrap kept, which the cluster's steps run in. private static final String IMAGE = "sha256:" + "2".repeat(64); @@ -1433,6 +1435,71 @@ class EmrServiceContainerStepsTest { }); } + @Test + void aFailedActionOrAScriptThatCannotBeReadGivesTheErrorDetailOfItsCodeAndABootstrapThatCannotRunNone() { + String failedPage = ERROR_CODE_PAGES + "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE.html"; + String unreadPage = ERROR_CODE_PAGES + "BOOTSTRAP_FAILURE_BA_DOWNLOAD_FAILED_PRIMARY.html"; + Map>> details = Map.of( + BootstrapRunner.Outcome.actionFailed(2, 7), List.of(List.of( + "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE", + List.of(List.of("primary-instance-id=i-00000000000000001", "bootstrap-action=2", + "return-code=7", "amazon-s3-path=s3://bucket/bootstrap/jars.sh", + "public-doc=" + failedPage)), + "On the master instance (i-00000000000000001), bootstrap action 2 returned a non-zero return" + + " code. For more information, see " + failedPage)), + BootstrapRunner.Outcome.notDownloaded(1, "The specified key does not exist."), List.of(List.of( + "BOOTSTRAP_FAILURE_BA_DOWNLOAD_FAILED_PRIMARY", + List.of(List.of("primary-instance-id=i-00000000000000001", "bootstrap-action=1", + "amazon-s3-path=" + INSTALL, "public-doc=" + unreadPage)), + "On the master instance (i-00000000000000001), bootstrap action 1 could not be read from " + + INSTALL + ": The specified key does not exist. For more information, see " + + unreadPage)), + // No ErrorDetail code is an image that cannot be pulled. + BootstrapRunner.Outcome.couldNotRun("the image could not be pulled"), List.of()); + details.forEach((outcome, expected) -> { + bootstrap.outcome = outcome; + EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null); + EmrCluster cluster = bootstrapping.service.runJobFlow(bootstrapped(cluster("broken", true)), REGION); + + bootstrapping.runStepThread(); + + assertEquals(expected, errorDetails(bootstrapping.service.describeCluster(cluster.getId()))); + assertEquals(expected, errorDetails(bootstrapping.restart().service.describeCluster(cluster.getId()))); + }); + } + + @Test + void describeClusterAndListClustersGiveTheErrorDetailsOfAClusterAFailedActionEndedAndNoneForAnother() + throws Exception { + bootstrap.outcome = BootstrapRunner.Outcome.actionFailed(1, 3); + EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null); + String failedId = bootstrapping.service.runJobFlow(bootstrapped(cluster("failed", true)), REGION).getId(); + bootstrapping.runStepThread(); + String waitingId = bootstrapping.service.runJobFlow(cluster("waiting", true), REGION).getId(); + ObjectMapper objectMapper = new ObjectMapper(); + EmrHandler handler = new EmrHandler(bootstrapping.service, objectMapper); + String page = ERROR_CODE_PAGES + "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE.html"; + JsonNode expected = objectMapper.readTree(""" + [{"ErrorCode": "BOOTSTRAP_FAILURE_PRIMARY_WITH_NON_ZERO_CODE", + "ErrorData": [{"primary-instance-id": "i-00000000000000001", "bootstrap-action": "1", + "return-code": "3", "amazon-s3-path": "%s", "public-doc": "%s"}], + "ErrorMessage": "On the master instance (i-00000000000000001), bootstrap action 1 returned a \ + non-zero return code. For more information, see %s"}]""".formatted(INSTALL, page, page)); + + Map statuses = Map.of( + failedId, describedStatus(handler, objectMapper, failedId), + waitingId, describedStatus(handler, objectMapper, waitingId)); + JsonNode listed = ((JsonNode) handler.handle("ListClusters", objectMapper.createObjectNode(), REGION) + .getEntity()).path("Clusters"); + + assertEquals(expected, statuses.get(failedId).path("ErrorDetails")); + assertFalse(statuses.get(waitingId).has("ErrorDetails")); + assertEquals(2, listed.size()); + for (JsonNode summary : listed) { + assertEquals(statuses.get(summary.path("Id").asText()), summary.path("Status")); + } + } + @Test void aBootstrapHandedOffBeforeATeardownLeavesItsClusterBootstrappingAndStartsNoStep() { EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null); @@ -1678,6 +1745,7 @@ class EmrServiceContainerStepsTest { assertEquals("TERMINATED_WITH_ERRORS", ended.getState()); assertEquals("INTERNAL_ERROR", ended.getStateChangeReasonCode()); assertEquals(EmrService.BOOTSTRAP_INTERRUPTED, ended.getStateChangeMessage()); + assertEquals(List.of(), ended.getErrorDetails()); } @Test @@ -1687,6 +1755,26 @@ class EmrServiceContainerStepsTest { assertEquals(List.of(Set.of()), bootstrap.kept); } + // A cluster's ErrorDetails, each as its code, each of its ErrorData maps as its pairs in order, and its message. + private static List> errorDetails(EmrCluster cluster) { + List> details = new ArrayList<>(); + for (EmrErrorDetail detail : cluster.getErrorDetails()) { + List> data = new ArrayList<>(); + for (Map pairs : detail.getErrorData()) { + data.add(pairs.entrySet().stream().map(pair -> pair.getKey() + "=" + pair.getValue()).toList()); + } + details.add(List.of(detail.getErrorCode(), data, detail.getErrorMessage())); + } + return details; + } + + // The Status DescribeCluster gives a cluster. + private static JsonNode describedStatus(EmrHandler handler, ObjectMapper objectMapper, String clusterId) { + JsonNode answer = (JsonNode) handler.handle("DescribeCluster", + objectMapper.createObjectNode().put("ClusterId", clusterId), REGION).getEntity(); + return answer.path("Cluster").path("Status"); + } + // Two bootstrap actions, as RunJobFlow stores them. private static EmrCluster bootstrapped(EmrCluster cluster) { cluster.setBootstrapActions("""