diff --git a/Documentation/cmd-index-project.txt b/Documentation/cmd-index-project.txt new file mode 100644 index 0000000000..2196a26a7f --- /dev/null +++ b/Documentation/cmd-index-project.txt @@ -0,0 +1,37 @@ += gerrit index project + +== NAME +gerrit index project - Index all the changes in one or more projects. + +== SYNOPSIS +[verse] +-- +_ssh_ -p _gerrit index project_ [ ...] +-- + +== DESCRIPTION +Index all the changes in one or more projects. + +== ACCESS +Caller must have the 'Maintain Server' capability. + +== SCRIPTING +This command is intended to be used in scripts. + +== OPTIONS +:: + Required; name of the project to be indexed. + +== EXAMPLES +Index all changes in projects MyProject and NiceProject. + +---- + $ ssh -p 29418 user@review.example.com gerrit index project MyProject NiceProject +---- + +GERRIT +------ +Part of link:index.html[Gerrit Code Review] + +SEARCHBOX +--------- diff --git a/Documentation/cmd-index.txt b/Documentation/cmd-index.txt index 61e78650de..62f7fd2c1a 100644 --- a/Documentation/cmd-index.txt +++ b/Documentation/cmd-index.txt @@ -133,6 +133,9 @@ link:cmd-index-start.html[gerrit index start]:: link:cmd-index-changes.html[gerrit index changes]:: Index one or more changes. +link:cmd-index-project.html[gerrit index project]:: + Index all the changes in one or more projects. + link:cmd-logging-ls-level.html[gerrit logging ls-level]:: List loggers and their logging level. diff --git a/Documentation/rest-api-projects.txt b/Documentation/rest-api-projects.txt index e2be6e6aeb..72c6a39b64 100644 --- a/Documentation/rest-api-projects.txt +++ b/Documentation/rest-api-projects.txt @@ -1066,6 +1066,24 @@ As result a link:#project-access-info[ProjectAccessInfo] entity is returned. } ---- +[[index]] +=== Index all changes in a project + +Adds or updates all the changes belonging to a project in the secondary index. +The indexing task is executed asynchronously in background, so this command +returns immediately. + +.Request +---- + POST /projects/MyProject/index HTTP/1.0 +---- + +.Response +---- +HTTP/1.1 202 Accepted +Content-Disposition: attachment +---- + [[branch-endpoints]] == Branch Endpoints diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/index/change/AllChangesIndexer.java b/gerrit-server/src/main/java/com/google/gerrit/server/index/change/AllChangesIndexer.java index e6492cfb2c..638ff4c78f 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/index/change/AllChangesIndexer.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/index/change/AllChangesIndexer.java @@ -194,7 +194,7 @@ public class AllChangesIndexer extends SiteIndexer reindexProject( + public Callable reindexProject( final ChangeIndexer indexer, final Project.NameKey project, final Task done, diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/Index.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/Index.java new file mode 100644 index 0000000000..9ce72fe4c8 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/Index.java @@ -0,0 +1,65 @@ +// Copyright (C) 2017 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.google.gerrit.server.project; + +import static com.google.gerrit.server.git.QueueProvider.QueueType.BATCH; + +import com.google.common.io.ByteStreams; +import com.google.common.io.CharStreams; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.gerrit.common.data.GlobalCapability; +import com.google.gerrit.extensions.annotations.RequiresCapability; +import com.google.gerrit.extensions.api.projects.ProjectInput; +import com.google.gerrit.extensions.restapi.Response; +import com.google.gerrit.extensions.restapi.RestModifyView; +import com.google.gerrit.reviewdb.client.Project; +import com.google.gerrit.server.git.MultiProgressMonitor; +import com.google.gerrit.server.git.MultiProgressMonitor.Task; +import com.google.gerrit.server.index.IndexExecutor; +import com.google.gerrit.server.index.change.AllChangesIndexer; +import com.google.gerrit.server.index.change.ChangeIndexer; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import java.io.PrintWriter; + +@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) +@Singleton +public class Index implements RestModifyView { + + private final AllChangesIndexer allChangesIndexer; + private final ChangeIndexer indexer; + private final ListeningExecutorService executor; + + @Inject + Index( + AllChangesIndexer allChangesIndexer, + ChangeIndexer indexer, + @IndexExecutor(BATCH) ListeningExecutorService executor) { + this.allChangesIndexer = allChangesIndexer; + this.indexer = indexer; + this.executor = executor; + } + + @Override + public Response.Accepted apply(ProjectResource resource, ProjectInput input) { + Project.NameKey project = resource.getNameKey(); + Task mpt = + new MultiProgressMonitor(ByteStreams.nullOutputStream(), "Reindexing project") + .beginSubTask("", MultiProgressMonitor.UNKNOWN); + PrintWriter pw = new PrintWriter(CharStreams.nullWriter()); + executor.submit(allChangesIndexer.reindexProject(indexer, project, mpt, mpt, pw)); + return Response.accepted("Project " + project + " submitted for reindexing"); + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/Module.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/Module.java index c0da5d10cf..d7af195085 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/Module.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/Module.java @@ -61,6 +61,7 @@ public class Module extends RestApiModule { get(PROJECT_KIND, "statistics.git").to(GetStatistics.class); post(PROJECT_KIND, "gc").to(GarbageCollect.class); + post(PROJECT_KIND, "index").to(Index.class); child(PROJECT_KIND, "branches").to(BranchesCollection.class); put(BRANCH_KIND).to(PutBranch.class); diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexCommandsModule.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexCommandsModule.java index 633bca8861..d00468ade7 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexCommandsModule.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexCommandsModule.java @@ -29,5 +29,6 @@ public class IndexCommandsModule extends CommandModule { command(index, IndexActivateCommand.class); command(index, IndexStartCommand.class); command(index, IndexChangesCommand.class); + command(index, IndexProjectCommand.class); } } diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexProjectCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexProjectCommand.java new file mode 100644 index 0000000000..476c25bfe4 --- /dev/null +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexProjectCommand.java @@ -0,0 +1,63 @@ +// Copyright (C) 2017 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.google.gerrit.sshd.commands; + +import static com.google.gerrit.common.data.GlobalCapability.MAINTAIN_SERVER; + +import com.google.gerrit.extensions.annotations.RequiresAnyCapability; +import com.google.gerrit.server.project.Index; +import com.google.gerrit.server.project.ProjectControl; +import com.google.gerrit.server.project.ProjectResource; +import com.google.gerrit.sshd.CommandMetaData; +import com.google.gerrit.sshd.SshCommand; +import com.google.inject.Inject; +import java.util.ArrayList; +import java.util.List; +import org.kohsuke.args4j.Argument; + +@RequiresAnyCapability({MAINTAIN_SERVER}) +@CommandMetaData(name = "project", description = "Index changes of a project") +final class IndexProjectCommand extends SshCommand { + + @Inject private Index index; + + @Argument( + index = 0, + required = true, + multiValued = true, + metaVar = "PROJECT", + usage = "projects for which the changes should be indexed" + ) + private List projects = new ArrayList<>(); + + @Override + protected void run() throws UnloggedFailure, Failure, Exception { + if (projects.isEmpty()) { + throw die("needs at least one project as command arguments"); + } + projects.stream().forEach(this::index); + } + + private void index(ProjectControl projectControl) { + try { + index.apply(new ProjectResource(projectControl), null); + } catch (Exception e) { + writeError( + "error", + String.format( + "Unable to index %s: %s", projectControl.getProject().getName(), e.getMessage())); + } + } +}