Search in sources :

Example 1 with GarbageCollectCommand

use of org.eclipse.jgit.api.GarbageCollectCommand in project gitblit by gitblit.

the class GarbageCollectorService method run.

@Override
public void run() {
    if (!isReady()) {
        return;
    }
    running.set(true);
    Date now = new Date();
    for (String repositoryName : repositoryManager.getRepositoryList()) {
        if (forceClose.get()) {
            break;
        }
        if (isCollectingGarbage(repositoryName)) {
            logger.warn(MessageFormat.format("Already collecting garbage from {0}?!?", repositoryName));
            continue;
        }
        boolean garbageCollected = false;
        RepositoryModel model = null;
        Repository repository = null;
        try {
            model = repositoryManager.getRepositoryModel(repositoryName);
            repository = repositoryManager.getRepository(repositoryName);
            if (repository == null) {
                logger.warn(MessageFormat.format("GCExecutor is missing repository {0}?!?", repositoryName));
                continue;
            }
            if (!repositoryManager.isIdle(repository)) {
                logger.debug(MessageFormat.format("GCExecutor is skipping {0} because it is not idle", repositoryName));
                continue;
            }
            // Think of this as a clutch in a manual transmission vehicle.
            if (!setGCStatus(repositoryName, GCStatus.COLLECTING)) {
                logger.warn(MessageFormat.format("Can not acquire GC lock for {0}, skipping", repositoryName));
                continue;
            }
            logger.debug(MessageFormat.format("GCExecutor locked idle repository {0}", repositoryName));
            Git git = new Git(repository);
            GarbageCollectCommand gc = git.gc();
            Properties stats = gc.getStatistics();
            // determine if this is a scheduled GC
            Calendar cal = Calendar.getInstance();
            cal.setTime(model.lastGC);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            cal.add(Calendar.DATE, model.gcPeriod);
            Date gcDate = cal.getTime();
            boolean shouldCollectGarbage = now.after(gcDate);
            // determine if filesize triggered GC
            long gcThreshold = FileUtils.convertSizeToLong(model.gcThreshold, 500 * 1024L);
            long sizeOfLooseObjects = (Long) stats.get("sizeOfLooseObjects");
            boolean hasEnoughGarbage = sizeOfLooseObjects >= gcThreshold;
            // if we satisfy one of the requirements, GC
            boolean hasGarbage = sizeOfLooseObjects > 0;
            if (hasGarbage && (hasEnoughGarbage || shouldCollectGarbage)) {
                long looseKB = sizeOfLooseObjects / 1024L;
                logger.info(MessageFormat.format("Collecting {1} KB of loose objects from {0}", repositoryName, looseKB));
                // do the deed
                gc.call();
                garbageCollected = true;
            }
        } catch (Exception e) {
            logger.error("Error collecting garbage in " + repositoryName, e);
        } finally {
            // cleanup
            if (repository != null) {
                if (garbageCollected) {
                    // update the last GC date
                    model.lastGC = new Date();
                    repositoryManager.updateConfiguration(repository, model);
                }
                repository.close();
            }
            // reset the GC lock
            releaseLock(repositoryName);
            logger.debug(MessageFormat.format("GCExecutor released GC lock for {0}", repositoryName));
        }
    }
    running.set(false);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) GarbageCollectCommand(org.eclipse.jgit.api.GarbageCollectCommand) Git(org.eclipse.jgit.api.Git) Calendar(java.util.Calendar) RepositoryModel(com.gitblit.models.RepositoryModel) Properties(java.util.Properties) Date(java.util.Date)

Example 2 with GarbageCollectCommand

use of org.eclipse.jgit.api.GarbageCollectCommand in project gitblit by gitblit.

the class GarbageCollectionCommand method runImpl.

@Override
protected void runImpl() throws IOException, Failure {
    try {
        GarbageCollectCommand gc = Git.wrap(repo).gc();
        logGcInfo("before:", gc.getStatistics());
        gc.setProgressMonitor(NullProgressMonitor.INSTANCE);
        Properties statistics = gc.call();
        logGcInfo("after: ", statistics);
    } catch (Exception e) {
        throw new Failure(1, "fatal: Cannot run gc: ", e);
    }
}
Also used : GarbageCollectCommand(org.eclipse.jgit.api.GarbageCollectCommand) Properties(java.util.Properties) IOException(java.io.IOException)

Example 3 with GarbageCollectCommand

use of org.eclipse.jgit.api.GarbageCollectCommand in project gerrit by GerritCodeReview.

the class GarbageCollection method run.

public GarbageCollectionResult run(List<Project.NameKey> projectNames, boolean aggressive, PrintWriter writer) {
    GarbageCollectionResult result = new GarbageCollectionResult();
    Set<Project.NameKey> projectsToGc = gcQueue.addAll(projectNames);
    for (Project.NameKey projectName : Sets.difference(Sets.newHashSet(projectNames), projectsToGc)) {
        result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.GC_ALREADY_SCHEDULED, projectName));
    }
    for (Project.NameKey p : projectsToGc) {
        try (Repository repo = repoManager.openRepository(p)) {
            logGcConfiguration(p, repo, aggressive);
            print(writer, "collecting garbage for \"" + p + "\":\n");
            GarbageCollectCommand gc = Git.wrap(repo).gc();
            gc.setAggressive(aggressive);
            logGcInfo(p, "before:", gc.getStatistics());
            gc.setProgressMonitor(writer != null ? new TextProgressMonitor(writer) : NullProgressMonitor.INSTANCE);
            Properties statistics = gc.call();
            logGcInfo(p, "after: ", statistics);
            print(writer, "done.\n\n");
            fire(p, statistics);
        } catch (RepositoryNotFoundException e) {
            logGcError(writer, p, e);
            result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.REPOSITORY_NOT_FOUND, p));
        } catch (Exception e) {
            logGcError(writer, p, e);
            result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.GC_FAILED, p));
        } finally {
            gcQueue.gcFinished(p);
        }
    }
    return result;
}
Also used : GarbageCollectCommand(org.eclipse.jgit.api.GarbageCollectCommand) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) Properties(java.util.Properties) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) GarbageCollectionResult(com.google.gerrit.common.data.GarbageCollectionResult)

Aggregations

Properties (java.util.Properties)3 GarbageCollectCommand (org.eclipse.jgit.api.GarbageCollectCommand)3 Repository (org.eclipse.jgit.lib.Repository)2 RepositoryModel (com.gitblit.models.RepositoryModel)1 GarbageCollectionResult (com.google.gerrit.common.data.GarbageCollectionResult)1 Project (com.google.gerrit.reviewdb.client.Project)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Git (org.eclipse.jgit.api.Git)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1 TextProgressMonitor (org.eclipse.jgit.lib.TextProgressMonitor)1