-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DROOLS-7589] ansible-rulebook : Throw Exception when heap reaches to…
… threshold
- Loading branch information
Showing
9 changed files
with
207 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
.../main/java/org/drools/ansible/rulebook/integration/api/rulesengine/MemoryMonitorUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.drools.ansible.rulebook.integration.api.rulesengine; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MemoryMonitorUtil { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(MemoryMonitorUtil.class.getName()); | ||
|
||
public static final String MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD_PROPERTY = "drools.memory.occupation.percentage.threshold"; | ||
|
||
private static final int DEFAULT_MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD = 90; | ||
|
||
private static final int MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD; | ||
|
||
static { | ||
String envValue = System.getenv("DROOLS_MEMORY_THRESHOLD"); | ||
if (envValue != null && !envValue.isEmpty()) { | ||
// Environment variable takes precedence over system property | ||
System.setProperty(MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD_PROPERTY, envValue); | ||
} | ||
MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD = Integer.getInteger(MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD_PROPERTY, DEFAULT_MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD); // percentage | ||
} | ||
|
||
private MemoryMonitorUtil() { | ||
// do not instantiate | ||
} | ||
|
||
public static void checkMemoryOccupation() { | ||
int memoryOccupationPercentage = getMemoryOccupationPercentage(); | ||
if (memoryOccupationPercentage > MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD) { | ||
System.gc(); // NOSONAR | ||
// double check to avoid frequent GC | ||
memoryOccupationPercentage = getMemoryOccupationPercentage(); | ||
if (memoryOccupationPercentage > MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD) { | ||
LOG.error("Memory occupation is above the threshold: {}% > {}%. MaxMemory = {}, UsedMemory = {}", | ||
memoryOccupationPercentage, MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD, Runtime.getRuntime().maxMemory(), Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()); | ||
throw new MemoryThresholdReachedException(MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD, memoryOccupationPercentage); | ||
} | ||
} | ||
} | ||
|
||
private static int getMemoryOccupationPercentage() { | ||
return (int) ((100 * (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())) / Runtime.getRuntime().maxMemory()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../drools/ansible/rulebook/integration/api/rulesengine/MemoryThresholdReachedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.drools.ansible.rulebook.integration.api.rulesengine; | ||
|
||
public class MemoryThresholdReachedException extends RuntimeException { | ||
|
||
private final int threshold; | ||
private final int actual; | ||
|
||
public MemoryThresholdReachedException(int threshold, int actual) { | ||
this.threshold = threshold; | ||
this.actual = actual; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return "Memory threshold reached: " + actual + "% > " + threshold + "%"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...egration-main/src/test/resources/1m_event_with_20kb_payload_match_multiple_rules_ast.json
Large diffs are not rendered by default.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
...le-rulebook-integration-main/src/test/resources/1m_event_with_20kb_payload_rules_ast.json
Large diffs are not rendered by default.
Oops, something went wrong.