Source:
<code>private void terminateContainerGracefully(DockerCloud dc, Container container) throws TerminationException, ContainerIsTaintedException { String containerId = container.getId(); Map<String, String> containerLabels = container.getLabels(); String removeVolumesString = containerLabels.get(DockerContainerLabelKeys.REMOVE_VOLUMES); if (removeVolumesString == null) { throw new ContainerIsTaintedException("The removeVolumes label is missing; thus the container must have been " <START> + "mangled unexpectedly; not cleaning up at all anymore, as another tool must have touched it"); <END> } boolean removeVolumes = Boolean.parseBoolean(removeVolumesString); boolean containerRunning = true; if (container.getStatus().startsWith("Dead") || container.getStatus().startsWith("Exited") || container.getStatus().startsWith("Created")) { containerRunning = false; } DockerAPI dockerApi = dc.getDockerApi(); boolean success = stopAndRemoveContainer(dockerApi, LOGGER, String.format("%s is terminating detached Container %s", DockerContainerWatchdog.class.getSimpleName(), containerId), removeVolumes, container.getId(), !containerRunning); if (success) { LOGGER.info("Successfully terminated container consistently", containerId); } else { throw new TerminationException("Graceful termination failed; see logs for detials"); } }</code><technical_language>suggest (a) user find (b) speculate causes. e.g. throw new ContainerIsTaintedException("Container ID " + container.getId() + " " + DockerContainerLabelKeys.REMOVE_VOLUMES + " label.");</technical_language>

Target:
private void terminateContainerGracefully(DockerCloud dc, Container container) throws TerminationException, ContainerIsTaintedException { String containerId = container.getId(); Map<String, String> containerLabels = container.getLabels(); final String nodeName = containerLabels.get(DockerContainerLabelKeys.NODE_NAME); if (nodeExistsBypassingCache(nodeName)) { LOGGER.warn("Attempt to terminate container ID gracefully, but a node for it suddenly has appeared", container.getId()); throw new ContainerIsTaintedException(String.format("Node for container ID %s has appeared", container.getId())); } containerLabels.get(DockerContainerLabelKeys.REMOVE_VOLUMES); String removeVolumesString = containerLabels.get(DockerContainerLabelKeys.REMOVE_VOLUMES); if (removeVolumesString == null) { throw new ContainerIsTaintedException(String.format("Container ID %s has no '%s' label; skipping.", container.getId(), DockerContainerLabelKeys.REMOVE_VOLUMES)); } boolean removeVolumes = Boolean.parseBoolean(removeVolumesString); boolean containerRunning = true; if (container.getStatus().startsWith("Dead") || container.getStatus().startsWith("Exited") || container.getStatus().startsWith("Created")) { containerRunning = false; } DockerAPI dockerApi = dc.getDockerApi(); Instant start = clock.instant(); boolean success = stopAndRemoveContainer(dockerApi, LOGGER, String.format("(orphaned container found by %s)", DockerContainerWatchdog.class.getSimpleName()), removeVolumes, container.getId(), !containerRunning); Instant stop = clock.instant(); if (success) { executionStatistics.addContainerRemovalGracefully(Duration.between(start, stop).toMillis()); LOGGER.info("Successfully terminated orphaned container {}", containerId); } else { throw new TerminationException("Graceful termination failed."); } }