Skip to content

Commit

Permalink
[bamboo] Automated branch merge (from foundation-2018:04d370e62706a0e…
Browse files Browse the repository at this point in the history
…aab07fd0c1ec669affae66035)
  • Loading branch information
opennms-bamboo committed Nov 14, 2018
2 parents 4994d1a + 04d370e commit f8c81fa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ public abstract class EventConstants {
*/
public static final String RELOAD_TOPOLOGY_UEI = "uei.opennms.org/internal/reloadTopology";

/**
* Reload Drools Correlation Engine.
*/
public static final String DROOLS_ENGINE_ENCOUNTERED_EXCEPTION = "uei.opennms.org/internal/droolsEngineException";

/**
* Reload Daemon configuration UEI.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@
<severity>Minor</severity>
<alarm-data reduction-key="%uei%:%dpname%:%nodeid%:%interface%:%service%" alarm-type="3" auto-clean="false"/>
</event>
</events>
<event>
<uei>uei.opennms.org/internal/droolsEngineException</uei>
<event-label>OpenNMS-defined Drools Engine Encountered Exception</event-label>
<descr>Drools engine encountered an exception while running rules</descr>
<logmsg dest="logndisplay">
Drools engine rule %parm[enginename]% has encountered an exception : %parm[stacktrace]%.
</logmsg>
<severity>Normal</severity>
<alarm-data reduction-key="%uei%:%parm[enginename]%" alarm-type="3"/>
</event>
</events>
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.core.RuleBaseConfiguration;
import org.drools.core.RuleBaseConfiguration.AssertBehaviour;
Expand All @@ -62,6 +63,7 @@
import org.opennms.netmgt.correlation.drools.config.RuleSet;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.xml.event.AlarmData;
import org.opennms.netmgt.xml.event.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -122,7 +124,11 @@ public ApplicationContext getConfigContext() {
public synchronized void correlate(final Event e) {
LOG.debug("Begin correlation for Event {} uei: {}", e.getDbid(), e.getUei());
m_kieSession.insert(e);
if (!m_isStreaming) m_kieSession.fireAllRules();
try {
if (!m_isStreaming) m_kieSession.fireAllRules();
} catch (Exception e1) {
LOG.error("Exception while firing rules ", e1);
}
m_eventsMeter.mark();
LOG.debug("End correlation for Event {} uei: {}", e.getDbid(), e.getUei());
}
Expand All @@ -133,7 +139,11 @@ protected synchronized void timerExpired(final Integer timerId) {
LOG.info("Begin correlation for Timer {}", timerId);
TimerExpired expiration = new TimerExpired(timerId);
m_kieSession.insert(expiration);
if (!m_isStreaming) m_kieSession.fireAllRules();
try {
if (!m_isStreaming) m_kieSession.fireAllRules();
} catch (Exception e) {
LOG.error("Exception while firing rules ", e);
}
LOG.debug("Begin correlation for Timer {}", timerId);
}

Expand Down Expand Up @@ -214,11 +224,24 @@ public void initialize() throws Exception {
if (m_isStreaming) {
new Thread(() -> {
Logging.putPrefix(getClass().getSimpleName() + '-' + getName());
m_kieSession.fireUntilHalt();
try {
m_kieSession.fireUntilHalt();
} catch (Exception e) {
LOG.error("Exception while running rules, reloading engine ", e);
triggerAlarm(e);
reloadConfig();
}
}, "FireTask").start();
}
}

private void triggerAlarm(Exception exception) {
EventBuilder eventBldr = new EventBuilder(EventConstants.DROOLS_ENGINE_ENCOUNTERED_EXCEPTION, getName());
eventBldr.addParam("enginename", getName());
eventBldr.addParam("stracktrace", ExceptionUtils.getStackTrace(exception));
sendEvent(eventBldr.getEvent());
}

private void loadRules(final KieFileSystem kfs) throws DroolsParserException, IOException {
int k = 0;
for (final Resource rulesFile : m_rules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ public class FusionRulesTest extends CorrelationRulesTestCase {

@Test
public void testDroolsFusion() throws Exception {

anticipate(createNodeUpEvent(1));

anticipate(new EventBuilder(EventConstants.RELOAD_DAEMON_CONFIG_SUCCESSFUL_UEI, "droolsFusion").getEvent());
anticipate(new EventBuilder(EventConstants.DROOLS_ENGINE_ENCOUNTERED_EXCEPTION, "droolsFusion").getEvent());
EventBuilder eb = new EventBuilder("uei.opennms.org/triggerTestForFusion", "droolsTest");
DroolsCorrelationEngine engine = findEngineByName("droolsFusion");

// This will create an exception and the engine should reload.
engine.correlate(eb.getEvent());
Thread.sleep(5000); // Wait time for the engine to reload.
// The engine should run other rules after reload.
engine.correlate(createNodeLostServiceEvent(1, "SSH"));
Thread.sleep(4000); // Give time to execute the time-based rules.
m_anticipatedMemorySize = 5;
engine.getKieSessionObjects().forEach(System.err::println);

verify(engine);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import org.opennms.netmgt.xml.event.Value;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.model.events.EventUtils;
import org.opennms.netmgt.events.api.EventConstants;
import org.drools.core.spi.KnowledgeHelper;
import org.drools.core.spi.KnowledgeHelper
import java.util.ConcurrentModificationException;

global DroolsCorrelationEngine engine;

Expand All @@ -34,6 +35,17 @@ then
engine.sendEvent(eventBuilder.getEvent());
end


rule "this-rule-should-throw-exception"
salience 400
when $e : Event(uei matches "uei.opennms.org/triggerTestForFusion")

then
System.err.println("running exception rule, this will throw exception");
// Generate ConcurrentModification Exception. see NMS-10363
throw new ConcurrentModificationException();
end

rule "test-two-things-in-a-hmmm-wtf"
salience 100
when
Expand Down

0 comments on commit f8c81fa

Please sign in to comment.