Skip to content

Commit

Permalink
DROOLS-7543 : benchmark to test session restore after failover (#266)
Browse files Browse the repository at this point in the history
* InsertFailoverFireBenchmark

* PR comments
  • Loading branch information
nprentza authored Oct 13, 2023
1 parent 06108c4 commit cc89965
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<name>Drools 8 Benchmarks comparing reliability performance</name>

<properties>
<version.infinispan>14.0.6.Final</version.infinispan>
<version.infinispan>14.0.13.Final</version.infinispan>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/


package org.drools.benchmarks.reliability;

import java.nio.file.Path;
Expand Down Expand Up @@ -62,12 +63,12 @@ public String getInfinispanStorageMode() {
}

@Param({"NONE", "EMBEDDED", "REMOTE", "REMOTEPROTO"})
private Mode mode;
protected Mode mode;

@Param({"true", "false"})
private boolean useSafepoints;
protected boolean useSafepoints;

private InfinispanContainer container;
protected InfinispanContainer container;

@Setup
public void setupEnvironment() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.benchmarks.reliability;

import org.drools.reliability.core.ReliableKieSession;
import org.drools.reliability.core.ReliableRuntimeComponentFactoryImpl;
import org.drools.reliability.core.StorageManagerFactory;
import org.drools.reliability.core.TestableStorageManager;
import org.drools.reliability.infinispan.InfinispanStorageManager;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.conf.PersistedSessionOption;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Setup;

public abstract class AbstractReliabilityBenchmarkFailoverSupport extends AbstractReliabilityBenchmark{

protected Long persistedSessionId;
protected PersistedSessionOption.PersistenceStrategy persistenceStrategy; // used strategy throughout the benchmark
protected PersistedSessionOption.SafepointStrategy safepointStrategy;
protected PersistedSessionOption.PersistenceObjectsStrategy persistenceObjectsStrategy;

@Setup(Level.Iteration)
@Override
public void setup() {
// overriding setup() because failover scenario requires more complicated iteration setup
}

protected abstract void setupKieBase();
protected abstract void populateKieSessionPerIteration();

protected KieSession createKieSession(PersistedSessionOption persistedSessionOption) {
KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration();
if (persistedSessionOption != null) {
conf.setOption(persistedSessionOption);
}
KieSession session = kieBase.newKieSession(conf, null);
this.persistedSessionId = persistedSessionOption == null || persistedSessionOption.isNewSession() ? session.getIdentifier() : persistedSessionOption.getSessionId();
return session;
}

protected KieSession restoreSession() {
return createKieSession(PersistedSessionOption.fromSession(persistedSessionId)
.withPersistenceStrategy(persistenceStrategy)
.withSafepointStrategy(safepointStrategy)
.withPersistenceObjectsStrategy(persistenceObjectsStrategy));
}

public void failover() {
// EXPLICIT is not used for now, because if useSafepoints then strategy is always SafepointStrategy.AFTER_FIRE
if (safepointStrategy == PersistedSessionOption.SafepointStrategy.EXPLICIT) {
((ReliableKieSession)kieSession).safepoint();
}

if (((TestableStorageManager) StorageManagerFactory.get().getStorageManager()).isRemote()) {
// fail-over means restarting Drools instance. Assuming remote infinispan keeps alive
StorageManagerFactory.get().getStorageManager().close(); // close remoteCacheManager
// Reclaim RemoteCacheManager
InfinispanStorageManager cacheManager = (InfinispanStorageManager) StorageManagerFactory.get().getStorageManager();
RemoteCacheManager remoteCacheManager = container.getRemoteCacheManager(cacheManager.provideAdditionalRemoteConfigurationBuilder());
cacheManager.setRemoteCacheManager(remoteCacheManager);
} else {
((TestableStorageManager) StorageManagerFactory.get().getStorageManager()).restart(); // restart embedded infinispan cacheManager. GlobalState and FireStore are kept
}
ReliableRuntimeComponentFactoryImpl.refreshCounterUsingStorage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Measurement(iterations = 1000)
public class FireAndAlarmBenchmark extends AbstractReliabilityBenchmark{

private static final String FIRE_AND_ALARM =
public static final String FIRE_AND_ALARM =
"import " + Alarm.class.getCanonicalName() + ";" +
"import " + Fire.class.getCanonicalName() + ";" +
"import " + Sprinkler.class.getCanonicalName() + ";" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.benchmarks.reliability;

import org.drools.benchmarks.common.util.BuildtimeUtil;
import org.drools.benchmarks.common.util.RuntimeUtil;
import org.drools.benchmarks.reliability.fireandalarm.Fire;
import org.drools.benchmarks.reliability.fireandalarm.Room;
import org.drools.benchmarks.reliability.fireandalarm.Sprinkler;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.runtime.conf.PersistedSessionOption;
import org.kie.internal.conf.ParallelExecutionOption;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.Warmup;

import java.util.ArrayList;
import java.util.List;

import static org.drools.benchmarks.reliability.FireAndAlarmBenchmark.FIRE_AND_ALARM;

@Warmup(iterations = 0)
@Measurement(iterations = 1)
public class InsertFailoverFireBenchmark extends AbstractReliabilityBenchmarkFailoverSupport {

@Param({"100"})
private int factsNr;

@Param({"EMBEDDED"})
private Mode mode;

@Param({"true", "false"})
private boolean useObjectStoreWithReferences;

@Param({"true", "false"})
private boolean useSafepoints;

@Setup
@Override
public void setupKieBase() {
kieBase = BuildtimeUtil.createKieBaseFromDrl(true, FIRE_AND_ALARM,
ParallelExecutionOption.SEQUENTIAL,
EventProcessingOption.CLOUD);
}


@Setup(Level.Iteration)
public void setupAndFailover() {
System.out.println("setupAndFailover!!");
if (mode != Mode.NONE) {
persistenceStrategy = PersistedSessionOption.PersistenceStrategy.STORES_ONLY;
safepointStrategy = useSafepoints ? PersistedSessionOption.SafepointStrategy.AFTER_FIRE : PersistedSessionOption.SafepointStrategy.ALWAYS;
persistenceObjectsStrategy = useObjectStoreWithReferences ? PersistedSessionOption.PersistenceObjectsStrategy.OBJECT_REFERENCES : PersistedSessionOption.PersistenceObjectsStrategy.SIMPLE;

// These 3 strategies will not change during the benchmark
PersistedSessionOption persistedSessionOption = PersistedSessionOption.newSession()
.withPersistenceStrategy(persistenceStrategy)
.withSafepointStrategy(safepointStrategy)
.withPersistenceObjectsStrategy(persistenceObjectsStrategy);
kieSession = createKieSession(persistedSessionOption);
} else {
kieSession = RuntimeUtil.createKieSession(kieBase);
}
populateKieSessionPerIteration();

kieSession.fireAllRules();

// failover
failover();

// recreate kieBase
setupKieBase();
}

@Override
protected void populateKieSessionPerIteration() {
List<Room> rooms = new ArrayList<Room>();
for (int i = 0; i < factsNr; i++) {
rooms.add(new Room("room_" + i));
kieSession.insert(rooms.get(i));
kieSession.insert(new Fire(rooms.get(i)));
kieSession.insert(new Sprinkler(rooms.get(i)));
}
}

@Benchmark
public long test() {
kieSession = restoreSession();
//System.out.println("restored : facts size = " + kieSession.getFactHandles().size());
return kieSession.getIdentifier();
}

public static void main(String[] args) {
InsertFailoverFireBenchmark benchmark = new InsertFailoverFireBenchmark();
benchmark.factsNr = 10;
benchmark.mode = Mode.EMBEDDED;
benchmark.useObjectStoreWithReferences = true;
benchmark.useSafepoints = true;

benchmark.setupKieBase();
benchmark.setupAndFailover();
benchmark.test();
benchmark.tearDown();

benchmark.setupKieBase();
benchmark.setupAndFailover();
benchmark.test();
benchmark.tearDown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ protected void addEventSender(EventSender eventSender, int nrOfEvents) {
eventSenders.put(eventSender, nrOfEvents);
}

@Override
protected KieBaseOption[] getKieBaseOptions() {
return new KieBaseOption[]{EventProcessingOption.STREAM};
}
Expand Down

0 comments on commit cc89965

Please sign in to comment.