Approach to reuse KieSession

3,334 views
Skip to first unread message

sumit dhaniya

unread,
Jan 11, 2015, 9:42:50 AM1/11/15
to drools...@googlegroups.com
I'm working on a highly concurrent application in which I need to execute rules for lots of user requests. Creating a new object of KieSession for each request was taking too much time and contributing to GC due to which I started reusing couple of KieSession by deleting all the facts after execution of all rules. Can you please confirm if there's any pitfalls in using this approach.
Please find the code snippet below :-


private Map<Object, KieSession> kieSessionMap = new HashMap<>();


public void ruleMethod(A a) {
       
KieSession kieSession = null;
       
if (kieSessionMap.get(Thread.currentThread()) != null) {
            kieSession
= kieSessionMap.get(Thread.currentThread());
       
} else {
            kieSession
= sortingWeightageKB.newKieSession();
            kieSessionMap
.put(Thread.currentThread(), kieSession);
       
}


       
for (RuleFactor ruleFactor : ruleFactors) {
            kieSession
.insert(ruleFactor);
       
}
        kieSession
.fireAllRules();
       
for (FactHandle factHandle : kieSession.getFactHandles()) {
            kieSession
.delete(factHandle);
       
}
}

David W

unread,
Jan 13, 2015, 1:52:15 PM1/13/15
to drools...@googlegroups.com
The main issue with this approach is that each user will use the same (default) KieBase. The kie base contains all the rules, globals etc so every user will execute every user's rules. You probably dont want that.
If you want each user to only execute their own rules, you need to create and save a KieBase for each user too.

David.

This email is intended solely for the recipients named above and may contain information that is confidential, privileged or legally protected.  If you receive this communication in error, please immediately notify the sender by return e-mail and delete all copies of the original email and attachments.

sumit dhaniya

unread,
Jan 15, 2015, 1:55:13 AM1/15/15
to drools...@googlegroups.com
Rules are not specific to particular user ,checks are in the rules based on user profile which I insert before execution of rules and delete from session after execution . I'm not keeping any global variables.

--
You received this message because you are subscribed to a topic in the Google Groups "Drools Usage" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/drools-usage/-6iGRyrCr8M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/a0095872-4ba3-4e6a-b609-012540f1f5e9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Gergely Bacsó

unread,
Feb 18, 2015, 7:59:22 AM2/18/15
to drools...@googlegroups.com
Hi Sumit,

We just ran into a similar situation where we have been using very short-lived sessions with very high-frequency. After doing some profiling we found out that around half of the the time we previously assumed to be used for "Drools processing" was in fact spent on "Initializing new kie session". Checking the internals it turned out that the thread was doing loads of loading and searching via classloaders, mostly to obtain a new KnowledgeSessionConfiguration.

SOLUTION:
instead of:

initializing the container like this:
        kieContainer = kieServices.newKieContainer(releaseId);

and repeatedly calling this:
        return kieContainer.newKieSession(KIE_SESSION_ID);

we should:

initialize like this:
        kieContainer = kieServices.newKieContainer(releaseId);
        kieEnvironment = kieServices.newEnvironment();
        kieSessionConfiguration = kieServices.newKieSessionConfiguration();

and get new sessions like this:
        return kieContainer.newKieSession(KIE_SESSION_ID, kieEnvironment, kieSessionConfiguration);

This is a non-intrusive change which I believe is much safer to do than cleaning up and re-allocating previously used sessions. The effect of this change in terms of performance is substantial.

Cheers,
Gergely

Matteo Mortari

unread,
Mar 10, 2015, 7:31:24 AM3/10/15
to drools...@googlegroups.com
Hi, I also have one similar scenario for high-frequency session needs: basically to perform reasoning over Messages received for filtering, processing, etc.
But instead of "reusing couple of KieSession by deleting all the facts after execution of all rules" I basically use StatelessKieSession created ad-hoc for each Message instance; I'm not sure if you have considered this option for your use-case scenario too.
Maybe you did and cannot be applied because you need to get hold of some state-facts across a few calls; I thought however worth mentioning, in case this was missed.

Ciao,
MM

David Weir

unread,
Mar 10, 2015, 11:21:22 AM3/10/15
to drools...@googlegroups.com
Have you thought about storing the KieSession (using the marshaller) and retrieving it (using unmarshaller) for each user, rather than creating one each time?

Nest

unread,
Jun 15, 2015, 1:33:08 AM6/15/15
to drools...@googlegroups.com
Hi,

   I'm facing a problem in this line of code:
   return kieContainer.newKieSession(KIE_SESSION_ID, kieEnvironment, kieSessionConfiguration);
 
   what is KIE_SESSION_ID, if it is kieSessionId so how can i get it. I have used the following line of code but it always return me 0, after that if I'm trying to initialize with this id but it's not working.

     KieSession objKieSession = kieContainer.newKieSession(); 
     objKieSession.getId()

regards,
Nest

Mark Proctor

unread,
Jun 15, 2015, 5:18:30 AM6/15/15
to drools...@googlegroups.com, Mario Fusco
The searching of the KnowledgeSessionConfiguration is probably a regression, at some point this was cached for a given KieBase. We’ll look into fixing this for 6.3. Can you open a JIRA?

Mark
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

Nest

unread,
Jun 15, 2015, 7:02:02 AM6/15/15
to drools...@googlegroups.com
Hi Gergely,


   I'm facing a problem in this line of code:
   return kieContainer.newKieSession(
KIE_SESSION_ID, kieEnvironment, kieSessionConfiguration);
 
   what is KIE_SESSION_ID, if it is kieSessionId so how can i get it. I have used the following line of code but it always return me 0, after that if I'm trying to initialize with this id but it's not working.

     KieSession objKieSession = kieContainer.newKieSession(); 
     objKieSession.getId()

regards,
Nest


dhivakaran padmanabhan

unread,
Apr 17, 2017, 12:41:52 PM4/17/17
to Drools Usage
Hi,

Is the above suggested issue fixed ???

Thanks
Dhiva

Sushil Kumar

unread,
Dec 5, 2019, 6:50:02 AM12/5/19
to Drools Usage
Hi,
I have tried below solution but not able to create kieServices.newEnvironment(); as getting below error:
Unable to evaluate the expression Method threw 'java.lang.UnsupportedOperationException' exception.

so can you help me here


On Wednesday, February 18, 2015 at 6:29:22 PM UTC+5:30, Gergely Bacsó wrote:
Reply all
Reply to author
Forward
0 new messages