Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute rules on a separate executor service to avoid congestion #23

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,6 @@ public synchronized void dispose() {
initFuture = null;
}
jRuleHandler.dispose();
jRuleEngine.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

import org.eclipse.jdt.annotation.NonNull;
Expand Down Expand Up @@ -93,6 +97,10 @@ public class JRuleEngine implements PropertyChangeListener {

private static volatile JRuleEngine instance;

private ThreadPoolExecutor ruleExecutorService;
private final static int MIN_EXECUTORS = 2; // Should be made configurable
private final static int MAX_EXECUTORS = 10;// Should be made configurable

private JRuleConfig config;

private final Map<String, List<JRule>> itemToRules = new HashMap<>();
Expand All @@ -108,6 +116,21 @@ public class JRuleEngine implements PropertyChangeListener {
.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);

private JRuleEngine() {

ThreadFactory ruleExecutorThreadFactory = new ThreadFactory() {
private final AtomicLong threadIndex = new AtomicLong(0);

@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("JRule-Executor-" + threadIndex.getAndIncrement());
return thread;
}
};

// Keep unused threads for 2 minutes before scaling back
ruleExecutorService = new ThreadPoolExecutor(MIN_EXECUTORS, MAX_EXECUTORS, 2L, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(), ruleExecutorThreadFactory);
}

public static JRuleEngine get() {
Expand Down Expand Up @@ -469,7 +492,11 @@ private String getItemNameFromEvent(Event event) {
return null;
}

private synchronized void invokeRule(JRuleExecutionContext context, JRuleEvent event) {
private void invokeRule(JRuleExecutionContext context, JRuleEvent event) {
ruleExecutorService.submit(() -> invokeRuleInSeparateThread(context, event));
}

private void invokeRuleInSeparateThread(JRuleExecutionContext context, JRuleEvent event) {
JRuleLog.debug(logger, context.getLogName(), "Invoking rule for context: {}", context);
final JRule rule = context.getJrule();
final Method method = context.getMethod();
Expand Down Expand Up @@ -497,4 +524,13 @@ private synchronized static String getStackTraceAsString(Throwable throwable) {
public void setConfig(@NonNull JRuleConfig config) {
this.config = config;
}

public void dispose() {
ruleExecutorService.shutdown();
try {
ruleExecutorService.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
logWarn("Not all rules ran to completion before rule engine shutdown");
}
}
}