Bug 8619: Introduce inheritance of progress trackers
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / AveragingProgressTracker.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
9 package org.opendaylight.controller.cluster.access.client;
10
11 import java.util.concurrent.TimeUnit;
12 import javax.annotation.concurrent.NotThreadSafe;
13
14 /**
15  * A ProgressTracker subclass which uses {@code ticksWorkedPerClosedTask} to compute delays.
16  *
17  * <p>This class has {@code tasksOpenLimit} used as a (weak) limit,
18  * as number of open tasks approaches that value, delays computed are increasing.
19  *
20  * <p>In order to keep {@code estimateIsolatedDelay} values from raising unreasonably high,
21  * {@code defaultTicksPerTask} acts as a maximal value. {@code openTask} may return
22  * higher value if there are tasks above the limit.
23  *
24  * <p>On the other hand, there is no delay when number of open tasks is half the limit or less,
25  * in order to prevent backend from running out of tasks while there may be waiting frontend threads.
26  *
27  * @author Vratko Polak
28  */
29 @NotThreadSafe
30 final class AveragingProgressTracker extends ProgressTracker {
31     private static final long DEFAULT_TICKS_PER_TASK = TimeUnit.MILLISECONDS.toNanos(500);
32
33     /**
34      * The implementation will avoid having more that this number of tasks open.
35      */
36     private final long tasksOpenLimit;
37
38     /**
39      * We do not delay tasks until their count hits this threshold.
40      */
41     private final long noDelayThreshold;
42
43     /**
44      * Create an idle tracker with limit and specified ticks per task value to use as default.
45      *
46      * @param limit of open tasks to avoid exceeding
47      * @param ticksPerTask value to use as default
48      */
49     private AveragingProgressTracker(final long limit, final long ticksPerTask) {
50         super(ticksPerTask);
51         tasksOpenLimit = limit;
52         noDelayThreshold = limit / 2;
53     }
54
55     /**
56      * Create a default idle tracker with given limit.
57      *
58      * @param limit of open tasks to avoid exceeding
59      */
60     AveragingProgressTracker(final long limit) {
61         this(limit, DEFAULT_TICKS_PER_TASK);
62     }
63
64     /**
65      * Construct a new tracker suitable for a new task queue related to a "reconnect".
66      *
67      * <p>The limit is set independently of the old tracker.
68      *
69      * @param oldTracker the tracker used for the previously used backend
70      * @param limit of open tasks to avoid exceeding
71      * @param now tick number corresponding to caller's present
72      */
73     AveragingProgressTracker(final ProgressTracker oldTracker, final long limit, final long now) {
74         super(oldTracker, now);
75         tasksOpenLimit = limit;
76         noDelayThreshold = limit / 2;
77     }
78
79     /**
80      * Construct a new tracker suitable for a new task queue related to a "reconnect".
81      *
82      * <p>The limit is copied from the old tracker.
83      *
84      * @param oldTracker the tracker used for the previously used backend
85      * @param now tick number corresponding to caller's present
86      */
87     AveragingProgressTracker(final AveragingProgressTracker oldTracker, final long now) {
88         this(oldTracker, oldTracker.tasksOpenLimit, now);
89     }
90
91     // Protected read-only methods
92
93     /**
94      * Give an estimate of a fair delay, assuming delays caused by other opened tasks are ignored.
95      *
96      * <p>This implementation returns zero delay if number of open tasks is half of limit or less.
97      * Else the delay is computed, aiming to keep number of open tasks at 3/4 of limit,
98      * assuming backend throughput remains constant.
99      *
100      * <p>As the number of open tasks approaches the limit,
101      * the computed delay increases, but it never exceeds defaultTicksPerTask.
102      * That means the actual number of open tasks can exceed the limit.
103      *
104      * @param now tick number corresponding to caller's present
105      * @return delay (in ticks) after which another openTask() would be fair to be called by the same thread again
106      */
107     @Override
108     protected long estimateIsolatedDelay(final long now) {
109         final long open = tasksOpen();
110         if (open <= noDelayThreshold) {
111             return 0L;
112         }
113         if (open >= tasksOpenLimit) {
114             return defaultTicksPerTask();
115         }
116
117         /*
118          * Calculate the task capacity relative to the limit on open tasks. In real terms this value can be
119          * in the open interval (0.0, 0.5).
120          */
121         final double relativeRemainingCapacity = 1.0 - (double) open / tasksOpenLimit;
122
123         /*
124          * Calculate delay coefficient. It increases in inverse proportion to relative remaining capacity, approaching
125          * infinity as remaining capacity approaches 0.0.
126          */
127         final double delayCoefficient = (0.5 - relativeRemainingCapacity) / relativeRemainingCapacity;
128         final long delay = (long) (ticksWorkedPerClosedTask(now) * delayCoefficient);
129
130         /*
131          * Cap the result to defaultTicksPerTask, since the calculated delay may overstep it.
132          */
133         return Math.min(delay, defaultTicksPerTask());
134     }
135 }