use of com.netflix.simianarmy.janitor.Rule in project SimianArmy by Netflix.
the class BasicJanitorRuleEngine method isValid.
/**
* Decides whether the resource should be a candidate of cleanup based on the underlying rules. If any rule in the
* rule set thinks the resource should be a candidate of cleanup, the method returns false which indicates that the
* resource should be marked for cleanup. If multiple rules think the resource should be cleaned up, the rule with
* the nearest expected termination time fills the termination reason and expected termination time.
*
* @param resource
* The resource
* @return true if the resource is valid and should not be a candidate of cleanup based on the underlying rules,
* false otherwise.
*/
@Override
public boolean isValid(Resource resource) {
LOGGER.debug(String.format("Checking if resource %s of type %s is a cleanup candidate against %d rules and %d exclusion rules.", resource.getId(), resource.getResourceType(), rules.size(), exclusionRules.size()));
for (Rule exclusionRule : exclusionRules) {
if (exclusionRule.isValid(resource)) {
LOGGER.info(String.format("Resource %s is not marked as a cleanup candidate because of an exclusion rule.", resource.getId()));
return true;
}
}
// We create a clone of the resource each time when we try the rule. In the first iteration of the rules
// we identify the rule with the nearest termination date if there is any rule considers the resource
// as a cleanup candidate. Then the rule is applied to the original resource.
Rule nearestRule = null;
if (rules.size() == 1) {
nearestRule = rules.get(0);
} else {
Date nearestTerminationTime = null;
for (Rule rule : rules) {
Resource clone = resource.cloneResource();
if (!rule.isValid(clone)) {
if (clone.getExpectedTerminationTime() != null) {
if (nearestTerminationTime == null || nearestTerminationTime.after(clone.getExpectedTerminationTime())) {
nearestRule = rule;
nearestTerminationTime = clone.getExpectedTerminationTime();
}
}
}
}
}
if (nearestRule != null && !nearestRule.isValid(resource)) {
LOGGER.info(String.format("Resource %s is marked as a cleanup candidate.", resource.getId()));
return false;
} else {
LOGGER.info(String.format("Resource %s is not marked as a cleanup candidate.", resource.getId()));
return true;
}
}