diff options
author | 2018-11-06 18:05:18 +0000 | |
---|---|---|
committer | 2018-11-06 18:05:18 +0000 | |
commit | 830ede893c2a05ac1e7d69068c25d25fd7cc0049 (patch) | |
tree | d357f36573d4089ef50f5b4c6047425830ec24f6 /controlloop/templates/archetype-cl-amsterdam/src/main/resources | |
parent | 72d4564ec8089f6217e7a3773a2aa0dde6306241 (diff) | |
parent | 6f4b15df2c0711f042a930eed614c826704954e9 (diff) |
Merge "Cleanup all Param objects when any rules change"
Diffstat (limited to 'controlloop/templates/archetype-cl-amsterdam/src/main/resources')
-rw-r--r-- | controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl | 121 |
1 files changed, 56 insertions, 65 deletions
diff --git a/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl b/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl index cac512eac..53b4ca8bd 100644 --- a/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl +++ b/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl @@ -81,6 +81,8 @@ import org.slf4j.Logger; import java.time.Instant; import java.util.LinkedList; import java.util.Iterator; +import java.util.HashSet; +import java.util.Set; import org.onap.policy.drools.system.PolicyEngine; @@ -109,7 +111,8 @@ end */ declare ParamsCleaner closedLoopControlName : String - controlLoopYaml : String + identified : boolean // true if all active Params have been identified + active : Set // Params that are still active end /* @@ -127,7 +130,8 @@ end /* * -* Called once and only once to insert the parameters into working memory for this Closed Loop policy. +* Called to insert the parameters into working memory for this Closed Loop policy. This is called +* once each time a closed loop is added or its YAML is updated. * This has a higher salience so we can ensure that the Params is created before we have a chance to * discard any events. * @@ -143,6 +147,12 @@ rule "${policyName}.SETUP" params.setClosedLoopControlName("${closedLoopControlName}"); params.setControlLoopYaml("${controlLoopYaml}"); insert(params); + + ParamsCleaner cleaner = new ParamsCleaner(); + cleaner.setClosedLoopControlName("${closedLoopControlName}"); + cleaner.setIdentified(false); + cleaner.setActive(new HashSet()); + insert(cleaner); // Note: globals have bad behavior when persistence is used, // hence explicitly getting the logger vs using a global @@ -378,8 +388,9 @@ rule "${policyName}.EVENT.MANAGER" $params.getClosedLoopControlName(), drools.getRule().getName()); retract($manager.getOnsetEvent()); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that + // // TODO - what if we get subsequent Events for this RequestId? // By default, it will all start over again. May be confusing for Ruby. @@ -422,8 +433,8 @@ rule "${policyName}.EVENT.MANAGER" $params.getClosedLoopControlName(), drools.getRule().getName()); retract($manager.getOnsetEvent()); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that } } } else { @@ -473,8 +484,8 @@ rule "${policyName}.EVENT.MANAGER" PolicyEngine.manager.deliver("POLICY-CL-MGT", notification); retract($event); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that if (result.getB() != null) { retract(result.getB()); @@ -507,8 +518,8 @@ rule "${policyName}.EVENT.MANAGER" PolicyEngine.manager.deliver("POLICY-CL-MGT", notification); retract($event); - retract($manager); - retract($clTimer); + + // don't retract manager, etc. - a clean-up rule will do that } end @@ -1568,95 +1579,75 @@ rule "${policyName}.EVENT.CLEANUP" end /* -* -* When rules are deleted, the associated Params (and its subordinate objects) -* remain in working memory, because there are no longer any rules to clean -* them up. However, ANY time new rules are loaded, this rule will trigger -* a clean-up of ALL Params, regardless of their name & yaml, thus removing -* any that no longer have associated rules. -* This has a higher salience so that we immediately check Params when the -* rules change, before processing any events. -* +* Indicates to the cleaner that this Params object is still active. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.PARAMS.CHECKUP" - salience 2 +rule "${policyName}.PARAMS.ACTIVE" + salience 4 when - Params( $clName: closedLoopControlName, $yaml: controlLoopYaml ) + $params: Params( getClosedLoopControlName() == "${closedLoopControlName}", + getControlLoopYaml() == "${controlLoopYaml}" ) + ParamsCleaner( !identified, $active: active ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $clName, drools.getRule().getName(), $yaml); - - ParamsCleaner cleaner = new ParamsCleaner(); - cleaner.setClosedLoopControlName($clName); - cleaner.setControlLoopYaml($yaml); + logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), + $params.getControlLoopYaml()); + + $active.add($params); - insert(cleaner); + // do NOT update anything at this point end /* -* -* This rule removes "cleaner" objects for rules that are still active, thus -* preventing the associated Params objects from being removed. Any cleaners -* that are left after this rule has fired will cause their corresponding Params -* to be removed. -* This has a higher salience so that we discard the cleaner before it has -* a chance to force the removal of the associated Params. -* +* Finished identifying active Params objects. Begin deleting inactive Params. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.CLEANER.ACTIVE" - salience 2 +rule "${policyName}.PARAMS.IDENTIFIED" + salience 3 when - $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}", - getControlLoopYaml() == "${controlLoopYaml}" ) + $cleaner: ParamsCleaner( !identified ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), - $cleaner.getControlLoopYaml()); + logger.info("{}: {}", $cleaner.getClosedLoopControlName(), drools.getRule().getName()); - retract($cleaner); + $cleaner.setIdentified(true); + update($cleaner); end /* -* -* This rule removes Params objects that no longer have associated rules; if a -* Params still had associated rules, then the cleaner would have been removed -* by those rules and thus this rule would not fire. -* This has a higher salience so that we remove old Params before it causes any -* events to be processed. -* +* Delete Params objects that have not been identified as being active. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.PARAMS.CLEANUP" - salience 1 +rule "${policyName}.PARAMS.DELETE" + salience 2 when - $params: Params( $clName: closedLoopControlName, $yaml: controlLoopYaml ) - ParamsCleaner( getClosedLoopControlName() == $clName, getControlLoopYaml() == $yaml ) + $params: Params( ) + ParamsCleaner( identified, !active.contains($params) ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), + logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), $params.getControlLoopYaml()); - + retract($params); - // Note: the cleaner may be needed for cleaning additional params, thus - // we do not retract it here - we'll leave that to another rule + // do NOT update anything at this point end /* -* -* This rule removes "cleaner" objects when they're no longer needed. -* +* Finished deleting inactive Params objects, so remove the cleaner. +* This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.CLEANER.CLEANUP" +rule "${policyName}.PARAMS.CLEANED" + salience 1 when - $cleaner: ParamsCleaner( ) + $cleaner: ParamsCleaner( identified ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), - $cleaner.getControlLoopYaml()); + logger.info("{}: {}", $cleaner.getClosedLoopControlName(), drools.getRule().getName()); retract($cleaner); end |