diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2022-10-18 12:08:45 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2022-10-19 08:31:02 -0500 |
commit | 6652164b384221018bf605b1d5fb809425b26a8e (patch) | |
tree | 6e440f5379010f5bbb20841384c55b86da37b533 | |
parent | e58a8c52dadfe14b8b77c8eb7c236697c710ddd5 (diff) |
Support optional cluster name in policy engine
This allows also for correlation with running pod hostnames
in the case of multiple drools pdp flavors coexist.
Issue-ID: POLICY-4403
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I81affeeec622e6c0e9627651bf34c0775a796827
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
24 files changed, 193 insertions, 87 deletions
diff --git a/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql b/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql index be56d35e..07b30738 100644 --- a/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql +++ b/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql @@ -1,7 +1,7 @@ # ============LICENSE_START======================================================= # feature-distributed-locking # ================================================================================ -# Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (C) 2018, 2022 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. @@ -16,8 +16,15 @@ # limitations under the License. # ============LICENSE_END========================================================= - set foreign_key_checks=0; - - CREATE TABLE if not exists pooling.locks (resourceId VARCHAR(128), host VARCHAR(128), owner VARCHAR(128), expirationTime BIGINT, PRIMARY KEY (resourceId), INDEX idx_expirationTime(expirationTime), INDEX idx_host(host)); +SET foreign_key_checks=0; - set foreign_key_checks=1;
\ No newline at end of file +CREATE TABLE if NOT EXISTS pooling.locks( + resourceId VARCHAR(128), + host VARCHAR(128), + owner VARCHAR(128), + expirationTime BIGINT, + PRIMARY KEY (resourceId), + INDEX idx_expirationTime(expirationTime), + INDEX idx_host(host)); + +SET foreign_key_checks=1;
\ No newline at end of file diff --git a/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql b/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql index 847c44ac..0cdad5b9 100644 --- a/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql +++ b/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql @@ -16,8 +16,8 @@ # limitations under the License. # ============LICENSE_END========================================================= - set foreign_key_checks=0; +SET foreign_key_checks=0; - ALTER TABLE pooling.locks modify expirationTime timestamp default '1971-01-01 00:00:00.000000'; +ALTER TABLE pooling.locks MODIFY expirationTime TIMESTAMP DEFAULT '1971-01-01 00:00:00.000000'; - set foreign_key_checks=1; +SET foreign_key_checks=1; diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java index 04138eed..198b3303 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 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. @@ -87,17 +87,16 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D @Setter(AccessLevel.PROTECTED) private static DistributedLockManager latestInstance = null; - /** * Name of the host on which this JVM is running. */ - @Getter - private final String pdpName; + @Getter(AccessLevel.PUBLIC) + private String pdpName; /** * UUID of this object. */ - @Getter + @Getter(AccessLevel.PUBLIC) private final String uuidString = UUID.randomUUID().toString(); /** @@ -133,7 +132,6 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D * Constructs the object. */ public DistributedLockManager() { - this.pdpName = PolicyEngineConstants.PDP_NAME; this.resource2lock = getResource2lock(); } @@ -146,6 +144,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D public PolicyResourceLockManager beforeCreateLockManager(PolicyEngine engine, Properties properties) { try { + this.pdpName = PolicyEngineConstants.getManager().getPdpName(); this.featProps = new DistributedLockProperties(getProperties(CONFIGURATION_PROPERTIES_NAME)); this.dataSource = makeDataSource(); @@ -256,8 +255,9 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D */ private void checkExpired() { try { - logger.info("checking for expired locks"); Set<String> expiredIds = new HashSet<>(resource2lock.keySet()); + logger.info("checking for expired locks: {}", this); + identifyDbLocks(expiredIds); expireLocks(expiredIds); @@ -337,7 +337,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D DistributedLock lock = lockref.get(); if (lock != null) { - logger.debug("removed lock from map {}", lock); + logger.info("lost lock: removed lock from map {}", lock); lock.deny(FeatureLockImpl.LOCK_LOST_MSG); } } @@ -704,7 +704,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D * @throws SQLException if a DB error occurs */ protected boolean doDbInsert(Connection conn) throws SQLException { - logger.debug("insert lock record {}", this); + logger.info("insert lock record {}", this); try (var stmt = conn.prepareStatement("INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) " + "values (?, ?, ?, timestampadd(second, ?, now()))")) { @@ -731,7 +731,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D * @throws SQLException if a DB error occurs */ protected boolean doDbUpdate(Connection conn) throws SQLException { - logger.debug("update lock record {}", this); + logger.info("update lock record {}", this); try (var stmt = conn.prepareStatement("UPDATE pooling.locks SET resourceId=?, host=?, owner=?," + " expirationTime=timestampadd(second, ?, now()) WHERE resourceId=?" + " AND ((host=? AND owner=?) OR expirationTime < now())")) { @@ -763,7 +763,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D * @throws SQLException if a DB error occurs */ protected void doDbDelete(Connection conn) throws SQLException { - logger.debug("delete lock record {}", this); + logger.info("delete lock record {}", this); try (var stmt = conn .prepareStatement("DELETE FROM pooling.locks WHERE resourceId=? AND host=? AND owner=?")) { @@ -780,7 +780,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D * thread. */ private void removeFromMap() { - logger.debug("remove lock from map {}", this); + logger.info("remove lock from map {}", this); feature.resource2lock.remove(getResourceId(), this); synchronized (this) { @@ -799,10 +799,16 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D } @FunctionalInterface - private static interface RunnableWithEx { + private interface RunnableWithEx { void run() throws SQLException; } + @Override + public String toString() { + return "DistributedLockManager [" + "pdpName=" + pdpName + ", uuidString=" + uuidString + + ", resource2lock=" + resource2lock + "]"; + } + // these may be overridden by junit tests protected Properties getProperties(String fileName) { diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java index 2fbdb26b..082a6199 100644 --- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java +++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 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. @@ -156,6 +156,7 @@ public class DistributedLockManagerTest { @BeforeClass public static void setUpBeforeClass() throws SQLException { SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources"); + PolicyEngineConstants.getManager().configure(new Properties()); conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); @@ -311,10 +312,9 @@ public class DistributedLockManagerTest { /** * Tests deleteExpiredDbLocks(), when getConnection() throws an exception. * - * @throws SQLException if an error occurs */ @Test - public void testDeleteExpiredDbLocksEx() throws SQLException { + public void testDeleteExpiredDbLocksEx() { feature = new InvalidDbLockingFeature(TRANSIENT); // get the clean-up function and execute it @@ -342,16 +342,15 @@ public class DistributedLockManagerTest { /** * Tests afterStop(), when the data source throws an exception when close() is called. * - * @throws SQLException if an error occurs */ @Test - public void testAfterStopEx() throws SQLException { + public void testAfterStopEx() { shutdownFeature(); // use a data source that throws an exception when closed feature = new InvalidDbLockingFeature(TRANSIENT); - assertThatCode(() -> shutdownFeature()).doesNotThrowAnyException(); + assertThatCode(this::shutdownFeature).doesNotThrowAnyException(); } @Test @@ -877,7 +876,7 @@ public class DistributedLockManagerTest { } @Test - public void testDistributedLockRescheduleRequest() throws SQLException { + public void testDistributedLockRescheduleRequest() { // use a data source that throws an exception when getConnection() is called InvalidDbLockingFeature invfeat = new InvalidDbLockingFeature(TRANSIENT); feature = invfeat; @@ -937,7 +936,7 @@ public class DistributedLockManagerTest { } @Test - public void testDistributedLockDoRequest() throws SQLException { + public void testDistributedLockDoRequest() { lock = getLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false); assertTrue(lock.isWaiting()); @@ -1019,7 +1018,7 @@ public class DistributedLockManagerTest { // use a data source that throws an exception when getConnection() is called feature = new MyLockingFeature(true) { @Override - protected BasicDataSource makeDataSource() throws Exception { + protected BasicDataSource makeDataSource() { return datasrc; } }; @@ -1052,7 +1051,7 @@ public class DistributedLockManagerTest { // use a data source that throws an exception when getConnection() is called feature = new MyLockingFeature(true) { @Override - protected BasicDataSource makeDataSource() throws Exception { + protected BasicDataSource makeDataSource() { return datasrc; } }; @@ -1232,10 +1231,9 @@ public class DistributedLockManagerTest { /** * Tests doUnlock() when a DB exception is thrown. * - * @throws SQLException if an error occurs */ @Test - public void testDistributedLockDoUnlockEx() throws SQLException { + public void testDistributedLockDoUnlockEx() { feature = new InvalidDbLockingFeature(PERMANENT); lock = getLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false); @@ -1338,10 +1336,9 @@ public class DistributedLockManagerTest { /** * Tests doExtend() when both update and insert fail. * - * @throws SQLException if an error occurs */ @Test - public void testDistributedLockDoExtendNeitherSucceeds() throws SQLException { + public void testDistributedLockDoExtendNeitherSucceeds() { /* * this feature will create a lock that returns false when doDbUpdate() is * invoked, or when doDbInsert() is invoked a second time @@ -1441,7 +1438,7 @@ public class DistributedLockManagerTest { feature.beforeCreateLockManager(engine, new Properties()); feature.afterStart(engine); - assertThatCode(() -> shutdownFeature()).doesNotThrowAnyException(); + assertThatCode(this::shutdownFeature).doesNotThrowAnyException(); } /** 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 611ac240..09dcab00 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 @@ -39,6 +39,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; import lombok.Setter; @@ -153,6 +154,8 @@ public class LifecycleFsm implements Startable { @Setter protected String pdpType; + protected volatile String pdpName; + @Getter protected Set<String> mandatoryPolicyTypes = new HashSet<>(); @@ -186,12 +189,6 @@ public class LifecycleFsm implements Startable { logger.info("The mandatory Policy Types are {}. Compliance is {}", mandatoryPolicyTypes, isMandatoryPolicyTypesCompliant()); - - stats.setPdpInstanceId(PolicyEngineConstants.PDP_NAME); - } - - public String getName() { - return PolicyEngineConstants.PDP_NAME; } @GsonJsonIgnore @@ -205,6 +202,18 @@ public class LifecycleFsm implements Startable { } /** + * Returns the PDP Name. + */ + public String getPdpName() { + if (this.pdpName == null) { + this.pdpName = PolicyEngineConstants.getManager().getPdpName(); + this.stats.setPdpInstanceId(pdpName); + } + + return this.pdpName; + } + + /** * Current state. */ public PdpState state() { @@ -231,6 +240,8 @@ public class LifecycleFsm implements Startable { @Override public synchronized boolean start() { + this.pdpName = PolicyEngineConstants.getManager().getPdpName(); + stats.setPdpInstanceId(pdpName); logger.info("lifecycle event: start engine"); return state.start(); } @@ -651,7 +662,7 @@ public class LifecycleFsm implements Startable { protected PdpStatus statusPayload(@NonNull PdpState state) { var status = new PdpStatus(); - status.setName(getName()); + status.setName(getPdpName()); status.setPdpGroup(group); status.setPdpSubgroup(subGroup); status.setState(state); @@ -711,7 +722,7 @@ public class LifecycleFsm implements Startable { } protected boolean isItMe(String name, String group, String subgroup) { - if (Objects.equals(name, getName())) { + if (Objects.equals(name, getPdpName())) { return true; } 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 494c708b..5a534639 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -155,7 +155,7 @@ public class RestLifecycleManager { change.setPdpGroup(LifecycleFeature.getFsm().getGroup()); change.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup()); change.setState(PdpState.valueOf(state)); - change.setName(LifecycleFeature.getFsm().getName()); + change.setName(LifecycleFeature.getFsm().getPdpName()); return Response.status(Response.Status.OK).entity(LifecycleFeature.getFsm().stateChange(change)).build(); } @@ -442,7 +442,7 @@ public class RestLifecycleManager { private PdpUpdate getPolicyUpdate() { var update = new PdpUpdate(); - update.setName(LifecycleFeature.getFsm().getName()); + update.setName(LifecycleFeature.getFsm().getPdpName()); update.setPdpGroup(LifecycleFeature.getFsm().getGroup()); update.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup()); return update; diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java index 0ad776cc..8c3aeef1 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java @@ -30,6 +30,7 @@ import java.nio.file.Paths; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.concurrent.ScheduledExecutorService; import org.junit.AfterClass; @@ -106,6 +107,7 @@ public class LifecycleFsmTest { LoggerUtils.setLevel("org.onap.policy.common.endpoints", "WARN"); LoggerUtils.setLevel("org.onap.policy.drools", "INFO"); SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes"); + PolicyEngineConstants.getManager().configure(new Properties()); fsm = new LifecycleFsm() { @Override @@ -240,7 +242,7 @@ public class LifecycleFsmTest { assertEquals(fsm.getGroup(), status.getStatistics().getPdpGroupName()); assertEquals(fsm.getSubGroup(), status.getStatistics().getPdpSubGroupName()); - assertEquals(PolicyEngineConstants.PDP_NAME, status.getStatistics().getPdpInstanceId()); + assertEquals(PolicyEngineConstants.getManager().getPdpName(), status.getStatistics().getPdpInstanceId()); assertEquals(6, status.getStatistics().getPolicyDeploySuccessCount()); assertEquals(2, status.getStatistics().getPolicyDeployFailCount()); assertEquals(8, status.getStatistics().getPolicyDeployCount()); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java index c97133b5..5d56143d 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2021-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -160,7 +160,7 @@ public class LifecycleFsmUpdateTest { Files.deleteIfExists(Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), "foo-controller.properties.bak")); } catch (IOException ignored) { // NOSONAR - ; // checkstyle + // checkstyle } ControllerSupport.setStaticField(LifecycleFeature.class, "fsm", savedFsm); @@ -186,7 +186,7 @@ public class LifecycleFsmUpdateTest { change.setPdpGroup("A"); change.setPdpSubgroup("a"); change.setState(PdpState.ACTIVE); - change.setName(fsm.getName()); + change.setName(fsm.getPdpName()); fsm.setSubGroup("a"); fsm.source.offer(new StandardCoder().encode(change)); @@ -359,7 +359,7 @@ public class LifecycleFsmUpdateTest { protected PdpUpdate getPdpUpdate(List<ToscaPolicy> policiesToDeploy, List<ToscaPolicy> policiesToUndeploy) { PdpUpdate update = new PdpUpdate(); - update.setName(PolicyEngineConstants.PDP_NAME); + update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("A"); update.setPdpSubgroup("a"); update.setPoliciesToBeDeployed(policiesToDeploy); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java index f37e8ef7..67efa72a 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -77,7 +77,7 @@ public class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest change.setPdpGroup("A"); change.setPdpSubgroup("a"); change.setState(PdpState.ACTIVE); - change.setName(fsm.getName()); + change.setName(fsm.getPdpName()); fsm.setSubGroup("a"); fsm.source.offer(new StandardCoder().encode(change)); @@ -116,7 +116,7 @@ public class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME); PdpUpdate update = new PdpUpdate(); - update.setName(PolicyEngineConstants.PDP_NAME); + update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("W"); update.setPdpSubgroup("w"); update.setPoliciesToBeDeployed(List.of(policyNativeController)); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java index f4c33140..d00c8a41 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -75,7 +75,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { change.setPdpGroup("A"); change.setPdpSubgroup("a"); change.setState(PdpState.ACTIVE); - change.setName(fsm.getName()); + change.setName(fsm.getPdpName()); fsm.setSubGroup("a"); fsm.source.offer(new StandardCoder().encode(change)); @@ -161,7 +161,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup()); assertNotEquals("b", fsm.getSubGroup()); - change.setName(fsm.getName()); + change.setName(fsm.getPdpName()); fsm.source.offer(new StandardCoder().encode(change)); assertEquals(PdpState.ACTIVE, fsm.state()); assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup()); @@ -189,7 +189,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { // TODO: extract repeated similar assertion blocks into their own helper methods PdpUpdate update = new PdpUpdate(); - update.setName(PolicyEngineConstants.PDP_NAME); + update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("W"); update.setPdpSubgroup("w"); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java index d99f011d..d6c94996 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -143,7 +143,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); PdpUpdate update = new PdpUpdate(); - update.setName(PolicyEngineConstants.PDP_NAME); + update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("Z"); update.setPdpSubgroup("z"); @@ -269,7 +269,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertNull(fsm.getSubGroup()); PdpUpdate update = new PdpUpdate(); - update.setName(PolicyEngineConstants.PDP_NAME); + update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("A"); update.setPdpSubgroup("a"); @@ -294,7 +294,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); /* correct name */ - change.setName(fsm.getName()); + change.setName(fsm.getPdpName()); fsm.source.offer(new StandardCoder().encode(change)); assertEquals(PdpState.ACTIVE, fsm.state()); @@ -330,8 +330,8 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertEquals("foo", status.getPdpType()); assertEquals(PdpState.TERMINATED, status.getState()); assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy()); - assertEquals(PolicyEngineConstants.PDP_NAME, status.getName()); - assertEquals(fsm.getName(), status.getName()); + assertEquals(PolicyEngineConstants.getManager().getPdpName(), status.getName()); + assertEquals(fsm.getPdpName(), status.getName()); assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName()); } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java index eeac888d..7b210db8 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 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. @@ -75,8 +75,8 @@ public abstract class LifecycleStateRunningTest { try { Files.deleteIfExists(Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), CONTROLLER_NAME + "-controller.properties.bak")); - } catch (IOException e) { - ; + } catch (IOException ignored) { + // ignored } SystemPersistenceConstants.getManager().setConfigurationDir(null); } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java index 3d8d08b7..23643302 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -139,7 +139,7 @@ public class LifecycleStateTerminatedTest { @Test public void testUpdate() { PdpUpdate update = new PdpUpdate(); - update.setName(PolicyEngineConstants.PDP_NAME); + update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("A"); update.setPdpSubgroup("a"); update.setPoliciesToBeDeployed(Collections.emptyList()); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java index 2d8b8688..b6f354e4 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2022 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. @@ -50,7 +50,7 @@ public class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningT * Test initialization. */ @Before - public void init() throws IOException, CoderException { + public void init() { fsm = makeFsmWithPseudoTime(); } diff --git a/packages/install/src/files/base.conf b/packages/install/src/files/base.conf index ecd9e22b..24d95a76 100644 --- a/packages/install/src/files/base.conf +++ b/packages/install/src/files/base.conf @@ -43,6 +43,9 @@ TRUSTSTORE_PASSWD=Pol1cy_0nap #http_proxy= #https_proxy= +# Cluster Identifier if any +CLUSTER_NAME= + # Telemetry credentials TELEMETRY_PORT=9696 TELEMETRY_HOST=0.0.0.0 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 10623c50..75e7c74a 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-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2022 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. @@ -122,6 +122,25 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { String setEnvironmentProperty(String key, String value); /** + * Gets the hostname used by this PDP-D. + */ + String getHostName(); + + /** + * Gets the cluster name as configured in $CLUSTER_NAME, + * otherwise it will assume an UUID as the cluster name. + */ + String getClusterName(); + + /** + * Gets the PDP Name from hostname and $CLUSTER_NAME, + * otherwise if CLUSTER_NAME is not set, the PdpName + * will be the concatenation of the hostname and a + * UUID. + */ + String getPdpName(); + + /** * registers a new Policy Controller with the Policy Engine initialized per properties. * * @param properties properties to initialize the Policy Controller diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java index 23fc247e..ffd0c858 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-management * ================================================================================ - * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021-2022 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. @@ -44,11 +44,6 @@ public final class PolicyEngineConstants { public static final String TELEMETRY_SERVER_DEFAULT_NAME = "TELEMETRY"; /** - * Unique name of this drools-pdp JVM. - */ - public static final String PDP_NAME = NetworkUtil.genUniqueName("drools"); - - /** * Policy Engine Manager. */ @Getter 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 ea4094fc..19aec79a 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 @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Properties; +import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.atomic.AtomicReference; @@ -41,6 +42,8 @@ import java.util.stream.Stream; import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; +import lombok.Setter; +import lombok.Synchronized; import lombok.ToString; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.event.comm.Topic; @@ -59,6 +62,7 @@ import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.gson.annotation.GsonJsonProperty; import org.onap.policy.common.utils.logging.LoggerUtils; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.common.utils.resources.PrometheusUtils; import org.onap.policy.common.utils.services.FeatureApiUtils; import org.onap.policy.drools.controller.DroolsControllerConstants; @@ -107,6 +111,8 @@ class PolicyEngineManager implements PolicyEngine { public static final String EXECUTOR_THREAD_PROP = "executor.threads"; protected static final int DEFAULT_EXECUTOR_THREADS = 5; + public static final String CLUSTER_NAME_PROP = "engine.cluster"; + /** * logger. */ @@ -170,6 +176,18 @@ class PolicyEngineManager implements PolicyEngine { @Getter private final PolicyStatsManager stats = new PolicyStatsManager(); + @Getter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + @Setter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + private String clusterName = UUID.randomUUID().toString(); + + @Getter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + @Setter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + private String hostName = NetworkUtil.getHostname(); + + @Getter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + @Setter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + private String pdpName; + /** * gson parser to decode configuration requests. */ @@ -368,6 +386,10 @@ class PolicyEngineManager implements PolicyEngine { } this.properties = properties; + if (!StringUtils.isBlank(this.properties.getProperty(CLUSTER_NAME_PROP))) { + this.clusterName = this.properties.getProperty(CLUSTER_NAME_PROP, this.clusterName); + } + this.pdpName = hostName + "." + this.clusterName; try { this.sources = getTopicEndpointManager().addTopicSources(properties); @@ -438,7 +460,6 @@ class PolicyEngineManager implements PolicyEngine { @Override public synchronized PolicyController createPolicyController(String name, Properties properties) { - String tempName = name; // check if a PROPERTY_CONTROLLER_NAME property is present // if so, override the given name diff --git a/policy-management/src/main/server/config/engine.properties b/policy-management/src/main/server/config/engine.properties index 89a749c9..e30682f4 100644 --- a/policy-management/src/main/server/config/engine.properties +++ b/policy-management/src/main/server/config/engine.properties @@ -2,7 +2,7 @@ # ============LICENSE_START======================================================= # ONAP # ================================================================================ -# Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. +# Copyright (C) 2019, 2021-2022 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. @@ -36,3 +36,5 @@ http.server.services.CONFIG.serialization.provider=org.onap.policy.common.gson.J aaf.namespace=${envd:AAF_NAMESPACE:false} aaf.root.permission=${envd:AAF_NAMESPACE:org.onap.policy}.pdpd + +engine.cluster=${envd:CLUSTER_NAME} diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java index 82c28695..fe307f28 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java @@ -23,6 +23,7 @@ package org.onap.policy.drools.system; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -46,6 +47,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Properties; +import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -330,6 +332,8 @@ public class PolicyEngineManagerTest { @Test public void testSerialize() { + mgr.setHostName("foo"); + mgr.setClusterName("bar"); mgr.configure(properties); assertThatCode(() -> gson.compareGson(mgr, PolicyEngineManagerTest.class)).doesNotThrowAnyException(); } @@ -443,6 +447,35 @@ public class PolicyEngineManagerTest { assertFalse(config.isEmpty()); } + @Test + public void testGetPdpName() { + properties.setProperty(PolicyEngineManager.CLUSTER_NAME_PROP, "east1"); + mgr.configure(properties); + + var pdpName = mgr.getPdpName(); + assertEquals("east1", extractCluster(pdpName)); + assertEquals(mgr.getClusterName(), extractCluster(pdpName)); + assertEquals(mgr.getHostName(), extractHostname(pdpName)); + + mgr.setHostName("foo"); + mgr.setClusterName("bar"); + mgr.setPdpName("foo.bar"); + + pdpName = mgr.getPdpName(); + assertEquals("bar", extractCluster(pdpName)); + assertEquals(mgr.getClusterName(), extractCluster(pdpName)); + assertEquals("foo", extractHostname(pdpName)); + assertEquals(mgr.getHostName(), extractHostname(pdpName)); + } + + private String extractCluster(String name) { + return name.substring(name.lastIndexOf(".") + 1); + } + + private String extractHostname(String name) { + return name.substring(0, name.lastIndexOf(".")); + } + /** * Tests that makeExecutorService() uses the value from the thread * property. diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java index 0fdc00a6..2054d91d 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2022 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. @@ -155,17 +155,18 @@ public class PolicyEngineTest { @Test public void test100Configure() { - logger.info("enter"); - - final Properties engineProps = PolicyEngineConstants.getManager().defaultTelemetryConfig(); + var manager = (PolicyEngineManager) PolicyEngineConstants.getManager(); + var engineProps = manager.defaultTelemetryConfig(); /* override default port */ engineProps.put(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PolicyEngineConstants.TELEMETRY_SERVER_DEFAULT_NAME + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "" + DEFAULT_TELEMETRY_PORT); - assertFalse(PolicyEngineConstants.getManager().isAlive()); - PolicyEngineConstants.getManager().configure(engineProps); + assertFalse(manager.isAlive()); + manager.setHostName("foo"); + manager.setClusterName("0"); + manager.configure(engineProps); assertFalse(PolicyEngineConstants.getManager().isAlive()); logger.info("engine {} has configuration {}", PolicyEngineConstants.getManager(), engineProps); diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json index d2d2075e..4c7e1f03 100644 --- a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json +++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json @@ -13,6 +13,9 @@ { "port": 1002 } ], "locked": false, + "hostName": "foo", + "clusterName": "bar", + "pdpName": "foo.bar", "sinks": [ { "name": "sink1-topic" }, { "name": "sink2-topic" } diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json index 555f361f..35afaff8 100644 --- a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json +++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json @@ -1,6 +1,9 @@ { "alive": true, "locked": false, + "hostName": "foo", + "clusterName": "0", + "pdpName": "foo.0", "sources": [], "sinks": [], "httpServers": [ diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json index 0e010323..9053c90a 100644 --- a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json +++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json @@ -14,6 +14,9 @@ "prometheus": false } ], + "clusterName": "0", + "hostName": "foo", + "pdpName": "foo.0", "features": [], "controllers": [], "stats": { |