Align the hazelcast cluster names 99/134399/2
authormpriyank <priyank.maheshwari@est.tech>
Fri, 28 Apr 2023 14:02:00 +0000 (15:02 +0100)
committermpriyank <priyank.maheshwari@est.tech>
Tue, 2 May 2023 11:09:29 +0000 (12:09 +0100)
- We need to align the hazelcast cluster names as members of the same
  cluster can join each other. We will still have exclusivity as the
  instance names and configs are different for each distributed object.
- Exposing env variable to override the cluster name depending on the
  env it is run on.
- Modified test cases to validate the cluster names as well

Issue-ID: CPS-1637
Change-Id: Ib0f8c80dc9b2268f976b0c2d3ccd6d64792d4781
Signed-off-by: mpriyank <priyank.maheshwari@est.tech>
cps-application/src/main/resources/application.yml
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/ForwardedSubscriptionEventCacheConfig.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/ForwardedSubscriptionEventCacheConfigSpec.groovy
cps-ncmp-service/src/test/resources/application.yml
cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java
cps-service/src/main/java/org/onap/cps/cache/HazelcastCacheConfig.java
cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy
cps-service/src/test/resources/application.yml

index 1eb1c11..ccd09c8 100644 (file)
@@ -199,6 +199,7 @@ ncmp:
 
 # Custom Hazelcast Config.
 hazelcast:
+    cluster-name: ${CPS_NCMP_CACHES_CLUSTER_NAME:"cps-and-ncmp-common-cache-cluster"}
     mode:
         kubernetes:
             enabled: ${HAZELCAST_MODE_KUBERNETES_ENABLED:false}
