Skip to content

Improve AWS Batch spot reclamation error messages #6253

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
return result.join(' - ')
}

protected boolean isSpotReclamationError(JobDetail job) {
if(!job)
return false
// Check if the error is related to spot instance reclamation
// AWS Batch uses "Host EC2*" pattern for spot reclamation events
final statusReason = job.statusReason
return statusReason && statusReason.startsWith('Host EC2')
}

protected String formatSpotReclamationError(JobDetail job) {
final baseReason = errReason(job)
final maxAttempts = maxSpotAttempts()
final StringBuilder message = new StringBuilder()

message.append("AWS Batch job failed due to EC2 spot instance reclamation.")
message.append("\n\nOriginal error: ").append(baseReason)

if( maxAttempts == 0 ) {
message.append("\n\nTo automatically retry jobs when spot instances are reclaimed, "
+ "set 'aws.batch.maxSpotAttempts' to a value greater than 0 in your configuration. "
+ "For example: aws.batch.maxSpotAttempts = 5")
} else {
message.append("\n\nThis job was configured to retry up to ").append(maxAttempts)
.append(" times on spot reclamation, but all attempts failed.")
}

message.append("\n\nFor more information about spot instance interruptions, see: "
+ "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-interruptions.html")

return message.toString()
}

/**
* {@inheritDoc}
*/
Expand All @@ -299,6 +331,11 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
final job = describeJob(jobId)
final done = job?.status in ['SUCCEEDED', 'FAILED']
if( done ) {
// Log retry attempts for spot reclamation visibility
final attemptCount = job?.attempts?.size() ?: 0
if( attemptCount > 1 ) {
log.info "[AWS BATCH] Process `${task.lazyName()}` completed after ${attemptCount} attempts (job=${jobId})"
}
// take the exit code of the container, if 0 (successful) or missing
// take the exit code from the `.exitcode` file create by nextflow
// the rationale of this is that, in case of error, the exit code return
Expand All @@ -310,7 +347,11 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
final reason = errReason(job)
// retry all CannotPullContainer errors apart when it does not exist or cannot be accessed
final unrecoverable = reason.contains('CannotPullContainer') && reason.contains('unauthorized')
task.error = unrecoverable ? new ProcessUnrecoverableException(reason) : new ProcessException(reason)

// Check for spot reclamation errors and provide clearer error messages
final errorMessage = isSpotReclamationError(job) ? formatSpotReclamationError(job) : reason

task.error = unrecoverable ? new ProcessUnrecoverableException(errorMessage) : new ProcessException(errorMessage)
task.stderr = executor.getJobOutputStream(jobId) ?: errorFile
}
else {
Expand Down Expand Up @@ -789,6 +830,7 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
.withAttempts( attempts )
.withEvaluateOnExit(cond1, cond2)
result.setRetryStrategy(retry)
log.debug "[AWS BATCH] Process `${task.lazyName()}` configured for spot reclamation retry (maxSpotAttempts=${attempts})"
}

// set task timeout
Expand Down
Loading