X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fcds-access-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Faccess%2Fclient%2FAbstractClientConnection.java;h=d37893bd7c3a9862ad89fb9d88b94337d0355f13;hb=e1c283de301355cb8fa3f7d4fa28a6dd0af501eb;hp=28d8a1b42228690177ae9498e7c77dcf518558ce;hpb=bab94bcc7f46edf23ef666dbcd389f7b5ea1ca0a;p=controller.git diff --git a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/AbstractClientConnection.java b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/AbstractClientConnection.java index 28d8a1b422..d37893bd7c 100644 --- a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/AbstractClientConnection.java +++ b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/AbstractClientConnection.java @@ -9,6 +9,8 @@ package org.opendaylight.controller.cluster.access.client; import akka.actor.ActorRef; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Preconditions; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.Optional; @@ -85,6 +87,10 @@ public abstract class AbstractClientConnection { return context.self(); } + public final long currentTime() { + return context.ticker().read(); + } + /** * Send a request to the backend and invoke a specified callback when it finishes. This method is safe to invoke * from any thread. @@ -96,13 +102,31 @@ public abstract class AbstractClientConnection { * @param callback Callback to invoke */ public final void sendRequest(final Request request, final Consumer> callback) { - final RequestException maybePoison = poisoned; - if (maybePoison != null) { - throw new IllegalStateException("Connection " + this + " has been poisoned", maybePoison); + final long now = currentTime(); + final long delay = enqueueEntry(new ConnectionEntry(request, callback, now), now); + try { + TimeUnit.NANOSECONDS.sleep(delay); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOG.debug("Interrupted after sleeping {}ns", e, currentTime() - now); } + } - final ConnectionEntry entry = new ConnectionEntry(request, callback, readTime()); - enqueueAndWait(entry, entry.getEnqueuedTicks()); + /** + * Send a request to the backend and invoke a specified callback when it finishes. This method is safe to invoke + * from any thread. + * + *

+ * Note that unlike {@link #sendRequest(Request, Consumer)}, this method does not exert backpressure, hence it + * should never be called from an application thread. + * + * @param request Request to send + * @param callback Callback to invoke + * @param enqueuedTicks Time (according to {@link #currentTime()} of request enqueue + */ + public final void enqueueRequest(final Request request, final Consumer> callback, + final long enqueuedTicks) { + enqueueEntry(new ConnectionEntry(request, callback, enqueuedTicks), currentTime()); } public abstract Optional getBackendInfo(); @@ -120,19 +144,20 @@ public abstract class AbstractClientConnection { @GuardedBy("lock") final void setForwarder(final ReconnectForwarder forwarder) { - queue.setForwarder(forwarder, readTime()); + queue.setForwarder(forwarder, currentTime()); } @GuardedBy("lock") abstract ClientActorBehavior lockedReconnect(ClientActorBehavior current); - private long readTime() { - return context.ticker().read(); - } - final long enqueueEntry(final ConnectionEntry entry, final long now) { lock.lock(); try { + final RequestException maybePoison = poisoned; + if (maybePoison != null) { + throw new IllegalStateException("Connection " + this + " has been poisoned", maybePoison); + } + if (queue.isEmpty()) { // The queue is becoming non-empty, schedule a timer scheduleTimer(REQUEST_TIMEOUT_DURATION); @@ -143,15 +168,6 @@ public abstract class AbstractClientConnection { } } - final void enqueueAndWait(final ConnectionEntry entry, final long now) { - final long delay = enqueueEntry(entry, now); - try { - TimeUnit.NANOSECONDS.sleep(delay); - } catch (InterruptedException e) { - LOG.debug("Interrupted while sleeping", e); - } - } - final ClientActorBehavior reconnect(final ClientActorBehavior current) { lock.lock(); try { @@ -195,7 +211,7 @@ public abstract class AbstractClientConnection { lock.lock(); try { haveTimer = false; - final long now = readTime(); + final long now = currentTime(); // The following line is only reliable when queue is not forwarding, but such state should not last long. final long ticksSinceProgress = queue.ticksStalling(now); if (ticksSinceProgress >= NO_PROGRESS_TIMEOUT_NANOS) { @@ -282,7 +298,7 @@ public abstract class AbstractClientConnection { } final void receiveResponse(final ResponseEnvelope envelope) { - final long now = readTime(); + final long now = currentTime(); final Optional maybeEntry; lock.lock(); @@ -298,4 +314,13 @@ public abstract class AbstractClientConnection { entry.complete(envelope.getMessage()); } } + + @Override + public final String toString() { + return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString(); + } + + ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { + return toStringHelper.add("client", context.getIdentifier()).add("cookie", cookie).add("poisoned", poisoned); + } }