Skip to content

Commit

Permalink
map a session to its persistedSessionId
Browse files Browse the repository at this point in the history
  • Loading branch information
nprentza committed Jun 22, 2023
1 parent 9de8d0b commit 7eb8b66
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -61,8 +62,7 @@ public abstract class ReliabilityTestBasics {
private InfinispanContainer container;

protected final List<KieSession> sessions = new ArrayList<>();

private long persistedSessionId = -1;
protected final HashMap<Long,Long> persistedSessionIds = new HashMap<>();

protected PersistedSessionOption.SafepointStrategy safepointStrategy;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<Object> 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;
}

Expand Down

0 comments on commit 7eb8b66

Please sign in to comment.