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