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