e2c2de97a3acfc65dc8ddc573c6eec2db1aa6978
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.test.locking;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.InetAddress;
29 import java.net.InetSocketAddress;
30 import java.net.NetworkInterface;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.TreeSet;
34
35 import org.apache.zookeeper.server.NIOServerCnxnFactory;
36 import org.apache.zookeeper.server.ZooKeeperServer;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.TemporaryFolder;
42 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
43 import org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager;
44 import org.onap.policy.apex.context.parameters.ContextParameters;
45 import org.onap.policy.apex.context.parameters.DistributorParameters;
46 import org.onap.policy.apex.context.parameters.LockManagerParameters;
47 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
48 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
50 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
51 import org.onap.policy.apex.model.utilities.ResourceUtils;
52 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
53 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
54 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
55 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
56 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
57 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
58 import org.slf4j.ext.XLogger;
59 import org.slf4j.ext.XLoggerFactory;
60
61 import com.hazelcast.config.Config;
62
63 /**
64  * The Class TestConcurrentContext tests concurrent use of context.
65  *
66  * @author Liam Fallon (liam.fallon@ericsson.com)
67  */
68 public class TestConcurrentContext {
69     private static final String HAZELCAST_CONFIG = "hazelcast.config";
70    
71     private static final String JAVA_NET_PREFER_IPV4_STACK = "java.net.preferIPv4Stack";
72     private static final String HAZELCAST_XML_FILE = "src/test/resources/hazelcast/hazelcast.xml";
73
74     // Logger for this class
75     private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class);
76
77     // Test parameters
78     private static final String ZOOKEEPER_ADDRESS = "127.0.0.1";
79     private static final int ZOOKEEPER_START_PORT = 62181;
80     private static final int TEST_JVM_COUNT_SINGLE_JVM = 1;
81     private static final int TEST_JVM_COUNT_MULTI_JVM = 3;
82     private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64;
83     private static final int TEST_THREAD_COUNT_MULTI_JVM = 20;
84     private static final int TEST_THREAD_LOOPS = 100;
85
86     private NIOServerCnxnFactory zookeeperFactory;
87
88     // We need to increment the Zookeeper port because sometimes the port is not released at the end
89     // of the test for a few seconds.
90     private static int nextZookeeperPort = ZOOKEEPER_START_PORT;
91     private int zookeeperPort;
92
93     @Rule
94     public final TemporaryFolder folder = new TemporaryFolder();
95
96     @BeforeClass
97     public static void configure() throws Exception {
98         System.setProperty(JAVA_NET_PREFER_IPV4_STACK, "true");
99         final String hazelCastfileLocation = ResourceUtils.getFilePath4Resource(HAZELCAST_XML_FILE);
100         System.setProperty(HAZELCAST_CONFIG, hazelCastfileLocation);
101
102         // The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
103         // work. IN order to ensure that all
104         // the JVMs in a test pick up the same IP address, this function sets the address to be the
105         // first non-loopback IPv4 address
106         // on a host
107         final TreeSet<String> ipAddressSet = new TreeSet<String>();
108
109         final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
110         for (final NetworkInterface netint : Collections.list(nets)) {
111             final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
112             for (final InetAddress inetAddress : Collections.list(inetAddresses)) {
113                 // Look for real IPv4 internet addresses
114                 if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
115                     ipAddressSet.add(inetAddress.getHostAddress());
116                 }
117             }
118         }
119
120         if (ipAddressSet.size() == 0) {
121             throw new Exception("cound not find real IP address for test");
122         }
123         System.out.println("For Infinispan, setting jgroups.tcp.address to: " + ipAddressSet.first());
124         System.setProperty("jgroups.tcp.address", ipAddressSet.first());
125
126         final Config config = new Config();
127         config.getNetworkConfig().setPublicAddress(ipAddressSet.first());
128         config.getNetworkConfig().getInterfaces().addInterface(ipAddressSet.first());
129     }
130
131     @AfterClass
132     public static void teardown() throws IOException {}
133
134     private void startZookeeperServer() throws IOException, InterruptedException {
135         final File zookeeperDirectory = folder.newFolder("zookeeperDirectory");
136
137         zookeeperPort = nextZookeeperPort++;
138
139         final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000);
140         zookeeperFactory = new NIOServerCnxnFactory();
141         zookeeperFactory.configure(new InetSocketAddress(zookeeperPort), 100);
142
143         zookeeperFactory.startup(server);
144     }
145
146     private void stopZookeeperServer() {
147         zookeeperFactory.shutdown();
148     }
149
150     @Test
151     public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException {
152         logger.debug("Running testConcurrentContextJVMLocalVarSet test . . .");
153
154         final ContextParameters contextParameters = new ContextParameters();
155         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
156         final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM,
157                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
158
159         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
160
161         logger.debug("Ran testConcurrentContextJVMLocalVarSet test");
162     }
163
164     @Test
165     public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException {
166         logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . .");
167
168         new ContextParameters();
169         final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM,
170                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
171
172         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
173
174         logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test");
175     }
176
177     @Test
178     public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException {
179         logger.debug("Running testConcurrentContextMultiJVMNoLock test . . .");
180
181         final ContextParameters contextParameters = new ContextParameters();
182         contextParameters.getDistributorParameters().setPluginClass(JVMLocalDistributor.class.getCanonicalName());
183         contextParameters.getLockManagerParameters().setPluginClass(JVMLocalLockManager.class.getCanonicalName());
184
185         final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock",
186                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
187
188         // No concurrent map so result will be zero
189         assertEquals(0, result);
190
191         logger.debug("Ran testConcurrentContextMultiJVMNoLock test");
192     }
193
194     @Test
195     public void testConcurrentContextHazelcastLock() throws ApexModelException, IOException, ApexException {
196         logger.debug("Running testConcurrentContextHazelcastLock test . . .");
197
198         final ContextParameters contextParameters = new ContextParameters();
199         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
200         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
201         final long result = new ConcurrentContext().testConcurrentContext("HazelcastLock", TEST_JVM_COUNT_SINGLE_JVM,
202                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
203
204         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
205         logger.debug("Ran testConcurrentContextHazelcastLock test");
206     }
207
208     @Test
209     public void testConcurrentContextCuratorLock()
210             throws ApexModelException, IOException, ApexException, InterruptedException {
211         logger.debug("Running testConcurrentContextCuratorLock test . . .");
212
213         startZookeeperServer();
214
215         final ContextParameters contextParameters = new ContextParameters();
216         contextParameters.getDistributorParameters()
217                 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
218
219         final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
220         curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
221         curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
222         contextParameters.setLockManagerParameters(curatorParameters);
223         ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
224
225         final long result = new ConcurrentContext().testConcurrentContext("CuratorLock", TEST_JVM_COUNT_SINGLE_JVM,
226                 TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS);
227
228         assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result);
229
230         stopZookeeperServer();
231         logger.debug("Ran testConcurrentContextCuratorLock test");
232     }
233
234     @Test
235     public void testConcurrentContextHazelcastMultiJVMHazelcastLock()
236             throws ApexModelException, IOException, ApexException {
237         logger.debug("Running testConcurrentContextHazelcastMultiJVMHazelcastLock test . . .");
238
239         final ContextParameters contextParameters = new ContextParameters();
240         contextParameters.getDistributorParameters()
241                 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
242         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
243         final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiHazelcastlock",
244                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
245
246         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
247         logger.debug("Ran testConcurrentContextHazelcastMultiJVMHazelcastLock test");
248     }
249
250     @Test
251     public void testConcurrentContextInfinispanMultiJVMHazelcastlock()
252             throws ApexModelException, IOException, ApexException {
253         logger.debug("Running testConcurrentContextInfinispanMultiJVMHazelcastlock test . . .");
254
255         final ContextParameters contextParameters = new ContextParameters();
256         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
257         infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
258         infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
259         contextParameters.setDistributorParameters(infinispanParameters);
260         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
261
262         final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiHazelcastlock",
263                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
264
265         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
266         logger.debug("Ran testConcurrentContextInfinispanMultiJVMHazelcastlock test");
267     }
268
269     @Test
270     public void testConcurrentContextInfinispanMultiJVMCuratorLock()
271             throws ApexModelException, IOException, ApexException, InterruptedException {
272         logger.debug("Running testConcurrentContextInfinispanMultiJVMCuratorLock test . . .");
273
274         startZookeeperServer();
275
276         final ContextParameters contextParameters = new ContextParameters();
277         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
278         infinispanParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
279         infinispanParameters.setConfigFile("infinispan/infinispan-context-test.xml");
280         contextParameters.setDistributorParameters(infinispanParameters);
281
282         final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
283         curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
284         curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
285         contextParameters.setLockManagerParameters(curatorParameters);
286         ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
287
288         final long result = new ConcurrentContext().testConcurrentContext("InfinispanMultiCuratorLock",
289                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
290
291         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
292
293         stopZookeeperServer();
294
295         logger.debug("Ran testConcurrentContextInfinispanMultiJVMCuratorLock test");
296     }
297
298     @Test
299     public void testConcurrentContextHazelcastMultiJVMCuratorLock()
300             throws ApexModelException, IOException, ApexException, InterruptedException {
301         logger.debug("Running testConcurrentContextHazelcastMultiJVMCuratorLock test . . .");
302
303         startZookeeperServer();
304
305         final ContextParameters contextParameters = new ContextParameters();
306         contextParameters.getDistributorParameters()
307                 .setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
308
309         final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
310         curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
311         curatorParameters.setZookeeperAddress(ZOOKEEPER_ADDRESS + ":" + zookeeperPort);
312         contextParameters.setLockManagerParameters(curatorParameters);
313         ParameterService.registerParameters(LockManagerParameters.class, curatorParameters);
314
315         final long result = new ConcurrentContext().testConcurrentContext("HazelcastMultiCuratorLock",
316                 TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS);
317
318         assertEquals(TEST_JVM_COUNT_MULTI_JVM * TEST_THREAD_COUNT_MULTI_JVM * TEST_THREAD_LOOPS, result);
319
320         stopZookeeperServer();
321         logger.debug("Ran testConcurrentContextHazelcastMultiJVMCuratorLock test");
322     }
323 }