diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java index 611a408534ff..1a386352bdd4 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java @@ -308,7 +308,7 @@ void insertNonMatching_Failover_UpdateWithMatching_ShouldFiredMatch(PersistedSes } @ParameterizedTest - @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") // FAILS in STORES_ONLY, EXPLICIT + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") void multipleKieSessions_BasicTest(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { KieSession session1 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); KieSession session2 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); @@ -338,4 +338,129 @@ void multipleKieSessions_BasicTest(PersistedSessionOption.PersistenceStrategy pe assertThat(fireAllRules(session2)).isEqualTo(1); } + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") // FULL fails with "ReliablePropagationList; no valid constructor" + void multipleKieSessions_insertFailoverInsertFire_shouldRecoverFromFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { + KieSession session1 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + KieSession session2 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + + insert(session1,"M"); + insertMatchingPerson(session1, "Mike", 37); + insert(session2,"N"); + insertNonMatchingPerson(session2,"Helen",33); + + failover(); + + session1 = restoreSession(session1.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + session2 = restoreSession(session2.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + + insertNonMatchingPerson(session1,"Toshiya", 35); + insertMatchingPerson(session2,"Nicole", 40); + + assertThat(fireAllRules(session1)).isEqualTo(1); + assertThat(fireAllRules(session2)).isEqualTo(1); + + assertThat(getResults(session1)).containsExactlyInAnyOrder("Mike"); + assertThat(getResults(session2)).containsExactlyInAnyOrder("Nicole"); + } + + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") // With Remote, FULL fails with "ReliablePropagationList; no valid constructor" even without failover + void multipleKieSessions_noFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { + + KieSession session1 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + KieSession session2 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + + insert(session1,"M"); + insertMatchingPerson(session1,"Matching Person One", 37); + insert(session2, new Person("Mary",32)); + + if (safepointStrategy == PersistedSessionOption.SafepointStrategy.EXPLICIT) { + safepoint(); + } + + session1 = restoreSession(session1.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + session2 = restoreSession(session2.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + + insertNonMatchingPerson(session1,"Toshiya", 41); + insertMatchingPerson(session1,"Matching Person Two", 40); + insert(session2, "H"); + insert(session2,new Person("Helen",43)); + + fireAllRules(session1); + assertThat(getResults(session1)).containsExactlyInAnyOrder("Matching Person One", "Matching Person Two"); + + assertThat(fireAllRules(session2)).isEqualTo(1); + } + + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") // FULL fails with "ReliablePropagationList; no valid constructor" + void multipleKieSessions_insertFireInsertFailoverInsertFire_shouldMatchFactInsertedBeforeFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { + + KieSession session1 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + KieSession session2 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + + insert(session1,"M"); + insertMatchingPerson(session1,"Matching Person One", 37); + insert(session2, "N"); + insertMatchingPerson(session2, "Nicole",34); + + fireAllRules(session1); + fireAllRules(session2); + + insertMatchingPerson(session1,"Matching Person Two", 40); + insertMatchingPerson(session2, "Nancy",23); + + failover(); + + session1 = restoreSession(session1.getIdentifier(),BASIC_RULE, persistenceStrategy, safepointStrategy); + session2 = restoreSession(session2.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + + clearResults(session1); + clearResults(session2); + + insertNonMatchingPerson(session1,"Nora", 35); + insertMatchingPerson(session1,"Matching Person Three", 41); + insertNonMatchingPerson(session2,"Mike", 35); + insertMatchingPerson(session2,"Noah", 41); + + fireAllRules(session1); + fireAllRules(session2); + + assertThat(getResults(session1)).containsExactlyInAnyOrder("Matching Person Two", "Matching Person Three"); + assertThat(getResults(session2)).containsExactlyInAnyOrder("Nancy", "Noah"); + } + + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") // FULL fails with "ReliablePropagationList; no valid constructor" + void multipleKieSessions_updateBeforeFailover_shouldRecoverFromFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { + KieSession session1 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + + insert(session1,"M"); + Person p1 = new Person("Mario", 49); + FactHandle fh1 = insert(session1,p1); + Person p2 = new Person("Toshiya", 45); + FactHandle fh2 = insert(session1, p2); + + assertThat(fireAllRules(session1)).isEqualTo(1); + assertThat(getResults(session1)).containsExactlyInAnyOrder("Mario"); + + p1.setName("SuperMario"); + update(session1, fh1, p1); + p2.setName("MegaToshiya"); + update(session1, fh2, p2); + + failover(); + session1 = restoreSession(session1.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + + assertThat(fireAllRules(session1)).isEqualTo(1); + assertThat(getResults(session1)).containsExactlyInAnyOrder("Mario", "MegaToshiya"); + + failover(); + session1 = restoreSession(session1.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); + clearResults(session1); + + assertThat(fireAllRules(session1)).isZero(); + assertThat(getResults(session1)).isEmpty(); + } } diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java index a1d0df939fb9..4a07aa39c73d 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java @@ -45,6 +45,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -61,8 +62,7 @@ public abstract class ReliabilityTestBasics { private InfinispanContainer container; protected final List sessions = new ArrayList<>(); - - private long persistedSessionId = -1; + protected final HashMap persistedSessionIds = new HashMap<>(); protected PersistedSessionOption.SafepointStrategy safepointStrategy; @@ -228,11 +228,13 @@ protected KieSession restoreSession(String drl, PersistedSessionOption.Persisten } protected KieSession restoreSession(String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy, Option... options) { - return restoreSession(persistedSessionId, drl, persistenceStrategy, safepointStrategy, options); + Long sessionIdToRestoreFrom = (Long)this.persistedSessionIds.values().toArray()[0]; + return restoreSession(sessionIdToRestoreFrom, drl, persistenceStrategy, safepointStrategy, options); } protected KieSession restoreSession(Long sessionId, String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy, Option... options) { - return getKieSession(drl, PersistedSessionOption.fromSession(sessionId).withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy), options); + Long sessionIdToRestoreFrom = this.persistedSessionIds.get(sessionId); + return getKieSession(drl, PersistedSessionOption.fromSession(sessionIdToRestoreFrom).withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy), options); } protected int fireAllRules() { @@ -289,12 +291,12 @@ protected KieSession getKieSession(String drl, PersistedSessionOption persistedS } Stream.of(optionsFilter.getKieSessionOption()).forEach(conf::setOption); KieSession session = kbase.newKieSession(conf, null); - sessions.add(session); if (persistedSessionOption == null || persistedSessionOption.isNewSession()) { List results = new ArrayList<>(); session.setGlobal("results", results); - persistedSessionId = session.getIdentifier(); } + sessions.add(session); + persistedSessionIds.put(session.getIdentifier(),persistedSessionOption == null || persistedSessionOption.isNewSession() ? session.getIdentifier() : persistedSessionOption.getSessionId()); return session; }