Skip to content
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

Return a Unique Exit Code for Out of Memory Incidents #1339

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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 @@ -25,6 +25,7 @@ public enum ExitCodeType {
),

FAILURE_ACCURACY_NOT_MET(15, "Detect was unable to meet the required accuracy."),
FAILURE_OUT_OF_MEMORY(16, "Detect encountered an Out of Memory error. Please review memory settings and system resources.", 0.5),

FAILURE_IMAGE_NOT_AVAILABLE(20, "Image scan attempted but no return data available."),

Expand All @@ -35,26 +36,30 @@ public enum ExitCodeType {

private final int exitCode;
private final String description;
private final double priority;

ExitCodeType(int exitCode, String description) {
this(exitCode, description, (double) exitCode);
}

ExitCodeType(int exitCode, String description, double priority) {
this.exitCode = exitCode;
this.description = description;
this.priority = priority;
}

/**
* A failure always beats a success and a failure with a lower exit code beats a failure with a higher exit code.
* A failure always beats a success. Among failures:
* - The one with a lower priority wins.
* - If priorities are equal, the one with a lower exit code wins.
*/
public static ExitCodeType getWinningExitCodeType(ExitCodeType first, ExitCodeType second) {
if (first.isSuccess()) {
return second;
} else if (second.isSuccess()) {
return first;
} else {
if (first.getExitCode() < second.getExitCode()) {
return first;
} else {
return second;
}
return (first.getPriority() < second.getPriority()) ? first : second;
}
}

Expand All @@ -69,4 +74,6 @@ public boolean isSuccess() {
public String getDescription() {
return description;
}

public double getPriority() { return priority; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.blackduck.integration.detect.workflow.status.DetectIssue;
import com.blackduck.integration.detect.workflow.status.DetectIssueType;
import com.blackduck.integration.detect.workflow.status.StatusEventPublisher;
import com.blackduck.integration.detector.base.DetectorStatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DetectorIssuePublisher {
public void publishIssues(StatusEventPublisher statusEventPublisher, List<DetectorDirectoryReport> reports) {
Expand All @@ -25,4 +28,12 @@ public void publishIssues(StatusEventPublisher statusEventPublisher, List<Detect
}
}

public boolean hasOutOfMemoryIssue(List<DetectorDirectoryReport> reports) {
return reports.stream()
.flatMap(report -> report.getNotExtractedDetectors().stream())
.flatMap(notExtracted -> notExtracted.getAttemptedDetectables().stream())
.anyMatch(attemptedDetectableReport -> {
return attemptedDetectableReport.getStatusCode() == DetectorStatusCode.EXECUTABLE_TERMINATED_LIKELY_OUT_OF_MEMORY;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import com.blackduck.integration.detector.base.DetectorStatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -108,13 +109,22 @@ public DetectorToolResult performDetectors(
List<DetectorDirectoryReport> reports = new DetectorReporter().generateReport(evaluation);
DetectorToolResult toolResult = publishAllResults(reports, directory, projectDetector, requiredDetectors, requiredAccuracyTypes);

checkAndHandleOutOfMemoryIssue(reports);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might just be me but I find this error checking a bit distracting in the overall flow of the performDetectors method. Perhaps consider refactoring these lines to a small method you can call.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored this error checking into a function called checkAndHandleOutOfMemoryIssue

//Completed.
logger.debug("Finished running detectors.");
detectorEventPublisher.publishDetectorsComplete(toolResult);

return toolResult;
}

private void checkAndHandleOutOfMemoryIssue(List<DetectorDirectoryReport> reports) {
if (detectorIssuePublisher.hasOutOfMemoryIssue(reports)) {
logger.error("Detected an issue. " + DetectorStatusCode.EXECUTABLE_TERMINATED_LIKELY_OUT_OF_MEMORY.getDescription());
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_OUT_OF_MEMORY, "Executable terminated likely due to out of memory.");
}
}

private DetectorToolResult publishAllResults(
List<DetectorDirectoryReport> reports,
File directory,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.blackduck.integration.detect.configuration;

import com.blackduck.integration.detect.configuration.enumeration.ExitCodeType;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;

class ExitCodeTypeTest {

@Test
void testUniquePriorities() {
Set<Double> priorities = new HashSet<>();
for (ExitCodeType exitCode : ExitCodeType.values()) {
assertTrue(priorities.add(exitCode.getPriority()),
"Duplicate priority found: " + exitCode.getPriority());
}
}

@Test
void testUniqueExitCodes() {
Set<Integer> exitCodes = new HashSet<>();
for (ExitCodeType exitCode : ExitCodeType.values()) {
assertTrue(exitCodes.add(exitCode.getExitCode()),
"Duplicate exit code found: " + exitCode.getExitCode());
}
}
}