From ef6a7bd2d2a967e959eba0e6fe8d7e9a41465d36 Mon Sep 17 00:00:00 2001 From: nprentza <90831697+nprentza@users.noreply.github.com> Date: Mon, 10 Jul 2023 12:43:35 +0300 Subject: [PATCH] delete after failover, a candidate fix (#5373) --- .../core/ReliableSessionInitializer.java | 14 ++++--- .../infinispan/ReliabilityTest.java | 41 +++++++++++++++++++ .../infinispan/ReliabilityTestBasics.java | 12 ++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/ReliableSessionInitializer.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/ReliableSessionInitializer.java index 9416296a2c5..c16eef57c7b 100644 --- a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/ReliableSessionInitializer.java +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/ReliableSessionInitializer.java @@ -15,10 +15,6 @@ package org.drools.reliability.core; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.drools.core.SessionConfiguration; import org.drools.core.WorkingMemoryEntryPoint; import org.drools.core.common.InternalFactHandle; @@ -33,6 +29,10 @@ import org.kie.api.runtime.conf.PersistedSessionOption; import org.kie.api.runtime.rule.EntryPoint; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static org.drools.reliability.core.ReliablePropagationList.PROPAGATION_LIST; public class ReliableSessionInitializer { @@ -68,8 +68,10 @@ public InternalWorkingMemory init(InternalWorkingMemory session, PersistedSessio private void onWorkingMemoryAction(InternalWorkingMemory session, PropagationEntry entry) { if (entry instanceof PropagationEntry.Insert) { InternalFactHandle fh = ((PropagationEntry.Insert) entry).getHandle(); - WorkingMemoryEntryPoint ep = fh.getEntryPoint(session); - ((SimpleReliableObjectStore) ep.getObjectStore()).putIntoPersistedStorage(fh, true); + if (fh.isValid()) { + WorkingMemoryEntryPoint ep = fh.getEntryPoint(session); + ((SimpleReliableObjectStore) ep.getObjectStore()).putIntoPersistedStorage(fh, true); + } } } 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 5382d728408..7f4af0d6b78 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 @@ -24,6 +24,8 @@ import org.kie.api.runtime.rule.FactHandle; import org.test.domain.Person; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(BeforeAllMethodExtension.class) @@ -215,6 +217,45 @@ void deleteBeforeFailover_shouldRecoverFromFailover(PersistedSessionOption.Persi assertThat(getResults()).containsExactlyInAnyOrder("Toshiya"); } + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") + void deleteAfterFailover_shouldNotMatch(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){ + createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + + insert("M"); + Person pMike = new Person("Mike", 23); + insert(pMike); + + failover(); + restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + clearResults(); + + Optional getFactHandleForPerson = getFactHandleForPerson(pMike); + if (!getFactHandleForPerson.isEmpty()){ + delete(getFactHandleForPerson.get()); + } + + assertThat(fireAllRules()).isEqualTo(0); + } + + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") + void deleteBeforeFailover_shouldNotMatch(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){ + createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + + insert("M"); + Person pMike = new Person("Mike", 23); + FactHandle fhMike = insert(pMike); + + delete(fhMike); + + failover(); + restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + clearResults(); + + assertThat(fireAllRules()).isEqualTo(0); + } + @ParameterizedTest @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") void updateByObjectBeforeFailover_shouldMatchUpdatedFact(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){ 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 757e8bba433..6b464f4aaf6 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 @@ -340,6 +340,18 @@ protected Optional getPersonByName(KieSession kieSession, String name) { .filter(p -> p.getName().equals(name) ).findFirst(); } + protected Optional getFactHandleForPerson(Person person){ + return getFactHandleForPerson(sessions.get(0), person); + } + + protected Optional getFactHandleForPerson(KieSession kieSession, Person person){ + return kieSession.getFactHandles() + .stream() + .filter(p -> p.getObject() instanceof Person) + .filter(p -> ( (Person) p.getObject()).getName().equals(person.getName()) ) + .filter(p -> ( (Person) p.getObject()).getAge()==person.getAge() ).findFirst(); + } + private static class OptionsFilter { private final Option[] options;