当多个KieBase并行执行时,为什么Drools会执行错误的规则?

当多个KieBase并行执行时,为什么Drools会执行错误的规则?,drools,kie,Drools,Kie,当多个进程并行运行时,Drools似乎给出了错误的结果,并且在每个进程中,每次都会创建和处理一个新的KieBase对象 试用版本:6.5.0.Final,7.32.0.Final 详情: 我并行执行了120个任务(使用7个线程)。在这120项任务中,drools对108项任务给出了正确的结果,但对12项任务执行了错误的规则(每次运行中失败的任务数量各不相同) 让我在这里发布代码和输出: public class TempClass { public List<Str

当多个进程并行运行时,Drools似乎给出了错误的结果,并且在每个进程中,每次都会创建和处理一个新的
KieBase
对象

试用版本:
6.5.0.Final
7.32.0.Final

详情:

我并行执行了120个任务(使用7个线程)。在这120项任务中,drools对108项任务给出了正确的结果,但对12项任务执行了错误的规则(每次运行中失败的任务数量各不相同)

让我在这里发布代码和输出:

    public class TempClass {
        public List<String> droolLogging = new ArrayList<>();
    }

   public void execute(){
        Map<String, List<String>> failedTasks = new ConcurrentHashMap<>(); // to see which tasks were incorrectly executed

        // Run 120 tasks in parallel using x threads (x depends upon no of processor)

        IntStream.range(1, 120).parallel()
        .forEach(taskCounter -> {

            String uniqueId = "Task-"+taskCounter;

            TempClass classObj = new TempClass();

            String ruleString = "package com.sample" + taskCounter + "\n" +
                    "import com.TempClass\n" +
                    "\n" +\
                    "rule \"droolLogging"+taskCounter+"\"\n" +
                    "\t when \n" +
                    "\t\t obj: TempClass(true)\n" +
                    "\t then \n" +
                    "\t\t obj.droolLogging.add(\"RuleOf-"+uniqueId+"\");\n" +
                    "\t end\n";

            // Above ruleString contains 1 rule and it is always executed. 
            // After execution, it will add an entry in array list 'droolLogging' 
            // of class 'TempClass'. In this entry, we are storing task counter 
            // to see rule of which task is executed.


            //following line of code seems to be the culprit as this is somehow returning incorrect KieBase sometime.

            KieBase kbase = new KieHelper()
                             .addContent(ruleString, ResourceType.DRL)
                             .build();

        /*
        //Same issue occurs even if I create different file with different name instead of using KieHelper.

        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem();

        String inMemoryDrlFileName = "src/main/resources/inmemoryrules-" + taskCounter + ".drl";

        kfs.write(inMemoryDrlFileName, ruleString);

        KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
        KieContainer kContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
        KieBaseConfiguration kbconf = ks.newKieBaseConfiguration();
        KieBase kbase = kContainer.newKieBase(kbconf);
        */


            StatelessKieSession kieSession = kbase.newStatelessKieSession();
            kieSession.execute(classObj);

            System.out.println("(" + Thread.currentThread().getName() + ") " +
                    uniqueId + "_" + classObj.droolLogging );

            //Important: 
            //  To see if correct rule is executed, task no. printed by variable 'droolLogging'
            //  should match with uniqueId

            if(classObj.droolLogging == null || classObj.droolLogging.size() != 1 ||
                    !classObj.droolLogging.get(0).endsWith(uniqueId)) {
                failedTasks.put("" + taskCounter, classObj.droolLogging);
            }
        });

        logger.info("Failed:\n {}", failedTasks);
    }


OUTPUT:
    (ForkJoinPool.commonPool-worker-1) Task-37_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-6) Task-8_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-3) Task-18_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-2) Task-108_[RuleOf-Task-4]
    (main) Task-78_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-7) Task-52_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-4) Task-97_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-5) Task-4_[RuleOf-Task-4]
    (ForkJoinPool.commonPool-worker-3) Task-19_[RuleOf-Task-19]
    (ForkJoinPool.commonPool-worker-5) Task-5_[RuleOf-Task-5]
    (ForkJoinPool.commonPool-worker-2) Task-109_[RuleOf-Task-109]
    (ForkJoinPool.commonPool-worker-7) Task-53_[RuleOf-Task-53]
    (ForkJoinPool.commonPool-worker-1) Task-38_[RuleOf-Task-38]
    (ForkJoinPool.commonPool-worker-4) Task-98_[RuleOf-Task-98]
    .... more

    Failed (12):
        {88=[RuleOf-Task-77], 78=[RuleOf-Task-4], 68=[RuleOf-Task-60], 37=[RuleOf-Task-4], 15=[RuleOf-Task-1], 18=[RuleOf-Task-4], 7=[RuleOf-Task-11], 8=[RuleOf-Task-4], 108=[RuleOf-Task-4], 71=[RuleOf-Task-76], 52=[RuleOf-Task-4], 97=[RuleOf-Task-4]}

