From ef4198b40ee5108cf8c549c09a0a04b09e3f959a Mon Sep 17 00:00:00 2001 From: jhh Date: Wed, 10 Feb 2021 16:20:35 -0600 Subject: support transaction reports in engine Issue-ID: POLICY-3033 Signed-off-by: jhh Change-Id: Id1f02c86be491d5145e74725f76953e294e2975c Signed-off-by: jhh --- .../org/onap/policy/drools/stats/PolicyStats.java | 121 +++++++++++++++++++++ .../policy/drools/stats/PolicyStatsManager.java | 52 +++++++++ .../onap/policy/drools/system/PolicyEngine.java | 34 +++++- .../policy/drools/system/PolicyEngineManager.java | 39 +++++-- 4 files changed, 233 insertions(+), 13 deletions(-) create mode 100644 policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStats.java create mode 100644 policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStatsManager.java (limited to 'policy-management/src/main') diff --git a/policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStats.java b/policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStats.java new file mode 100644 index 00000000..d6d8d500 --- /dev/null +++ b/policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStats.java @@ -0,0 +1,121 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.stats; + +import static org.slf4j.LoggerFactory.getLogger; + +import java.time.Instant; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.drools.metrics.TransMetric; +import org.slf4j.Logger; + +/** + * Basic policy execution statistics. + */ + +@Data +@NoArgsConstructor +@EqualsAndHashCode +@ToString +public class PolicyStats { + + private static final Logger logger = getLogger(PolicyStats.class); + + /** + * Number of executed policy transactions. + */ + private long policyExecutedCount; + + /** + * Number of successfully executed policy transactions. + */ + private long policyExecutedSuccessCount; + + /** + * Number of failed executions of policy transactions. + */ + private long policyExecutedFailCount; + + /** + * Last time the policy transaction was executed. + */ + private long lastExecutionTime; + + /** + * Average execution time of a policy transaction. + */ + private double averageExecutionTime; + + /** + * Total policy execution times. + */ + private double totalElapsedTime; + + /** + * Uptime of the entity holding the stats. + */ + private long birthTime = Instant.now().toEpochMilli(); + + /** + * Time last transaction was started. + */ + private long lastStart; + + /** + * add a stat transaction record. + */ + public void stat(@NonNull TransMetric trans) { + policyExecutedCount++; + if (trans.isSuccess()) { + policyExecutedSuccessCount++; + } else { + policyExecutedFailCount++; + } + + // make sure transaction has values that we care about + + if (trans.getStartTime() == null) { + logger.warn("policy transaction contains no start time: {}", trans); + trans.setStartTime(null); + } + + if (trans.getEndTime() == null) { + logger.warn("policy transaction contains no end time: {}", trans); + trans.setEndTime(null); + } + + if (trans.getElapsedTime() == null) { + logger.warn("policy transaction contains no elapsed time: {}", trans); + trans.setElapsedTime(null); + } + + // compute after the preconditions are satisfied + + lastExecutionTime = trans.getEndTime().toEpochMilli(); + totalElapsedTime += trans.getElapsedTime(); + averageExecutionTime = totalElapsedTime / policyExecutedCount; + lastStart = trans.getStartTime().toEpochMilli(); + } +} diff --git a/policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStatsManager.java b/policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStatsManager.java new file mode 100644 index 00000000..bb1b2679 --- /dev/null +++ b/policy-management/src/main/java/org/onap/policy/drools/stats/PolicyStatsManager.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.stats; + +import java.util.HashMap; +import java.util.Map; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.drools.metrics.TransMetric; + +/** + * Policy Stats Manager that manages PDP-D statistics. + */ + +@NoArgsConstructor +@Data +@EqualsAndHashCode +@ToString +public class PolicyStatsManager { + + private final PolicyStats groupStat = new PolicyStats(); + private final Map subgroupStats = new HashMap<>(); + + /** + * stat a new transaction. + */ + public synchronized void stat(@NonNull String subGroupName, @NonNull TransMetric transaction) { + groupStat.stat(transaction); + subgroupStats.computeIfAbsent(subGroupName, key -> new PolicyStats()).stat(transaction); + } +} diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java index 685cf235..ef641f31 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,9 +33,12 @@ import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.drools.core.lock.Lock; import org.onap.policy.drools.core.lock.LockCallback; import org.onap.policy.drools.features.PolicyEngineFeatureApi; +import org.onap.policy.drools.metrics.Metric; +import org.onap.policy.drools.metrics.TransMetric; import org.onap.policy.drools.policies.DomainMaker; import org.onap.policy.drools.protocol.configuration.ControllerConfiguration; import org.onap.policy.drools.protocol.configuration.PdpdConfiguration; +import org.onap.policy.drools.stats.PolicyStatsManager; /** * Policy Engine, the top abstraction for the Drools PDP Policy Engine. It abstracts away a Drools @@ -147,7 +150,6 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { * @return the updated Policy Controller * @throws IllegalArgumentException in the configuration is invalid * @throws IllegalStateException if the controller is in a bad state - * @throws Exception any other reason */ PolicyController updatePolicyController(ControllerConfiguration configuration); @@ -234,14 +236,20 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { */ List getFeatures(); - /** * get domain maker. * - * @return the domain maker. + * @return the domain maker */ DomainMaker getDomainMaker(); + /** + * get statistics for this PDP. + * + * @return statistics + */ + PolicyStatsManager getStats(); + /** * Attempts the dispatching of an "event" object. * @@ -337,4 +345,22 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { * @return policy engine configuration */ Properties defaultTelemetryConfig(); + + /** + * Track a policy execution metric. + * + * @param controllerName controller name + * @param policyName policy name + * @param metric metric + */ + void metric(String controllerName, String policyName, Metric metric); + + /** + * Track a policy execution transaction. + * + * @param controllerName controller name + * @param policyName policy name + * @param transaction transaction + */ + void transaction(String controllerName, String policyName, TransMetric transaction); } diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java index 49461210..e20986cb 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,6 +67,8 @@ import org.onap.policy.drools.features.PolicyControllerFeatureApi; import org.onap.policy.drools.features.PolicyControllerFeatureApiConstants; import org.onap.policy.drools.features.PolicyEngineFeatureApi; import org.onap.policy.drools.features.PolicyEngineFeatureApiConstants; +import org.onap.policy.drools.metrics.Metric; +import org.onap.policy.drools.metrics.TransMetric; import org.onap.policy.drools.persistence.SystemPersistence; import org.onap.policy.drools.persistence.SystemPersistenceConstants; import org.onap.policy.drools.policies.DomainMaker; @@ -77,6 +79,7 @@ import org.onap.policy.drools.protocol.configuration.ControllerConfiguration; import org.onap.policy.drools.protocol.configuration.PdpdConfiguration; import org.onap.policy.drools.server.restful.RestManager; import org.onap.policy.drools.server.restful.aaf.AafTelemetryAuthFilter; +import org.onap.policy.drools.stats.PolicyStatsManager; import org.onap.policy.drools.system.internal.SimpleLockManager; import org.onap.policy.drools.utils.PropertyUtil; import org.onap.policy.drools.utils.logging.LoggerUtil; @@ -84,7 +87,6 @@ import org.onap.policy.drools.utils.logging.MdcTransaction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** * Policy Engine Manager Implementation. */ @@ -157,7 +159,10 @@ class PolicyEngineManager implements PolicyEngine { @Getter(AccessLevel.PROTECTED) private PolicyResourceLockManager lockManager = null; - private DomainMaker domainMaker = new DomainMaker(); + private final DomainMaker domainMaker = new DomainMaker(); + + @Getter + private final PolicyStatsManager stats = new PolicyStatsManager(); /** * gson parser to decode configuration requests. @@ -244,6 +249,21 @@ class PolicyEngineManager implements PolicyEngine { return defaultConfig; } + @Override + public void metric(String controllerName, String policyName, Metric metric) { + // sub-operations are not being tracked + } + + @Override + public void transaction(@NonNull String controllerName, + @NonNull String policyName, @NonNull TransMetric transaction) { + + // will stat on a per policy name that for an admin would + // be more significant than a controller name. + + getStats().stat(controllerName + "[" + policyName + "]", transaction); + } + @Override @GsonJsonIgnore public ScheduledExecutorService getExecutorService() { @@ -252,8 +272,9 @@ class PolicyEngineManager implements PolicyEngine { private ScheduledExecutorService makeExecutorService(Properties properties) { int nthreads = DEFAULT_EXECUTOR_THREADS; + try { - nthreads = Integer.valueOf( + nthreads = Integer.parseInt( properties.getProperty(EXECUTOR_THREAD_PROP, String.valueOf(DEFAULT_EXECUTOR_THREADS))); } catch (NumberFormatException e) { @@ -370,7 +391,7 @@ class PolicyEngineManager implements PolicyEngine { } else { final String msg = "Configuration Entity is not supported: " + entity; mdcTrans.resetSubTransaction().setStatusCode(false).setResponseDescription(msg).flush(); - logger.warn(LoggerUtil.TRANSACTION_LOG_MARKER_NAME, msg); + logger.warn(LoggerUtil.TRANSACTION_LOG_MARKER, msg); throw new IllegalArgumentException(msg); } } @@ -440,7 +461,7 @@ class PolicyEngineManager implements PolicyEngine { } catch (final Exception e) { mdcTrans.setStatusCode(false).setResponseCode(e.getClass().getName()) .setResponseDescription(e.getMessage()).flush(); - logger.error(LoggerUtil.TRANSACTION_LOG_MARKER_NAME, + logger.error(LoggerUtil.TRANSACTION_LOG_MARKER, "{}: cannot update-policy-controllers because of {}", this, e.getMessage(), e); } } @@ -679,7 +700,7 @@ class PolicyEngineManager implements PolicyEngine { } @FunctionalInterface - private static interface PredicateWithEx { + private interface PredicateWithEx { boolean test(T value) throws InterruptedException; } @@ -961,10 +982,10 @@ class PolicyEngineManager implements PolicyEngine { this.locked = false; - boolean success = true; + boolean success; try { - success = (this.lockManager == null || this.lockManager.unlock()) && success; + success = this.lockManager == null || this.lockManager.unlock(); } catch (final RuntimeException e) { logger.warn("{}: cannot unlock() lock manager because of {}", this, e.getMessage(), e); success = false; -- cgit 1.2.3-korg