3d257f89a5b830da506d7b1bd1051f6602f7e748
[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.metrics;
22
23 import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
24 import static org.onap.policy.apex.context.parameters.LockManagerParameters.DEFAULT_LOCK_MANAGER_PLUGIN_CLASS;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.onap.policy.apex.context.parameters.ContextParameters;
33 import org.onap.policy.apex.context.parameters.DistributorParameters;
34 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
35 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
36 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
37 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
38 import org.onap.policy.apex.context.test.utils.ZooKeeperServerServiceProvider;
39 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
41 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
42 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
43 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
44 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
45 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
46 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
47 import org.slf4j.ext.XLogger;
48 import org.slf4j.ext.XLoggerFactory;
49
50 /**
51  * The Class concurrentContextMetrics tests concurrent use of context.
52  *
53  * @author Liam Fallon (liam.fallon@ericsson.com)
54  */
55 public class ConcurrentContextMetrics {
56     private static final int NUM_ARGS = 9;
57     private static final int ARG_LABEL = 0;
58     private static final int ARG_JVM_COUNT = 1;
59     private static final int ARG_THREAD_COUNT = 2;
60     private static final int ARG_ITERATIONS = 3;
61     private static final int ARG_ARRAY_SIZE = 4;
62     private static final int ARG_LOCK_TYPE = 5;
63     private static final int ARG_ZOOKEEPER_ADDRESS = 6;
64     private static final int ARG_ZOOKEEPER_PORT = 7;
65     private static final int ARG_ZOOKEEPER_DIRECTORY = 8;
66
67     // Logger for this class
68     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetrics.class);
69
70     private String zookeeperAddress = null;
71     private final ConfigrationProvider configrationProvider;
72     private final File zookeeperDirectory;
73     private final int zookeeperPort;
74
75     /**
76      * The main method.
77      *
78      * @param args the args
79      * @throws Exception the exception
80      */
81     public static void main(final String[] args) throws Exception {
82         if (args.length != NUM_ARGS) {
83             System.err.println("Args: " + Arrays.toString(args));
84             System.err.println(
85                     "usage: testLabel jvmCount threadCount threadLoops longArraySize lockType zookeeperAddress zookeeperPort zookeeperDirectory");
86             return;
87         }
88
89         final ConfigrationProvider configrationProvider =
90                 new ConfigrationProviderImpl(args[ARG_LABEL], Integer.valueOf(args[ARG_JVM_COUNT]),
91                         Integer.valueOf(args[ARG_THREAD_COUNT]), Integer.valueOf(args[ARG_ITERATIONS]),
92                         Integer.valueOf(args[ARG_ARRAY_SIZE]), Integer.valueOf(args[ARG_LOCK_TYPE]));
93
94         final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
95                 args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]), args[ARG_ZOOKEEPER_DIRECTORY]);
96
97         concurrentContextMetrics.concurrentContextMetricsJVMLocal();
98         concurrentContextMetrics.concurrentContextMetricsCurator();
99         concurrentContextMetrics.concurrentContextMetricsHazelcast();
100         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMHazelcastLock();
101         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMHazelcastlock();
102         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMCuratorLock();
103         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMCuratorLock();
104     }
105
106     /**
107      * The Constructor.
108      * 
109      * @param configrationProvider
110      * @param zookeeperAddress
111      * @param zookeeperPort
112      * @param zookeeperDirectory
113      */
114     public ConcurrentContextMetrics(final ConfigrationProvider configrationProvider, final String zookeeperAddress,
115             final int zookeeperPort, final String zookeeperDirectory) {
116         this.configrationProvider = configrationProvider;
117         this.zookeeperAddress = zookeeperAddress;
118         this.zookeeperPort = zookeeperPort;
119         this.zookeeperDirectory = new File(zookeeperDirectory);
120     }
121
122     /**
123      * Concurrent context metrics JVM local.
124      *
125      * @throws ApexModelException the apex model exception
126      * @throws IOException the IO exception
127      * @throws ApexException the apex exception
128      */
129     private void concurrentContextMetricsJVMLocal() throws ApexModelException, IOException, ApexException {
130         if (configrationProvider.getJvmCount() != 1) {
131             return;
132         }
133
134         LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
135
136         final ContextParameters contextParameters = new ContextParameters();
137         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
138         contextParameters.getLockManagerParameters().setPluginClass(DEFAULT_LOCK_MANAGER_PLUGIN_CLASS);
139         runConcurrentContextMetrics("JVMLocal");
140
141         LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
142     }
143
144     /**
145      * Concurrent context metrics hazelcast.
146      *
147      * @throws IOException the IO exception
148      * @throws ApexException the apex exception
149      */
150     private void concurrentContextMetricsHazelcast() throws IOException, ApexException {
151         if (configrationProvider.getJvmCount() != 1) {
152             return;
153         }
154
155         LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
156
157         final ContextParameters contextParameters = new ContextParameters();
158         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
159         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
160         runConcurrentContextMetrics("Hazelcast");
161
162         LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
163     }
164
165     /**
166      * Concurrent context metrics curator.
167      *
168      * @throws IOException the IO exception
169      * @throws ApexException the apex exception
170      */
171     private void concurrentContextMetricsCurator() throws IOException, ApexException {
172         if (configrationProvider.getJvmCount() != 1) {
173             return;
174         }
175
176         LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
177
178         final ContextParameters contextParameters = new ContextParameters();
179         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
180
181         final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
182         curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
183         contextParameters.setLockManagerParameters(curatorParameters);
184         curatorParameters.setZookeeperAddress(zookeeperAddress);
185
186         runConcurrentContextMetrics("Curator");
187
188         LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
189     }
190
191     /**
192      * Concurrent context metrics hazelcast multi JVM hazelcast lock.
193      *
194      * @throws IOException the IO exception
195      * @throws ApexException the apex exception
196      */
197     private void concurrentContextMetricsHazelcastMultiJVMHazelcastLock() throws IOException, ApexException {
198         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
199
200         final ContextParameters contextParameters = new ContextParameters();
201         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
202         distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
203         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
204         runConcurrentContextMetrics("HazelcastMultiJVMHazelcastLock");
205
206         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
207     }
208
209     /**
210      * Concurrent context metrics infinispan multi JVM hazelcastlock.
211      *
212      * @throws IOException the IO exception
213      * @throws ApexException the apex exception
214      */
215     private void concurrentContextMetricsInfinispanMultiJVMHazelcastlock() throws IOException, ApexException {
216         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
217
218         final ContextParameters contextParameters = new ContextParameters();
219         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
220         distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
221         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
222
223         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
224         contextParameters.setDistributorParameters(infinispanParameters);
225
226         runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
227
228         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
229     }
230
231     /**
232      * Concurrent context metrics infinispan multi JVM curator lock.
233      *
234      * @throws IOException the IO exception
235      * @throws ApexException the apex exception
236      * @throws InterruptedException
237      */
238     private void concurrentContextMetricsInfinispanMultiJVMCuratorLock()
239             throws IOException, ApexException, InterruptedException {
240
241         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
242
243         final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider =
244                 new ZooKeeperServerServiceProvider(zookeeperDirectory, zookeeperAddress, zookeeperPort);
245         try {
246             zooKeeperServerServiceProvider.startZookeeperServer();
247             final ContextParameters contextParameters = new ContextParameters();
248             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
249             distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
250
251             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
252             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
253             contextParameters.setLockManagerParameters(curatorParameters);
254             curatorParameters.setZookeeperAddress(zookeeperAddress);
255
256             final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
257             contextParameters.setDistributorParameters(infinispanParameters);
258
259             runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
260         } finally {
261             zooKeeperServerServiceProvider.stopZookeeperServer();
262         }
263         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
264     }
265
266     /**
267      * Concurrent context metrics hazelcast multi JVM curator lock.
268      *
269      * @throws IOException the IO exception
270      * @throws ApexException the apex exception
271      * @throws InterruptedException
272      */
273     private void concurrentContextMetricsHazelcastMultiJVMCuratorLock()
274             throws IOException, ApexException, InterruptedException {
275         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
276
277         final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider =
278                 new ZooKeeperServerServiceProvider(zookeeperDirectory, zookeeperAddress, zookeeperPort);
279
280         try {
281             zooKeeperServerServiceProvider.startZookeeperServer();
282             final ContextParameters contextParameters = new ContextParameters();
283             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
284             distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
285
286             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
287             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
288             contextParameters.setLockManagerParameters(curatorParameters);
289             curatorParameters.setZookeeperAddress(zookeeperAddress);
290
291             runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
292         } finally {
293             zooKeeperServerServiceProvider.stopZookeeperServer();
294         }
295         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
296     }
297
298     /**
299      * Run concurrent context metrics.
300      *
301      * @param testName the test name
302      * @throws IOException the IO exception
303      * @throws ApexException the apex exception
304      */
305     private void runConcurrentContextMetrics(final String testName) throws IOException, ApexException {
306         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
307
308         LOGGER.info("Running {} ...", testName);
309         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
310
311         long total = 0;
312         for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
313             LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
314             total += entry.getValue().getLongValue();
315         }
316         LOGGER.info("Album total value after execution: {}", total);
317
318         LOGGER.info("Completed {} ...", testName);
319     }
320
321
322 }