This shows:

 - Rule of task 77 was executed in task 88
 - Rule of task 4 was executed in task 78
 - Rule of task 60 was executed in task 68
 - ....

This is wrong. For correct results, in each process, Rule of task X should be executed in task X only.
由于类别数量巨大,我们正在并行构建
kiebase
(针对每个类别),并将其存储x分钟。x分钟后,我们检查任何类别的规则是否已更改,如果更改,
KieBase
将为这些类别再次编译(这将再次并行)

此外,还可以在运行时添加新类别。因此,对于新添加的类别,也遵循上述步骤

   category1 -> KieBase1   (compiled rules: rule101, rule102, rule103)
   category2 -> KieBase2   (compiled rules: rule201, rule202, rule203)
   category3 -> KieBase3

   Note: As already mentioned above, execution of KieBase X should NOT interfere with execution of KieBase Y as KieBases are created category wise and for each category, only particular set of rules should be executed.

最后证明它是Drools中的一个bug,因为在多线程环境中使用
kieheloper
是不安全的

在Drools开发社区的一名成员提出解决方案后,出现上述异常现象的原因和克服此问题的解决方法如下:

问题的根本原因:KieHelper使用相同的默认releaseId构建KieModule

解决方案:为每个版本使用不同的版本ID

代码:(在上述代码中使用此代码)


通过并行运行>500个进程进行验证,问题没有发生。

嗯,是的。如果您阅读kieheloper的源代码,您可以看到
addContent
创建了一个文件系统资源,其中包含循环中的规则。每次文件资源被覆盖时,都会有一点竞争条件。@RoddyOfFrozenpeas我假设每个Kieheloper对象都创建自己的独立内存,并在该内存中存储内容。不使用KieHelper,即使我创建了不同名称的文件,问题似乎仍然存在(我已经更新了上面的代码,请查看一下)。请帮帮我。现在的原因是什么?我如何解决这个问题?你为什么要这么做?你想解决的实际问题是什么?你应该有一个单独的KieBase,并根据需要创建KieSessions来执行它。我不认为有任何理由并行创建120个KieBase,而不是只创建一个,然后从中创建120个KieSessions来并行使用。你能澄清一下你想要实现什么吗?@MarioFusco/roddyoffrozenpeas:我已经用用例更新了描述。请查收。
   category1 -> KieBase1   (compiled rules: rule101, rule102, rule103)
   category2 -> KieBase2   (compiled rules: rule201, rule202, rule203)
   category3 -> KieBase3

   Note: As already mentioned above, execution of KieBase X should NOT interfere with execution of KieBase Y as KieBases are created category wise and for each category, only particular set of rules should be executed.
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/rules.drl", ruleString);

ReleaseId releaseId = ks.newReleaseId("com.rule", "test" + taskCounter, "1.0.0");
kfs.generateAndWritePomXML(releaseId);

KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();

if (results.hasMessages(Message.Level.ERROR)) {
  throw new RuntimeException(results.getMessages().toString());
}

KieContainer kContainer = ks.newKieContainer(releaseId);
KieBase kbase = kContainer.newKieBase(ks.newKieBaseConfiguration());

StatelessKieSession kieSession = kbase.newStatelessKieSession();
kieSession.execute(classObj);