2 * Copyright (c) 2015 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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
11 import com.google.common.util.concurrent.FutureCallback;
12 import io.netty.channel.Channel;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Class is designed for stacking Statistics and propagate immediate response for all
23 public class StackedOutboundQueueNoBarrier extends AbstractStackedOutboundQueue {
25 private static final Logger LOG = LoggerFactory.getLogger(StackedOutboundQueueNoBarrier.class);
27 StackedOutboundQueueNoBarrier(final AbstractOutboundQueueManager<?, ?> manager) {
32 * This method is expected to be called from multiple threads concurrently
35 public void commitEntry(final Long xid, final OfHeader message, final FutureCallback<OfHeader> callback) {
36 final OutboundQueueEntry entry = getEntry(xid);
38 if (message instanceof FlowModInput) {
39 callback.onSuccess(null);
40 entry.commit(message, null);
42 entry.commit(message, callback);
45 LOG.trace("Queue {} committed XID {}", this, xid);
46 manager.ensureFlushing();
50 int writeEntries(@Nonnull final Channel channel, final long now) {
52 StackedSegment segment = firstSegment;
55 while (channel.isWritable()) {
56 final OutboundQueueEntry entry = segment.getEntry(flushOffset);
57 if (!entry.isCommitted()) {
58 LOG.debug("Queue {} XID {} segment {} offset {} not committed yet", this, segment.getBaseXid()
59 + flushOffset, segment, flushOffset);
63 LOG.trace("Queue {} flushing entry at offset {}", this, flushOffset);
64 final OfHeader message = entry.takeMessage();
68 if (message != null) {
69 manager.writeMessage(message, now);
74 if (flushOffset >= StackedSegment.SEGMENT_SIZE) {
76 * Slow path: purge the current segment unless it's the last one.
77 * If it is, we leave it for replacement when a new reservation
79 * This costs us two slow paths, but hey, this should be very rare,
80 * so let's keep things simple.
82 synchronized (unflushedSegments) {
83 LOG.debug("Flush offset {} unflushed segments {}", flushOffset, unflushedSegments.size());
85 // We may have raced ahead of reservation code and need to allocate a segment
86 ensureSegment(segment, flushOffset);
88 // Remove the segment, update the firstSegment and reset flushOffset
89 final StackedSegment oldSegment = unflushedSegments.remove(0);
90 oldSegment.completeAll();
91 uncompletedSegments.remove(oldSegment);
94 // Reset the first segment and add it to the uncompleted list
95 segment = unflushedSegments.get(0);
96 uncompletedSegments.add(segment);
98 // Update the shutdown offset
99 if (shutdownOffset != null) {
100 shutdownOffset -= StackedSegment.SEGMENT_SIZE;
103 // Allow reservations back on the fast path by publishing the new first segment
104 firstSegment = segment;
107 LOG.debug("Queue {} flush moved to segment {}", this, segment);