Add repo-stats ssh command The command can be useful to quickly check repo dirtiness and adjust gc configuration. Change-Id: I5ca47e958195808518dfb41ac323cbf92503f41e
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java index eadf28c..8144f18 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java +++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java
@@ -104,7 +104,7 @@ } } - private String extractFrom(String path) { + static String extractFrom(String path) { String name = path; if (name.startsWith("/")) { name = name.substring(1);
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java new file mode 100644 index 0000000..e6573c9 --- /dev/null +++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java
@@ -0,0 +1,103 @@ +// Copyright (C) 2022 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.ericsson.gerrit.plugins.gcconductor.command; + +import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; + +import com.google.gerrit.common.data.GlobalCapability; +import com.google.gerrit.entities.Project; +import com.google.gerrit.extensions.annotations.RequiresCapability; +import com.google.gerrit.server.git.GitRepositoryManager; +import com.google.gerrit.server.git.LocalDiskRepositoryManager; +import com.google.gerrit.server.project.ProjectCache; +import com.google.gerrit.sshd.AdminHighPriorityCommand; +import com.google.gerrit.sshd.CommandMetaData; +import com.google.gerrit.sshd.SshCommand; +import com.google.inject.Inject; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.internal.storage.file.GC; +import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.RepositoryCache; +import org.eclipse.jgit.lib.RepositoryCache.FileKey; +import org.eclipse.jgit.util.FS; +import org.kohsuke.args4j.Argument; + +@AdminHighPriorityCommand +@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) +@CommandMetaData( + name = "repo-stats", + description = "display repo dirtiness statistics", + runsAt = MASTER_OR_SLAVE) +final class RepoStats extends SshCommand { + @Argument(index = 0, required = true, metaVar = "REPOSITORY") + private String repository; + + @Inject private GitRepositoryManager gitRepositoryManager; + + @Inject private ProjectCache projectCache; + + @Override + protected void run() throws UnloggedFailure { + try { + Path repositoryPath = Paths.get(repository); + if (repositoryPath.toFile().exists()) { + repositoryPath = repositoryPath.toRealPath(); + } + if (!FileKey.isGitRepository(repositoryPath.toFile(), FS.DETECTED)) { + repositoryPath = resolvePath(); + } + + stdout.println(getRepoStatistics(repositoryPath.toString())); + } catch (IOException e) { + throw die(e); + } + } + + private Path resolvePath() throws UnloggedFailure { + if (!(gitRepositoryManager instanceof LocalDiskRepositoryManager)) { + throw die("Unable to resolve path to " + repository); + } + String projectName = AddToQueue.extractFrom(repository); + Project.NameKey nameKey = Project.nameKey(projectName); + if (projectCache.get(nameKey) == null) { + throw die(String.format("Repository %s not found", repository)); + } + LocalDiskRepositoryManager localDiskRepositoryManager = + (LocalDiskRepositoryManager) gitRepositoryManager; + try { + return localDiskRepositoryManager + .getBasePath(nameKey) + .resolve(projectName.concat(Constants.DOT_GIT_EXT)) + .toRealPath(); + } catch (IOException e) { + throw die(e); + } + } + + private RepoStatistics getRepoStatistics(String repositoryPath) throws UnloggedFailure { + try (FileRepository repository = + (FileRepository) + RepositoryCache.open(FileKey.exact(new File(repositoryPath), FS.DETECTED))) { + return new GC(repository).getStatistics(); + } catch (IOException e) { + throw die(e); + } + } +}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java index 7940bba..9435cfc 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java +++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java
@@ -24,5 +24,6 @@ command(ShowQueue.class); command(AddToQueue.class); command(BumpToFirst.class); + command(RepoStats.class); } }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md index 870a2ad..83f6e26 100644 --- a/src/main/resources/Documentation/about.md +++ b/src/main/resources/Documentation/about.md
@@ -32,6 +32,7 @@ helpful to be able to change the _set_queued_from_ field, so that the running one can pick up repositories that were not initially added to the queue by its corresponding Gerrit instance. +* _repo-stats_ Display a repository dirtiness statistics [build]: build.html [config]: config.html
diff --git a/src/main/resources/Documentation/cmd-repo-stats.md b/src/main/resources/Documentation/cmd-repo-stats.md new file mode 100644 index 0000000..25434ab --- /dev/null +++ b/src/main/resources/Documentation/cmd-repo-stats.md
@@ -0,0 +1,53 @@ +repo-stats +===================== + +NAME +---- +repo-stats - Display a repository dirtiness statistics + +SYNOPSIS +-------- +> ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats <REPOSITORY> + +DESCRIPTION +----------- +Display a repository dirtiness statistics. + +An absolute path to the repository (including the .git suffix) or the project +name are accepted. A symlink pointing to a repository is also admitted. + +Displaying statistic can be usefully to determine repo condition. Can be a part of script with +adding a repository to the GC queue. + +ACCESS +------ +Any user who has configured an SSH key and has been granted the +`Administrate Server` global capability. + +SCRIPTING +--------- +This command is intended to be used in a script. + +EXAMPLES +-------- +Absolute path to a repository: + +``` +$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats /repos/my/repo.git +``` + +Symlink pointing to a repository: + +``` +$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats /opt/gerrit/repos/my/repo.git +``` + +Name of the project: + +``` +$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats my/repo +``` + +GERRIT +------ +Part of [Gerrit Code Review](../../../Documentation/index.html)