当多个进程并行运行时,Drools似乎给出了错误的结果,并且在每个进程中,每次都会创建并处理一个新的KieBase
对象。
尝试过以下版本:6.5.0.Final
,7.32.0.Final
详细信息:
我并行执行了120个任务(使用7个线程)。在这120项任务中,流口水为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
的生成和执行在多线程环境中的行为。实际用例如下:
我们有一套明智的规则类别。对于每个类别,都需要执行特定的规则集。
Example:
for category 1 , I need to execute rule101, rule102, rule103
for category 2 , I need to execute rule201, rule202, rule203
....
Note: During evaluation, rules of category X should NOT interfere with Rules of category Y, i.e., they should be run independently.
因为没有类别巨大,我们正在并行构建KieBases
(针对每个类别)并将其存储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.
答案 0 :(得分:1)
最终发现这是Drools中的一个错误,因为KieHelper
在多线程环境中使用并不安全。
在Drools Dev社区的一位成员提出了解决方案的建议后,以下似乎是上述异常的原因以及解决此问题的解决方法:
问题的根本原因:KieHelper使用相同的默认releaseId构建KieModule。
解决方案:为每个内部版本使用不同的发行版ID。
代码:(在上述代码中使用此代码)
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);
通过并行运行> 500个进程进行了验证,并且未发生任何问题。