Example usage for org.eclipse.jgit.api GarbageCollectCommand getStatistics

List of usage examples for org.eclipse.jgit.api GarbageCollectCommand getStatistics

Introduction

In this page you can find the example usage for org.eclipse.jgit.api GarbageCollectCommand getStatistics.

Prototype

public Properties getStatistics() throws GitAPIException 

Source Link

Document

Computes and returns the repository statistics.

Usage

From source file:com.gitblit.GCExecutor.java

License:Apache License

@Override
public void run() {
    if (!isReady()) {
        return;/* w  w w  . j  a va2s . c  om*/
    }

    running.set(true);
    Date now = new Date();

    for (String repositoryName : GitBlit.self().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 = GitBlit.self().getRepositoryModel(repositoryName);
            repository = GitBlit.self().getRepository(repositoryName);
            if (repository == null) {
                logger.warn(MessageFormat.format("GCExecutor is missing repository {0}?!?", repositoryName));
                continue;
            }

            if (!isRepositoryIdle(repository)) {
                logger.debug(MessageFormat.format("GCExecutor is skipping {0} because it is not idle",
                        repositoryName));
                continue;
            }

            // By setting the GCStatus to COLLECTING we are
            // disabling *all* access to this repository from Gitblit.
            // 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();
                    GitBlit.self().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);
}

From source file:com.gitblit.service.GarbageCollectorService.java

License:Apache License

@Override
public void run() {
    if (!isReady()) {
        return;/*from  w  ww.j  a  v  a 2s .c om*/
    }

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

            // By setting the GCStatus to COLLECTING we are
            // disabling *all* access to this repository from Gitblit.
            // 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);
}

From source file:com.gitblit.transport.ssh.git.GarbageCollectionCommand.java

License:Apache License

@Override
protected void runImpl() throws IOException, Failure {
    try {/*from ww w  .j av  a  2  s. com*/
        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);
    }
}

From source file:com.google.gerrit.server.git.GarbageCollection.java

License:Apache License

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));
    }// w  w  w  .ja  va 2  s. co m
    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;
}

From source file:com.google.gerrit.server.project.GetStatistics.java

License:Apache License

@Override
public RepositoryStatistics apply(ProjectResource rsrc)
        throws ResourceNotFoundException, ResourceConflictException {
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        GarbageCollectCommand gc = Git.wrap(repo).gc();
        return new RepositoryStatistics(gc.getStatistics());
    } catch (GitAPIException | JGitInternalException e) {
        throw new ResourceConflictException(e.getMessage());
    } catch (IOException e) {
        throw new ResourceNotFoundException(rsrc.getName());
    }/*from   w  w w . ja  va 2  s  .c o m*/
}