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.Comparator;
12 import java.util.List;
14 import java.util.concurrent.ArrayBlockingQueue;
15 import java.util.concurrent.BlockingQueue;
16 import java.util.concurrent.ConcurrentSkipListSet;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.RejectedExecutionHandler;
20 import java.util.concurrent.ThreadPoolExecutor;
21 import java.util.concurrent.TimeUnit;
23 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
24 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
25 import org.opendaylight.openflowplugin.openflow.md.core.TranslatorKey;
26 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpy.STATISTIC_GROUP;
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);
85 messageSources = new ConcurrentSkipListSet<>(
86 new Comparator<QueueKeeper<OfHeader>>() {
88 public int compare(QueueKeeper<OfHeader> o1,
89 QueueKeeper<OfHeader> o2) {
90 return Integer.valueOf(o1.hashCode()).compareTo(o2.hashCode());
94 processorPool = new ThreadPoolLoggingExecutor(processingPoolSize, processingPoolSize, 0,
95 TimeUnit.MILLISECONDS,
96 new ArrayBlockingQueue<Runnable>(ticketQueueCapacity),
98 // force blocking when pool queue is full
99 processorPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
101 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
103 executor.getQueue().put(r);
104 } catch (InterruptedException e) {
105 Thread.currentThread().interrupt();
106 throw new IllegalStateException(e);
111 harvesterPool = new ThreadPoolLoggingExecutor(1, 1, 0,
112 TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), "OFmsgHarvester");
113 finisherPool = new ThreadPoolLoggingExecutor(1, 1, 0,
114 TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), "OFmsgFinisher");
115 finisher = new TicketFinisherImpl(
116 ticketQueue, popListenersMapping);
117 finisherPool.execute(finisher);
119 harvester = new QueueKeeperHarvester<OfHeader>(this, messageSources);
120 harvesterPool.execute(harvester);
122 ticketProcessorFactory = new TicketProcessorFactoryImpl();
123 ticketProcessorFactory.setTranslatorMapping(translatorMapping);
124 ticketProcessorFactory.setSpy(messageSpy);
125 ticketProcessorFactory.setTicketFinisher(finisher);
129 * stop processing queue
131 public void shutdown() {
132 processorPool.shutdown();
136 public void enqueueQueueItem(QueueItem<OfHeader> queueItem) {
137 messageSpy.spyMessage(queueItem.getMessage(), STATISTIC_GROUP.FROM_SWITCH_ENQUEUED);
138 TicketImpl<OfHeader, DataObject> ticket = new TicketImpl<>();
139 ticket.setConductor(queueItem.getConnectionConductor());
140 ticket.setMessage(queueItem.getMessage());
141 ticket.setQueueType(queueItem.getQueueType());
143 LOG.trace("ticket scheduling: {}, ticket: {}",
144 queueItem.getMessage().getImplementedInterface().getSimpleName(),
145 System.identityHashCode(queueItem));
146 scheduleTicket(ticket);
151 public void directProcessQueueItem(QueueItem<OfHeader> queueItem) {
152 messageSpy.spyMessage(queueItem.getMessage(), STATISTIC_GROUP.FROM_SWITCH_ENQUEUED);
153 TicketImpl<OfHeader, DataObject> ticket = new TicketImpl<>();
154 ticket.setConductor(queueItem.getConnectionConductor());
155 ticket.setMessage(queueItem.getMessage());
157 LOG.debug("ticket scheduling: {}, ticket: {}",
158 queueItem.getMessage().getImplementedInterface().getSimpleName(),
159 System.identityHashCode(queueItem));
161 ticketProcessorFactory.createProcessor(ticket).run();
163 // publish notification
164 finisher.firePopNotification(ticket.getDirectResult());
170 private void scheduleTicket(Ticket<OfHeader, DataObject> ticket) {
171 switch (ticket.getQueueType()) {
173 Runnable ticketProcessor = ticketProcessorFactory.createProcessor(ticket);
174 processorPool.execute(ticketProcessor);
176 ticketQueue.put(ticket);
177 } catch (InterruptedException e) {
178 LOG.warn("enqeueue of unordered message ticket failed", e);
182 Runnable ticketProcessorSync = ticketProcessorFactory.createSyncProcessor(ticket);
183 processorPool.execute(ticketProcessorSync);
186 LOG.warn("unsupported enqueue type: {}", ticket.getQueueType());
191 * @param poolSize the poolSize to set
193 public void setProcessingPoolSize(int poolSize) {
194 this.processingPoolSize = poolSize;
198 public void setTranslatorMapping(
199 Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping) {
200 this.translatorMapping = translatorMapping;
204 public void setPopListenersMapping(
205 Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenersMapping) {
206 this.popListenersMapping = popListenersMapping;
210 * @param messageSpy the messageSpy to set
212 public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
213 this.messageSpy = messageSpy;
217 public AutoCloseable registerMessageSource(QueueKeeper<OfHeader> queue) {
218 boolean added = messageSources.add(queue);
220 LOG.debug("registration of message source queue failed - already registered");
222 MessageSourcePollRegistration<QueueKeeper<OfHeader>> queuePollRegistration =
223 new MessageSourcePollRegistration<>(this, queue);
224 return queuePollRegistration;
228 public boolean unregisterMessageSource(QueueKeeper<OfHeader> queue) {
229 return messageSources.remove(queue);
233 public Collection<QueueKeeper<OfHeader>> getMessageSources() {
234 return messageSources;
238 public HarvesterHandle getHarvesterHandle() {