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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.metrics;
23 import java.util.Random;
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;
38 * The Class ConcurrentContextMetricsThread gets metrics for concurrent use of context.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
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;
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
62 public ConcurrentContextMetricsThread(final int jvm, final int instance, final int threadLoops,
63 final int longArraySize, final int lockType) throws ApexException {
65 this.instance = instance;
66 this.threadLoops = threadLoops;
67 this.longArraySize = longArraySize;
68 this.lockType = lockType;
70 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
71 contextDistributor = new DistributorFactory().getDistributor(distributorKey);
77 * @see java.lang.Runnable#run()
81 LOGGER.info("running ConcurrentContextMetricsThread_" + jvm + "_" + instance + " . . .");
83 ContextAlbum lTypeAlbum = null;
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);
94 if (lTypeAlbum == null) {
95 LOGGER.error("could not find the test context album");
96 LOGGER.error("failed ConcurrentContextMetricsThread_" + jvm + "_" + instance);
100 final AxArtifactKey[] usedArtifactStackArray =
101 {new AxArtifactKey("testCC-top", "0.0.1"), new AxArtifactKey("testCC-" + instance, "0.0.1")};
103 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
105 final Random rand = new Random();
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));
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);
122 lTypeAlbum.lockForReading(nextLongKey);
123 } catch (final ContextException e) {
124 LOGGER.error("could not acquire read lock on context album, key=" + nextLongKey, e);
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);
135 lTypeAlbum.unlockForReading(nextLongKey);
136 } catch (final ContextException e) {
137 LOGGER.error("could not release read lock on context album, key=" + nextLongKey, e);
145 lTypeAlbum.lockForWriting(nextLongKey);
146 } catch (final ContextException e) {
147 LOGGER.error("could not acquire write lock on context album, key=" + nextLongKey, e);
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);
156 item.setLongValue(++value);
157 lTypeAlbum.put(nextLongKey, item);
160 lTypeAlbum.unlockForWriting(nextLongKey);
161 } catch (final ContextException e) {
162 LOGGER.error("could not release write lock on context album, key=" + nextLongKey, e);
168 LOGGER.info("completed ConcurrentContextMetricsThread_" + jvm + "_" + instance);