/*******************************************************************************
 * Copyright 2011 Netflix
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.netflix.astyanax.retry;

import java.util.Random;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Unbounded exponential backoff will sleep a random number of intervals within an
 * exponentially increasing number of intervals.  
 * 
 * @author elandau
 *
 */
public class ExponentialBackoff extends SleepingRetryPolicy {

    private final Random random = new Random();
    private final int baseSleepTimeMs;

    public ExponentialBackoff(int baseSleepTimeMs, int maxAttempts) {
        super(maxAttempts);
        this.baseSleepTimeMs = baseSleepTimeMs;
    }

    @Override
    public long getSleepTimeMs() {
        return baseSleepTimeMs * Math.max(1, random.nextInt(1 << (this.getAttemptCount() + 1)));
    }

    public int getBaseSleepTimeMs() {
        return baseSleepTimeMs;
    }
    
    @Override
    public RetryPolicy duplicate() {
        return new ExponentialBackoff(baseSleepTimeMs, getMaxAttemptCount());
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

@@ -1090,16 +1090,58 @@
 olicy %7B%0A
+    private final int MAX_SHIFT = 30;%0A    
 %0A    pri
@@ -1415,32 +1415,160 @@
 SleepTimeMs() %7B%0A
+        int attempt = this.getAttemptCount() + 1;%0A        if (attempt %3E MAX_SHIFT) %7B%0A            attempt = MAX_SHIFT;%0A        %7D%0A
         return b
@@ -1620,36 +1620,15 @@
  %3C%3C 
-(this.getAttemptCount() + 1)
+attempt
 ));%0A
@@ -1625,32 +1625,36 @@
 ttempt));%0A    %7D%0A
+    
 %0A    public int 

Fix exponential backoff overflow