Extend capability of distributed cache 93/134293/5
authormpriyank <priyank.maheshwari@est.tech>
Mon, 24 Apr 2023 11:51:24 +0000 (12:51 +0100)
committermpriyank <priyank.maheshwari@est.tech>
Mon, 24 Apr 2023 16:58:10 +0000 (17:58 +0100)
- Extend cache configs to be able to work in stanalone mode as well as
  in cluster mode in kubernetes.
- Expose the parameters to enable or disable the feature.
- to make it work in standalone mode autodiscovery config will take care , and
  to run it on kubernetes enable the kubernetes option and provide the
  service name property.

Issue-ID: CPS-1637
Change-Id: I704c4aa11e65b17b5be80048e4246079014d8bb7
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/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy
cps-ncmp-service/src/test/resources/application.yml
cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java
cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy
cps-service/src/test/resources/application.yml

index 0007bc8..6d45835 100644 (file)
@@ -187,4 +187,11 @@ ncmp:
             parallelism-level: 10\r
 \r
     model-loader:\r
-        subscription: false
\ No newline at end of file
+        subscription: false\r
+\r
+# Custom Hazelcast Config.\r
+hazelcast:\r
+  mode:\r
+    kubernetes:\r
+      enabled: ${HAZELCAST_MODE_KUBERNETES_ENABLED:false}\r
+      service-name: ${CPS_NCMP_SERVICE_NAME:"cps-and-ncmp-service"}
\ No newline at end of file
index ac2bd45..0b67266 100644 (file)
@@ -28,19 +28,28 @@ import com.hazelcast.core.Hazelcast;
 import com.hazelcast.core.HazelcastInstance;
 import com.hazelcast.map.IMap;
 import java.util.concurrent.BlockingQueue;
+import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.spi.model.DataNode;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 /**
  * Core infrastructure of the hazelcast distributed caches for Module Sync and Data Sync use cases.
  */
+@Slf4j
 @Configuration
 public class SynchronizationCacheConfig {
 
     public static final int MODULE_SYNC_STARTED_TTL_SECS = 600;
     public static final int DATA_SYNC_SEMAPHORE_TTL_SECS = 1800;
 
+    @Value("${hazelcast.mode.kubernetes.enabled}")
+    private boolean cacheKubernetesEnabled;
+
+    @Value("${hazelcast.mode.kubernetes.service-name}")
+    private String cacheKubernetesServiceName;
+
     private static final QueueConfig commonQueueConfig = createQueueConfig();
     private static final MapConfig moduleSyncStartedConfig = createMapConfig("moduleSyncStartedConfig");
     private static final MapConfig dataSyncSemaphoresConfig = createMapConfig("dataSyncSemaphoresConfig");
@@ -92,6 +101,7 @@ public class SynchronizationCacheConfig {
             config.addQueueConfig((QueueConfig) namedConfig);
         }
         config.setClusterName("synchronization-caches");
+        updateDiscoveryMode(config);
         return config;
     }
 
@@ -109,4 +119,12 @@ public class SynchronizationCacheConfig {
         return mapConfig;
     }
 
+    private void updateDiscoveryMode(final Config config) {
+        if (cacheKubernetesEnabled) {
+            log.info("Enabling kubernetes mode with service-name : {}", cacheKubernetesServiceName);
+            config.getNetworkConfig().getJoin().getKubernetesConfig().setEnabled(true)
+                    .setProperty("service-name", cacheKubernetesServiceName);
+        }
+    }
+
 }
index 868ee7a..6829d83 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START========================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2023 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
 
 package org.onap.cps.ncmp.api.impl.config.embeddedcache
 
+import com.hazelcast.config.Config
 import com.hazelcast.core.Hazelcast
 import com.hazelcast.map.IMap
 import org.onap.cps.spi.model.DataNode
@@ -74,6 +75,40 @@ class SynchronizationCacheConfigSpec extends Specification {
             assert dataSyncSemaphoresConfig.asyncBackupCount == 3
     }
 
+    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
+
+    }
+
+    def 'Verify network config'() {
+        given: 'Synchronization config object and test configuration'
+            def objectUnderTest = new SynchronizationCacheConfig()
+            def testConfig = new Config()
+        when: 'kubernetes properties are enabled'
+            objectUnderTest.cacheKubernetesEnabled = true
+            objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
+        and: 'method called to update the discovery mode'
+            objectUnderTest.updateDiscoveryMode(testConfig)
+        then: 'applied properties are reflected'
+            assert testConfig.networkConfig.join.kubernetesConfig.enabled
+            assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
+
+    }
+
     def 'Time to Live Verify for Module Sync Semaphore'() {
         when: 'the key is inserted with a TTL of 1 second (Hazelcast TTL resolution is seconds!)'
             moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1, TimeUnit.SECONDS)
