2 * Copyright (c) 2015 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
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.util.concurrent.FutureCallback;
13 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
16 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
17 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 final class OutboundQueueImpl implements OutboundQueue {
24 private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueImpl.class);
25 private static final AtomicIntegerFieldUpdater<OutboundQueueImpl> CURRENT_OFFSET_UPDATER =
26 AtomicIntegerFieldUpdater.newUpdater(OutboundQueueImpl.class, "reserveOffset");
27 private static final AtomicIntegerFieldUpdater<OutboundQueueImpl> BARRIER_OFFSET_UPDATER =
28 AtomicIntegerFieldUpdater.newUpdater(OutboundQueueImpl.class, "barrierOffset");
29 private final OutboundQueueManager<?> manager;
30 private final OutboundQueueEntry[] queue;
31 private final long baseXid;
32 private final long endXid;
33 private final int reserve;
35 // Updated concurrently
36 private volatile int barrierOffset = -1;
37 private volatile int reserveOffset = 0;
39 // Updated from Netty only
40 private int flushOffset;
41 private int completeCount;
42 private int lastBarrierOffset = -1;
44 OutboundQueueImpl(final OutboundQueueManager<?> manager, final long baseXid, final int maxQueue) {
46 * We use the last entry as an emergency should a timeout-triggered
47 * flush request race with normal users for the last entry in this
48 * queue. In that case the flush request will take the last entry and
49 * schedule a flush, which means that we will get around sending the
50 * message as soon as the user finishes the reservation.
52 Preconditions.checkArgument(maxQueue > 1);
53 this.baseXid = baseXid;
54 this.endXid = baseXid + maxQueue;
55 this.reserve = maxQueue - 1;
56 this.manager = Preconditions.checkNotNull(manager);
57 queue = new OutboundQueueEntry[maxQueue];
58 for (int i = 0; i < maxQueue; ++i) {
59 queue[i] = new OutboundQueueEntry();
63 private OutboundQueueImpl(final OutboundQueueManager<?> manager, final long baseXid, final OutboundQueueEntry[] queue) {
64 this.manager = Preconditions.checkNotNull(manager);
65 this.queue = Preconditions.checkNotNull(queue);
66 this.baseXid = baseXid;
67 this.endXid = baseXid + queue.length;
68 this.reserve = queue.length - 1;
72 for (OutboundQueueEntry element : queue) {
77 OutboundQueueImpl reuse(final OutboundQueueManager<?> manager, final long baseXid) {
78 return new OutboundQueueImpl(manager, baseXid, queue);
82 public Long reserveEntry() {
83 return reserveEntry(false);
87 public void commitEntry(final Long xid, final OfHeader message, final FutureCallback<OfHeader> callback) {
88 final int offset = (int)(xid - baseXid);
89 if (message != null) {
90 Preconditions.checkArgument(xid.equals(message.getXid()), "Message %s has wrong XID %s, expected %s", message, message.getXid(), xid);
93 final int ro = reserveOffset;
94 Preconditions.checkArgument(offset < ro, "Unexpected commit to offset %s reserved %s message %s", offset, ro, message);
96 final OutboundQueueEntry entry = queue[offset];
97 entry.commit(message, callback);
98 LOG.debug("Queue {} XID {} at offset {} (of {}) committed", this, xid, offset, ro);
100 if (entry.isBarrier()) {
103 final int prev = BARRIER_OFFSET_UPDATER.getAndSet(this, my);
105 LOG.debug("Queue {} recorded pending barrier offset {}", this, my);
109 // We have traveled back, recover
114 manager.ensureFlushing(this);
117 private Long reserveEntry(final boolean forBarrier) {
118 final int offset = CURRENT_OFFSET_UPDATER.getAndIncrement(this);
119 if (offset >= reserve) {
121 LOG.debug("Queue {} offset {}/{}, using emergency slot", this, offset, queue.length);
124 LOG.debug("Queue {} offset {}/{}, not allowing reservation", this, offset, queue.length);
129 final Long xid = baseXid + offset;
130 LOG.debug("Queue {} allocated XID {} at offset {}", this, xid, offset);
134 Long reserveBarrierIfNeeded() {
135 final int bo = barrierOffset;
136 if (bo >= flushOffset) {
137 LOG.debug("Barrier found at offset {} (currently at {})", bo, flushOffset);
140 return reserveEntry(true);
144 int startShutdown() {
145 // Increment the offset by the queue size, hence preventing any normal
146 // allocations. We should not be seeing a barrier reservation after this
147 // and if there is one issued, we can disregard it.
148 final int offset = CURRENT_OFFSET_UPDATER.getAndAdd(this, queue.length);
150 // If this offset is larger than reserve, trim it. That is not an accurate
151 // view of which slot was actually "reserved", but it indicates at which
152 // entry we can declare the queue flushed (e.g. at the emergency slot).
153 return offset > reserve ? reserve : offset;
156 boolean isShutdown(final int offset) {
157 // This queue is shutdown if the flushOffset (e.g. the next entry to
158 // be flushed) points to the offset 'reserved' in startShutdown()
159 return flushOffset >= offset;
163 * An empty queue is a queue which has no further unflushed entries.
165 * @return True if this queue does not have unprocessed entries.
167 private boolean isEmpty() {
168 int ro = reserveOffset;
170 if (queue[reserve].isCommitted()) {
177 LOG.debug("Effective flush/reserve offset {}/{}", flushOffset, ro);
178 return ro <= flushOffset;
182 * A queue is finished when all of its entries have been completed.
184 * @return False if there are any uncompleted requests.
186 boolean isFinished() {
187 if (completeCount < reserve) {
191 // We need to check if the last entry was used
192 final OutboundQueueEntry last = queue[reserve];
193 return !last.isCommitted() || last.isCompleted();
196 boolean isFlushed() {
197 LOG.debug("Check queue {} for completeness (offset {}, reserve {})", this, flushOffset, reserve);
198 if (flushOffset < reserve) {
202 // flushOffset implied == reserve
203 return flushOffset >= queue.length || !queue[reserve].isCommitted();
206 boolean needsFlush() {
207 if (flushOffset < reserve) {
208 return queue[flushOffset].isCommitted();
212 LOG.trace("Queue {} is flushed, schedule a replace", this);
216 LOG.trace("Queue {} is finished, schedule a cleanup", this);
223 OfHeader flushEntry() {
227 LOG.trace("Flushed all reserved entries up to {}", flushOffset);
231 final OutboundQueueEntry entry = queue[flushOffset];
232 if (!entry.isCommitted()) {
233 LOG.trace("Request at offset {} not ready yet, giving up", flushOffset);
237 final OfHeader msg = entry.takeMessage();
243 LOG.trace("Null message, skipping to offset {}", flushOffset);
247 // Argument is 'long' to explicitly convert before performing operations
248 private boolean xidInRange(final long xid) {
249 return xid < endXid && (xid >= baseXid || baseXid > endXid);
252 private static boolean completeEntry(final OutboundQueueEntry entry, final OfHeader response) {
253 if (response instanceof Error) {
254 final Error err = (Error)response;
255 LOG.debug("Device-reported request XID {} failed {}:{}", response.getXid(), err.getTypeString(), err.getCodeString());
256 entry.fail(new DeviceRequestFailedException("Device-side failure", err));
259 return entry.complete(response);
264 * Return the request entry corresponding to a response. Returns null
265 * if there is no request matching the response.
267 * @param response Response message
268 * @return Matching request entry, or null if no match is found.
270 OutboundQueueEntry pairRequest(@Nonnull final OfHeader response) {
271 final Long xid = response.getXid();
272 if (!xidInRange(xid)) {
273 LOG.debug("Queue {} {}/{} ignoring XID {}", this, baseXid, queue.length, xid);
277 final int offset = (int)(xid - baseXid);
278 final OutboundQueueEntry entry = queue[offset];
279 if (entry.isCompleted()) {
280 LOG.debug("Entry {} already is completed, not accepting response {}", entry, response);
284 if (entry.isBarrier()) {
285 // This has been a barrier -- make sure we complete all preceding requests.
286 // XXX: Barriers are expected to complete in one message.
287 // If this assumption is changed, this logic will need to be expanded
288 // to ensure that the requests implied by the barrier are reported as
289 // completed *after* the barrier.
290 LOG.trace("Barrier XID {} completed, cascading completion to XIDs {} to {}", xid, baseXid + lastBarrierOffset + 1, xid - 1);
291 completeRequests(offset);
292 lastBarrierOffset = offset;
294 final boolean success = completeEntry(entry, response);
295 Verify.verify(success, "Barrier request failed to complete");
297 } else if (completeEntry(entry, response)) {
304 private void completeRequests(final int toOffset) {
305 for (int i = lastBarrierOffset + 1; i < toOffset; ++i) {
306 final OutboundQueueEntry entry = queue[i];
307 if (!entry.isCompleted() && entry.complete(null)) {
314 completeRequests(queue.length);
317 int failAll(final OutboundQueueException cause) {
319 for (int i = lastBarrierOffset + 1; i < queue.length; ++i) {
320 final OutboundQueueEntry entry = queue[i];
321 if (!entry.isCommitted()) {
325 if (!entry.isCompleted()) {