Browse Source

Make GC cancellable when called programmatically

Sometimes, it is necessary to cancel a garbage collection operation.
When GC is called using the standalone executable, i.e., from a command
line, Control-Cing the process does the trick. When calling GC
programmatically, though, there is no mechanism to do it.

Add checks in the GC process so that a custom cancellable progress
monitor could be passed in order to cancel the operation at specific
points. In this case, the calling process set the cancel flag in the
progress monitor and the GC process will throw an exception that can
be caught and handled by the caller accordingly.

Change-Id: Ieaecf3dbdf244539ec734939c065735f6785aacf
Signed-off-by: Hector Caballero <hector.caballero@ericsson.com>
stable-4.7
Hector Caballero 8 years ago committed by David Pursehouse
parent
commit
27b710c394
  1. 62
      org.eclipse.jgit/src/org/eclipse/jgit/errors/CancelledException.java
  2. 47
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

62
org.eclipse.jgit/src/org/eclipse/jgit/errors/CancelledException.java

@ -0,0 +1,62 @@
/*
* Copyright (C) 2017 Ericsson
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.errors;
import java.io.IOException;
/**
* Thrown when an operation was canceled
*
* @since 4.7
*/
public class CancelledException extends IOException {
private static final long serialVersionUID = 1L;
/**
* @param message
*/
public CancelledException(String message) {
super(message);
}
}

47
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

