summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuca Milanesio <luca.milanesio@gmail.com>2021-10-02 00:50:29 +0100
committerLuca Milanesio <luca.milanesio@gmail.com>2021-10-06 21:16:50 +0000
commit3b5bca50d5be1259caf19cd4c1f67aaa088f09bf (patch)
treed2f7bc08850ccaf06e3544b055d7859e2e0b51c6
parent7b131cdbafdb80fd3dc68c6f3aa12a9d711cc9e3 (diff)
Reuse the same Repository when listing changes by status
Certain queries for multiple changes based on status need to read the change meta-data from NoteDb on the same repository. Do not re-open the same repository over and over again but rather reuse the same in-memory Repository object with consequent saving in CPU and lowering the systemload during the execution of change queries. The existing loading of multiple change notes without passing the Repository object as parameter is kept for backward compatibility and can be safely removed on master because no more referenced by the Gerrit code-base. Change-Id: I683a1b0cd4dcc64e191df6777dfce059745d5ce9
-rw-r--r--java/com/google/gerrit/server/notedb/ChangeNotes.java27
-rw-r--r--java/com/google/gerrit/server/query/change/InternalChangeQuery.java1
2 files changed, 27 insertions, 1 deletions
diff --git a/java/com/google/gerrit/server/notedb/ChangeNotes.java b/java/com/google/gerrit/server/notedb/ChangeNotes.java
index 8eb5c5cdaa..047aa0c16a 100644
--- a/java/com/google/gerrit/server/notedb/ChangeNotes.java
+++ b/java/com/google/gerrit/server/notedb/ChangeNotes.java
@@ -66,12 +66,14 @@ import com.google.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
@@ -198,13 +200,14 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
}
public List<ChangeNotes> create(
+ Repository repo,
Project.NameKey project,
Collection<Change.Id> changeIds,
Predicate<ChangeNotes> predicate) {
List<ChangeNotes> notes = new ArrayList<>();
for (Change.Id cid : changeIds) {
try {
- ChangeNotes cn = create(project, cid);
+ ChangeNotes cn = create(repo, project, cid);
if (cn.getChange() != null && predicate.test(cn)) {
notes.add(cn);
}
@@ -217,6 +220,28 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
return notes;
}
+ /* TODO: This is now unused in the Gerrit code-base, however it is kept in the code
+ /* because it is a public method in a stable branch.
+ * It can be removed in master branch where we have more flexibility to change the API
+ * interface.
+ */
+ public List<ChangeNotes> create(
+ Project.NameKey project,
+ Collection<Change.Id> changeIds,
+ Predicate<ChangeNotes> predicate) {
+ try (Repository repo = args.repoManager.openRepository(project)) {
+ return create(repo, project, changeIds, predicate);
+ } catch (RepositoryNotFoundException e) {
+ // The repository does not exist, hence it does not contain
+ // any change.
+ } catch (IOException e) {
+ logger.atWarning().withCause(e).log(
+ "Unable to open project=%s when trying to retrieve changeId=%s from NoteDb",
+ project, changeIds);
+ }
+ return Collections.emptyList();
+ }
+
public ListMultimap<Project.NameKey, ChangeNotes> create(Predicate<ChangeNotes> predicate)
throws IOException {
ListMultimap<Project.NameKey, ChangeNotes> m =
diff --git a/java/com/google/gerrit/server/query/change/InternalChangeQuery.java b/java/com/google/gerrit/server/query/change/InternalChangeQuery.java
index 6605c23da2..1012f4a54e 100644
--- a/java/com/google/gerrit/server/query/change/InternalChangeQuery.java
+++ b/java/com/google/gerrit/server/query/change/InternalChangeQuery.java
@@ -193,6 +193,7 @@ public class InternalChangeQuery extends InternalQuery<ChangeData, InternalChang
List<ChangeNotes> notes =
notesFactory.create(
+ repo,
branch.project(),
changeIds,
cn -> {