2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.openflow.md.queue;
10 import java.util.Collection;
11 import java.util.List;
13 import java.util.concurrent.ArrayBlockingQueue;
14 import java.util.concurrent.BlockingQueue;
15 import java.util.concurrent.CopyOnWriteArraySet;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.RejectedExecutionHandler;
19 import java.util.concurrent.ThreadPoolExecutor;
20 import java.util.concurrent.TimeUnit;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
23 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
24 import org.opendaylight.openflowplugin.api.statistics.MessageSpy.STATISTIC_GROUP;
25 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
26 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
35 * {@link QueueKeeper} implementation focused to keep order and use up mutiple threads for translation phase.
37 * There is internal thread pool of limited size ({@link QueueProcessorLightImpl#setProcessingPoolSize(int)})
38 * dedicated to translation. Then there is singleThreadPool dedicated to publishing (via popListeners)
42 * <li>upon message push ticket is created and enqueued</li>
43 * <li>available threads from internal pool translate the massage wrapped in ticket</li>
44 * <li>when translation of particular message is finished, result is set in future result of wrapping ticket</br>
45 * (order of tickets in queue is not touched during translate)
47 * <li>at the end of queue there is {@link TicketFinisher} running in singleThreadPool and for each ticket it does:
49 * <li>invoke blocking {@link BlockingQueue#take()} method in order to get the oldest ticket</li>
50 * <li>invoke blocking {@link Future#get()} on the dequeued ticket</li>
51 * <li>as soon as the result of translation is available, appropriate popListener is invoked</li>
53 * and this way the order of messages is preserved and also multiple threads are used by translating
59 public class QueueProcessorLightImpl implements QueueProcessor<OfHeader, DataObject> {
61 protected static final Logger LOG = LoggerFactory
62 .getLogger(QueueProcessorLightImpl.class);
64 private BlockingQueue<TicketResult<DataObject>> ticketQueue;
65 private ThreadPoolExecutor processorPool;
66 private int processingPoolSize = 4;
67 private ExecutorService harvesterPool;
68 private ExecutorService finisherPool;
70 protected Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenersMapping;
71 private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping;
72 private TicketProcessorFactory<OfHeader, DataObject> ticketProcessorFactory;
73 private MessageSpy<DataContainer> messageSpy;
74 protected Collection<QueueKeeper<OfHeader>> messageSources;
75 private QueueKeeperHarvester<OfHeader> harvester;
77 protected TicketFinisher<DataObject> finisher;
83 int ticketQueueCapacity = 1500;
84 ticketQueue = new ArrayBlockingQueue<>(ticketQueueCapacity);
86 * TODO FIXME - DOES THIS REALLY NEED TO BE CONCURRENT? Can we figure out
87 * a better lifecycle? Why does this have to be a Set?
89 messageSources = new CopyOnWriteArraySet<>();
91 processorPool = new ThreadPoolLoggingExecutor(processingPoolSize, processingPoolSize, 0,
92 TimeUnit.MILLISECONDS,
93 new ArrayBlockingQueue<Runnable>(ticketQueueCapacity),
95 // force blocking when pool queue is full
96 processorPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
98 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
100 executor.getQueue().put(r);
101 } catch (InterruptedException e) {
102 Thread.currentThread().interrupt();
103 throw new IllegalStateException(e);
108 harvesterPool = new ThreadPoolLoggingExecutor(1, 1, 0,
109 TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), "OFmsgHarvester");
110 finisherPool = new ThreadPoolLoggingExecutor(1, 1, 0,
111 TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), "OFmsgFinisher");
112 finisher = new TicketFinisherImpl(
113 ticketQueue, popListenersMapping);
114 finisherPool.execute(finisher);
116 harvester = new QueueKeeperHarvester<OfHeader>(this, messageSources);
117 harvesterPool.execute(harvester);
119 ticketProcessorFactory = new TicketProcessorFactoryImpl();
120 ticketProcessorFactory.setTranslatorMapping(translatorMapping);
121 ticketProcessorFactory.setSpy(messageSpy);
122 ticketProcessorFactory.setTicketFinisher(finisher);
126 * stop processing queue
128 public void shutdown() {
129 processorPool.shutdown();
133 public void enqueueQueueItem(QueueItem<OfHeader> queueItem) {
134 messageSpy.spyMessage(queueItem.getMessage(), STATISTIC_GROUP.FROM_SWITCH_ENQUEUED);
135 TicketImpl<OfHeader, DataObject> ticket = new TicketImpl<>();
136 ticket.setConductor(queueItem.getConnectionConductor());
137 ticket.setMessage(queueItem.getMessage());
138 ticket.setQueueType(queueItem.getQueueType());
140 LOG.trace("ticket scheduling: {}, ticket: {}",
141 queueItem.getMessage().getImplementedInterface().getSimpleName(),
142 System.identityHashCode(queueItem));
143 scheduleTicket(ticket);
148 public void directProcessQueueItem(QueueItem<OfHeader> queueItem) {
149 messageSpy.spyMessage(queueItem.getMessage(), STATISTIC_GROUP.FROM_SWITCH_ENQUEUED);
150 TicketImpl<OfHeader, DataObject> ticket = new TicketImpl<>();
151 ticket.setConductor(queueItem.getConnectionConductor());
152 ticket.setMessage(queueItem.getMessage());
154 LOG.debug("ticket scheduling: {}, ticket: {}",
155 queueItem.getMessage().getImplementedInterface().getSimpleName(),
156 System.identityHashCode(queueItem));
158 ticketProcessorFactory.createProcessor(ticket).run();
160 // publish notification
161 finisher.firePopNotification(ticket.getDirectResult());
167 private void scheduleTicket(Ticket<OfHeader, DataObject> ticket) {
168 switch (ticket.getQueueType()) {
170 Runnable ticketProcessor = ticketProcessorFactory.createProcessor(ticket);
171 processorPool.execute(ticketProcessor);
173 ticketQueue.put(ticket);
174 } catch (InterruptedException e) {
175 LOG.warn("enqeueue of unordered message ticket failed", e);
179 Runnable ticketProcessorSync = ticketProcessorFactory.createSyncProcessor(ticket);
180 processorPool.execute(ticketProcessorSync);
183 LOG.warn("unsupported enqueue type: {}", ticket.getQueueType());
188 * @param poolSize the poolSize to set
190 public void setProcessingPoolSize(int poolSize) {
191 this.processingPoolSize = poolSize;
195 public void setTranslatorMapping(
196 Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping) {
197 this.translatorMapping = translatorMapping;
201 public void setPopListenersMapping(
202 Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenersMapping) {
203 this.popListenersMapping = popListenersMapping;
207 * @param messageSpy the messageSpy to set
209 public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
210 this.messageSpy = messageSpy;
214 public AutoCloseable registerMessageSource(QueueKeeper<OfHeader> queue) {
215 boolean added = messageSources.add(queue);
217 LOG.debug("registration of message source queue failed - already registered");
219 MessageSourcePollRegistration<QueueKeeper<OfHeader>> queuePollRegistration =
220 new MessageSourcePollRegistration<>(this, queue);
221 return queuePollRegistration;
225 public boolean unregisterMessageSource(QueueKeeper<OfHeader> queue) {
226 return messageSources.remove(queue);
230 public Collection<QueueKeeper<OfHeader>> getMessageSources() {
231 return messageSources;
235 public HarvesterHandle getHarvesterHandle() {