@ -76,6 +76,7 @@ import java.util.regex.Pattern;
import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.CancelledException;
import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.MissingObjectException;
@ -270,6 +271,7 @@ public class GC {
prunePreserved(); prunePreserved();
long packExpireDate = getPackExpireDate(); long packExpireDate = getPackExpireDate();
oldPackLoop: for (PackFile oldPack : oldPacks) { oldPackLoop: for (PackFile oldPack : oldPacks) {
checkCancelled();
String oldName = oldPack.getPackName(); String oldName = oldPack.getPackName();
// check whether an old pack file is also among the list of new // check whether an old pack file is also among the list of new
// pack files. Then we must not delete it. // pack files. Then we must not delete it.
@ -383,6 +385,7 @@ public class GC {
pm.beginTask(JGitText.get().pruneLoosePackedObjects, fanout.length); pm.beginTask(JGitText.get().pruneLoosePackedObjects, fanout.length);
try { try {
for (String d : fanout) { for (String d : fanout) {
checkCancelled();
pm.update(1); pm.update(1);
if (d.length() != 2) if (d.length() != 2)
continue; continue;
@ -390,6 +393,7 @@ public class GC {
if (entries == null) if (entries == null)
continue; continue;
for (String e : entries) { for (String e : entries) {
checkCancelled();
if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2) if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
continue; continue;
ObjectId id; ObjectId id;
@ -401,11 +405,13 @@ public class GC {
continue; continue;
} }
boolean found = false; boolean found = false;
for (PackFile p : packs) for (PackFile p : packs) {
checkCancelled();
if (p.hasObject(id)) { if (p.hasObject(id)) {
found = true; found = true;
break; break;
} }
}
if (found) if (found)
FileUtils.delete(objdb.fileFor(id), FileUtils.RETRY FileUtils.delete(objdb.fileFor(id), FileUtils.RETRY
| FileUtils.SKIP_MISSING | FileUtils.SKIP_MISSING
@ -446,6 +452,7 @@ public class GC {
fanout.length); fanout.length);
try { try {
for (String d : fanout) { for (String d : fanout) {
checkCancelled();
pm.update(1); pm.update(1);
if (d.length() != 2) if (d.length() != 2)
continue; continue;
@ -453,6 +460,7 @@ public class GC {
if (entries == null) if (entries == null)
continue; continue;
for (File f : entries) { for (File f : entries) {
checkCancelled();
String fName = f.getName(); String fName = f.getName();
if (fName.length() != Constants.OBJECT_ID_STRING_LENGTH - 2) if (fName.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
continue; continue;
@ -481,6 +489,8 @@ public class GC {
if (deletionCandidates.isEmpty()) if (deletionCandidates.isEmpty())
return; return;
checkCancelled();
// From the set of current refs remove all those which have been handled // From the set of current refs remove all those which have been handled
// during last repack(). Only those refs will survive which have been // during last repack(). Only those refs will survive which have been
// added or modified since the last repack. Only these can save existing // added or modified since the last repack. Only these can save existing
@ -510,11 +520,14 @@ public class GC {
// leave this method. // leave this method.
ObjectWalk w = new ObjectWalk(repo); ObjectWalk w = new ObjectWalk(repo);
try { try {
for (Ref cr : newRefs) for (Ref cr : newRefs) {
checkCancelled();
w.markStart(w.parseAny(cr.getObjectId())); w.markStart(w.parseAny(cr.getObjectId()));
}
if (lastPackedRefs != null) if (lastPackedRefs != null)
for (Ref lpr : lastPackedRefs) for (Ref lpr : lastPackedRefs) {
w.markUninteresting(w.parseAny(lpr.getObjectId())); w.markUninteresting(w.parseAny(lpr.getObjectId()));
}
removeReferenced(deletionCandidates, w); removeReferenced(deletionCandidates, w);
} finally { } finally {
w.dispose(); w.dispose();
@ -532,11 +545,15 @@ public class GC {
ObjectWalk w = new ObjectWalk(repo); ObjectWalk w = new ObjectWalk(repo);
try { try {
for (Ref ar : getAllRefs()) for (Ref ar : getAllRefs())
for (ObjectId id : listRefLogObjects(ar, lastRepackTime)) for (ObjectId id : listRefLogObjects(ar, lastRepackTime)) {
checkCancelled();
w.markStart(w.parseAny(id)); w.markStart(w.parseAny(id));
}
if (lastPackedRefs != null) if (lastPackedRefs != null)
for (Ref lpr : lastPackedRefs) for (Ref lpr : lastPackedRefs) {
checkCancelled();
w.markUninteresting(w.parseAny(lpr.getObjectId())); w.markUninteresting(w.parseAny(lpr.getObjectId()));
}
removeReferenced(deletionCandidates, w); removeReferenced(deletionCandidates, w);
} finally { } finally {
w.dispose(); w.dispose();
@ -545,6 +562,8 @@ public class GC {
if (deletionCandidates.isEmpty()) if (deletionCandidates.isEmpty())
return; return;
checkCancelled();
// delete all candidates which have survived: these are unreferenced // delete all candidates which have survived: these are unreferenced
// loose objects. Make a last check, though, to avoid deleting objects // loose objects. Make a last check, though, to avoid deleting objects
// that could have been referenced while the candidates list was being // that could have been referenced while the candidates list was being
@ -613,6 +632,7 @@ public class GC {
IncorrectObjectTypeException, IOException { IncorrectObjectTypeException, IOException {
RevObject ro = w.next(); RevObject ro = w.next();
while (ro != null) { while (ro != null) {
checkCancelled();
if (id2File.remove(ro.getId()) != null) if (id2File.remove(ro.getId()) != null)
if (id2File.isEmpty()) if (id2File.isEmpty())
return; return;
@ -620,6 +640,7 @@ public class GC {
} }
ro = w.nextObject(); ro = w.nextObject();
while (ro != null) { while (ro != null) {
checkCancelled();
if (id2File.remove(ro.getId()) != null) if (id2File.remove(ro.getId()) != null)
if (id2File.isEmpty()) if (id2File.isEmpty())
return; return;
@ -653,6 +674,7 @@ public class GC {
pm.beginTask(JGitText.get().packRefs, refs.size()); pm.beginTask(JGitText.get().packRefs, refs.size());
try { try {
for (Ref ref : refs) { for (Ref ref : refs) {
checkCancelled();
if (!ref.isSymbolic() && ref.getStorage().isLoose()) if (!ref.isSymbolic() && ref.getStorage().isLoose())
refsToBePacked.add(ref.getName()); refsToBePacked.add(ref.getName());
pm.update(1); pm.update(1);
@ -691,6 +713,7 @@ public class GC {
RefDatabase refdb = repo.getRefDatabase(); RefDatabase refdb = repo.getRefDatabase();
for (Ref ref : refsBefore) { for (Ref ref : refsBefore) {
checkCancelled();
nonHeads.addAll(listRefLogObjects(ref, 0)); nonHeads.addAll(listRefLogObjects(ref, 0));
if (ref.isSymbolic() || ref.getObjectId() == null) if (ref.isSymbolic() || ref.getObjectId() == null)
continue; continue;
@ -705,9 +728,11 @@ public class GC {
} }
List<ObjectIdSet> excluded = new LinkedList<ObjectIdSet>(); List<ObjectIdSet> excluded = new LinkedList<ObjectIdSet>();
for (final PackFile f : repo.getObjectDatabase().getPacks()) for (final PackFile f : repo.getObjectDatabase().getPacks()) {
checkCancelled();
if (f.shouldBeKept()) if (f.shouldBeKept())
excluded.add(f.getIndex()); excluded.add(f.getIndex());
}
tagTargets.addAll(allHeads); tagTargets.addAll(allHeads);
nonHeads.addAll(indexObjects); nonHeads.addAll(indexObjects);
@ -805,6 +830,7 @@ public class GC {
all.addAll(refs); all.addAll(refs);
// add additional refs which start with refs/ // add additional refs which start with refs/
for (Ref r : addl) { for (Ref r : addl) {
checkCancelled();
if (r.getName().startsWith(Constants.R_REFS)) { if (r.getName().startsWith(Constants.R_REFS)) {
all.add(r); all.add(r);
} }
@ -842,6 +868,7 @@ public class GC {
Set<ObjectId> ret = new HashSet<ObjectId>(); Set<ObjectId> ret = new HashSet<ObjectId>();
while (treeWalk.next()) { while (treeWalk.next()) {
checkCancelled();
ObjectId objectId = treeWalk.getObjectId(0); ObjectId objectId = treeWalk.getObjectId(0);
switch (treeWalk.getRawMode(0) & FileMode.TYPE_MASK) { switch (treeWalk.getRawMode(0) & FileMode.TYPE_MASK) {
case FileMode.TYPE_MISSING: case FileMode.TYPE_MISSING:
@ -869,6 +896,7 @@ public class GC {
private PackFile writePack(@NonNull Set<? extends ObjectId> want, private PackFile writePack(@NonNull Set<? extends ObjectId> want,
@NonNull Set<? extends ObjectId> have, Set<ObjectId> tagTargets, @NonNull Set<? extends ObjectId> have, Set<ObjectId> tagTargets,
List<ObjectIdSet> excludeObjects) throws IOException { List<ObjectIdSet> excludeObjects) throws IOException {
checkCancelled();
File tmpPack = null; File tmpPack = null;
Map<PackExt, File> tmpExts = new TreeMap<PackExt, File>( Map<PackExt, File> tmpExts = new TreeMap<PackExt, File>(
new Comparator<PackExt>() { new Comparator<PackExt>() {
@ -900,6 +928,7 @@ public class GC {
pw.preparePack(pm, want, have); pw.preparePack(pm, want, have);
if (pw.getObjectCount() == 0) if (pw.getObjectCount() == 0)
return null; return null;
checkCancelled();
// create temporary files // create temporary files
String id = pw.computeName().getName(); String id = pw.computeName().getName();
@ -1016,6 +1045,12 @@ public class GC {
return new File(packdir, "pack-" + name + ext); //$NON-NLS-1$ return new File(packdir, "pack-" + name + ext); //$NON-NLS-1$
} }
private void checkCancelled() throws CancelledException {
if (pm.isCancelled()) {
throw new CancelledException(JGitText.get().operationCanceled);
}
}
/** /**
* A class holding statistical data for a FileRepository regarding how many * A class holding statistical data for a FileRepository regarding how many
* objects are stored as loose or packed objects * objects are stored as loose or packed objects

Loading…
Cancel
Save