2 * Copyright (c) 2014 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
11 import com.google.common.base.Preconditions;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.util.concurrent.EventExecutor;
17 import io.netty.util.concurrent.Future;
18 import io.netty.util.concurrent.GenericFutureListener;
19 import java.net.InetSocketAddress;
20 import java.util.Queue;
21 import java.util.concurrent.LinkedBlockingQueue;
22 import java.util.concurrent.RejectedExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Channel handler which bypasses wraps on top of normal Netty pipeline, allowing
30 * writes to be enqueued from any thread, it then schedules a task pipeline task,
31 * which shuffles messages from the queue into the pipeline.
33 * Note this is an *Inbound* handler, as it reacts to channel writability changing,
34 * which in the Netty vocabulary is an inbound event. This has already changed in
35 * the Netty 5.0.0 API, where Handlers are unified.
37 final class ChannelOutboundQueue extends ChannelInboundHandlerAdapter {
38 public interface MessageHolder<T> {
40 * Take ownership of the encapsulated listener. Guaranteed to
41 * be called at most once.
43 * @return listener encapsulated in the holder, may be null
44 * @throws IllegalStateException if the listener is no longer
45 * available (for example because it has already been
48 GenericFutureListener<Future<Void>> takeListener();
51 * Take ownership of the encapsulated message. Guaranteed to be
52 * called at most once.
54 * @return message encapsulated in the holder, may not be null
55 * @throws IllegalStateException if the message is no longer
56 * available (for example because it has already been
63 * This is the default upper bound we place on the flush task running
64 * a single iteration. We relinquish control after about this amount
67 private static final long DEFAULT_WORKTIME_MICROS = TimeUnit.MILLISECONDS.toMicros(100);
70 * We re-check the time spent flushing every this many messages. We do this because
71 * checking after each message may prove to be CPU-intensive. Set to Integer.MAX_VALUE
72 * or similar to disable the feature.
74 private static final int WORKTIME_RECHECK_MSGS = 64;
75 private static final Logger LOG = LoggerFactory.getLogger(ChannelOutboundQueue.class);
77 // Passed to executor to request triggering of flush
78 private final Runnable flushRunnable = new Runnable() {
81 ChannelOutboundQueue.this.flush();
86 * Instead of using an AtomicBoolean object, we use these two. It saves us
87 * from allocating an extra object.
89 private static final AtomicIntegerFieldUpdater<ChannelOutboundQueue> FLUSH_SCHEDULED_UPDATER =
90 AtomicIntegerFieldUpdater.newUpdater(ChannelOutboundQueue.class, "flushScheduled");
91 private volatile int flushScheduled = 0;
93 private final Queue<MessageHolder<?>> queue;
94 private final long maxWorkTime;
95 private final Channel channel;
96 private final InetSocketAddress address;
98 public ChannelOutboundQueue(final Channel channel, final int queueDepth, final InetSocketAddress address) {
99 Preconditions.checkArgument(queueDepth > 0, "Queue depth has to be positive");
102 * This looks like a good trade-off for throughput. Alternative is
103 * to use an ArrayBlockingQueue -- but that uses a single lock to
104 * synchronize both producers and consumers, potentially leading
105 * to less throughput.
107 this.queue = new LinkedBlockingQueue<>(queueDepth);
108 this.channel = Preconditions.checkNotNull(channel);
109 this.maxWorkTime = TimeUnit.MICROSECONDS.toNanos(DEFAULT_WORKTIME_MICROS);
110 this.address = address;
114 * Enqueue a message holder for transmission. Is a thread-safe entry point
115 * for the channel. If the cannot be placed on the queue, this
117 * @param holder MessageHolder which should be enqueue
118 * @return Success indicator, true if the enqueue operation succeeded,
119 * false if the queue is full.
121 public boolean enqueue(final MessageHolder<?> holder) {
122 LOG.trace("Enqueuing message {}", holder);
123 if (queue.offer(holder)) {
124 LOG.trace("Message enqueued");
129 LOG.debug("Message queue is full");
133 private void scheduleFlush(final EventExecutor executor) {
134 if (FLUSH_SCHEDULED_UPDATER.compareAndSet(this, 0, 1)) {
135 LOG.trace("Scheduling flush task");
136 executor.execute(flushRunnable);
138 LOG.trace("Flush task is already present");
143 * Schedule a queue flush if it is not empty and the channel is found
146 private void conditionalFlush() {
147 if (queue.isEmpty()) {
148 LOG.trace("Queue is empty, not flush needed");
151 if (!channel.isWritable()) {
152 LOG.trace("Channel {} is not writable, not issuing a flush", channel);
156 scheduleFlush(channel.pipeline().lastContext().executor());
160 * The synchronized keyword should be unnecessary, really, but it enforces
161 * queue order should something go terribly wrong. It should be completely
164 private synchronized void flush() {
166 final long start = System.nanoTime();
167 final long deadline = start + maxWorkTime;
169 LOG.debug("Dequeuing messages to channel {}", channel);
172 for (;; ++messages) {
173 if (!channel.isWritable()) {
174 LOG.trace("Channel is no longer writable");
178 final MessageHolder<?> h = queue.poll();
180 LOG.trace("The queue is completely drained");
184 final GenericFutureListener<Future<Void>> l = h.takeListener();
186 final ChannelFuture p;
187 if (address == null) {
188 p = channel.write(new MessageListenerWrapper(h.takeMessage(), l));
190 p = channel.write(new UdpMessageListenerWrapper(h.takeMessage(), l, address));
197 * Check every WORKTIME_RECHECK_MSGS for exceeded time.
199 * XXX: given we already measure our flushing throughput, we
200 * should be able to perform dynamic adjustments here.
201 * is that additional complexity needed, though?
203 if ((messages % WORKTIME_RECHECK_MSGS) == 0 && System.nanoTime() >= deadline) {
204 LOG.trace("Exceeded allotted work time {}us",
205 TimeUnit.NANOSECONDS.toMicros(maxWorkTime));
211 LOG.debug("Flushing {} message(s) to channel {}", messages, channel);
215 if (LOG.isDebugEnabled()) {
216 final long stop = System.nanoTime();
217 LOG.debug("Flushed {} messages in {}us to channel {}",
218 messages, TimeUnit.NANOSECONDS.toMicros(stop - start), channel);
222 * We are almost ready to terminate. This is a bit tricky, because
223 * we do not want to have a race window where a message would be
224 * stuck on the queue without a flush being scheduled.
226 * So we mark ourselves as not running and then re-check if a
227 * flush out is needed. That will re-synchronized with other threads
228 * such that only one flush is scheduled at any given time.
230 if (!FLUSH_SCHEDULED_UPDATER.compareAndSet(this, 1, 0)) {
231 LOG.warn("Channel {} queue {} flusher found unscheduled", channel, queue);
237 private void conditionalFlush(final ChannelHandlerContext ctx) {
238 Preconditions.checkState(ctx.channel().equals(channel), "Inconsistent channel %s with context %s", channel, ctx);
243 public void channelActive(final ChannelHandlerContext ctx) throws Exception {
244 super.channelActive(ctx);
245 conditionalFlush(ctx);
249 public void channelWritabilityChanged(final ChannelHandlerContext ctx) throws Exception {
250 super.channelWritabilityChanged(ctx);
251 conditionalFlush(ctx);
255 public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
256 super.channelInactive(ctx);
259 LOG.debug("Channel shutdown, flushing queue...");
260 final Future<Void> result = ctx.newFailedFuture(new RejectedExecutionException("Channel disconnected"));
262 final MessageHolder<?> e = queue.poll();
267 e.takeListener().operationComplete(result);
271 LOG.debug("Flushed {} queue entries", entries);
275 public String toString() {
276 return String.format("Channel %s queue [%s messages flushing=%s]", channel, queue.size(), flushScheduled);