use one hazelcast instance per jvm 30/139130/5
authormpriyank <priyank.maheshwari@est.tech>
Tue, 10 Sep 2024 15:12:28 +0000 (16:12 +0100)
committermpriyank <priyank.maheshwari@est.tech>
Thu, 10 Oct 2024 11:46:36 +0000 (12:46 +0100)
- updated all the cache(distributed datastructures) to use the same instance config now.
- there will be just one instance per JVM now
- Better cleanup of hz instance in the testware
- Refactored the testware to verify the configs
- Expected Impact on lowering the memory usage, less number of TCP
  communications between members
- NOTE: we need to do a full regression as the changes impacts all the
  use cases which involves cache

Issue-ID: CPS-2408
Change-Id: I7564992a9990f44ef3defb4f50cb7d094cad7b92
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/impl/cache/HazelcastCacheConfig.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/cmnotificationsubscription/cache/CmSubscriptionConfig.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/SynchronizationCacheConfig.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelCacheConfig.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/cache/HazelcastCacheConfigSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/cmnotificationsubscription/cache/CmSubscriptionConfigSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/sync/SynchronizationCacheConfigSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelCacheConfigSpec.groovy
cps-ncmp-service/src/test/resources/application.yml
integration-test/src/test/resources/application.yml

index 05b0d09..cf9c4fb 100644 (file)
@@ -249,6 +249,7 @@ ncmp:
 # Custom Hazelcast Config.
 hazelcast:
     cluster-name: ${CPS_NCMP_CACHES_CLUSTER_NAME:"cps-and-ncmp-common-cache-cluster"}
+    instance-config-name: ${CPS_NCMP_INSTANCE_CONFIG_NAME:"cps-and-ncmp-hazelcast-instance-config"}
     mode:
         kubernetes:
             enabled: ${HAZELCAST_MODE_KUBERNETES_ENABLED:false}
index d911fc6..ad8025b 100644 (file)
@@ -40,19 +40,30 @@ public class HazelcastCacheConfig {
     @Value("${hazelcast.cluster-name}")
     protected String clusterName;
 
+    @Value("${hazelcast.instance-config-name}")
+    protected String instanceConfigName;
+
     @Value("${hazelcast.mode.kubernetes.enabled}")
     protected boolean cacheKubernetesEnabled;
 
     @Value("${hazelcast.mode.kubernetes.service-name}")
     protected String cacheKubernetesServiceName;
 
-    protected HazelcastInstance createHazelcastInstance(final String hazelcastInstanceName,
-                                                        final NamedConfig namedConfig) {
-        return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig));
+    protected HazelcastInstance getOrCreateHazelcastInstance(final NamedConfig namedConfig) {
+        return Hazelcast.getOrCreateHazelcastInstance(defineInstanceConfig(instanceConfigName, namedConfig));
+    }
+
+    private Config defineInstanceConfig(final String instanceConfigName, final NamedConfig namedConfig) {
+        final Config config = getHazelcastInstanceConfig(instanceConfigName);
+        config.setClusterName(clusterName);
+        config.setClassLoader(org.onap.cps.spi.model.Dataspace.class.getClassLoader());
+        dataStructuresConfig(namedConfig, config);
+        exposeClusterInformation(config);
+        updateDiscoveryMode(config);
+        return config;
     }
 
