Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.

Bug 1106300

Summary: NPE at org.drools.core.util.index.LeftTupleIndexHashTable.remove for Stateful Knowledge Session
Product: [Retired] JBoss BRMS Platform 6 Reporter: Thiago Araki <taraki>
Component: BREAssignee: Mario Fusco <mfusco>
Status: CLOSED CURRENTRELEASE QA Contact: Marek Winkler <mwinkler>
Severity: high Docs Contact:
Priority: high    
Version: 6.0.1CC: etirelli, kverlaen, mfusco, rrajasek
Target Milestone: CR1   
Target Release: 6.0.2   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2014-08-06 19:54:45 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
Project with a test case
none
Project with a new test case none

Description Thiago Araki 2014-06-09 04:36:24 UTC
Created attachment 904461 [details]
Project with a test case

Description of problem:
I am creating an application where there is a set of sensors sending feeds and rules which have to raise alerts with different levels based on average values. After some correct executions, this NPE is occuring.

Version-Release number of selected component (if applicable):
JBoss BRMS 6.0.1 GA

How reproducible:
Easy. Use the attached project (test-case.zip).

Steps to Reproduce:
1. Create rules that receive events from an entry-point and calculate average values with Accumulate function in a window with length 10.
2. Create an Stateful Knowledge Session with Stream Mode and use the method fireUntilhalt() to execute rules.
3. Insert events in the entry point until the "Null Pointer Exception" occurs

Actual results:
Exception in thread "Thread-0" java.lang.NullPointerException
	at org.drools.core.util.index.LeftTupleIndexHashTable.remove(LeftTupleIndexHashTable.java:386)
	at org.drools.core.phreak.RuleNetworkEvaluator.doUpdatesReorderLeftMemory(RuleNetworkEvaluator.java:799)
	at org.drools.core.phreak.PhreakJoinNode.doNode(PhreakJoinNode.java:38)
	at org.drools.core.phreak.RuleNetworkEvaluator.switchOnDoBetaNode(RuleNetworkEvaluator.java:547)
	at org.drools.core.phreak.RuleNetworkEvaluator.evalBetaNode(RuleNetworkEvaluator.java:533)
	at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:334)
	at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:161)
	at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:116)
	at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:194)
	at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:67)
	at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:935)
	at org.drools.core.common.DefaultAgenda.fireUntilHalt(DefaultAgenda.java:1160)
	at org.drools.core.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:1002)
	at org.drools.core.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:978)
	at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireUntilHalt(StatefulKnowledgeSessionImpl.java:273)
	at br.com.redhat.testcase.Runner$1.run(TestCase.java:30)

Expected results:
Rules should fire correctly

Additional info:
I am attaching a maven project (test-case.zip) which has a test case for this scenario. You just need to import it, execute main class "TestCase" and check results on console. After the sixth event inserted, the NPE occurs.

Comment 2 Mario Fusco 2014-06-09 14:55:04 UTC
Thiago,

I am investigating the issue you reported using your reproducer (thanks a lot for it). Indeed we have a bug there and I'll try to fix it asap.

In the meanwhile if you need a workaround for it you can try to replace the

eval ($cnt > 0)

after the accumulates with a 

exists Reading( type == $type ) 

before it.

Comment 3 Thiago Araki 2014-06-09 22:56:04 UTC
Hello Mario, thanks for investigating it.

I tried the workaround you explained (see one of my rules below), but I had the same results.

Is there something else I should be doing?


rule "Abnormal"
dialect "mvel"

when
	threshold : Threshold ($type : type, $limHL : limitHardLow, $limSL : limitSoftLow, $limHH : limitHardHigh, $limSH : limitSoftHigh)
	exists Reading( type == $type ) from entry-point "entrypoint"
	accumulate( reading : Reading( type == $type ) over window:length( 10 ) from entry-point "entrypoint", $avg : average( reading.value ), $cnt : count( 1 ) )
	eval (($avg < $limSL && $avg >= $limHL) || ($avg > $limSH && $avg <= $limHH))
	not Alarm (type == $type, endDate == null)

