Reverted the introduction of an automation threadpool (#1890)

Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
Kai Kreuzer 2020-12-05 11:50:16 +01:00 committed by GitHub
parent 34a3487e3e
commit f99135ae3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 33 deletions

View File

@ -963,12 +963,11 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
* @param td {@link TriggerData} object containing new values for {@link Trigger}'s {@link Output}s
*/
protected void runRule(String ruleUID, TriggerHandlerCallbackImpl.TriggerData td) {
TriggerHandlerCallbackImpl callback = thCallbacks.get(ruleUID);
if (callback == null) {
if (thCallbacks.get(ruleUID) == null) {
// the rule was unregistered
return;
}
synchronized (callback) {
synchronized (this) {
final RuleStatus ruleStatus = getRuleStatus(ruleUID);
if (ruleStatus != null && ruleStatus != RuleStatus.IDLE) {
logger.error("Failed to execute rule {}' with status '{}'", ruleUID, ruleStatus.name());
@ -976,28 +975,29 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
}
// change state to RUNNING
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING));
try {
clearContext(ruleUID);
setTriggerOutputs(ruleUID, td);
final WrappedRule rule = managedRules.get(ruleUID);
if (rule != null) {
boolean isSatisfied = calculateConditions(rule);
if (isSatisfied) {
executeActions(rule, true);
logger.debug("The rule '{}' is executed.", ruleUID);
} else {
logger.debug("The rule '{}' is NOT executed, since it has unsatisfied conditions.", ruleUID);
}
}
try {
clearContext(ruleUID);
setTriggerOutputs(ruleUID, td);
final WrappedRule rule = managedRules.get(ruleUID);
if (rule != null) {
boolean isSatisfied = calculateConditions(rule);
if (isSatisfied) {
executeActions(rule, true);
logger.debug("The rule '{}' is executed.", ruleUID);
} else {
logger.debug("The rule '{}' is NOT executed, since it has unsatisfied conditions.", ruleUID);
}
} catch (Throwable t) {
logger.error("Failed to execute rule '{}': {}", ruleUID, t.getMessage());
logger.debug("", t);
}
// change state to IDLE only if the rule has not been DISABLED.
synchronized (this) {
if (getRuleStatus(ruleUID) == RuleStatus.RUNNING) {
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.IDLE));
}
} catch (Throwable t) {
logger.error("Failed to execute rule '{}': {}", ruleUID, t.getMessage());
logger.debug("", t);
}
// change state to IDLE only if the rule has not been DISABLED.
synchronized (this) {
if (getRuleStatus(ruleUID) == RuleStatus.RUNNING) {
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.IDLE));
}
}
}

View File

@ -12,28 +12,30 @@
*/
package org.openhab.core.automation.internal;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.openhab.core.automation.RuleStatus;
import org.openhab.core.automation.RuleStatusInfo;
import org.openhab.core.automation.Trigger;
import org.openhab.core.automation.handler.TriggerHandlerCallback;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.common.NamedThreadFactory;
/**
* This class is implementation of {@link TriggerHandlerCallback} used by the {@link Trigger}s to notify rule engine
* about appearing of new triggered data. There is one and only one {@link TriggerHandlerCallback} per RuleImpl and
* it is used by all rule's {@link Trigger}s.
* about
* appearing of new triggered data. There is one and only one {@link TriggerHandlerCallback} per RuleImpl and it is used
* by all rule's {@link Trigger}s.
*
* @author Yordan Mihaylov - Initial contribution
* @author Kai Kreuzer - improved stability
*/
public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
private static final String AUTOMATION_THREADPOOL_NAME = "automation";
private final String ruleUID;
private ExecutorService executor;
@ -45,7 +47,7 @@ public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
protected TriggerHandlerCallbackImpl(RuleEngineImpl re, String ruleUID) {
this.re = re;
this.ruleUID = ruleUID;
executor = ThreadPoolManager.getScheduledPool(AUTOMATION_THREADPOOL_NAME);
executor = Executors.newSingleThreadExecutor(new NamedThreadFactory("rule-" + ruleUID));
}
@Override
@ -90,7 +92,13 @@ public class TriggerHandlerCallbackImpl implements TriggerHandlerCallback {
}
public void dispose() {
executor = null;
synchronized (this) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
executor.shutdownNow();
return null;
});
executor = null;
}
}
@Override

View File

@ -13,13 +13,14 @@
package org.openhab.core.model.rule.jvmmodel;
import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.common.NamedThreadFactory;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.events.system.StartlevelEvent;
import org.openhab.core.events.system.SystemEventFactory;
@ -58,13 +59,13 @@ public class RulesRefresher implements ReadyTracker {
// delay in seconds before rule resources are refreshed after items or services have changed
private static final long REFRESH_DELAY = 5;
private static final String POOL_NAME = "automation";
public static final String RULES_REFRESH = "rules_refresh";
private final Logger logger = LoggerFactory.getLogger(RulesRefresher.class);
private @Nullable ScheduledFuture<?> job;
private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(POOL_NAME);
private final ScheduledExecutorService scheduler = Executors
.newSingleThreadScheduledExecutor(new NamedThreadFactory("rulesRefresher"));
private boolean started;
private final ReadyMarker marker = new ReadyMarker("dsl", RULES_REFRESH);