YARN-7914. Fix exit code handling for short lived Docker containers. Contributed by Shane Kumpf

(cherry picked from commit 5a1db60ab1)
This commit is contained in:
Jason Lowe 2018-02-12 15:50:10 -06:00
parent 600e919469
commit 5f7ef73b7a
1 changed files with 46 additions and 46 deletions

View File

@ -1577,58 +1577,58 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
sleep(1); sleep(1);
} }
#endif #endif
}
sprintf(docker_inspect_exitcode_command, sprintf(docker_inspect_exitcode_command,
"%s inspect --format {{.State.ExitCode}} %s", "%s inspect --format {{.State.ExitCode}} %s",
docker_binary, container_id); docker_binary, container_id);
fprintf(LOGFILE, "Obtaining the exit code...\n"); fprintf(LOGFILE, "Obtaining the exit code...\n");
fprintf(LOGFILE, "Docker inspect command: %s\n", docker_inspect_exitcode_command); fprintf(LOGFILE, "Docker inspect command: %s\n", docker_inspect_exitcode_command);
FILE* inspect_exitcode_docker = popen(docker_inspect_exitcode_command, "r"); FILE* inspect_exitcode_docker = popen(docker_inspect_exitcode_command, "r");
if(inspect_exitcode_docker == NULL) { if(inspect_exitcode_docker == NULL) {
fprintf(ERRORFILE, "Done with inspect_exitcode, inspect_exitcode_docker is null\n"); fprintf(ERRORFILE, "Done with inspect_exitcode, inspect_exitcode_docker is null\n");
fflush(ERRORFILE); fflush(ERRORFILE);
exit_code = -1; exit_code = -1;
goto cleanup; goto cleanup;
} }
res = fscanf (inspect_exitcode_docker, "%d", &exit_code); res = fscanf (inspect_exitcode_docker, "%d", &exit_code);
if (pclose (inspect_exitcode_docker) != 0 || res <= 0) { if (pclose (inspect_exitcode_docker) != 0 || res <= 0) {
fprintf (ERRORFILE, fprintf (ERRORFILE,
"Could not inspect docker to get exitcode: %s.\n", docker_inspect_exitcode_command); "Could not inspect docker to get exitcode: %s.\n", docker_inspect_exitcode_command);
fflush(ERRORFILE); fflush(ERRORFILE);
exit_code = -1; exit_code = -1;
goto cleanup; goto cleanup;
} }
fprintf(LOGFILE, "Exit code from docker inspect: %d\n", exit_code); fprintf(LOGFILE, "Exit code from docker inspect: %d\n", exit_code);
if(exit_code != 0) { if(exit_code != 0) {
fprintf(ERRORFILE, "Docker container exit code was not zero: %d\n", fprintf(ERRORFILE, "Docker container exit code was not zero: %d\n",
exit_code); exit_code);
snprintf(docker_logs_command, command_size, "%s logs --tail=250 %s", snprintf(docker_logs_command, command_size, "%s logs --tail=250 %s",
docker_binary, container_id); docker_binary, container_id);
FILE* logs = popen(docker_logs_command, "r"); FILE* logs = popen(docker_logs_command, "r");
if(logs != NULL) { if(logs != NULL) {
clearerr(logs); clearerr(logs);
res = fread(buffer, BUFFER_SIZE, 1, logs); res = fread(buffer, BUFFER_SIZE, 1, logs);
if(res < 1) { if(res < 1) {
fprintf(ERRORFILE, "%s %d %d\n", fprintf(ERRORFILE, "%s %d %d\n",
"Unable to read from docker logs(ferror, feof):", ferror(logs), feof(logs)); "Unable to read from docker logs(ferror, feof):", ferror(logs), feof(logs));
fflush(ERRORFILE); fflush(ERRORFILE);
}
else {
fprintf(ERRORFILE, "%s\n", buffer);
fflush(ERRORFILE);
}
} }
else { else {
fprintf(ERRORFILE, "%s\n", "Failed to get output of docker logs"); fprintf(ERRORFILE, "%s\n", buffer);
fprintf(ERRORFILE, "Command was '%s'\n", docker_logs_command);
fprintf(ERRORFILE, "%s\n", strerror(errno));
fflush(ERRORFILE);
}
if(pclose(logs) != 0) {
fprintf(ERRORFILE, "%s\n", "Failed to fetch docker logs");
fflush(ERRORFILE); fflush(ERRORFILE);
} }
} }
else {
fprintf(ERRORFILE, "%s\n", "Failed to get output of docker logs");
fprintf(ERRORFILE, "Command was '%s'\n", docker_logs_command);
fprintf(ERRORFILE, "%s\n", strerror(errno));
fflush(ERRORFILE);
}
if(pclose(logs) != 0) {
fprintf(ERRORFILE, "%s\n", "Failed to fetch docker logs");
fflush(ERRORFILE);
}
} }
cleanup: cleanup: