Skip to content

Commit

Permalink
[DO-NOT-MERGE] [incubator-kie-drools-5847] Flaky KieContainerTest.tes…
Browse files Browse the repository at this point in the history
…tIncrementalCompilationSynchronization timeout

- Quick test
  • Loading branch information
tkobayas committed Jun 26, 2024
1 parent bdf4211 commit 21ad8e2
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 52 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pr-downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ on:

jobs:
kogito-downstream-build:
if: false
concurrency:
group: pr-${{ matrix.job_name }}_${{ matrix.os }}_${{ matrix.java-version }}_${{ matrix.maven-version }}_${{ github.head_ref }}
cancel-in-progress: true
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-drools-ansible.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ on:

jobs:
drools-ansible:
if: false
concurrency:
group: pr-drools_ansible_${{ matrix.os }}_${{ matrix.java-version }}_${{ matrix.maven-version }}_${{ github.head_ref }}
cancel-in-progress: true
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-drools-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ on:

jobs:
drools-build:
if: false
concurrency:
group: pr-drools_docs_${{ matrix.os }}_${{ matrix.java-version }}_${{ matrix.maven-version }}_${{ github.head_ref }}
cancel-in-progress: true
Expand Down
35 changes: 21 additions & 14 deletions .github/workflows/pr-drools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,30 @@ jobs:
- name: Support long paths
if: ${{ matrix.os == 'windows-latest' }}
uses: apache/incubator-kie-kogito-pipelines/.ci/actions/long-paths@main
- uses: actions/checkout@v3
- name: Java and Maven Setup
uses: apache/incubator-kie-kogito-pipelines/.ci/actions/maven@main
with:
java-version: ${{ matrix.java-version }}
maven-version: ${{ matrix.maven-version }}
cache-key-prefix: ${{ runner.os }}-${{ matrix.java-version }}-maven${{ matrix.maven-version }}
- name: Build Chain
uses: apache/incubator-kie-kogito-pipelines/.ci/actions/build-chain@main
env:
BUILD_MVN_OPTS_CURRENT: '-Dfull -Dreproducible'
MAVEN_OPTS: "-Dfile.encoding=UTF-8"
with:
definition-file: https://raw.githubusercontent.com/${GROUP:apache}/incubator-kie-kogito-pipelines/${BRANCH:main}/.ci/pull-request-config.yaml
annotations-prefix: ${{ runner.os }}-${{ matrix.java-version }}/${{ matrix.maven-version }}
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Surefire Report
uses: apache/incubator-kie-kogito-pipelines/.ci/actions/surefire-report@main
if: ${{ always() }}
with:
report_paths: '**/*-reports/TEST-*.xml'
allow-snapshots: true
- name: Build drools-core
run: mvn -B clean install -Dquickly --file drools-core/pom.xml
- name: Build drools-compiler
run: mvn -B clean install -Dquickly --file drools-compiler/pom.xml
- name: Run loop Windows
if: ${{ matrix.os == 'windows-latest' }}
shell: cmd
run: |
FOR /L %%G IN (1,1,100) DO (
echo Iteration %%G
mvn -B clean test --file drools-test-coverage/test-compiler-integration/pom.xml -Dtest=ConcurrentBasesParallelTest,org.drools.mvel.integrationtests.KieContainerTest#testIncrementalCompilationSynchronization*
)
- name: Run loop Linux
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
for i in {1..100}; do
echo "Iteration $i"
mvn -B clean test --file drools-test-coverage/test-compiler-integration/pom.xml -Dtest=ConcurrentBasesParallelTest,org.drools.mvel.integrationtests.KieContainerTest#testIncrementalCompilationSynchronization*
done
1 change: 1 addition & 0 deletions .github/workflows/pr-jenkins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ on:

jobs:
dsl-tests:
if: false
concurrency:
group: ${{ github.repository.name }}_dsl_tests-${{ github.head_ref }}
cancel-in-progress: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.kie.api.io.Resource;
import org.kie.internal.builder.ResourceChange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RuleCompilationPhase extends ImmutableRuleCompilationPhase {

private static final Logger LOG = LoggerFactory.getLogger(RuleCompilationPhase.class);

public static CompilationPhase of(
PackageRegistry pkgRegistry,
PackageDescr packageDescr,
Expand Down Expand Up @@ -129,6 +133,7 @@ private void preProcessRules(PackageDescr packageDescr, PackageRegistry pkgRegis
}
}

LOG.info("preProcessRules 1: {}", rulesToBeRemoved);
rulesToBeRemoved.forEach(pkg::removeRule);

for (RuleDescr ruleDescr : packageDescr.getRules()) {
Expand All @@ -142,6 +147,7 @@ private void preProcessRules(PackageDescr packageDescr, PackageRegistry pkgRegis

if (!rulesToBeRemoved.isEmpty()) {
rulesToBeRemoved.addAll(findChildrenRulesToBeRemoved(packageDescr, rulesToBeRemoved));
LOG.info("preProcessRules 2: {}", rulesToBeRemoved);
kBase.removeRules(rulesToBeRemoved);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ private KieBaseUpdate createKieBaseUpdate() {

for (ResourceChange change : changeSet.getChanges()) {
if (change.getType() == ResourceChange.Type.RULE) {
log.info("createKieBaseUpdate : change = " + change);
switch (change.getChangeType()) {
case ADDED:
kieBaseUpdate.registerRuleToBeAdded(newPkg.getRule( change.getName() ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ private Results update( InternalKieModule currentKM, InternalKieModule newKM ) {
)))
.forEach(compositeUpdater::add);

log.warn("update");
cs.getChanges().entrySet().forEach( entry -> {
String resourceName = entry.getKey();
ResourceChangeSet resourceChangeSet = entry.getValue();
if (resourceName.equals("rules1.drl")) {
log.warn(" " + resourceName + " -> " + resourceChangeSet.getChanges());
}
});
kBase.enqueueModification(compositeUpdater);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,14 @@ public void removeRule( final String packageName, final String ruleName ) {
}

public void kBaseInternal_removeRule(InternalKnowledgePackage pkg, RuleImpl rule, Collection<InternalWorkingMemory> workingMemories) {
logger.info("kBaseInternal_removeRule: " + rule.getName());
this.reteooBuilder.removeRules(Collections.singletonList(rule), workingMemories);
pkg.removeRule( rule );
addReloadDialectDatas( pkg.getDialectRuntimeRegistry() );
}

public void kBaseInternal_removeRules(Collection<? extends Rule> rules, Collection<InternalWorkingMemory> workingMemories) {
logger.info("kBaseInternal_removeRules: " + rules);
this.reteooBuilder.removeRules(rules, workingMemories);
}

Expand Down Expand Up @@ -1237,6 +1239,7 @@ public boolean removeObjectsGeneratedFromResource(Resource resource, Collection<
for (InternalKnowledgePackage pkg : pkgs.values()) {
List<RuleImpl> rulesToBeRemoved = pkg.getRulesGeneratedFromResource(resource);
if (!rulesToBeRemoved.isEmpty()) {
logger.info("removeObjectsGeneratedFromResource: " + rulesToBeRemoved);
this.reteooBuilder.removeRules( rulesToBeRemoved, workingMemories );
// removal of rule from package has to be delayed after the rule has been removed from the phreak network
// in order to allow the correct flushing of all outstanding staged tuples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.drools.core.reteoo.builder.ReteooRuleBuilder;
import org.drools.core.util.TupleRBTree.Node;
import org.kie.api.definition.rule.Rule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Builds the Rete-OO network for a <code>Package</code>.
Expand All @@ -57,6 +59,7 @@ public class ReteooBuilder
implements
Externalizable {

private static final Logger LOG = LoggerFactory.getLogger(ReteooBuilder.class);

private static final long serialVersionUID = 510l;

Expand Down Expand Up @@ -158,6 +161,7 @@ public synchronized Map<String, TerminalNode[]> getTerminalNodes() {
}

public synchronized void removeRules(Collection<? extends Rule> rulesToBeRemoved, Collection<InternalWorkingMemory> workingMemories) {
LOG.info("removeRules: " + rulesToBeRemoved);
for (Rule r : rulesToBeRemoved) {
RuleImpl rule = (RuleImpl) r;
if (rule.hasChildren() && !rulesToBeRemoved.containsAll( rule.getChildren() )) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.IOUtils;
import org.drools.compiler.compiler.io.Folder;
Expand All @@ -49,6 +51,8 @@
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.core.util.DroolsAssert.assertEnumerationSize;
Expand All @@ -57,6 +61,8 @@
@RunWith(Parameterized.class)
public class KieContainerTest {

private final Logger LOG = LoggerFactory.getLogger(KieContainerTest.class);

private final KieBaseTestConfiguration kieBaseTestConfiguration;

public KieContainerTest(final KieBaseTestConfiguration kieBaseTestConfiguration) {
Expand Down Expand Up @@ -248,47 +254,91 @@ private void insertMessageFromTypeDeclaration(KieSession ksession) throws Instan
ksession.insert(message);
}

private static volatile boolean running = true;

@Test(timeout = 20000)
public void testIncrementalCompilationSynchronization() {
final KieServices kieServices = KieServices.Factory.get();

ReleaseId releaseId = kieServices.newReleaseId("org.kie.test", "sync-scanner-test", "1.0.0");
KieUtil.getKieModuleFromDrls(releaseId, kieBaseTestConfiguration, createDRL("rule0"));

final KieContainer kieContainer = kieServices.newKieContainer(releaseId);

KieSession kieSession = kieContainer.newKieSession();
List<String> list = new ArrayList<>();
kieSession.setGlobal("list", list);
kieSession.fireAllRules();
kieSession.dispose();
assertThat(list.size()).isEqualTo(1);

Thread t = new Thread(() -> {
for (int i = 1; i < 10; i++) {
ReleaseId releaseId1 = kieServices.newReleaseId("org.kie.test", "sync-scanner-test", "1.0." + i);
KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, createDRL("rule" + i));
kieContainer.updateToVersion(releaseId1);
}
});

t.setDaemon(true);
t.start();

while (true) {
kieSession = kieContainer.newKieSession();
list = new ArrayList<>();
kieSession.setGlobal("list", list);
kieSession.fireAllRules();
kieSession.dispose();
// There can be multiple items in the list if an updateToVersion is triggered during a fireAllRules
// (updateToVersion can be called multiple times during fireAllRules, especially on slower machines)
// in that case it may fire with the old rule and multiple new ones
assertThat(list).isNotEmpty();
if (list.get(0).equals("rule9")) {
break;
int MAX_THREAD = 8;
ExecutorService executor = Executors.newFixedThreadPool(MAX_THREAD);

try {
for (int n = 0; n < 1; n++) {
LOG.info("*** run : n = " + n);

final KieServices kieServices = KieServices.Factory.get();

ReleaseId releaseId = kieServices.newReleaseId("org.kie.test", "sync-scanner-test", "1.0.0");
KieUtil.getKieModuleFromDrls(releaseId, kieBaseTestConfiguration, createDRL("rule0"));

final KieContainer kieContainer = kieServices.newKieContainer(releaseId);

KieSession kieSession = kieContainer.newKieSession();
List<String> list = new ArrayList<>();
kieSession.setGlobal("list", list);
kieSession.fireAllRules();
kieSession.dispose();
assertThat(list.size()).isEqualTo(1);
LOG.info(" 1st iteration done");

running = true;
// for (int m = 0; m < MAX_THREAD; m++) {
// executor.execute(new Runnable() {
//
// public void run() {
// long dummy = 0;
// while (running) { // The thread runs until 'running' is set to false
// dummy += 1; // Simple increment to keep the thread busy
// if (dummy < 0) {
// dummy = 0;
// }
// dummy += 1;
// }
// }
// });
// }

Thread t = new Thread(() -> {
for (int i = 1; i < 10; i++) {
ReleaseId releaseId1 = kieServices.newReleaseId("org.kie.test", "sync-scanner-test", "1.0." + i);
LOG.info(" getKieModuleFromDrls: " + releaseId1.toExternalForm());
KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, createDRL("rule" + i));
LOG.info(" updateToVersion: " + releaseId1.toExternalForm());
kieContainer.updateToVersion(releaseId1);
}
});

t.setDaemon(true);
t.start();

String lastResult = "";
int sameResultCounter = 0;
while (true) {
kieSession = kieContainer.newKieSession();
list = new ArrayList<>();
kieSession.setGlobal("list", list);
kieSession.fireAllRules();
kieSession.dispose();
// There can be multiple items in the list if an updateToVersion is triggered during a fireAllRules
// (updateToVersion can be called multiple times during fireAllRules, especially on slower machines)
// in that case it may fire with the old rule and multiple new ones
assertThat(list).isNotEmpty();
if (list.get(0).equals("rule9")) {
running = false;
break;
} else {
if (!lastResult.equals(list.toString())) {
LOG.info(" Fired rule: list = " + list + ", previous sameResultCounter = " + sameResultCounter);
lastResult = list.toString();
sameResultCounter = 0;
} else {
sameResultCounter++;
}
}
}
}
} finally {
running = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

<logger name="org.kie" level="warn"/>
<logger name="org.drools" level="warn"/>
<logger name="org.drools.core" level="INFO"/>
<logger name="org.drools.compiler.builder.impl.processors" level="INFO"/>
<logger name="org.drools.compiler.kie.builder.impl" level="INFO"/>
<logger name="org.drools.compiler.kie.builder.impl.KieContainerImpl" level="warn"/>
<logger name="org.drools.mvel.integrationtests" level="INFO"/>

<!-- <logger name="org.drools.core.management" level="debug"/> -->
<!-- <logger name="org.drools.modelcompiler.builder" level="debug"/>-->
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
<module>drools-model</module>
<module>drools-examples-api</module>
<module>drools-test-coverage</module>
<module>drools-scenario-simulation</module>
<!-- <module>drools-scenario-simulation</module>
<module>drools-metric</module>
<module>drools-alphanetwork-compiler</module>
<module>drools-engine</module>
Expand All @@ -223,7 +223,7 @@
<module>kie-archetypes</module>
<module>drools-quarkus-extension</module>
<module>drools-reliability</module>
<module>drools-drlonyaml-parent</module>
<module>drools-drlonyaml-parent</module> -->
</modules>

<profiles>
Expand Down

0 comments on commit 21ad8e2

Please sign in to comment.