From d64fa3440d5ed07aa3f35573cfaec5825f002811 Mon Sep 17 00:00:00 2001 From: mpriyank Date: Mon, 24 Apr 2023 12:51:24 +0100 Subject: [PATCH] Extend capability of distributed cache - 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 --- cps-application/src/main/resources/application.yml | 9 +++++- .../embeddedcache/SynchronizationCacheConfig.java | 18 +++++++++++ .../SynchronizationCacheConfigSpec.groovy | 37 +++++++++++++++++++++- .../src/test/resources/application.yml | 9 +++++- .../org/onap/cps/cache/AnchorDataCacheConfig.java | 20 +++++++++++- .../cps/cache/AnchorDataCacheConfigSpec.groovy | 28 +++++++++++++++- cps-service/src/test/resources/application.yml | 7 ++++ 7 files changed, 123 insertions(+), 5 deletions(-) diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml index 0007bc824..6d4583566 100644 --- a/cps-application/src/main/resources/application.yml +++ b/cps-application/src/main/resources/application.yml @@ -187,4 +187,11 @@ ncmp: parallelism-level: 10 model-loader: - subscription: false \ No newline at end of file + subscription: false + +# Custom Hazelcast Config. +hazelcast: + mode: + kubernetes: + enabled: ${HAZELCAST_MODE_KUBERNETES_ENABLED:false} + service-name: ${CPS_NCMP_SERVICE_NAME:"cps-and-ncmp-service"} \ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java index ac2bd4596..0b6726637 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java @@ -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); + } + } + } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy index 868ee7a70..6829d834d 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy @@ -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) diff --git a/cps-ncmp-service/src/test/resources/application.yml b/cps-ncmp-service/src/test/resources/application.yml index e66f23d23..679248ba8 100644 --- a/cps-ncmp-service/src/test/resources/application.yml +++ b/cps-ncmp-service/src/test/resources/application.yml @@ -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 diff --git a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java index 54d6ff395..3acba0bb3 100644 --- a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java +++ b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java @@ -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); + } + } + } diff --git a/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy index 839444b68..76b534553 100644 --- a/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy @@ -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' + + } + } diff --git a/cps-service/src/test/resources/application.yml b/cps-service/src/test/resources/application.yml index 04295eb74..21f374533 100644 --- a/cps-service/src/test/resources/application.yml +++ b/cps-service/src/test/resources/application.yml @@ -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" -- 2.16.6