From cd1d0fb2229af043c36f8225d6e5d8a8e8fe78dd Mon Sep 17 00:00:00 2001 From: jhh Date: Tue, 16 Feb 2021 18:09:46 -0600 Subject: deploy stats in lifecycle module Issue-ID: POLICY-3033 Signed-off-by: jhh Change-Id: Iaa78aa48064e8288265aeb498279e9d8324538d8 Signed-off-by: jhh --- .../onap/policy/drools/lifecycle/LifecycleFsm.java | 122 +++++++++++++++++---- .../drools/lifecycle/LifecycleStateRunning.java | 17 ++- .../server/restful/RestLifecycleManager.java | 10 +- 3 files changed, 117 insertions(+), 32 deletions(-) (limited to 'feature-lifecycle/src/main/java/org') diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java index ff53a00a..e2c50d4f 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java @@ -21,8 +21,10 @@ package org.onap.policy.drools.lifecycle; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -38,6 +40,7 @@ import java.util.stream.Collectors; import lombok.Getter; import lombok.NonNull; import lombok.Setter; +import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; @@ -50,12 +53,14 @@ import org.onap.policy.common.endpoints.listeners.ScoListener; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.drools.metrics.Metric; import org.onap.policy.drools.persistence.SystemPersistenceConstants; import org.onap.policy.drools.policies.DomainMaker; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngineConstants; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpHealthStatus; @@ -125,12 +130,10 @@ public class LifecycleFsm implements Startable { protected long statusTimerSeconds = DEFAULT_STATUS_TIMER_SECONDS; @Getter - @Setter private String group; @Getter - @Setter - protected String subgroup; + protected String subGroup; @Getter protected Set mandatoryPolicyTypes = new HashSet<>(); @@ -141,26 +144,31 @@ public class LifecycleFsm implements Startable { @Getter protected final Map policiesMap = new HashMap<>(); + @Getter + protected final PdpStatistics stats = new PdpStatistics(); + /** * Constructor. */ public LifecycleFsm() { - this.properties = SystemPersistenceConstants.getManager().getProperties(CONFIGURATION_PROPERTIES_NAME); - this.group = this.properties.getProperty(GROUP_NAME, DEFAULT_PDP_GROUP); + properties = SystemPersistenceConstants.getManager().getProperties(CONFIGURATION_PROPERTIES_NAME); + setGroup(properties.getProperty(GROUP_NAME, DEFAULT_PDP_GROUP)); - this.policyTypesMap.put(POLICY_TYPE_DROOLS_NATIVE_CONTROLLER, + policyTypesMap.put(POLICY_TYPE_DROOLS_NATIVE_CONTROLLER, new PolicyTypeNativeDroolsController(this, POLICY_TYPE_DROOLS_NATIVE_CONTROLLER)); - this.policyTypesMap.put( + policyTypesMap.put( POLICY_TYPE_DROOLS_NATIVE_RULES, new PolicyTypeNativeArtifactController(this, POLICY_TYPE_DROOLS_NATIVE_RULES)); - String commaSeparatedPolicyTypes = this.properties.getProperty(MANDATORY_POLICY_TYPES); + String commaSeparatedPolicyTypes = properties.getProperty(MANDATORY_POLICY_TYPES); if (!StringUtils.isBlank(commaSeparatedPolicyTypes)) { - Collections.addAll(this.mandatoryPolicyTypes, commaSeparatedPolicyTypes.split("\\s*,\\s*")); + Collections.addAll(mandatoryPolicyTypes, commaSeparatedPolicyTypes.split("\\s*,\\s*")); } logger.info("The mandatory Policy Types are {}. Compliance is {}", - this.mandatoryPolicyTypes, this.isMandatoryPolicyTypesCompliant()); + mandatoryPolicyTypes, isMandatoryPolicyTypesCompliant()); + + stats.setPdpInstanceId(Metric.HOSTNAME); } @GsonJsonIgnore @@ -180,6 +188,21 @@ public class LifecycleFsm implements Startable { return state.state(); } + /** + * set group. + */ + public synchronized void setGroup(String group) { + this.group = group; + this.stats.setPdpGroupName(group); + } + + /** + * set subgroup. + */ + public synchronized void setSubGroup(String subGroup) { + this.subGroup = subGroup; + this.stats.setPdpSubGroupName(subGroup); + } /* ** FSM events - entry points of events into the FSM ** */ @@ -274,7 +297,8 @@ public class LifecycleFsm implements Startable { logger.info("lifecycle event: update"); return state.update(update); } - /* ** FSM State Actions ** */ + + /* FSM State Actions (executed sequentially) */ protected boolean startAction() { if (isAlive()) { @@ -300,7 +324,7 @@ public class LifecycleFsm implements Startable { } protected boolean statusAction() { - return statusAction(state(), null); + return statusAction(null); } protected boolean statusAction(PdpResponseDetails response) { @@ -314,17 +338,13 @@ public class LifecycleFsm implements Startable { PdpStatus status = statusPayload(state); if (response != null) { - status.setRequestId(response.getResponseTo()); // for standard logging of transactions + status.setRequestId(response.getResponseTo()); status.setResponse(response); } return client.send(status); } - protected void setSubGroupAction(String subgroup) { - this.subgroup = subgroup; - } - protected synchronized void transitionToAction(@NonNull LifecycleState newState) { state = newState; } @@ -403,14 +423,58 @@ public class LifecycleFsm implements Startable { } protected void deployedPolicyAction(@NonNull ToscaPolicy policy) { - policiesMap.put(policy.getIdentifier(), policy); + policiesMap.computeIfAbsent(policy.getIdentifier(), key -> { + // avoid counting reapplies in a second pass when a mix of native and non-native + // policies are present. + getStats().setPolicyDeployCount(getStats().getPolicyDeployCount() + 1); + getStats().setPolicyDeploySuccessCount(getStats().getPolicyDeploySuccessCount() + 1); + return policy; + }); } protected void undeployedPolicyAction(@NonNull ToscaPolicy policy) { - policiesMap.remove(policy.getIdentifier()); + policiesMap.computeIfPresent(policy.getIdentifier(), (key, value) -> { + // avoid counting reapplies in a second pass when a mix of native and non-native + // policies are present. + getStats().setPolicyDeployCount(getStats().getPolicyDeployCount() + 1); + getStats().setPolicyDeploySuccessCount(getStats().getPolicyDeploySuccessCount() + 1); + return null; + }); + } + + protected void failedDeployPolicyAction(@NonNull ToscaPolicy failedPolicy) { // NOSONAR + getStats().setPolicyDeployCount(getStats().getPolicyDeployCount() + 1); + getStats().setPolicyDeployFailCount(getStats().getPolicyDeployFailCount() + 1); + } + + protected void failedUndeployPolicyAction(ToscaPolicy failedPolicy) { + failedDeployPolicyAction(failedPolicy); + policiesMap.remove(failedPolicy.getIdentifier()); + } + + protected void updateDeployCountsAction(Long deployCount, Long deploySuccesses, Long deployFailures) { + PdpStatistics statistics = getStats(); + if (deployCount != null) { + statistics.setPolicyDeployCount(deployCount); + } + + if (deploySuccesses != null) { + statistics.setPolicyDeploySuccessCount(deploySuccesses); + } + + if (deployFailures != null) { + statistics.setPolicyDeployFailCount(deployFailures); + } + } + + protected void resetDeployCountsAction() { + getStats().setPolicyDeployCount(0); + getStats().setPolicyDeployFailCount(0); + getStats().setPolicyDeploySuccessCount(0); } protected List resetPoliciesAction() { + resetDeployCountsAction(); List policies = new ArrayList<>(getActivePolicies()); policiesMap.clear(); return policies; @@ -509,18 +573,32 @@ public class LifecycleFsm implements Startable { scheduler.shutdownNow(); } - private PdpStatus statusPayload(@NonNull PdpState state) { + protected PdpStatus statusPayload(@NonNull PdpState state) { PdpStatus status = new PdpStatus(); status.setName(name); status.setPdpGroup(group); - status.setPdpSubgroup(subgroup); + status.setPdpSubgroup(subGroup); status.setState(state); status.setHealthy(isAlive() ? PdpHealthStatus.HEALTHY : PdpHealthStatus.NOT_HEALTHY); status.setPdpType("drools"); status.setPolicies(new ArrayList<>(policiesMap.keySet())); + status.setStatistics(statisticsPayload()); return status; } + private PdpStatistics statisticsPayload() { + PdpStatistics updateStats = new PdpStatistics(stats); + updateStats.setTimeStamp(new Date()); + + try { + BeanUtils.copyProperties(updateStats, PolicyEngineConstants.getManager().getStats()); + } catch (IllegalAccessException | InvocationTargetException ex) { + logger.debug("statistics mapping failure", ex); + } + + return updateStats; + } + private boolean source() { List sources = TopicEndpointManager.getManager().addTopicSources(properties); if (sources.isEmpty()) { @@ -560,7 +638,7 @@ public class LifecycleFsm implements Startable { return name == null && group != null && Objects.equals(group, getGroup()) - && Objects.equals(subgroup, getSubgroup()); + && Objects.equals(subgroup, getSubGroup()); } /* **** IO listeners ***** */ diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java index a4a5d66d..dacb6058 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java @@ -122,6 +122,9 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { @Override public boolean update(@NonNull PdpUpdate update) { + // UPDATE messages contain the complete universe of active policies in this PDP-D + // regardless if they were already functioning in the PDP-D or new. + synchronized (fsm) { if (update.getPdpHeartbeatIntervalMs() != null && !fsm.setStatusIntervalAction(update.getPdpHeartbeatIntervalMs() / 1000)) { @@ -132,7 +135,7 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { // update subgroup if applicable per update message - fsm.setSubGroupAction(update.getPdpSubgroup()); + fsm.setSubGroup(update.getPdpSubgroup()); // snapshot the active policies previous to apply the new set of active // policies as given by the PAP in the update message @@ -168,9 +171,12 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { failedPolicies.addAll(reApplyNonNativePolicies(activePoliciesPreUpdateMap)); - return fsm.statusAction(response(update.getRequestId(), - (failedPolicies.isEmpty()) ? PdpResponseStatus.SUCCESS : PdpResponseStatus.FAIL, - fsm.getPolicyIdsMessage(failedPolicies))) && failedPolicies.isEmpty(); + PdpResponseDetails response = + response(update.getRequestId(), + (failedPolicies.isEmpty()) ? PdpResponseStatus.SUCCESS : PdpResponseStatus.FAIL, + fsm.getPolicyIdsMessage(failedPolicies)); + + return fsm.statusAction(response) && failedPolicies.isEmpty(); } } @@ -195,12 +201,13 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { List failedUndeployPolicies = undeployPolicies(policies); if (!failedUndeployPolicies.isEmpty()) { logger.warn("update-policies: undeployment failures: {}", fsm.getPolicyIdsMessage(failedUndeployPolicies)); - failedUndeployPolicies.forEach(fsm::undeployedPolicyAction); + failedUndeployPolicies.forEach(fsm::failedUndeployPolicyAction); } List failedDeployPolicies = deployPolicies(policies); if (!failedDeployPolicies.isEmpty()) { logger.warn("update-policies: deployment failures: {}", fsm.getPolicyIdsMessage(failedDeployPolicies)); + failedDeployPolicies.forEach(fsm::failedDeployPolicyAction); } return Pair.of(failedUndeployPolicies, failedDeployPolicies); diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java index c1b0aab0..e9969094 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java @@ -101,7 +101,7 @@ public class RestLifecycleManager { @ApiOperation(value = "Retrieves the Lifecycle subgroup", notes = "Lifecycle Subgroup", response = String.class) public Response subgroup() { - return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubgroup()).build(); + return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubGroup()).build(); } /** @@ -114,8 +114,8 @@ public class RestLifecycleManager { notes = "Lifecycle Subgroup", response = String.class) public Response subgroup( @ApiParam(value = "Subgroup", required = true) @PathParam("subgroup") String subgroup) { - LifecycleFeature.getFsm().setSubgroup(subgroup); - return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubgroup()).build(); + LifecycleFeature.getFsm().setSubGroup(subgroup); + return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().getSubGroup()).build(); } /** @@ -153,7 +153,7 @@ public class RestLifecycleManager { PdpStateChange change = new PdpStateChange(); change.setPdpGroup(LifecycleFeature.getFsm().getGroup()); - change.setPdpSubgroup(LifecycleFeature.getFsm().getSubgroup()); + change.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup()); change.setState(PdpState.valueOf(state)); change.setName(LifecycleFeature.getFsm().getName()); @@ -440,7 +440,7 @@ public class RestLifecycleManager { PdpUpdate update = new PdpUpdate(); update.setName(LifecycleFeature.getFsm().getName()); update.setPdpGroup(LifecycleFeature.getFsm().getGroup()); - update.setPdpSubgroup(LifecycleFeature.getFsm().getSubgroup()); + update.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup()); update.setPolicies(policies); return update; } -- cgit 1.2.3-korg