Drools performance as a rule engine using multiple threads
Asked Answered
B

0

6

I'm using a single threaded drools project as a first-match policy/condition evaluation engine. I'm using the following code to get a kieSession from the kieContainer, and evaluate the predicate based on the conditions.

KieContainer currentContainer = kieContainer.get();
StatelessKieSession newKIESession = currentContainer.newStatelessKieSession();
newKieSession.execute(predicate);

The evaluation takes about 10 ms for a given request. However, to increase the performance when I used 10 threads, each of these requests starting taking about 100ms effectively giving me the same performance with a larger number of threads.

  1. What is the RIGHT WAY to use multi-threading to improve condition evaluation in Drools?
  2. Should I spawn a new container for each processing thread instead spawning of a new session from existing containers?

    //SEE LAST LINE BELOW
    
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    
    byte[] drlAsBytes = retrieveDRLResource();
    kfs.write(ResourceFactory.newByteArrayResource(drlAsBytes).setTargetPath(DROOLS_DEFAULT_PATH));
    
    KieBuilder kb = ks.newKieBuilder(kfs);
    
    kb.buildAll(); // kieModule is automatically deployed to KieRepository if successfully built.
    
    if (kb.getResults().hasMessages(Level.ERROR)) {
        throw new PolicyServiceException("Build Errors:\n" + kb.getResults().toString());
    }
    
    //******* SHOULD THIS BE DONE FOR EACH PROCESSING THREAD? ******
    KieContainer newkieContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    

I'm using the following version of DROOLS.

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>7.2.0.Final</version>
    </dependency>
Breadfruit answered 30/1, 2018 at 19:23 Comment(2)
Did you figure this out?Dominance
You should be reusing the same container and KieBase and just creating new sessions as needed. Unless you legitimately have different sets of rules that each of those threads should be firing.Erythema

© 2022 - 2024 — McMap. All rights reserved.