2752536e9fe469a69a05aca841f8640028b8428f
[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 java.util.Random;
24
25 import org.onap.policy.apex.context.ContextAlbum;
26 import org.onap.policy.apex.context.ContextException;
27 import org.onap.policy.apex.context.Distributor;
28 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
29 import org.onap.policy.apex.context.test.concepts.TestContextItem003;
30 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * The Class ConcurrentContextMetricsThread gets metrics for concurrent use of context.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class ConcurrentContextMetricsThread implements Runnable {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetricsThread.class);
45     private final Distributor contextDistributor;
46     private final int jvm;
47     private final int instance;
48     private final int threadLoops;
49     private final int longArraySize;
50     private final int lockType;
51
52     /**
53      * The Constructor.
54      *
55      * @param jvm the jvm
56      * @param instance the instance
57      * @param threadLoops the thread loops
58      * @param longArraySize the long array size
59      * @param lockType the lock type
60      * @throws ApexException the apex exception
61      */
62     public ConcurrentContextMetricsThread(final int jvm, final int instance, final int threadLoops,
63             final int longArraySize, final int lockType) throws ApexException {
64         this.jvm = jvm;
65         this.instance = instance;
66         this.threadLoops = threadLoops;
67         this.longArraySize = longArraySize;
68         this.lockType = lockType;
69
70         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
71         contextDistributor = new DistributorFactory().getDistributor(distributorKey);
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see java.lang.Runnable#run()
78      */
79     @Override
80     public void run() {
81         LOGGER.info("running ConcurrentContextMetricsThread_" + jvm + "_" + instance + " . . .");
82
83         ContextAlbum lTypeAlbum = null;
84         try {
85             final AxContextModel axTestContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
86             contextDistributor.registerModel(axTestContextModel);
87             lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
88         } catch (final Exception e) {
89             LOGGER.error("could not get the test context album", e);
90             LOGGER.error("failed ConcurrentContextMetricsThread_" + jvm + "_" + instance);
91             return;
92         }
93
94         if (lTypeAlbum == null) {
95             LOGGER.error("could not find the test context album");
96             LOGGER.error("failed ConcurrentContextMetricsThread_" + jvm + "_" + instance);
97             return;
98         }
99
100         final AxArtifactKey[] usedArtifactStackArray =
101                 {new AxArtifactKey("testCC-top", "0.0.1"), new AxArtifactKey("testCC-" + instance, "0.0.1")};
102
103         lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
104
105         final Random rand = new Random();
106
107         for (int i = 0; i < threadLoops; i++) {
108             // Get the next random entry to use
109             final String nextLongKey = Integer.toString(rand.nextInt(longArraySize));
110
111             if (lockType == 0) {
112                 final TestContextItem003 item = (TestContextItem003) lTypeAlbum.get(nextLongKey);
113                 final long value = item.getLongValue();
114                 if (LOGGER.isTraceEnabled()) {
115                     LOGGER.trace("lock type=" + lockType + ", value=" + value);
116                 }
117                 continue;
118             }
119
120             if (lockType == 1) {
121                 try {
122                     lTypeAlbum.lockForReading(nextLongKey);
123                 } catch (final ContextException e) {
124                     LOGGER.error("could not acquire read lock on context album, key=" + nextLongKey, e);
125                     continue;
126                 }
127
128                 final TestContextItem003 item = (TestContextItem003) lTypeAlbum.get(nextLongKey);
129                 final long value = item.getLongValue();
130                 if (LOGGER.isTraceEnabled()) {
131                     LOGGER.trace("lock type=" + lockType + ", value=" + value);
132                 }
133
134                 try {
135                     lTypeAlbum.unlockForReading(nextLongKey);
136                 } catch (final ContextException e) {
137                     LOGGER.error("could not release read lock on context album, key=" + nextLongKey, e);
138                 }
139
140                 continue;
141             }
142
143             if (lockType == 2) {
144                 try {
145                     lTypeAlbum.lockForWriting(nextLongKey);
146                 } catch (final ContextException e) {
147                     LOGGER.error("could not acquire write lock on context album, key=" + nextLongKey, e);
148                     continue;
149                 }
150
151                 final TestContextItem003 item = (TestContextItem003) lTypeAlbum.get(nextLongKey);
152                 long value = item.getLongValue();
153                 if (LOGGER.isTraceEnabled()) {
154                     LOGGER.trace("lock type=" + lockType + ", value=" + value);
155                 }
156                 item.setLongValue(++value);
157                 lTypeAlbum.put(nextLongKey, item);
158
159                 try {
160                     lTypeAlbum.unlockForWriting(nextLongKey);
161                 } catch (final ContextException e) {
162                     LOGGER.error("could not release write lock on context album, key=" + nextLongKey, e);
163                 }
164                 continue;
165             }
166         }
167
168         LOGGER.info("completed ConcurrentContextMetricsThread_" + jvm + "_" + instance);
169     }
170 }