-    private Config initializeConfig(final String instanceName, final NamedConfig namedConfig) {
-        final Config config = new Config(instanceName);
+    private static void dataStructuresConfig(final NamedConfig namedConfig, final Config config) {
         if (namedConfig instanceof MapConfig) {
             config.addMapConfig((MapConfig) namedConfig);
         }
@@ -62,11 +73,16 @@ public class HazelcastCacheConfig {
         if (namedConfig instanceof SetConfig) {
             config.addSetConfig((SetConfig) namedConfig);
         }
+    }
 
-        config.setClusterName(clusterName);
-        config.setClassLoader(org.onap.cps.spi.model.Dataspace.class.getClassLoader());
-        exposeClusterInformation(config);
-        updateDiscoveryMode(config);
+    private Config getHazelcastInstanceConfig(final String instanceConfigName) {
+        final HazelcastInstance hazelcastInstance = Hazelcast.getHazelcastInstanceByName(instanceConfigName);
+        Config config = null;
+        if (hazelcastInstance != null) {
+            config = hazelcastInstance.getConfig();
+        } else {
+            config = new Config(instanceConfigName);
+        }
         return config;
     }
 
index e890d72..cb51020 100644 (file)
@@ -43,7 +43,7 @@ public class CmSubscriptionConfig extends HazelcastCacheConfig {
      */
     @Bean
     public IMap<String, Map<String, DmiCmSubscriptionDetails>> cmNotificationSubscriptionCache() {
-        return createHazelcastInstance("hazelCastInstanceCmNotificationSubscription",
-                cmNotificationSubscriptionCacheMapConfig).getMap("cmNotificationSubscriptionCache");
+        return getOrCreateHazelcastInstance(cmNotificationSubscriptionCacheMapConfig).getMap(
+                "cmNotificationSubscriptionCache");
     }
 }
index 8ef98bc..c5fae0d 100644 (file)
@@ -51,7 +51,7 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public BlockingQueue<DataNode> moduleSyncWorkQueue() {
-        return createHazelcastInstance("moduleSyncWorkQueue", commonQueueConfig).getQueue("moduleSyncWorkQueue");
+        return getOrCreateHazelcastInstance(commonQueueConfig).getQueue("moduleSyncWorkQueue");
     }
 
     /**
@@ -61,7 +61,7 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public IMap<String, Object> moduleSyncStartedOnCmHandles() {
-        return createHazelcastInstance("moduleSyncStartedOnCmHandles", moduleSyncStartedConfig).getMap(
+        return getOrCreateHazelcastInstance(moduleSyncStartedConfig).getMap(
                 "moduleSyncStartedOnCmHandles");
     }
 
@@ -72,6 +72,6 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
      */
     @Bean
     public IMap<String, Boolean> dataSyncSemaphores() {
-        return createHazelcastInstance("dataSyncSemaphores", dataSyncSemaphoresConfig).getMap("dataSyncSemaphores");
+        return getOrCreateHazelcastInstance(dataSyncSemaphoresConfig).getMap("dataSyncSemaphores");
     }
 }
index 0e9eaf5..06ca67e 100644 (file)
@@ -46,8 +46,7 @@ public class TrustLevelCacheConfig extends HazelcastCacheConfig {
      */
     @Bean(TRUST_LEVEL_PER_CM_HANDLE)
     public Map<String, TrustLevel> trustLevelPerCmHandle() {
-        return createHazelcastInstance("hazelcastInstanceTrustLevelPerCmHandleMap",
-                trustLevelPerCmHandleCacheConfig).getMap(TRUST_LEVEL_PER_CM_HANDLE);
+        return getOrCreateHazelcastInstance(trustLevelPerCmHandleCacheConfig).getMap(TRUST_LEVEL_PER_CM_HANDLE);
     }
 
     /**
@@ -57,7 +56,7 @@ public class TrustLevelCacheConfig extends HazelcastCacheConfig {
      */
     @Bean(TRUST_LEVEL_PER_DMI_PLUGIN)
     public Map<String, TrustLevel> trustLevelPerDmiPlugin() {
-        return createHazelcastInstance("hazelcastInstanceTrustLevelPerDmiPluginMap",
+        return getOrCreateHazelcastInstance(
                 trustLevelPerDmiPluginCacheConfig).getMap(TRUST_LEVEL_PER_DMI_PLUGIN);
     }
 
index e7eb893..dc38e0f 100644 (file)
@@ -29,29 +29,24 @@ class HazelcastCacheConfigSpec extends Specification {
     def objectUnderTest = new HazelcastCacheConfig()
 
     def 'Create Hazelcast instance with a #scenario'() {
-        given: 'a cluster name'
+        given: 'a cluster name and instance name'
             objectUnderTest.clusterName = 'my cluster'
+            objectUnderTest.instanceConfigName = 'my instance config'
         when: 'an hazelcast instance is created (name has to be unique)'
-            def result = objectUnderTest.createHazelcastInstance(scenario, config)
+            def result = objectUnderTest.getOrCreateHazelcastInstance(config)
         then: 'the instance is created and has the correct name'
-            assert result.name == scenario
+            assert result.name == 'my instance config'
         and: 'if applicable it has a map config with the expected name'
             if (expectMapConfig) {
                 assert result.config.mapConfigs.values()[0].name == 'my map config'
-            } else {
-                assert result.config.mapConfigs.isEmpty()
             }
         and: 'if applicable it has a queue config with the expected name'
             if (expectQueueConfig) {
                 assert result.config.queueConfigs.values()[0].name == 'my queue config'
-            } else {
-                assert result.config.queueConfigs.isEmpty()
             }
         and: 'if applicable it has a set config with the expected name'
             if (expectSetConfig) {
                 assert result.config.setConfigs.values()[0].name == 'my set config'
-            } else {
-                assert result.config.setConfigs.isEmpty()
             }
         where: 'the following configs are used'
             scenario       | config                                                    || expectMapConfig | expectQueueConfig | expectSetConfig
index 915bccb..740567c 100644 (file)
@@ -36,13 +36,17 @@ class CmSubscriptionConfigSpec extends Specification {
     @Autowired
     IMap<String, Map<String, DmiCmSubscriptionDetails>> cmNotificationSubscriptionCache;
 
+    def cleanupSpec() {
+        Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').shutdown()
+    }
+
     def 'Embedded (hazelcast) cache for Cm Notification Subscription Cache.'() {
         expect: 'system is able to create an instance of the Cm Notification Subscription Cache'
             assert null != cmNotificationSubscriptionCache
         and: 'there is at least 1 instance'
             assert Hazelcast.allHazelcastInstances.size() > 0
         and: 'Cm Notification Subscription Cache is present'
-            assert Hazelcast.allHazelcastInstances.name.contains('hazelCastInstanceCmNotificationSubscription')
+            assert Hazelcast.allHazelcastInstances.name.contains('cps-and-ncmp-hazelcast-instance-test-config')
     }
 
     def 'Provided CM Subscription data'() {
index 8e59922..4c96d6b 100644 (file)
@@ -46,6 +46,10 @@ class SynchronizationCacheConfigSpec extends Specification {
     @Autowired
     private IMap<String, Boolean> dataSyncSemaphores
 
+    def cleanupSpec() {
+        Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').shutdown()
+    }
+
     def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
         expect: 'system is able to create an instance of the Module Sync Work Queue'
             assert null != moduleSyncWorkQueue
@@ -53,22 +57,19 @@ class SynchronizationCacheConfigSpec extends Specification {
             assert null != moduleSyncStartedOnCmHandles
         and: 'system is able to create an instance of a map to hold data sync semaphores'
             assert null != dataSyncSemaphores
-        and: 'there are at least 3 instances'
-            assert Hazelcast.allHazelcastInstances.size() > 2
         and: 'they have the correct names (in any order)'
-            assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores')
+            assert Hazelcast.allHazelcastInstances.name.contains('cps-and-ncmp-hazelcast-instance-test-config')
     }
 
     def 'Verify configs for Distributed objects'(){
-        given: 'the Module Sync Work Queue config'
-            def moduleSyncWorkQueueConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config
-            def moduleSyncDefaultWorkQueueConfig =  moduleSyncWorkQueueConfig.queueConfigs.get('defaultQueueConfig')
+        given: 'hazelcast common config'
+            def hzConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config
+        and: 'the Module Sync Work Queue config'
+            def moduleSyncDefaultWorkQueueConfig =  hzConfig.queueConfigs.get('defaultQueueConfig')
         and: 'the Module Sync Started Cm Handle Map config'
-            def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config
-            def moduleSyncStartedOnCmHandlesMapConfig =  moduleSyncStartedOnCmHandlesConfig.mapConfigs.get('moduleSyncStartedConfig')
+            def moduleSyncStartedOnCmHandlesMapConfig =  hzConfig.mapConfigs.get('moduleSyncStartedConfig')
         and: 'the Data Sync Semaphores Map config'
-            def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config
-            def dataSyncSemaphoresMapConfig =  dataSyncSemaphoresConfig.mapConfigs.get('dataSyncSemaphoresConfig')
+            def dataSyncSemaphoresMapConfig =  hzConfig.mapConfigs.get('dataSyncSemaphoresConfig')
         expect: 'system created instance with correct config of Module Sync Work Queue'
             assert moduleSyncDefaultWorkQueueConfig.backupCount == 1
             assert moduleSyncDefaultWorkQueueConfig.asyncBackupCount == 0
@@ -79,28 +80,15 @@ class SynchronizationCacheConfigSpec extends Specification {
             assert dataSyncSemaphoresMapConfig.backupCount == 1
             assert dataSyncSemaphoresMapConfig.asyncBackupCount == 0
         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
+            assert hzConfig.clusterName == 'cps-and-ncmp-test-caches'
     }
 
     def 'Verify deployment network configs for Distributed objects'() {
-        given: 'the Module Sync Work Queue config'
-            def queueNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.networkConfig
-        and: 'the Module Sync Started Cm Handle Map config'
-            def moduleSyncStartedOnCmHandlesNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.networkConfig
-        and: 'the Data Sync Semaphores Map config'
-            def dataSyncSemaphoresNetworkConfig = Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.networkConfig
-        expect: 'system created instance with correct config of Module Sync Work Queue'
-            assert queueNetworkConfig.join.autoDetectionConfig.enabled
-            assert !queueNetworkConfig.join.kubernetesConfig.enabled
-        and: 'Module Sync Started Cm Handle Map has the correct settings'
-            assert moduleSyncStartedOnCmHandlesNetworkConfig.join.autoDetectionConfig.enabled
-            assert !moduleSyncStartedOnCmHandlesNetworkConfig.join.kubernetesConfig.enabled
-        and: 'Data Sync Semaphore Map has the correct settings'
-            assert dataSyncSemaphoresNetworkConfig.join.autoDetectionConfig.enabled
-            assert !dataSyncSemaphoresNetworkConfig.join.kubernetesConfig.enabled
+        given: 'common hazelcast network config'
+            def hzConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config.networkConfig
+        and: 'all configs has the correct settings'
+            assert hzConfig.join.autoDetectionConfig.enabled
+            assert !hzConfig.join.kubernetesConfig.enabled
     }
 
     def 'Verify network config'() {
index e79a471..9391fa0 100644 (file)
@@ -36,13 +36,17 @@ class TrustLevelCacheConfigSpec extends Specification {
     @Autowired
     private Map<String, TrustLevel> trustLevelPerCmHandle
 
+    def cleanupSpec() {
+        Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').shutdown()
+    }
+
     def 'Hazelcast cache for trust level per dmi plugin'() {
         expect: 'system is able to create an instance of the trust level per dmi plugin cache'
             assert null != trustLevelPerDmiPlugin
         and: 'there is at least 1 instance'
             assert Hazelcast.allHazelcastInstances.size() > 0
         and: 'Dmi Plugin Trust Level Cache is present'
-            assert Hazelcast.allHazelcastInstances.name.contains('hazelcastInstanceTrustLevelPerDmiPluginMap')
+            assert Hazelcast.allHazelcastInstances.name.contains('cps-and-ncmp-hazelcast-instance-test-config')
     }
 
     def 'Hazelcast cache for trust level per cm handle'() {
@@ -51,13 +55,13 @@ class TrustLevelCacheConfigSpec extends Specification {
         and: 'there is at least 1 instance'
             assert Hazelcast.allHazelcastInstances.size() > 0
         and: 'Hazelcast cache instance for trust level is present'
-            assert Hazelcast.allHazelcastInstances.name.contains('hazelcastInstanceTrustLevelPerCmHandleMap')
+            assert Hazelcast.allHazelcastInstances.name.contains('cps-and-ncmp-hazelcast-instance-test-config')
     }
 
     def 'Trust level cache configurations: #scenario'() {
-        when: 'retrieving the cache config for trustLevel'
-            def cacheConfig = Hazelcast.getHazelcastInstanceByName(hazelcastInstanceName).config
-        then: 'the cache config has the right cluster'
+        given: 'retrieving the common cache config'
+            def cacheConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config
+        and: 'the cache config has the right cluster'
             assert cacheConfig.clusterName == 'cps-and-ncmp-test-caches'
         when: 'retrieving the map config for trustLevel'
             def mapConfig = cacheConfig.mapConfigs.get(hazelcastMapConfigName)
@@ -65,14 +69,14 @@ class TrustLevelCacheConfigSpec extends Specification {
             assert mapConfig.backupCount == 1
             assert mapConfig.asyncBackupCount == 0
         where: 'the following caches are used'
-            scenario         | hazelcastInstanceName                        | hazelcastMapConfigName
-            'cmhandle map'   | 'hazelcastInstanceTrustLevelPerCmHandleMap'  | 'trustLevelPerCmHandleCacheConfig'
-            'dmi plugin map' | 'hazelcastInstanceTrustLevelPerDmiPluginMap' | 'trustLevelPerDmiPluginCacheConfig'
+            scenario         | hazelcastMapConfigName
+            'cmhandle map'   | 'trustLevelPerCmHandleCacheConfig'
+            'dmi plugin map' | 'trustLevelPerDmiPluginCacheConfig'
     }
 
     def 'Verify deployment network configs for Distributed Caches'() {
         given: 'the Trust Level Per Dmi Plugin Cache config'
-            def trustLevelDmiPerPluginCacheConfig = Hazelcast.getHazelcastInstanceByName('hazelcastInstanceTrustLevelPerDmiPluginMap').config.networkConfig
+            def trustLevelDmiPerPluginCacheConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config.networkConfig
         expect: 'system created instance with correct config'
             assert trustLevelDmiPerPluginCacheConfig.join.autoDetectionConfig.enabled
             assert !trustLevelDmiPerPluginCacheConfig.join.kubernetesConfig.enabled
@@ -80,7 +84,7 @@ class TrustLevelCacheConfigSpec extends Specification {
 
     def 'Verify deployment network configs for Cm Handle Distributed Caches'() {
         given: 'the Trust Level Per Cm Handle Cache config'
-            def trustLevelPerCmHandlePluginCacheConfig = Hazelcast.getHazelcastInstanceByName('hazelcastInstanceTrustLevelPerCmHandleMap').config.networkConfig
+            def trustLevelPerCmHandlePluginCacheConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config.networkConfig
         expect: 'system created instance with correct config'
             assert trustLevelPerCmHandlePluginCacheConfig.join.autoDetectionConfig.enabled
             assert !trustLevelPerCmHandlePluginCacheConfig.join.kubernetesConfig.enabled
index df3375d..12db639 100644 (file)
@@ -99,6 +99,7 @@ ncmp:
 # Custom Hazelcast Config.
 hazelcast:
   cluster-name: "cps-and-ncmp-test-caches"
+  instance-config-name: "cps-and-ncmp-hazelcast-instance-test-config"
   mode:
     kubernetes:
       enabled: false
index 793acc6..f995865 100644 (file)
@@ -230,6 +230,7 @@ ncmp:
 
 hazelcast:
   cluster-name: cps-and-ncmp-test-caches
+  instance-config-name: "cps-and-ncmp-hazelcast-instance-test-config"
   mode:
     kubernetes:
       enabled: false