5e48a58946a7f68bb3ca2eb7d3bc8e62cd42ba88
[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.service.engine.event;
22
23 import java.util.AbstractMap.SimpleEntry;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * This class holds a cache of the synchronous events sent into Apex and that have not yet been replied to. It runs a
37  * thread to time out events that have not been replied to in the specified timeout.
38  * 
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class SynchronousEventCache extends PeeredReference implements Runnable {
42     // Get a reference to the logger
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(SynchronousEventCache.class);
44
45     // The default amount of time to wait for a synchronous event to be replied to is 1 second
46     private static final long DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT = 1000;
47
48     // The timeout to wait between event polls in milliseconds and the time to wait for the thread
49     // to stop
50     private static final long OUTSTANDING_EVENT_POLL_TIMEOUT = 50;
51     private static final long CACHE_STOP_WAIT_INTERVAL = 10;
52
53     // The time in milliseconds to wait for the reply to a sent synchronous event
54     private long synchronousEventTimeout = DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT;
55
56     // Map holding outstanding synchronous events
57     private final Map<Long, SimpleEntry<Long, Object>> toApexEventMap = new HashMap<>();
58
59     // Map holding reply events
60     private final Map<Long, SimpleEntry<Long, Object>> fromApexEventMap = new HashMap<>();
61
62     // The message listener thread and stopping flag
63     private final Thread synchronousEventCacheThread;
64     private boolean stopOrderedFlag = false;
65
66     /**
67      * Create a synchronous event cache that caches outstanding synchronous Apex events.
68      * 
69      * @param peeredMode the peered mode for which to return the reference
70      * @param consumer the consumer that is populating the cache
71      * @param producer the producer that is emptying the cache
72      * @param synchronousEventTimeout the time in milliseconds to wait for the reply to a sent synchronous event
73      */
74     public SynchronousEventCache(final EventHandlerPeeredMode peeredMode, final ApexEventConsumer consumer,
75                     final ApexEventProducer producer, final long synchronousEventTimeout) {
76         super(peeredMode, consumer, producer);
77
78         if (synchronousEventTimeout != 0) {
79             this.synchronousEventTimeout = synchronousEventTimeout;
80         } else {
81             this.synchronousEventTimeout = DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT;
82         }
83
84         // Start scanning the outstanding events
85         synchronousEventCacheThread = new Thread(this);
86         synchronousEventCacheThread.setDaemon(true);
87         synchronousEventCacheThread.start();
88     }
89
90     /**
91      * Gets the timeout value for synchronous events.
92      *
93      * @return the synchronous event timeout
94      */
95     public long getSynchronousEventTimeout() {
96         return synchronousEventTimeout;
97     }
98
99     /**
100      * Cache a synchronized event sent into Apex in the event cache.
101      *
102      * @param executionId the execution ID that was assigned to the event
103      * @param event the apex event
104      */
105     public void cacheSynchronizedEventToApex(final long executionId, final Object event) {
106         // Add the event to the map
107         synchronized (toApexEventMap) {
108             cacheSynchronizedEvent(toApexEventMap, executionId, event);
109         }
110     }
111
112     /**
113      * Remove the record of an event sent to Apex if it exists in the cache.
114      * 
115      * @param executionId the execution ID of the event
116      * @return The removed event
117      */
118     public Object removeCachedEventToApexIfExists(final long executionId) {
119         synchronized (toApexEventMap) {
120             return removeCachedEventIfExists(toApexEventMap, executionId);
121         }
122     }
123
124     /**
125      * Check if an event exists in the to apex cache.
126      * 
127      * @param executionId the execution ID of the event
128      * @return true if the event exists, false otherwise
129      */
130     public boolean existsEventToApex(final long executionId) {
131         synchronized (toApexEventMap) {
132             return toApexEventMap.containsKey(executionId);
133         }
134     }
135
136     /**
137      * Cache synchronized event received from Apex in the event cache.
138      *
139      * @param executionId the execution ID of the event
140      * @param event the apex event
141      */
142     public void cacheSynchronizedEventFromApex(final long executionId, final Object event) {
143         // Add the event to the map
144         synchronized (fromApexEventMap) {
145             cacheSynchronizedEvent(fromApexEventMap, executionId, event);
146         }
147     }
148
149     /**
150      * Remove the record of an event received from Apex if it exists in the cache.
151      * 
152      * @param executionId the execution ID of the event
153      * @return The removed event
154      */
155     public Object removeCachedEventFromApexIfExists(final long executionId) {
156         synchronized (fromApexEventMap) {
157             return removeCachedEventIfExists(fromApexEventMap, executionId);
158         }
159     }
160
161     /**
162      * Check if an event exists in the from apex cache.
163      * 
164      * @param executionId the execution ID of the event
165      * @return true if the event exists, false otherwise
166      */
167     public boolean existsEventFromApex(final long executionId) {
168         synchronized (fromApexEventMap) {
169             return fromApexEventMap.containsKey(executionId);
170         }
171     }
172
173     /*
174      * (non-Javadoc)
175      * 
176      * @see java.lang.Runnable#run()
177      */
178     @Override
179     public void run() {
180         LOGGER.entry();
181
182         // Periodic scan of outstanding events
183         while (synchronousEventCacheThread.isAlive() && !stopOrderedFlag) {
184             ThreadUtilities.sleep(OUTSTANDING_EVENT_POLL_TIMEOUT);
185
186             // Check for timeouts on events
187             synchronized (toApexEventMap) {
188                 timeoutEventsOnCache(toApexEventMap);
189             }
190             synchronized (fromApexEventMap) {
191                 timeoutEventsOnCache(fromApexEventMap);
192             }
193         }
194
195         LOGGER.exit();
196     }
197
198     /**
199      * Stops the scanning thread and clears the cache.
200      */
201     public synchronized void stop() {
202         LOGGER.entry();
203         stopOrderedFlag = true;
204
205         while (synchronousEventCacheThread.isAlive()) {
206             ThreadUtilities.sleep(CACHE_STOP_WAIT_INTERVAL);
207         }
208
209         // Check if there are any unprocessed events
210         if (!toApexEventMap.isEmpty()) {
211             String message = toApexEventMap.size() + " synchronous events dropped due to system shutdown";
212             LOGGER.warn(message);
213         }
214
215         toApexEventMap.clear();
216         LOGGER.exit();
217     }
218
219     /**
220      * Cache a synchronized event sent in an event cache.
221      * 
222      * @param eventCacheMap the map to cache the event on
223      * @param executionId the execution ID of the event
224      * @param event the event to cache
225      */
226     private void cacheSynchronizedEvent(final Map<Long, SimpleEntry<Long, Object>> eventCacheMap,
227                     final long executionId, final Object event) {
228         LOGGER.entry("Adding event with execution ID: " + executionId);
229
230         // Check if the event is already in the cache
231         if (eventCacheMap.containsKey(executionId)) {
232             // If there was no sent event then the event timed out or some unexpected event was
233             // received
234             final String errorMessage = "an event with ID " + executionId
235                             + " already exists in the synchronous event cache, "
236                             + "execution IDs must be unique in the system";
237             LOGGER.warn(errorMessage);
238             throw new ApexEventRuntimeException(errorMessage);
239         }
240
241         // Add the event to the map
242         eventCacheMap.put(executionId, new SimpleEntry<Long, Object>(System.currentTimeMillis(), event));
243
244         if (LOGGER.isDebugEnabled()) {
245             String message = "event has been cached:" + event;
246             LOGGER.debug(message);
247         }
248
249         LOGGER.exit("Added: " + executionId);
250     }
251
252     /**
253      * Remove the record of an event if it exists in the cache.
254      * 
255      * @param eventCacheMap the map to remove the event from
256      * @param executionId the execution ID of the event
257      * @return The removed event
258      */
259     private Object removeCachedEventIfExists(final Map<Long, SimpleEntry<Long, Object>> eventCacheMap,
260                     final long executionId) {
261         LOGGER.entry("Removing: " + executionId);
262
263         final SimpleEntry<Long, Object> removedEventEntry = eventCacheMap.remove(executionId);
264
265         if (removedEventEntry != null) {
266             LOGGER.exit("Removed: " + executionId);
267             return removedEventEntry.getValue();
268         } else {
269             // The event may not be one of the events in our cache, so we just ignore removal
270             // failures
271             return null;
272         }
273     }
274
275     /**
276      * Time out events on an event cache map. Events that have a timeout longer than the configured timeout are timed
277      * out.
278      * 
279      * @param eventCacheMap the event cache to operate on
280      */
281     private void timeoutEventsOnCache(final Map<Long, SimpleEntry<Long, Object>> eventCacheMap) {
282         // Use a set to keep track of the events that have timed out
283         final Set<Long> timedOutEventSet = new HashSet<>();
284
285         for (final Entry<Long, SimpleEntry<Long, Object>> cachedEventEntry : eventCacheMap.entrySet()) {
286             // The amount of time we are waiting for the event reply
287             final long eventWaitTime = System.currentTimeMillis() - cachedEventEntry.getValue().getKey();
288
289             // Have we a timeout?
290             if (eventWaitTime > synchronousEventTimeout) {
291                 timedOutEventSet.add(cachedEventEntry.getKey());
292             }
293         }
294
295         // Remove timed out events from the map
296         for (final long timedoutEventExecutionId : timedOutEventSet) {
297             // Remove the map entry and issue a warning
298             final SimpleEntry<Long, Object> timedOutEventEntry = eventCacheMap.remove(timedoutEventExecutionId);
299
300             String message = "synchronous event timed out, reply not received in " + synchronousEventTimeout
301                             + " milliseconds on event " + timedOutEventEntry.getValue();
302             LOGGER.warn(message);
303         }
304     }
305 }