Search code examples
javamultithreadingdroolskie

One drools rule not executed Intermittently


I have a set of drool rules in the file expected to fire on multiple fields of Java object. One particular rule is not fired on production randomly. I tried to reproduce with simulating prod load but it's not reproducible.

Scenario:
Default valube city = London

Rule:
If Name = John
then set City = Sydney

Issue:
Name = John
City = London //expected Sydney if rule execute properly based on drl file

Implementation:
I am using ExecuterService with FixedThreadPool of size 2.
Inside call() of Callable which is passed to ExecuterService submit(), I am creating a ConcurrentLinkedQueue of KieSession with size 3. The rule file is passed here to the KieSession constructor.

My Analysis:
I assume that this issue has something to do with multithreading and synchronization. As the KieSession object is shared between threads, can that cause such an issue?
Or is there any known issue with Drool rules which maybe I am unaware of?
If anyone has any leads please let me know.
Thanks in advance


Solution

  • Things I would check.

    1. Unlike execute(Runnable command), submit(Callable<T> task) will catch any throwable and set it into inner field. You must not forget to do Future#get() to reveal any exception happened in the background otherwise nobody will ever notice the error which interrupted your Callable. Or you can use execute and exception will kill the thread and appear in system error log.

    2. You refer to 'random behavior' and the only randomness came into my mind is that drools does not manifest predicted ordering for then blocks execution of the rules with the same salience. Could it be the case that you have dependencies between the rules and one rule creates condition that causes another rule not to start? And because of random nature of drools then blocks execution it happens rarely.

    3. Of course, you are responsible to make your classes thread safe and data to be accessed in thread safe manner. By default the same thread which fireAllRules() will execute then blocks. But if you create multiple threads you must think about thread safeness of your beans and data, (usually) not about drools itself. I see by default city is London, and it is not always getting updated by (parallel?) thread. You have it volatile, don't you?

    4. Write a test. You can't fix something you can't reproduce. Even intermittent issues can be reproduced by load or stress testing.