diff options
author | Jim Hahn <jrh3@att.com> | 2018-11-07 13:44:21 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-11-09 16:47:15 -0500 |
commit | 6c72ec89f54bce0741350d3f299c5b441b4f60cc (patch) | |
tree | dff7c94dd2665f8dd5b17d7ab7b76a054e54ebef /controlloop/templates/archetype-cl-amsterdam | |
parent | 830ede893c2a05ac1e7d69068c25d25fd7cc0049 (diff) |
Close timing loop-hole when YAML updated
Noticed when YAML updates are pushed close together that some
Params objects are deleted that should not be. Fixed that by
eliminating the "active" list concept and only deleting a Params
object that has an associated cleaner. If it has no cleaner,
then it can't be deleted, regardless of timing with rule updates.
Added more tests for rule updates, including cases to check
for event objects being retracted, as well as Params objects
being retracted when rules are deleted.
Change-Id: I6b744b29fca228022f43e9322ea149b16d097675
Issue-ID: POLICY-1248
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/templates/archetype-cl-amsterdam')
-rw-r--r-- | controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl | 93 |
1 files changed, 58 insertions, 35 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 53b4ca8bd..be399d9f7 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,8 +81,6 @@ 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; @@ -107,12 +105,18 @@ declare Params end /* + * Used to trigger clean up Params that no longer have associated rules. + */ +declare ParamsInitCleaner + closedLoopControlName : String // only used when logging +end + +/* * Used to clean up Params that no longer have associated rules. */ declare ParamsCleaner closedLoopControlName : String - identified : boolean // true if all active Params have been identified - active : Set // Params that are still active + controlLoopYaml : String end /* @@ -148,11 +152,9 @@ rule "${policyName}.SETUP" params.setControlLoopYaml("${controlLoopYaml}"); insert(params); - ParamsCleaner cleaner = new ParamsCleaner(); - cleaner.setClosedLoopControlName("${closedLoopControlName}"); - cleaner.setIdentified(false); - cleaner.setActive(new HashSet()); - insert(cleaner); + ParamsInitCleaner initCleaner = new ParamsInitCleaner(); + initCleaner.setClosedLoopControlName("${closedLoopControlName}"); + insert(initCleaner); // Note: globals have bad behavior when persistence is used, // hence explicitly getting the logger vs using a global @@ -1579,52 +1581,74 @@ rule "${policyName}.EVENT.CLEANUP" end /* -* 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. +* Creates a cleaner for every Params object. +* This has a higher salience so that it is fired before PARAMS.FINISHED in ANY policy. */ -rule "${policyName}.PARAMS.ACTIVE" - salience 4 +rule "${policyName}.PARAMS.CLEANING" + salience 2 when - $params: Params( getClosedLoopControlName() == "${closedLoopControlName}", - getControlLoopYaml() == "${controlLoopYaml}" ) - ParamsCleaner( !identified, $active: active ) + $params: Params( ) + ParamsInitCleaner( ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), $params.getControlLoopYaml()); - $active.add($params); - - // do NOT update anything at this point + ParamsCleaner cleaner = new ParamsCleaner(); + cleaner.setClosedLoopControlName($params.getClosedLoopControlName()); + cleaner.setControlLoopYaml($params.getControlLoopYaml()); + insert(cleaner); end /* -* Finished identifying active Params objects. Begin deleting inactive Params. +* Finished creating cleaner objects, so remove the trigger. * This has a higher salience so that it is fired before processing any events. */ -rule "${policyName}.PARAMS.IDENTIFIED" - salience 3 +rule "${policyName}.PARAMS.FINISHED" + salience 1 when - $cleaner: ParamsCleaner( !identified ) + $initCleaner: ParamsInitCleaner( ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {}", $cleaner.getClosedLoopControlName(), drools.getRule().getName()); + logger.info("{}: {}", $initCleaner.getClosedLoopControlName(), drools.getRule().getName()); - $cleaner.setIdentified(true); - update($cleaner); + retract($initCleaner); end /* -* 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. +* Identifies Params objects that are still active, removing their associated cleaners. +* This should only leave one active Params object for each policy. +* This has a higher salience so that it is fired before PARAMS.DELETE in ANY policy. +*/ +rule "${policyName}.PARAMS.ACTIVE" + salience 3 + when + $params: Params( getClosedLoopControlName() == "${closedLoopControlName}", + getControlLoopYaml() == "${controlLoopYaml}" ) + $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}", + getControlLoopYaml() == "${controlLoopYaml}" ) + then + + Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); + logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), + $params.getControlLoopYaml()); + + retract($cleaner); +end + +/* +* Delete Params objects that are not active (i.e., those that still have an associated +* cleaner object). +* This has a higher salience so that it is fired before PARAMS.CLEANED in ANY policy. */ rule "${policyName}.PARAMS.DELETE" salience 2 when $params: Params( ) - ParamsCleaner( identified, !active.contains($params) ) + $cleaner: ParamsCleaner( getClosedLoopControlName() == $params.getClosedLoopControlName(), + getControlLoopYaml() == $params.getControlLoopYaml() ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); @@ -1632,22 +1656,21 @@ rule "${policyName}.PARAMS.DELETE" $params.getControlLoopYaml()); retract($params); - - // do NOT update anything at this point end /* -* Finished deleting inactive Params objects, so remove the cleaner. +* Finished clean-up, so delete the cleaner objects. * This has a higher salience so that it is fired before processing any events. */ rule "${policyName}.PARAMS.CLEANED" salience 1 when - $cleaner: ParamsCleaner( identified ) + $cleaner: ParamsCleaner( ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {}", $cleaner.getClosedLoopControlName(), drools.getRule().getName()); - + logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), + $cleaner.getControlLoopYaml()); + retract($cleaner); end |