You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kie.apache.org by "gpadbidri (via GitHub)" <gi...@apache.org> on 2025/04/21 17:52:09 UTC
[I] Inconsistent "Unexpected Global" Error in Drools with Spring Boot [incubator-kie-drools]
gpadbidri opened a new issue, #6318:
URL: https://github.com/apache/incubator-kie-drools/issues/6318
We’re using Drools with Spring Boot and encountering an "Unexpected Global" exception under inconsistent conditions.
The same rule behaves differently across different inputs — it works for some executions and fails with this exception for others. We're unable to identify a consistent pattern for when the global is accepted vs when it's rejected.
Environment:
Drools version: 8.41.0.Final
Spring Boot version: (please include if known)
Maven dependency:
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-mvel</artifactId>
<version>8.41.0.Final</version>
</dependency>
Symptoms:
We dynamically assign globals before inserting facts and firing rules.
Example code:
`references.forEach((key, value) -> {
try {
session.setGlobal(key, value);
} catch (Exception e) {
log.error("Failed to set global: {}", key, e);
}
});
`
The error only occurs for some executions, even when using the same rule and same code path.
Question:
What could cause a global to be accepted in some cases and throw Unexpected Global in others? Could it be due to rule compilation caching, or something in how the session or globals are initialized?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org
Re: [I] Inconsistent "Unexpected Global" Error in Drools with Spring Boot [incubator-kie-drools]
Posted by "tkobayas (via GitHub)" <gi...@apache.org>.
tkobayas commented on issue #6318:
URL: https://github.com/apache/incubator-kie-drools/issues/6318#issuecomment-2819964310
"Unexpected global" is thrown when kbase doesn't know the global's name. Generally it happens when a DRL doesn't define the global with the name.
Does `key` dynamically changes? Does the application dynamically update rules?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org
Re: [I] Inconsistent "Unexpected Global" Error in Drools with Spring Boot [incubator-kie-drools]
Posted by "tkobayas (via GitHub)" <gi...@apache.org>.
tkobayas commented on issue #6318:
URL: https://github.com/apache/incubator-kie-drools/issues/6318#issuecomment-2875757764
Closing the issue now. Feel free to reopen if you still face the same issue even with the solution.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org
Re: [I] Inconsistent "Unexpected Global" Error in Drools with Spring Boot [incubator-kie-drools]
Posted by "tkobayas (via GitHub)" <gi...@apache.org>.
tkobayas commented on issue #6318:
URL: https://github.com/apache/incubator-kie-drools/issues/6318#issuecomment-2826571909
Hi @gpadbidri ,
App's `evaluateRules` method has a concurrency issue. It builds rules without specifying a ReleaseId, so the default ReleaseId is "org.default:artifact:1.0.0". The created `KieModule` is cached in singleton KieRepositoryImpl with a key "org.default:artifact:1.0.0". Then `kieServices.newKieContainer(kieModule.getReleaseId())` takes the `KieModule`. If `evaluateRules` method is executed concurrently, one thread may use a `KieModule` which is built by another thread. It explains that it hits "Unexpected global".
Solutions would be:
A) Specify a unique ReleaseId
You can specify a unique ReleaseId like this:
```
// Instead of these code...
// KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
// kieBuilder.buildAll();
// KieModule kieModule = kieBuilder.getKieModule();
// session = kieServices.newKieContainer(kieModule.getReleaseId()).newKieSession();
ReleaseId releaseId = kieServices.newReleaseId("com.sample", "my-sample-" + UUID.randomUUID(), "1.0.0");
kieFileSystem.generateAndWritePomXML(releaseId);
kieServices.newKieBuilder(kieFileSystem).buildAll();
session = kieServices.newKieContainer(releaseId).newKieSession();
```
also remove the kieModule at the end of the mothod.
```
} finally {
...
kieServices.getRepository().removeKieModule(releaseId);
}
```
or
B) Redesign to share rules
Building rules for every request is an excessive approach and I haven't seen before. I hope you can generalize the rules and change the behavior using facts and globals. Then, you will be able to build the rules only once, share the KieContainer and safely call newKieSession() in multithreads.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org
Re: [I] Inconsistent "Unexpected Global" Error in Drools with Spring Boot [incubator-kie-drools]
Posted by "gpadbidri (via GitHub)" <gi...@apache.org>.
gpadbidri commented on issue #6318:
URL: https://github.com/apache/incubator-kie-drools/issues/6318#issuecomment-2823751312
@tkobayas Yes application dynamically updates rules and this same rule is created every time for each user and we have good traffic on our site. Her is my code sample and generated drl which works in some cases and fails in some cases :
`
public static boolean evaluateRules(List<RuleSnapshot> ruleList, ContactMessage contactMessage, String parentId, String childId) {
var email = contactMessage.getEmail();
KieSession session = null;
try {
Map<String, Object> references = ConditionUtils.references(ruleList);
String drl = ConditionUtils.getDrlString(ruleList, references);
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write("src/main/resources/simple.drl",
kieServices.getResources().newReaderResource(new StringReader(drl)));
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
session = kieServices.newKieContainer(kieModule.getReleaseId()).newKieSession();
var result = new RuleResult();
session.setGlobal("result", result);
if (Objects.nonNull(references)) {
for (Map.Entry<String, Object> entry : references.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
session.setGlobal(key, value);
}
session.insert(contactMessage);
session.fireAllRules();
var ruleResult = result.isValid();
return ruleResult;
} catch (Exception e) {
log.info("Exception in check rule : " + e.getMessage() + " email = " + email );
e.printStackTrace();
return false;
} finally {
if (session != null) {
session.dispose(); // Always dispose session to prevent memory leaks
log.info("[Thread: {}] KieSession disposed", Thread.currentThread().getName());
}
}
}`
This is the generated drl which runs for few contacts and fails for few :
`package com.project.model.processing.contact;
global com.project.model.processing.contact.ContactEvent ref1;
global com.project.dto.compositetypes.rules.RuleResult result;
dialect "mvel"
rule "Match contact to fit Condition"
when
contactMessageInstance:ContactMessage( ( contactEvents contains ref1 ) );
then
result.setValid(true);
end`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org
Re: [I] Inconsistent "Unexpected Global" Error in Drools with Spring Boot [incubator-kie-drools]
Posted by "tkobayas (via GitHub)" <gi...@apache.org>.
tkobayas closed issue #6318: Inconsistent "Unexpected Global" Error in Drools with Spring Boot
URL: https://github.com/apache/incubator-kie-drools/issues/6318
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org