From 39e4eef51e44b73569fe82e214afab04edc5bba0 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Tue, 17 Sep 2024 17:48:22 +0100 Subject: Remove Hazelcast cache for prefix resolver (CPS-2417 #2) This patch removes the AnchorDataCache from CPS, which is used for prefix resolution in get/query operations. As such, Hazelcast is no longer a dependency of CPS, only NCMP. - Changed PrefixResolver to be more efficient. - Removed AnchorDataCache and associated classes. - Moved HazelcastCacheConfig to NCMP. - Removed Hazelcast dependency from cps-service/pom.xml This shows good performance improvements in some APIs such as v2 GET which is nearly 2x faster (also 5x faster including base patch). Issue-ID: CPS-2417 Signed-off-by: danielhanrahan Change-Id: I24768469f24e90b70f7a6187faa4f5b3d75777d2 --- .../org/onap/cps/cache/AnchorDataCacheConfig.java | 45 --------- .../org/onap/cps/cache/AnchorDataCacheEntry.java | 44 --------- .../org/onap/cps/cache/HazelcastCacheConfig.java | 104 --------------------- .../java/org/onap/cps/utils/PrefixResolver.java | 79 ++++------------ 4 files changed, 16 insertions(+), 256 deletions(-) delete mode 100644 cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java delete mode 100644 cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java delete mode 100644 cps-service/src/main/java/org/onap/cps/cache/HazelcastCacheConfig.java (limited to 'cps-service/src/main/java/org/onap') 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 deleted file mode 100644 index efe19c6cb7..0000000000 --- a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ============LICENSE_START======================================================== - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.cache; - -import com.hazelcast.config.MapConfig; -import com.hazelcast.map.IMap; -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. - */ -@Configuration -public class AnchorDataCacheConfig extends HazelcastCacheConfig { - - private static final MapConfig anchorDataCacheMapConfig = createMapConfig("anchorDataCacheMapConfig"); - - /** - * Distributed instance of anchor data cache that contains module prefix by anchor name as properties. - * - * @return configured map of anchor data cache - */ - @Bean - public IMap anchorDataCache() { - return createHazelcastInstance("hazelCastInstanceCpsCore", anchorDataCacheMapConfig).getMap("anchorDataCache"); - } -} diff --git a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java deleted file mode 100644 index 41adbdd5dc..0000000000 --- a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * ============LICENSE_START======================================================== - * Copyright (C) 2022 Nordix Foundation - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.cache; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -public class AnchorDataCacheEntry implements Serializable { - - private static final long serialVersionUID = 2111243947810370698L; - - private Map properties = new HashMap<>(); - - public Object getProperty(final String propertyName) { - return properties.get(propertyName); - } - - public void setProperty(final String propertyName, final Serializable value) { - properties.put(propertyName, value); - } - - public boolean hasProperty(final String propertyName) { - return properties.containsKey(propertyName); - } -} diff --git a/cps-service/src/main/java/org/onap/cps/cache/HazelcastCacheConfig.java b/cps-service/src/main/java/org/onap/cps/cache/HazelcastCacheConfig.java deleted file mode 100644 index 418de9b17a..0000000000 --- a/cps-service/src/main/java/org/onap/cps/cache/HazelcastCacheConfig.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * ============LICENSE_START======================================================== - * Copyright (C) 2023-2024 Nordix Foundation - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.cache; - -import com.hazelcast.config.Config; -import com.hazelcast.config.MapConfig; -import com.hazelcast.config.NamedConfig; -import com.hazelcast.config.QueueConfig; -import com.hazelcast.config.RestEndpointGroup; -import com.hazelcast.config.SetConfig; -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; - -/** - * Core infrastructure of the hazelcast distributed cache. - */ -@Slf4j -public class HazelcastCacheConfig { - - @Value("${hazelcast.cluster-name}") - protected String clusterName; - - @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)); - } - - private Config initializeConfig(final String instanceName, final NamedConfig namedConfig) { - final Config config = new Config(instanceName); - if (namedConfig instanceof MapConfig) { - config.addMapConfig((MapConfig) namedConfig); - } - if (namedConfig instanceof QueueConfig) { - config.addQueueConfig((QueueConfig) namedConfig); - } - 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); - return config; - } - - protected static MapConfig createMapConfig(final String configName) { - final MapConfig mapConfig = new MapConfig(configName); - mapConfig.setBackupCount(1); - return mapConfig; - } - - protected static QueueConfig createQueueConfig(final String configName) { - final QueueConfig commonQueueConfig = new QueueConfig(configName); - commonQueueConfig.setBackupCount(1); - return commonQueueConfig; - } - - protected static SetConfig createSetConfig(final String configName) { - final SetConfig commonSetConfig = new SetConfig(configName); - commonSetConfig.setBackupCount(1); - return commonSetConfig; - } - - protected 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); - } - } - - protected void exposeClusterInformation(final Config config) { - config.getNetworkConfig().getRestApiConfig().setEnabled(true) - .enableGroups(RestEndpointGroup.HEALTH_CHECK, RestEndpointGroup.CLUSTER_READ); - } - -} diff --git a/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java b/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java index 93fb72864f..c3097cc552 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java +++ b/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation. + * Copyright (C) 2022-2024 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,40 +20,25 @@ package org.onap.cps.utils; -import com.hazelcast.map.IMap; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; import lombok.RequiredArgsConstructor; -import org.onap.cps.api.CpsAnchorService; import org.onap.cps.api.impl.YangTextSchemaSourceSetCache; -import org.onap.cps.cache.AnchorDataCacheEntry; import org.onap.cps.cpspath.parser.CpsPathPrefixType; import org.onap.cps.cpspath.parser.CpsPathQuery; import org.onap.cps.cpspath.parser.CpsPathUtil; import org.onap.cps.spi.model.Anchor; import org.onap.cps.yang.YangTextSchemaSourceSet; -import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.springframework.stereotype.Component; @Component @RequiredArgsConstructor public class PrefixResolver { - private static final long ANCHOR_DATA_CACHE_TTL_SECS = TimeUnit.HOURS.toSeconds(1); - - private static final String CACHE_ENTRY_PROPERTY_NAME = "prefixPerContainerName"; - - private final CpsAnchorService cpsAnchorService; - private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache; - private final IMap anchorDataCache; - /** * Get the module prefix for the given xpath under the given anchor. * @@ -62,57 +47,25 @@ public class PrefixResolver { * @return the prefix of the module the top level element of given xpath */ public String getPrefix(final Anchor anchor, final String xpath) { - final Map prefixPerContainerName = getPrefixPerContainerName(anchor); - return getPrefixForTopContainer(prefixPerContainerName, xpath); - } - - private Map getPrefixPerContainerName(final Anchor anchor) { - if (anchorDataCache.containsKey(anchor.getName())) { - final AnchorDataCacheEntry anchorDataCacheEntry = anchorDataCache.get(anchor.getName()); - if (anchorDataCacheEntry.hasProperty(CACHE_ENTRY_PROPERTY_NAME)) { - return (Map) anchorDataCacheEntry.getProperty(CACHE_ENTRY_PROPERTY_NAME); - } - } - return createAndCachePrefixPerContainerNameMap(anchor); - } - - private String getPrefixForTopContainer(final Map prefixPerContainerName, - final String xpath) { final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(xpath); - if (cpsPathQuery.getCpsPathPrefixType() == CpsPathPrefixType.ABSOLUTE) { - final String topLevelContainerName = cpsPathQuery.getContainerNames().get(0); - if (prefixPerContainerName.containsKey(topLevelContainerName)) { - return prefixPerContainerName.get(topLevelContainerName); - } + if (cpsPathQuery.getCpsPathPrefixType() != CpsPathPrefixType.ABSOLUTE) { + return ""; } - return ""; - } + final String topLevelContainerName = cpsPathQuery.getContainerNames().get(0); - private Map createAndCachePrefixPerContainerNameMap(final Anchor anchor) { final YangTextSchemaSourceSet yangTextSchemaSourceSet = - yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(), anchor.getSchemaSetName()); + yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(), anchor.getSchemaSetName()); final SchemaContext schemaContext = yangTextSchemaSourceSet.getSchemaContext(); - final Map prefixPerQNameModule = new HashMap<>(schemaContext.getModules().size()); - for (final Module module : schemaContext.getModules()) { - prefixPerQNameModule.put(module.getQNameModule(), module.getPrefix()); - } - final HashMap prefixPerContainerName = new HashMap<>(); - for (final DataSchemaNode dataSchemaNode : schemaContext.getChildNodes()) { - if (dataSchemaNode instanceof DataNodeContainer) { - final String containerName = dataSchemaNode.getQName().getLocalName(); - final String prefix = prefixPerQNameModule.get(dataSchemaNode.getQName().getModule()); - prefixPerContainerName.put(containerName, prefix); - } - } - cachePrefixPerContainerNameMap(anchor.getName(), prefixPerContainerName); - return prefixPerContainerName; - } - private void cachePrefixPerContainerNameMap(final String anchorName, - final Serializable prefixPerContainerName) { - final AnchorDataCacheEntry anchorDataCacheEntry = new AnchorDataCacheEntry(); - anchorDataCacheEntry.setProperty(CACHE_ENTRY_PROPERTY_NAME, prefixPerContainerName); - anchorDataCache.put(anchorName, anchorDataCacheEntry, ANCHOR_DATA_CACHE_TTL_SECS, TimeUnit.SECONDS); + return schemaContext.getChildNodes().stream() + .filter(DataNodeContainer.class::isInstance) + .map(SchemaNode::getQName) + .filter(qname -> qname.getLocalName().equals(topLevelContainerName)) + .findFirst() + .map(QName::getModule) + .flatMap(schemaContext::findModule) + .map(Module::getPrefix) + .orElse(""); } } -- cgit 1.2.3-korg