EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.http.handlers]

COVERAGE SUMMARY FOR SOURCE FILE [BackoffLimitedRetryHandler.java]

nameclass, %method, %block, %line, %
BackoffLimitedRetryHandler.java100% (1/1)88%  (7/8)87%  (131/151)82%  (23/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BackoffLimitedRetryHandler100% (1/1)88%  (7/8)87%  (131/151)82%  (23/28)
shouldRetryRequest (HttpCommand, IOException): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
ifReplayableBackoffAndReturnTrue (HttpCommand): boolean 100% (1/1)79%  (44/56)78%  (7/9)
imposeBackoffExponentialDelay (long, long, int, int, int, String): void 100% (1/1)92%  (47/51)75%  (6/8)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
BackoffLimitedRetryHandler (): void 100% (1/1)100% (9/9)100% (3/3)
imposeBackoffExponentialDelay (int, String): void 100% (1/1)100% (9/9)100% (2/2)
imposeBackoffExponentialDelay (long, int, int, int, String): void 100% (1/1)100% (11/11)100% (2/2)
shouldRetryRequest (HttpCommand, HttpResponse): boolean 100% (1/1)100% (6/6)100% (2/2)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.http.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.io.IOException;
24 
25import javax.annotation.Resource;
26import javax.inject.Named;
27import javax.inject.Singleton;
28 
29import org.jclouds.Constants;
30import org.jclouds.http.HttpCommand;
31import org.jclouds.http.HttpResponse;
32import org.jclouds.http.HttpRetryHandler;
33import org.jclouds.http.IOExceptionRetryHandler;
34import org.jclouds.http.TransformingHttpCommand;
35import org.jclouds.logging.Logger;
36 
37import com.google.common.base.Throwables;
38import com.google.inject.Inject;
39 
40/**
41 * Allow replayable request to be retried a limited number of times, and impose an exponential
42 * back-off delay before returning.
43 * <p>
44 * The back-off delay grows rapidly according to the formula
45 * <code>50 * (<i>{@link TransformingHttpCommand#getFailureCount()}</i> ^ 2)</code>. For example:
46 * <table>
47 * <tr>
48 * <th>Number of Attempts</th>
49 * <th>Delay in milliseconds</th>
50 * </tr>
51 * <tr>
52 * <td>1</td>
53 * <td>50</td>
54 * </tr>
55 * <tr>
56 * <td>2</td>
57 * <td>200</td>
58 * </tr>
59 * <tr>
60 * <td>3</td>
61 * <td>450</td>
62 * </tr>
63 * <tr>
64 * <td>4</td>
65 * <td>800</td>
66 * </tr>
67 * <tr>
68 * <td>5</td>
69 * <td>1250</td>
70 * </tr>
71 * </table>
72 * <p>
73 * This implementation has two side-effects. It increments the command's failure count with
74 * {@link TransformingHttpCommand#incrementFailureCount()}, because this failure count value is used
75 * to determine how many times the command has already been tried. It also closes the response's
76 * content input stream to ensure connections are cleaned up.
77 * 
78 * @author James Murty
79 */
80@Singleton
81public class BackoffLimitedRetryHandler implements HttpRetryHandler, IOExceptionRetryHandler {
82 
83   public static BackoffLimitedRetryHandler INSTANCE = new BackoffLimitedRetryHandler();
84 
85   @Inject(optional = true)
86   @Named(Constants.PROPERTY_MAX_RETRIES)
87   private int retryCountLimit = 5;
88 
89   @Resource
90   protected Logger logger = Logger.NULL;
91 
92   public boolean shouldRetryRequest(HttpCommand command, IOException error) {
93      return ifReplayableBackoffAndReturnTrue(command);
94   }
95 
96   public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
97      releasePayload(response);
98      return ifReplayableBackoffAndReturnTrue(command);
99   }
100 
101   private boolean ifReplayableBackoffAndReturnTrue(HttpCommand command) {
102      command.incrementFailureCount();
103 
104      if (!command.isReplayable()) {
105         logger.warn("Cannot retry after server error, command is not replayable: %1$s", command);
106         return false;
107      } else if (command.getFailureCount() > retryCountLimit) {
108         logger.warn("Cannot retry after server error, command has exceeded retry limit %1$d: %2$s", retryCountLimit,
109                  command);
110         return false;
111      } else {
112         imposeBackoffExponentialDelay(command.getFailureCount(), "server error: " + command.toString());
113         return true;
114      }
115   }
116 
117   public void imposeBackoffExponentialDelay(int failureCount, String commandDescription) {
118      imposeBackoffExponentialDelay(50L, 2, failureCount, retryCountLimit, commandDescription);
119   }
120 
121   public void imposeBackoffExponentialDelay(long period, int pow, int failureCount, int max, String commandDescription) {
122      imposeBackoffExponentialDelay(period, period * 10l, pow, failureCount, max, commandDescription);
123   }
124 
125   public void imposeBackoffExponentialDelay(long period, long maxPeriod, int pow, int failureCount, int max,
126            String commandDescription) {
127      long delayMs = (long) (period * Math.pow(failureCount, pow));
128      delayMs = delayMs > maxPeriod ? maxPeriod : delayMs;
129      logger.debug("Retry %d/%d: delaying for %d ms: %s", failureCount, max, delayMs, commandDescription);
130      try {
131         Thread.sleep(delayMs);
132      } catch (InterruptedException e) {
133         Throwables.propagate(e);
134      }
135   }
136 
137}

[all classes][org.jclouds.http.handlers]
EMMA 2.0.5312 (C) Vladimir Roubtsov