then
	Alarm alarm = new Alarm();
	alarm.setType($type);
	alarm.setBeginDate(new java.util.Date());
        alarm.setLevel( "ABNORMAL" );
	insert(alarm);
	System.out.println("CREATED ABNORMAL ALARM - VALUE = " + $avg);
end

Comment 4 Mario Fusco 2014-06-10 07:02:08 UTC
Sorry Thiago, you're right. I am still investigating the problem and I believe the bug is somewhat related with evals. So, in addition to this, you should also eliminate the 2nd eval. For instance you could replace it with a pattern matching like it follows.

rule "Abnormal"
dialect "mvel"

when
	threshold : Threshold ($type : type, $limHL : limitHardLow, $limSL : limitSoftLow, $limHH : limitHardHigh, $limSH : limitSoftHigh)
	exists Reading( type == $type ) from entry-point "entrypoint"
	Number( floatValue < $limSL && floatValue >= $limHL ) from accumulate( reading : Reading( type == $type ) over window:length( 10 ) from entry-point "entrypoint", average( reading.value ) )
	not Alarm (type == $type, endDate == null)

then
	Alarm alarm = new Alarm();
	alarm.setType($type);
	alarm.setBeginDate(new java.util.Date());
        alarm.setLevel( "ABNORMAL" );
	insert(alarm);
	System.out.println("CREATED ABNORMAL ALARM - VALUE = " + $avg);
end

Let me know if this works for you.

Mario

Comment 5 Thiago Araki 2014-06-10 19:16:49 UTC
Mario, I removed the "evals" from my rules, and changed them all to use the pattern matching as you instructed, but now I am receiving the following error after the second fact insertion:

Exception in thread "Thread-0" java.lang.NullPointerException
	at org.drools.core.common.DoubleBetaConstraints.isAllowedCachedLeft(DoubleBetaConstraints.java:108)
	at org.drools.core.phreak.PhreakAccumulateNode.evaluateResultConstraints(PhreakAccumulateNode.java:710)
	at org.drools.core.phreak.PhreakAccumulateNode.doNode(PhreakAccumulateNode.java:89)
	at org.drools.core.phreak.RuleNetworkEvaluator.switchOnDoBetaNode(RuleNetworkEvaluator.java:562)
	at org.drools.core.phreak.RuleNetworkEvaluator.evalBetaNode(RuleNetworkEvaluator.java:533)
	at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:334)
	at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:161)
	at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:116)
	at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:194)
	at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:67)
	at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:935)
	at org.drools.core.common.DefaultAgenda.fireUntilHalt(DefaultAgenda.java:1160)
	at org.drools.core.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:1002)
	at org.drools.core.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:978)
	at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireUntilHalt(StatefulKnowledgeSessionImpl.java:273)
	at br.com.redhat.testcase.Runner$1.run(TestCase.java:30)

Comment 6 Mario Fusco 2014-06-11 07:04:48 UTC
Thiago, this seems to be a different issue and I cannot reproduce it. Can you please also attach your modified test case reproducing this problem?

Thanks,
Mario

Comment 7 Mario Fusco 2014-06-11 07:50:02 UTC
Fixed by https://github.com/droolsjbpm/drools/commit/320f34fd0

Comment 8 Mario Fusco 2014-06-11 07:51:43 UTC
Cherry-picked to 6.1.x branch with https://github.com/droolsjbpm/drools/commit/f6af4dbc1

Comment 9 Thiago Araki 2014-06-11 12:06:59 UTC
Created attachment 907611 [details]
Project with a new test case

Mario, I am attaching "test-case2" project that reproduces this different issue (NPE at  org.drools.core.common.DoubleBetaConstraints.isAllowedCachedLeft). 

If this is the case, I can open a new BZ, but please let me know.

Thanks,

Thiago

Comment 11 Mario Fusco 2014-06-12 06:56:45 UTC
Cherry-picked to 6.0.x branch with https://github.com/droolsjbpm/drools/commit/0f990e02b

Comment 12 Marek Winkler 2014-06-18 17:57:33 UTC
Verified on BRMS 6.0.2 CR1.