a27470db8959455182c8705699464e142ff2315d
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / AbstractReceivingClientConnection.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 com.google.common.base.MoreObjects.ToStringHelper;
11 import com.google.common.base.Preconditions;
12 import java.util.Optional;
13
14 /**
15  * Implementation-internal intermediate subclass between {@link AbstractClientConnection} and two-out of three of its
16  * subclasses. It allows us to share some code.
17  *
18  * @author Robert Varga
19  *
20  * @param <T> Concrete {@link BackendInfo} type
21  */
22 abstract class AbstractReceivingClientConnection<T extends BackendInfo> extends AbstractClientConnection<T> {
23     /**
24      * Multiplication factor applied to remote's advertised limit on outstanding messages. Our default strategy
25      * rate-limiting strategy in {@link AveragingProgressTracker} does not penalize threads as long as we have not
26      * reached half of the target.
27      *
28      * <p>
29      * By multiplying the advertised maximum by four, our queue steady-state should end up with:
30      * - the backend pipeline being full,
31      * - another full batch of messages being in the queue while not paying any throttling cost
32      * - another 2 full batches of messages with incremental throttling cost
33      */
34     private static final int MESSAGE_QUEUE_FACTOR = 4;
35
36     private final T backend;
37
38     AbstractReceivingClientConnection(final ClientActorContext context, final Long cookie, final T backend) {
39         super(context, cookie, new TransmitQueue.Transmitting(targetQueueSize(backend), backend));
40         this.backend = Preconditions.checkNotNull(backend);
41     }
42
43     AbstractReceivingClientConnection(final AbstractReceivingClientConnection<T> oldConnection) {
44         super(oldConnection, targetQueueSize(oldConnection.backend));
45         this.backend = oldConnection.backend;
46     }
47
48     private static int targetQueueSize(final BackendInfo backend) {
49         return backend.getMaxMessages() * MESSAGE_QUEUE_FACTOR;
50     }
51
52     @Override
53     public final Optional<T> getBackendInfo() {
54         return Optional.of(backend);
55     }
56
57     final T backend() {
58         return backend;
59     }
60
61     @Override
62     ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
63         return super.addToStringAttributes(toStringHelper).add("backend", backend);
64     }
65 }