diff options
author | Jim Hahn <jrh3@att.com> | 2019-11-14 15:16:13 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-11-14 16:42:52 -0500 |
commit | 58c3811bfba7e421af8c9d2d72f55e95b4b01a50 (patch) | |
tree | 23e35bdb304e17665aae1e5662494bac77a30772 /policy-management/src/main/java/org/onap | |
parent | 8bb11a84b833c7db1342af0c5823ee8309f15c1a (diff) |
Invoke lock callback in session thread
Injects the callback as a DroolsRunnable into the session, if
there is one. Otherwise, it invokes it via the engine's
thread pool.
Issue-ID: POLICY-2246
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: I214480ae675d89e7335dde4eb4abe2684f7ef8ab
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-management/src/main/java/org/onap')
3 files changed, 33 insertions, 29 deletions
diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java index d4e4f5fc..5690b187 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java @@ -21,6 +21,8 @@ package org.onap.policy.drools.system.internal; import java.util.concurrent.ScheduledExecutorService; +import org.onap.policy.drools.core.DroolsRunnable; +import org.onap.policy.drools.core.PolicySession; import org.onap.policy.drools.core.lock.LockCallback; import org.onap.policy.drools.core.lock.LockImpl; import org.onap.policy.drools.core.lock.LockState; @@ -66,13 +68,9 @@ public abstract class FeatureLockImpl extends LockImpl { } /** - * Grants this lock. The notification is <i>always</i> invoked via a background - * thread. - * - * @param foreground {@code true} if to invoke the callback in the foreground thread, - * {@code false} otherwise + * Grants this lock. */ - protected synchronized void grant(boolean foreground) { + protected synchronized void grant() { if (isUnavailable()) { return; } @@ -81,32 +79,37 @@ public abstract class FeatureLockImpl extends LockImpl { updateGrant(); logger.info("lock granted: {}", this); - - if (foreground) { - notifyAvailable(); - } else { - getThreadPool().execute(this::notifyAvailable); - } + doNotify(this::notifyAvailable); } /** * Permanently denies this lock. * * @param reason the reason the lock was denied - * @param foreground {@code true} if to invoke the callback in the foreground thread, - * {@code false} otherwise */ - public void deny(String reason, boolean foreground) { + public void deny(String reason) { synchronized (this) { setState(LockState.UNAVAILABLE); } logger.info("{}: {}", reason, this); + doNotify(this::notifyUnavailable); + } + + /** + * Notifies the session of a change in the lock state. If a session is attached, then + * it simply injects the notifier into the session. Otherwise, it executes it via a + * background thread. + * + * @param notifier function to invoke the callback + */ + private void doNotify(DroolsRunnable notifier) { + PolicySession sess = getSession(); + if (sess != null) { + sess.insertDrools(notifier); - if (foreground) { - notifyUnavailable(); } else { - getThreadPool().execute(this::notifyUnavailable); + getThreadPool().execute(notifier); } } @@ -164,7 +167,7 @@ public abstract class FeatureLockImpl extends LockImpl { // do a quick check of the state if (isUnavailable() || !attachFeature()) { - deny(LOCK_LOST_MSG, true); + deny(LOCK_LOST_MSG); return false; } @@ -200,12 +203,13 @@ public abstract class FeatureLockImpl extends LockImpl { */ protected abstract boolean addToFeature(); - /** - * Gets the thread pool. - * - * @return the thread pool - */ + // these may be overridden by junit tests + protected ScheduledExecutorService getThreadPool() { return PolicyEngineConstants.getManager().getExecutorService(); } + + protected PolicySession getSession() { + return PolicySession.getCurrentSession(); + } } diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/LockManager.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/LockManager.java index 7e4505be..ef6b48d2 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/LockManager.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/LockManager.java @@ -156,7 +156,7 @@ public abstract class LockManager<T extends FeatureLockImpl> implements PolicyRe logger.debug("added lock to map {}", lock); finishLock(lock); } else { - lock.deny("resource is busy", true); + lock.deny("resource is busy"); } return lock; diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java index 839c17dc..a62d7667 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java @@ -158,14 +158,14 @@ public class SimpleLockManager extends LockManager<SimpleLockManager.SimpleLock> SimpleLock lock = lockref.get(); if (lock != null) { - lock.deny("lock expired", false); + lock.deny("lock expired"); } } } @Override protected void finishLock(SimpleLock lock) { - lock.grant(true); + lock.grant(); } @Override @@ -257,9 +257,9 @@ public class SimpleLockManager extends LockManager<SimpleLockManager.SimpleLock> } if (resource2lock.get(getResourceId()) == this) { - grant(true); + grant(); } else { - deny(NOT_LOCKED_MSG, true); + deny(NOT_LOCKED_MSG); } } |