index ff7afc9..62a380c 100644 (file)
@@ -51,9 +51,7 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public BlockingQueue<DataNode> moduleSyncWorkQueue() {
-        return createHazelcastInstance("moduleSyncWorkQueue", commonQueueConfig,
-            "synchronization-caches")
-            .getQueue("moduleSyncWorkQueue");
+        return createHazelcastInstance("moduleSyncWorkQueue", commonQueueConfig).getQueue("moduleSyncWorkQueue");
     }
 
     /**
@@ -63,9 +61,8 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public IMap<String, Object> moduleSyncStartedOnCmHandles() {
-        return createHazelcastInstance("moduleSyncStartedOnCmHandles", moduleSyncStartedConfig,
-            "synchronization-caches")
-            .getMap("moduleSyncStartedOnCmHandles");
+        return createHazelcastInstance("moduleSyncStartedOnCmHandles", moduleSyncStartedConfig).getMap(
+                "moduleSyncStartedOnCmHandles");
     }
 
     /**
@@ -75,8 +72,6 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public IMap<String, Boolean> dataSyncSemaphores() {
-        return createHazelcastInstance("dataSyncSemaphores", dataSyncSemaphoresConfig,
-            "synchronization-caches")
-            .getMap("dataSyncSemaphores");
+        return createHazelcastInstance("dataSyncSemaphores", dataSyncSemaphoresConfig).getMap("dataSyncSemaphores");
     }
 }
index d2c3dc2..443ebc6 100644 (file)
@@ -45,7 +45,6 @@ public class ForwardedSubscriptionEventCacheConfig extends HazelcastCacheConfig
     @Bean
     public IMap<String, Set<String>> forwardedSubscriptionEventCache() {
         return createHazelcastInstance("hazelCastInstanceSubscriptionEvents",
-            forwardedSubscriptionEventCacheMapConfig, "cps-ncmp-service-caches")
-            .getMap("forwardedSubscriptionEventCache");
+                forwardedSubscriptionEventCacheMapConfig).getMap("forwardedSubscriptionEventCache");
     }
 }
index 567debd..c0fc18a 100644 (file)
@@ -59,20 +59,28 @@ class SynchronizationCacheConfigSpec extends Specification {
 
     def 'Verify configs for Distributed objects'(){
         given: 'the Module Sync Work Queue config'
-            def queueConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.queueConfigs.get('defaultQueueConfig')
+            def moduleSyncWorkQueueConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config
+            def moduleSyncDefaultWorkQueueConfig =  moduleSyncWorkQueueConfig.queueConfigs.get('defaultQueueConfig')
         and: 'the Module Sync Started Cm Handle Map config'
-            def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.mapConfigs.get('moduleSyncStartedConfig')
+            def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config
+            def moduleSyncStartedOnCmHandlesMapConfig =  moduleSyncStartedOnCmHandlesConfig.mapConfigs.get('moduleSyncStartedConfig')
         and: 'the Data Sync Semaphores Map config'
-            def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.mapConfigs.get('dataSyncSemaphoresConfig')
+            def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config
+            def dataSyncSemaphoresMapConfig =  dataSyncSemaphoresConfig.mapConfigs.get('dataSyncSemaphoresConfig')
         expect: 'system created instance with correct config of Module Sync Work Queue'
-            assert queueConfig.backupCount == 3
-            assert queueConfig.asyncBackupCount == 3
+            assert moduleSyncDefaultWorkQueueConfig.backupCount == 3
+            assert moduleSyncDefaultWorkQueueConfig.asyncBackupCount == 3
         and: 'Module Sync Started Cm Handle Map has the correct settings'
-            assert moduleSyncStartedOnCmHandlesConfig.backupCount == 3
-            assert moduleSyncStartedOnCmHandlesConfig.asyncBackupCount == 3
+            assert moduleSyncStartedOnCmHandlesMapConfig.backupCount == 3
+            assert moduleSyncStartedOnCmHandlesMapConfig.asyncBackupCount == 3
         and: 'Data Sync Semaphore Map has the correct settings'
-            assert dataSyncSemaphoresConfig.backupCount == 3
-            assert dataSyncSemaphoresConfig.asyncBackupCount == 3
+            assert dataSyncSemaphoresMapConfig.backupCount == 3
+            assert dataSyncSemaphoresMapConfig.asyncBackupCount == 3
+        and: 'all instances are part of same cluster'
+            def testClusterName = 'cps-and-ncmp-test-caches'
+            assert moduleSyncWorkQueueConfig.clusterName == testClusterName
+            assert moduleSyncStartedOnCmHandlesConfig.clusterName == testClusterName
+            assert dataSyncSemaphoresConfig.clusterName == testClusterName
     }
 
     def 'Verify deployment network configs for Distributed objects'() {
index 7448daf..03d3a1c 100644 (file)
@@ -44,10 +44,12 @@ class ForwardedSubscriptionEventCacheConfigSpec extends Specification {
 
     def 'Verify configs for Distributed Caches'(){
         given: 'the Forwarded Subscription Event Cache config'
-            def forwardedSubscriptionEventCacheConfig =  Hazelcast.getHazelcastInstanceByName('hazelCastInstanceSubscriptionEvents').config.mapConfigs.get('forwardedSubscriptionEventCacheMapConfig')
+            def forwardedSubscriptionEventCacheConfig =  Hazelcast.getHazelcastInstanceByName('hazelCastInstanceSubscriptionEvents').config
+            def forwardedSubscriptionEventCacheMapConfig =  forwardedSubscriptionEventCacheConfig.mapConfigs.get('forwardedSubscriptionEventCacheMapConfig')
         expect: 'system created instance with correct config'
-            assert forwardedSubscriptionEventCacheConfig.backupCount == 3
-            assert forwardedSubscriptionEventCacheConfig.asyncBackupCount == 3
+            assert forwardedSubscriptionEventCacheConfig.clusterName == 'cps-and-ncmp-test-caches'
+            assert forwardedSubscriptionEventCacheMapConfig.backupCount == 3
+            assert forwardedSubscriptionEventCacheMapConfig.asyncBackupCount == 3
     }
 
     def 'Verify deployment network configs for Distributed Caches'() {
index 679248b..66194ad 100644 (file)
@@ -39,6 +39,7 @@ ncmp:
 
 # Custom Hazelcast Config.
 hazelcast:
+  cluster-name: "cps-and-ncmp-test-caches"
   mode:
     kubernetes:
       enabled: false
index 5ee6b38..efe19c6 100644 (file)
@@ -40,7 +40,6 @@ public class AnchorDataCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public IMap<String, AnchorDataCacheEntry> anchorDataCache() {
-        return createHazelcastInstance("hazelCastInstanceCpsCore", anchorDataCacheMapConfig, "cps-service-caches")
-            .getMap("anchorDataCache");
+        return createHazelcastInstance("hazelCastInstanceCpsCore", anchorDataCacheMapConfig).getMap("anchorDataCache");
     }
 }
index 4aebcea..405e6e2 100644 (file)
@@ -35,6 +35,9 @@ import org.springframework.beans.factory.annotation.Value;
 @Slf4j
 public class HazelcastCacheConfig {
 
+    @Value("${hazelcast.cluster-name}")
+    protected String clusterName;
+
     @Value("${hazelcast.mode.kubernetes.enabled}")
     protected boolean cacheKubernetesEnabled;
 
@@ -42,12 +45,11 @@ public class HazelcastCacheConfig {
     protected String cacheKubernetesServiceName;
 
     protected HazelcastInstance createHazelcastInstance(final String hazelcastInstanceName,
-                                                        final NamedConfig namedConfig, final String clusterName) {
-        return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig, clusterName));
+                                                        final NamedConfig namedConfig) {
+        return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig));
     }
 
-    private Config initializeConfig(final String instanceName, final NamedConfig namedConfig,
-                                    final String clusterName) {
+    private Config initializeConfig(final String instanceName, final NamedConfig namedConfig) {
         final Config config = new Config(instanceName);
         if (namedConfig instanceof MapConfig) {
             config.addMapConfig((MapConfig) namedConfig);
index 76b5345..e219945 100644 (file)
@@ -46,10 +46,12 @@ class AnchorDataCacheConfigSpec extends Specification {
 
     def 'Verify configs for Distributed Caches'(){
         given: 'the Anchor Data Cache config'
-            def anchorDataCacheConfig =  Hazelcast.getHazelcastInstanceByName('hazelCastInstanceCpsCore').config.mapConfigs.get('anchorDataCacheMapConfig')
+            def anchorDataCacheConfig =  Hazelcast.getHazelcastInstanceByName('hazelCastInstanceCpsCore').config
+            def anchorDataCacheMapConfig =  anchorDataCacheConfig.mapConfigs.get('anchorDataCacheMapConfig')
         expect: 'system created instance with correct config'
-            assert anchorDataCacheConfig.backupCount == 3
-            assert anchorDataCacheConfig.asyncBackupCount == 3
+            assert anchorDataCacheConfig.clusterName == 'cps-and-ncmp-test-caches'
+            assert anchorDataCacheMapConfig.backupCount == 3
+            assert anchorDataCacheMapConfig.asyncBackupCount == 3
     }
 
     def 'Verify deployment network configs for Distributed Caches'() {
index 21f3745..dc10066 100644 (file)
@@ -51,6 +51,7 @@ logging:
 
 # Custom Hazelcast Config.
 hazelcast:
+  cluster-name: "cps-and-ncmp-test-caches"
   mode:
     kubernetes:
       enabled: false