index e66f23d..679248b 100644 (file)
@@ -35,4 +35,11 @@ ncmp:
             parallelism-level: 3
 
     model-loader:
-        subscription: true
\ No newline at end of file
+        subscription: true
+
+# Custom Hazelcast Config.
+hazelcast:
+  mode:
+    kubernetes:
+      enabled: false
+      service-name: "cps-and-ncmp-service"
\ No newline at end of file
index 54d6ff3..3acba0b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START========================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2023 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -26,15 +26,24 @@ import com.hazelcast.config.NamedConfig;
 import com.hazelcast.core.Hazelcast;
 import com.hazelcast.core.HazelcastInstance;
 import com.hazelcast.map.IMap;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 /**
  * Core infrastructure of the hazelcast distributed cache for anchor data config use cases.
  */
+@Slf4j
 @Configuration
 public class AnchorDataCacheConfig {
 
+    @Value("${hazelcast.mode.kubernetes.enabled}")
+    private boolean cacheKubernetesEnabled;
+
+    @Value("${hazelcast.mode.kubernetes.service-name}")
+    private String cacheKubernetesServiceName;
+
     private static final MapConfig anchorDataCacheMapConfig = createMapConfig("anchorDataCacheMapConfig");
 
     /**
@@ -57,6 +66,7 @@ public class AnchorDataCacheConfig {
         final Config config = new Config(instanceName);
         config.addMapConfig((MapConfig) namedConfig);
         config.setClusterName("cps-service-caches");
+        updateDiscoveryMode(config);
         return config;
     }
 
@@ -67,4 +77,12 @@ public class AnchorDataCacheConfig {
         return mapConfig;
     }
 
+    private void updateDiscoveryMode(final Config config) {
+        if (cacheKubernetesEnabled) {
+            log.info("Enabling kubernetes mode with service-name : {}", cacheKubernetesServiceName);
+            config.getNetworkConfig().getJoin().getKubernetesConfig().setEnabled(true)
+                    .setProperty("service-name", cacheKubernetesServiceName);
+        }
+    }
+
 }
index 839444b..76b5345 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2023 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
  */
 
 package org.onap.cps.cache
+
+import com.hazelcast.config.Config
 import com.hazelcast.core.Hazelcast
 import com.hazelcast.map.IMap
 import org.springframework.beans.factory.annotation.Autowired
@@ -49,4 +51,28 @@ class AnchorDataCacheConfigSpec extends Specification {
             assert anchorDataCacheConfig.backupCount == 3
             assert anchorDataCacheConfig.asyncBackupCount == 3
     }
+
+    def 'Verify deployment network configs for Distributed Caches'() {
+        given: 'the Anchor Data Cache config'
+            def anchorDataCacheNetworkConfig = Hazelcast.getHazelcastInstanceByName('hazelCastInstanceCpsCore').config.networkConfig
+        expect: 'system created instance with correct config'
+            assert anchorDataCacheNetworkConfig.join.autoDetectionConfig.enabled
+            assert !anchorDataCacheNetworkConfig.join.kubernetesConfig.enabled
+    }
+
+    def 'Verify network config'() {
+        given: 'Synchronization config object and test configuration'
+            def objectUnderTest = new AnchorDataCacheConfig()
+            def testConfig = new Config()
+        when: 'kubernetes properties are enabled'
+            objectUnderTest.cacheKubernetesEnabled = true
+            objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
+        and: 'method called to update the discovery mode'
+            objectUnderTest.updateDiscoveryMode(testConfig)
+        then: 'applied properties are reflected'
+            assert testConfig.networkConfig.join.kubernetesConfig.enabled
+            assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
+
+    }
+
 }
index 04295eb..21f3745 100644 (file)
@@ -48,3 +48,10 @@ spring:
 logging:
   level:
     org.apache.kafka: ERROR
+
+# Custom Hazelcast Config.
+hazelcast:
+  mode:
+    kubernetes:
+      enabled: false
+      service-name: "cps-and-ncmp-service"