2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.framework;
10 import io.netty.util.concurrent.EventExecutor;
11 import io.netty.util.concurrent.Future;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
17 import javax.annotation.concurrent.GuardedBy;
18 import javax.annotation.concurrent.ThreadSafe;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.common.base.Preconditions;
26 * Swiss army knife equivalent for reconnect strategies.
28 * This strategy continues to schedule reconnect attempts, each having to complete in a fixed time (connectTime).
30 * Initial sleep time is specified as minSleep. Each subsequent unsuccessful attempt multiplies this time by a constant
31 * factor (sleepFactor) -- this allows for either constant reconnect times (sleepFactor = 1), or various degrees of
32 * exponential back-off (sleepFactor > 1). Maximum sleep time between attempts can be capped to a specific value
35 * The strategy can optionally give up based on two criteria:
37 * A preset number of connection retries (maxAttempts) has been reached, or
39 * A preset absolute deadline is reached (deadline nanoseconds, as reported by System.nanoTime(). In this specific case,
40 * both connectTime and maxSleep will be controlled such that the connection attempt is resolved as closely to the
41 * deadline as possible.
43 * Both these caps can be combined, with the strategy giving up as soon as the first one is reached.
47 public final class TimedReconnectStrategy implements ReconnectStrategy {
48 private static final Logger LOG = LoggerFactory.getLogger(TimedReconnectStrategy.class);
49 private final EventExecutor executor;
50 private final Long deadline, maxAttempts, maxSleep;
51 private final double sleepFactor;
52 private final int connectTime;
53 private final long minSleep;
56 private long attempts;
59 private long lastSleep;
62 private boolean scheduled;
64 public TimedReconnectStrategy(final EventExecutor executor, final int connectTime, final long minSleep, final double sleepFactor,
65 final Long maxSleep, final Long maxAttempts, final Long deadline) {
66 Preconditions.checkArgument(maxSleep == null || minSleep <= maxSleep);
67 Preconditions.checkArgument(sleepFactor >= 1);
68 Preconditions.checkArgument(connectTime >= 0);
69 this.executor = Preconditions.checkNotNull(executor);
70 this.deadline = deadline;
71 this.maxAttempts = maxAttempts;
72 this.minSleep = minSleep;
73 this.maxSleep = maxSleep;
74 this.sleepFactor = sleepFactor;
75 this.connectTime = connectTime;
79 public synchronized Future<Void> scheduleReconnect(final Throwable cause) {
80 LOG.debug("Connection attempt failed", cause);
82 // Check if a reconnect attempt is scheduled
83 Preconditions.checkState(!this.scheduled);
85 // Get a stable 'now' time for deadline calculations
86 final long now = System.nanoTime();
88 // Obvious stop conditions
89 if (this.maxAttempts != null && this.attempts >= this.maxAttempts) {
90 return this.executor.newFailedFuture(new Throwable("Maximum reconnection attempts reached"));
92 if (this.deadline != null && this.deadline <= now) {
93 return this.executor.newFailedFuture(new TimeoutException("Reconnect deadline reached"));
97 * First connection attempt gets initialized to minimum sleep,
98 * each subsequent is exponentially backed off by sleepFactor.
100 if (this.attempts != 0) {
101 this.lastSleep *= this.sleepFactor;
103 this.lastSleep = this.minSleep;
106 // Cap the sleep time to maxSleep
107 if (this.maxSleep != null && this.lastSleep > this.maxSleep) {
108 LOG.debug("Capped sleep time from {} to {}", this.lastSleep, this.maxSleep);
109 this.lastSleep = this.maxSleep;
114 // Check if the reconnect attempt is within the deadline
115 if (this.deadline != null && this.deadline <= now + TimeUnit.MILLISECONDS.toNanos(this.lastSleep)) {
116 return this.executor.newFailedFuture(new TimeoutException("Next reconnect would happen after deadline"));
119 LOG.debug("Connection attempt {} sleeping for {} milliseconds", this.attempts, this.lastSleep);
121 // If we are not sleeping at all, return an already-succeeded future
122 if (this.lastSleep == 0) {
123 return this.executor.newSucceededFuture(null);
126 // Need to retain a final reference to this for locking purposes,
127 // also set the scheduled flag.
128 final Object lock = this;
129 this.scheduled = true;
131 // Schedule a task for the right time. It will also clear the flag.
132 return this.executor.schedule(new Callable<Void>() {
134 public Void call() throws TimeoutException {
135 synchronized (lock) {
136 Preconditions.checkState(TimedReconnectStrategy.this.scheduled);
137 TimedReconnectStrategy.this.scheduled = false;
142 }, this.lastSleep, TimeUnit.MILLISECONDS);
146 public synchronized void reconnectSuccessful() {
147 Preconditions.checkState(!this.scheduled);
152 public int getConnectTimeout() throws TimeoutException {
153 int timeout = this.connectTime;
155 if (this.deadline != null) {
157 // If there is a deadline, we may need to cap the connect
158 // timeout to meet the deadline.
159 final long now = System.nanoTime();
160 if (now >= this.deadline) {
161 throw new TimeoutException("Reconnect deadline already passed");
164 final long left = TimeUnit.NANOSECONDS.toMillis(this.deadline - now);
166 throw new TimeoutException("Connect timeout too close to deadline");
171 * - if time left is less than the timeout, set it directly
172 * - if there is no timeout, and time left is:
173 * - less than maximum integer, set timeout to time left
174 * - more than maximum integer, set timeout Integer.MAX_VALUE
176 if (timeout > left) {
177 timeout = (int) left;
178 } else if (timeout == 0) {
179 timeout = left <= Integer.MAX_VALUE ? (int) left : Integer.MAX_VALUE;