From 74bfec411223bdfdd0d60aa912d420d9a4447375 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Wed, 13 Nov 2019 07:12:05 -0800 Subject: [PATCH] Move KetchSystem.delay to FileUtils. This will provide exponential backoff with jitter to other JGit components too. Signed-off-by: Han-Wen Nienhuys Change-Id: Idd44e3bbaef6d71134ce2e3f7d405f35e7397cbd --- .../jgit/internal/ketch/KetchReplica.java | 6 ++--- .../jgit/internal/ketch/KetchSystem.java | 21 --------------- .../src/org/eclipse/jgit/util/FileUtils.java | 27 +++++++++++++++++++ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java index 0e8377dd0..52c8f29dd 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java @@ -77,6 +77,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.transport.ReceiveCommand; import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.SystemReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -532,9 +533,8 @@ public abstract class KetchReplica { queued.add(0, new ReplicaPushRequest(this, cmds)); if (!waitingForRetry()) { - long delay = KetchSystem.delay( - lastRetryMillis, - minRetryMillis, maxRetryMillis); + long delay = FileUtils + .delay(lastRetryMillis, minRetryMillis, maxRetryMillis); if (log.isDebugEnabled()) { log.debug("Retrying {} after {} ms", //$NON-NLS-1$ describeForLog(), Long.valueOf(delay)); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java index d1d4f67d8..fd334f149 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java @@ -350,25 +350,4 @@ public class KetchSystem { } } - /** - * Compute a delay in a {@code min..max} interval with random jitter. - * - * @param last - * amount of delay waited before the last attempt. This is used - * to seed the next delay interval. Should be 0 if there was no - * prior delay. - * @param min - * shortest amount of allowable delay between attempts. - * @param max - * longest amount of allowable delay between attempts. - * @return new amount of delay to wait before the next attempt. - */ - static long delay(long last, long min, long max) { - long r = Math.max(0, last * 3 - min); - if (r > 0) { - int c = (int) Math.min(r + 1, Integer.MAX_VALUE); - r = RNG.nextInt(c); - } - return Math.max(Math.min(min + r, max), min); - } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java index 4d791e470..e026e9274 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java @@ -73,6 +73,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Random; import java.util.regex.Pattern; import org.eclipse.jgit.internal.JGitText; @@ -87,6 +88,8 @@ import org.slf4j.LoggerFactory; public class FileUtils { private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class); + private static final Random RNG = new Random(); + /** * Option to delete given {@code File} */ @@ -986,4 +989,28 @@ public class FileUtils { } Files.setLastModifiedTime(f, FileTime.from(Instant.now())); } + + /** + * Compute a delay in a {@code min..max} interval with random jitter. + * + * @param last + * amount of delay waited before the last attempt. This is used + * to seed the next delay interval. Should be 0 if there was no + * prior delay. + * @param min + * shortest amount of allowable delay between attempts. + * @param max + * longest amount of allowable delay between attempts. + * @return new amount of delay to wait before the next attempt. + * + * @since 5.6 + */ + public static long delay(long last, long min, long max) { + long r = Math.max(0, last * 3 - min); + if (r > 0) { + int c = (int) Math.min(r + 1, Integer.MAX_VALUE); + r = RNG.nextInt(c); + } + return Math.max(Math.min(min + r, max), min); + } }