Browse Source

Move KetchSystem.delay to FileUtils.

This will provide exponential backoff with jitter to other JGit
components too.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Change-Id: Idd44e3bbaef6d71134ce2e3f7d405f35e7397cbd
next
Han-Wen Nienhuys 5 years ago
parent
commit
74bfec4112
  1. 6
      org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchReplica.java
  2. 21
      org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchSystem.java
  3. 27
      org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java

6
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));

21
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);
}
}

27
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);
}
}

Loading…
Cancel
Save