diff options
Diffstat (limited to 'plugins/plugins-context/context-distribution/context-distribution-infinispan')
6 files changed, 569 insertions, 0 deletions
diff --git a/plugins/plugins-context/context-distribution/context-distribution-infinispan/pom.xml b/plugins/plugins-context/context-distribution/context-distribution-infinispan/pom.xml new file mode 100644 index 000000000..7592edd91 --- /dev/null +++ b/plugins/plugins-context/context-distribution/context-distribution-infinispan/pom.xml @@ -0,0 +1,47 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2018 Ericsson. All rights reserved. + ================================================================================ + 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========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.context-distribution</groupId> + <artifactId>context-distribution</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <artifactId>context-distribution-infinispan</artifactId> + <name>${project.artifactId}</name> + <description>[${project.parent.artifactId}] Plugin for distribution using Infinispan</description> + + <dependencies> + <dependency> + <groupId>org.infinispan</groupId> + <artifactId>infinispan-core</artifactId> + <version>9.2.2.Final</version> + <exclusions> + <exclusion> + <groupId>org.jboss.slf4j</groupId> + <artifactId>slf4j-jboss-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> + +</project>
\ No newline at end of file diff --git a/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanContextDistributor.java b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanContextDistributor.java new file mode 100644 index 000000000..9361d0b4c --- /dev/null +++ b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanContextDistributor.java @@ -0,0 +1,115 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.policy.apex.plugins.context.distribution.infinispan; + +import java.util.Map; + +import org.infinispan.Cache; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.impl.distribution.AbstractDistributor; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This context distributor distributes context across threads in multiple JVMs on multiple hosts. + * It uses Infinispan to distribute maps. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class InfinispanContextDistributor extends AbstractDistributor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanContextDistributor.class); + + // The infinispan manager for distributing context for this JVM + private static InfinispanManager infinispanManager = null; + + /** + * Create an instance of an Infinispan Context Distributor. + * + * @throws ContextException On errors creating the context distributor + */ + public InfinispanContextDistributor() throws ContextException { + LOGGER.entry("InfinispanContextDistributor()"); + + LOGGER.exit("InfinispanContextDistributor()"); + } + + /* + * (non-Javadoc) + * + * @see + * com.ericsson.apex.context.impl.distribution.AbstractContextDistributor#init(com.ericsson.apex + * .model.basicmodel.concepts.AxArtifactKey) + */ + @Override + public void init(final AxArtifactKey key) throws ContextException { + LOGGER.entry("init(" + key + ")"); + + super.init(key); + + // Create the infinispan manager if it does not already exist + if (infinispanManager == null) { + // Get the parameters from the parameter service + final InfinispanDistributorParameters parameters = + ParameterService.getParameters(InfinispanDistributorParameters.class); + + LOGGER.debug("initiating Infinispan with the parameters: " + parameters); + + // Create the manager + infinispanManager = new InfinispanManager(parameters); + } + + LOGGER.exit("init(" + key + ")"); + } + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.core.context.impl.distribution.AbstractContextDistributor# + * getContextAlbumMap(com.ericsson.apex.core.model.concepts.AxArtifactKey) + */ + @Override + public Map<String, Object> getContextAlbumMap(final AxArtifactKey contextAlbumKey) { + LOGGER.info("InfinispanContextDistributor: create album: " + contextAlbumKey.getID()); + + // Get the Cache from Infinispan + final Cache<String, Object> infinispanCache = + infinispanManager.getCacheManager().getCache(contextAlbumKey.getID().replace(':', '_')); + + return infinispanCache; + } + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.core.context.impl.distribution.AbstractContextDistributor#shutdown() + */ + @Override + public void shutdown() { + // Shut down the infinispan manager + if (infinispanManager != null) { + infinispanManager.shutdown(); + } + infinispanManager = null; + } +} diff --git a/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanDistributorParameters.java b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanDistributorParameters.java new file mode 100644 index 000000000..d73955224 --- /dev/null +++ b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanDistributorParameters.java @@ -0,0 +1,144 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.policy.apex.plugins.context.distribution.infinispan; + +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * Distributor parameters for the Infinspan Distributor. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class InfinispanDistributorParameters extends DistributorParameters { + // @formatter:off + + /** The default Infinispan configuration file location. */ + public static final String DEFAULT_INFINISPAN_DISTRIBUTION_CONFIG_FILE = "/infinispan/infinispan.xml"; + + /** The default Infinispan jgroups configuration file location. */ + public static final String DEFAULT_INFINISPAN_DISTRIBUTION_JGROUPS_FILE = null; + + /** The default Infinispan IP stack is IPV4. */ + public static final boolean DEFAULT_INFINISPAN_JAVA_NET_PREFER_IPV4_STACK = true; + + /** The default Infinispan bind address is localhost. */ + public static final String DEFAULT_INFINSPAN_JGROUPS_BIND_ADDRESS = "localhost"; + + // Infinspan configuration file names + private String configFile = DEFAULT_INFINISPAN_DISTRIBUTION_CONFIG_FILE; + private String jgroupsFile = DEFAULT_INFINISPAN_DISTRIBUTION_JGROUPS_FILE; + private boolean preferIPv4Stack = DEFAULT_INFINISPAN_JAVA_NET_PREFER_IPV4_STACK; + private String jGroupsBindAddress = DEFAULT_INFINSPAN_JGROUPS_BIND_ADDRESS; + // @formatter:on + + /** + * The Constructor. + */ + public InfinispanDistributorParameters() { + super(InfinispanDistributorParameters.class.getCanonicalName()); + ParameterService.registerParameters(InfinispanDistributorParameters.class, this); + ParameterService.registerParameters(DistributorParameters.class, this); + } + + /** + * Gets the config file. + * + * @return the config file + */ + public String getConfigFile() { + return configFile; + } + + /** + * Sets the config file. + * + * @param configFile the config file + */ + public void setConfigFile(final String configFile) { + this.configFile = configFile; + } + + /** + * Gets the jgroups file. + * + * @return the jgroups file + */ + public String getJgroupsFile() { + return jgroupsFile; + } + + /** + * Sets the jgroups file. + * + * @param jgroupsFile the jgroups file + */ + public void setJgroupsFile(final String jgroupsFile) { + this.jgroupsFile = jgroupsFile; + } + + /** + * Prefer I pv 4 stack. + * + * @return true, if prefer I pv 4 stack + */ + public boolean preferIPv4Stack() { + return preferIPv4Stack; + } + + /** + * Sets the prefer I pv 4 stack. + * + * @param preferIPv4Stack the prefer I pv 4 stack + */ + public void setPreferIPv4Stack(final boolean preferIPv4Stack) { + this.preferIPv4Stack = preferIPv4Stack; + } + + /** + * Getj groups bind address. + * + * @return the j groups bind address + */ + public String getjGroupsBindAddress() { + return jGroupsBindAddress; + } + + /** + * Setj groups bind address. + * + * @param jGroupsBindAddress the j groups bind address + */ + public void setjGroupsBindAddress(final String jGroupsBindAddress) { + this.jGroupsBindAddress = jGroupsBindAddress; + } + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.context.parameters.DistributorParameters#toString() + */ + @Override + public String toString() { + return "InfinispanDistributorParameters [configFile=" + configFile + ", jgroupsFile=" + jgroupsFile + + ", preferIPv4Stack=" + preferIPv4Stack + ", jGroupsBindAddress=" + jGroupsBindAddress + "]"; + } +} diff --git a/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java new file mode 100644 index 000000000..803d99adc --- /dev/null +++ b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java @@ -0,0 +1,196 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.policy.apex.plugins.context.distribution.infinispan; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.manager.EmbeddedCacheManager; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.model.utilities.ResourceUtils; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class InfinispanManager holds the Infinispan cache manager for a JVM. + */ +public class InfinispanManager { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanManager.class); + + // The Infinispan Cache Manager + private EmbeddedCacheManager cacheManager; + + /** + * Constructor, set up an Infinispan cache manager. + * + * @param infinispanDistributorParameters the infinispan distributor parameters + * @throws ContextException On errors connecting to Infinispan + */ + public InfinispanManager(final InfinispanDistributorParameters infinispanDistributorParameters) + throws ContextException { + LOGGER.entry("Creating Infinispan Manager: " + infinispanDistributorParameters); + + setSystemProperties(infinispanDistributorParameters); + + // First, try and open a local input stream for Infinispan configuration + InputStream infinispanConfigStream = + getLocalInfinispanConfigurationStream(infinispanDistributorParameters.getConfigFile()); + + // Check if a local file was found, if not then go to the class path + if (infinispanConfigStream == null) { + // If a local file is not specified, then check for an infinispan configuration file on + // the class path + infinispanConfigStream = + getClasspathInfinispanConfigurationStream(infinispanDistributorParameters.getConfigFile()); + } + + // Check if we found configuration for Infinispan + if (infinispanConfigStream == null) { + final String errorMessage = + "failed to start infinispan cache manager, no infinispan configuration found on local file system or in classpath, " + + "try setting Infinspan \"configFile\" parameter"; + LOGGER.error(errorMessage); + throw new ContextException(errorMessage); + } + + try { + LOGGER.debug("starting infinispan cache manager using specified configuration . . ."); + cacheManager = new DefaultCacheManager(infinispanConfigStream); + LOGGER.debug("started infinispan cache manager using specified configuration"); + } catch (final Exception e) { + LOGGER.error("failed to start infinispan cache manager using specified configuration", e); + throw new ContextException("failed to start infinispan cache manager using specified configuration", e); + } + + // Start the cache manager + cacheManager.start(); + + Runtime.getRuntime().addShutdownHook(new InfinspanManagerShutdownHook()); + + LOGGER.exit("Created Infinispan Manager: " + infinispanDistributorParameters); + } + + /** + * Shutdown the manager. + */ + public void shutdown() { + if (cacheManager == null) { + return; + } + + cacheManager.stop(); + cacheManager = null; + } + + /** + * Get the cache manager. + * + * @return the infinispan cache manager + */ + public EmbeddedCacheManager getCacheManager() { + return cacheManager; + } + + /** + * Set system properties used by Infinispan. + * + * @param infinispanDistributorParameters The parameter values to set are passed as properties + */ + private void setSystemProperties(final InfinispanDistributorParameters infinispanDistributorParameters) { + System.setProperty("java.net.preferIPv4Stack", + Boolean.toString(infinispanDistributorParameters.preferIPv4Stack())); + System.setProperty("jgroups.bind_addr", infinispanDistributorParameters.getjGroupsBindAddress()); + } + + /** + * Get an Infinispan configuration stream from the local file system. + * + * @param infinispanConfigFileName The file name to open + * @return The file opened as a stream + * @throws ContextException If the local file could not be found or is invalid + */ + private InputStream getLocalInfinispanConfigurationStream(final String infinispanConfigFileName) + throws ContextException { + LOGGER.debug("checking infinispan configuration file exists at \"" + infinispanConfigFileName + "\". . ."); + + // Check if the file exists + final File infinispanConfigFile = new File(infinispanConfigFileName); + if (!infinispanConfigFile.exists()) { + return null; + } + + // Check the file + if (!infinispanConfigFile.isFile() || !infinispanConfigFile.canRead()) { + LOGGER.error("infinispan configuration file at \"" + infinispanConfigFileName + + "\" does not exist or is invalid"); + throw new ContextException("infinispan configuration file at \"" + infinispanConfigFileName + + "\" does not exist or is invalid"); + } + + try { + final InputStream infinispanConfigStream = new FileInputStream(infinispanConfigFile); + LOGGER.debug("infinispan configuration file exists at \"" + infinispanConfigFileName + "\""); + return infinispanConfigStream; + } catch (final Exception e) { + LOGGER.error("infinispan configuration file at \"" + infinispanConfigFileName + + "\" does not exist or is invalid", e); + throw new ContextException("infinispan configuration file at \"" + infinispanConfigFileName + + "\" does not exist or is invalid", e); + } + } + + /** + * Get an Infinispan configuration stream from the class path. + * + * @param apexInfinispanConfigFile the apex infinispan config file + * @return The file opened as a stream + */ + private InputStream getClasspathInfinispanConfigurationStream(final String apexInfinispanConfigFile) { + LOGGER.debug( + "checking infinispan configuration file exists at resource \"" + apexInfinispanConfigFile + "\". . ."); + final InputStream infinispanConfigStream = ResourceUtils.getResourceAsStream(apexInfinispanConfigFile); + + if (infinispanConfigStream != null) { + LOGGER.debug("infinispan configuration file exists at resource \"" + apexInfinispanConfigFile + "\""); + } else { + LOGGER.debug("infinispan configuration file at resource \"" + apexInfinispanConfigFile + "\" not found"); + } + return infinispanConfigStream; + } + + /** + * Private class to implement the shutdown hook for this infinispan manager. + */ + public class InfinspanManagerShutdownHook extends Thread { + /* + * (non-Javadoc) + * + * @see java.lang.Thread#run() + */ + @Override + public void run() { + shutdown(); + } + } +} diff --git a/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/package-info.java b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/package-info.java new file mode 100644 index 000000000..b9ceeb668 --- /dev/null +++ b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/package-info.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +/** + * Implements the <a href="http://infinispan.org/">Infinispan</a> context distributor for + * distribution of APEX context across JVMs. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +package org.onap.policy.apex.plugins.context.distribution.infinispan; diff --git a/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/resources/infinispan/infinispan.xml.sample b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/resources/infinispan/infinispan.xml.sample new file mode 100644 index 000000000..302eac935 --- /dev/null +++ b/plugins/plugins-context/context-distribution/context-distribution-infinispan/src/main/resources/infinispan/infinispan.xml.sample @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2016-2018 Ericsson. All rights reserved. + ================================================================================ + 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========================================================= +--> + +<infinispan xmlns="urn:infinispan:config:8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:infinispan:config:8.0 http://infinispan.org/schemas/infinispan-config-8.0.xsd"> + <jgroups> + <stack-file name="tcpStack" path="default-configs/default-jgroups-tcp.xml" /> + </jgroups> + + <cache-container name="ApexCacheContainer" default-cache="TestContext_0.0.1"> + <transport cluster="apexCluster" stack="tcpStack" /> + <jmx /> + <replicated-cache name="LargeContextMap_0.0.1" mode="SYNC" statistics="true"> + <state-transfer enabled="true" /> + </replicated-cache> + <replicated-cache name="LongSameTypeContextMap_0.0.1" mode="SYNC" statistics="true"> + <state-transfer enabled="true" /> + </replicated-cache> + <replicated-cache name="TestContext_0.0.1" mode="SYNC"> + <state-transfer enabled="true" /> + </replicated-cache> + </cache-container> +</infinispan> |