|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal.retry; |
| 20 | + |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.concurrent.ThreadLocalRandom; |
| 23 | + |
| 24 | +import org.neo4j.driver.internal.util.Clock; |
| 25 | +import org.neo4j.driver.v1.exceptions.ServiceUnavailableException; |
| 26 | +import org.neo4j.driver.v1.exceptions.SessionExpiredException; |
| 27 | +import org.neo4j.driver.v1.exceptions.TransientException; |
| 28 | + |
| 29 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 30 | + |
| 31 | +public class ExponentialBackoff implements RetryLogic<ExponentialBackoffDecision> |
| 32 | +{ |
| 33 | + public static final long DEFAULT_MAX_RETRY_TIME_MS = SECONDS.toMillis( 30 ); |
| 34 | + |
| 35 | + private static final long INITIAL_RETRY_DELAY_MS = SECONDS.toMillis( 1 ); |
| 36 | + private static final double RETRY_DELAY_MULTIPLIER = 2.0; |
| 37 | + private static final double RETRY_DELAY_JITTER_FACTOR = 0.2; |
| 38 | + |
| 39 | + private final long maxRetryTimeMs; |
| 40 | + private final long initialRetryDelayMs; |
| 41 | + private final double multiplier; |
| 42 | + private final double jitterFactor; |
| 43 | + private final Clock clock; |
| 44 | + |
| 45 | + ExponentialBackoff( long maxRetryTimeMs, long initialRetryDelayMs, double multiplier, double jitterFactor, |
| 46 | + Clock clock ) |
| 47 | + { |
| 48 | + this.maxRetryTimeMs = maxRetryTimeMs; |
| 49 | + this.initialRetryDelayMs = initialRetryDelayMs; |
| 50 | + this.multiplier = multiplier; |
| 51 | + this.jitterFactor = jitterFactor; |
| 52 | + this.clock = clock; |
| 53 | + |
| 54 | + verifyAfterConstruction(); |
| 55 | + } |
| 56 | + |
| 57 | + public static RetryLogic<RetryDecision> defaultRetryLogic() |
| 58 | + { |
| 59 | + return create( RetrySettings.DEFAULT, Clock.SYSTEM ); |
| 60 | + } |
| 61 | + |
| 62 | + public static RetryLogic<RetryDecision> noRetries() |
| 63 | + { |
| 64 | + return create( new RetrySettings( 0 ), Clock.SYSTEM ); |
| 65 | + } |
| 66 | + |
| 67 | + @SuppressWarnings( "unchecked" ) |
| 68 | + public static RetryLogic<RetryDecision> create( RetrySettings settings, Clock clock ) |
| 69 | + { |
| 70 | + return (RetryLogic) new ExponentialBackoff( settings.maxRetryTimeMs(), INITIAL_RETRY_DELAY_MS, |
| 71 | + RETRY_DELAY_MULTIPLIER, RETRY_DELAY_JITTER_FACTOR, clock ); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public ExponentialBackoffDecision apply( Throwable error, ExponentialBackoffDecision previousDecision ) |
| 76 | + { |
| 77 | + Objects.requireNonNull( error ); |
| 78 | + ExponentialBackoffDecision decision = decision( previousDecision ); |
| 79 | + |
| 80 | + long elapsedTimeMs = clock.millis() - decision.startTimestamp(); |
| 81 | + if ( elapsedTimeMs > maxRetryTimeMs || !canRetryOn( error ) ) |
| 82 | + { |
| 83 | + return decision.stopRetrying(); |
| 84 | + } |
| 85 | + |
| 86 | + long delayWithJitterMs = computeDelayWithJitter( decision.delay() ); |
| 87 | + sleep( delayWithJitterMs ); |
| 88 | + |
| 89 | + long nextDelayWithoutJitterMs = (long) (decision.delay() * multiplier); |
| 90 | + return decision.withDelay( nextDelayWithoutJitterMs ); |
| 91 | + } |
| 92 | + |
| 93 | + private ExponentialBackoffDecision decision( ExponentialBackoffDecision previous ) |
| 94 | + { |
| 95 | + return previous == null ? new ExponentialBackoffDecision( clock.millis(), initialRetryDelayMs ) : previous; |
| 96 | + } |
| 97 | + |
| 98 | + private long computeDelayWithJitter( long delayMs ) |
| 99 | + { |
| 100 | + long jitter = (long) (delayMs * jitterFactor); |
| 101 | + long min = delayMs - jitter; |
| 102 | + long max = delayMs + jitter; |
| 103 | + if ( max < 0 ) |
| 104 | + { |
| 105 | + // overflow detected, truncate min and max values |
| 106 | + min -= 1; |
| 107 | + max = min; |
| 108 | + } |
| 109 | + return ThreadLocalRandom.current().nextLong( min, max + 1 ); |
| 110 | + } |
| 111 | + |
| 112 | + private void sleep( long delayMs ) |
| 113 | + { |
| 114 | + try |
| 115 | + { |
| 116 | + clock.sleep( delayMs ); |
| 117 | + } |
| 118 | + catch ( InterruptedException e ) |
| 119 | + { |
| 120 | + Thread.currentThread().interrupt(); |
| 121 | + throw new IllegalStateException( "Retries interrupted", e ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void verifyAfterConstruction() |
| 126 | + { |
| 127 | + if ( maxRetryTimeMs < 0 ) |
| 128 | + { |
| 129 | + throw new IllegalArgumentException( "Max retry time should be >= 0: " + maxRetryTimeMs ); |
| 130 | + } |
| 131 | + if ( initialRetryDelayMs < 0 ) |
| 132 | + { |
| 133 | + throw new IllegalArgumentException( "Initial retry delay should >= 0: " + initialRetryDelayMs ); |
| 134 | + } |
| 135 | + if ( multiplier < 1.0 ) |
| 136 | + { |
| 137 | + throw new IllegalArgumentException( "Multiplier should be >= 1.0: " + multiplier ); |
| 138 | + } |
| 139 | + if ( jitterFactor < 0 || jitterFactor > 1 ) |
| 140 | + { |
| 141 | + throw new IllegalArgumentException( "Jitter factor should be in [0.0, 1.0]: " + jitterFactor ); |
| 142 | + } |
| 143 | + if ( clock == null ) |
| 144 | + { |
| 145 | + throw new IllegalArgumentException( "Clock should not be null" ); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static boolean canRetryOn( Throwable error ) |
| 150 | + { |
| 151 | + return error instanceof SessionExpiredException || |
| 152 | + error instanceof ServiceUnavailableException || |
| 153 | + error instanceof TransientException; |
| 154 | + } |
| 155 | +} |
0 commit comments