BUG-5280: fix problems identified by integration tests
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / ConnectedClientConnection.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.access.client;
9
10 import akka.actor.ActorRef;
11 import com.google.common.annotations.Beta;
12 import javax.annotation.concurrent.NotThreadSafe;
13 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 @Beta
18 @NotThreadSafe
19 public final class ConnectedClientConnection<T extends BackendInfo> extends AbstractReceivingClientConnection<T> {
20     private static final Logger LOG = LoggerFactory.getLogger(ConnectedClientConnection.class);
21
22     private long nextTxSequence;
23
24     public ConnectedClientConnection(final ClientActorContext context, final Long cookie, final T backend) {
25         super(context, cookie, backend);
26     }
27
28     private void transmit(final ConnectionEntry entry) {
29         final long txSequence = nextTxSequence++;
30
31         final RequestEnvelope toSend = new RequestEnvelope(entry.getRequest().toVersion(remoteVersion()), sessionId(),
32             txSequence);
33
34         // We need to enqueue the request before we send it to the actor, as we may be executing on a different thread
35         // than the client actor thread, in which case the round-trip could be made faster than we can enqueue --
36         // in which case the receive routine would not find the entry.
37         final TransmittedConnectionEntry txEntry = new TransmittedConnectionEntry(entry, sessionId(), txSequence,
38             readTime());
39         appendToInflight(txEntry);
40
41         final ActorRef actor = remoteActor();
42         LOG.trace("Transmitting request {} as {} to {}", entry.getRequest(), toSend, actor);
43         actor.tell(toSend, ActorRef.noSender());
44     }
45
46     @Override
47     void enqueueEntry(final ConnectionEntry entry) {
48         if (inflightSize() < remoteMaxMessages()) {
49             transmit(entry);
50             LOG.debug("Enqueued request {} to queue {}", entry.getRequest(), this);
51         } else {
52             LOG.debug("Queue is at capacity, delayed sending of request {}", entry.getRequest());
53             super.enqueueEntry(entry);
54         }
55     }
56
57     @Override
58     void sendMessages(final int count) {
59         int toSend = count;
60
61         while (toSend > 0) {
62             final ConnectionEntry e = dequeEntry();
63             if (e == null) {
64                 break;
65             }
66
67             LOG.debug("Transmitting entry {}", e);
68             transmit(e);
69             toSend--;
70         }
71     }
72
73     @Override
74     ClientActorBehavior<T> reconnectConnection(final ClientActorBehavior<T> current) {
75         final ReconnectingClientConnection<T> next = new ReconnectingClientConnection<>(this);
76         setForwarder(new SimpleReconnectForwarder(next));
77         current.reconnectConnection(this, next);
78         return current;
79     }
80 }