Merge "Add retry logic for lock failure when deleting a branch"
This commit is contained in:
@@ -32,6 +32,7 @@ import com.google.inject.Inject;
|
|||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
|
import org.eclipse.jgit.errors.LockFailedException;
|
||||||
import org.eclipse.jgit.lib.RefUpdate;
|
import org.eclipse.jgit.lib.RefUpdate;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -42,6 +43,8 @@ import java.io.IOException;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public class DeleteBranch implements RestModifyView<BranchResource, Input>{
|
public class DeleteBranch implements RestModifyView<BranchResource, Input>{
|
||||||
private static final Logger log = LoggerFactory.getLogger(DeleteBranch.class);
|
private static final Logger log = LoggerFactory.getLogger(DeleteBranch.class);
|
||||||
|
private static final int MAX_LOCK_FAILURE_CALLS = 10;
|
||||||
|
private static final long SLEEP_ON_LOCK_FAILURE_MS = 15;
|
||||||
|
|
||||||
static class Input {
|
static class Input {
|
||||||
}
|
}
|
||||||
@@ -81,14 +84,28 @@ public class DeleteBranch implements RestModifyView<BranchResource, Input>{
|
|||||||
Repository r = repoManager.openRepository(rsrc.getNameKey());
|
Repository r = repoManager.openRepository(rsrc.getNameKey());
|
||||||
try {
|
try {
|
||||||
RefUpdate.Result result;
|
RefUpdate.Result result;
|
||||||
RefUpdate u;
|
RefUpdate u = r.updateRef(rsrc.getRef());
|
||||||
try {
|
u.setForceUpdate(true);
|
||||||
u = r.updateRef(rsrc.getRef());
|
int remainingLockFailureCalls = MAX_LOCK_FAILURE_CALLS;
|
||||||
u.setForceUpdate(true);
|
for (;;) {
|
||||||
result = u.delete();
|
try {
|
||||||
} catch (IOException e) {
|
result = u.delete();
|
||||||
log.error("Cannot delete " + rsrc.getBranchKey(), e);
|
} catch (LockFailedException e) {
|
||||||
throw e;
|
result = RefUpdate.Result.LOCK_FAILURE;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Cannot delete " + rsrc.getBranchKey(), e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
if (result == RefUpdate.Result.LOCK_FAILURE
|
||||||
|
&& --remainingLockFailureCalls > 0) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(SLEEP_ON_LOCK_FAILURE_MS);
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (result) {
|
switch (result) {
|
||||||
|
|||||||
Reference in New Issue
Block a user