Merge "Allow to index all changes of a project"

This commit is contained in:
Saša Živkov 2017-02-24 13:34:02 +00:00 committed by Gerrit Code Review
commit 96b6a98a17
8 changed files with 189 additions and 1 deletions

View File

@ -0,0 +1,37 @@
= gerrit index project
== NAME
gerrit index project - Index all the changes in one or more projects.
== SYNOPSIS
[verse]
--
_ssh_ -p <port> <host> _gerrit index project_ <PROJECT> [<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
<PROJECT>::
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
---------

View File

@ -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.

View File

@ -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

View File

@ -194,7 +194,7 @@ public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, Change
return new Result(sw, ok.get(), nDone, nFailed);
}
private Callable<Void> reindexProject(
public Callable<Void> reindexProject(
final ChangeIndexer indexer,
final Project.NameKey project,
final Task done,

View File

@ -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<ProjectResource, ProjectInput> {
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");
}
}

View File

@ -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);

View File

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

View File

@ -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<ProjectControl> 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()));
}
}
}