From a3d0ed721a493214701f4af7683c0c3432aeccd9 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Fri, 9 Feb 2018 10:53:42 +0100 Subject: [PATCH] Allow to reindex groups and accounts if they are stale Being able to reindex a group only if it is stale can save effort on startup of Gerrit slaves (see follow-up change). Change-Id: I66d5bb09e49a4dfcd9d20db12c4ab818c94fd399 Signed-off-by: Edwin Kempin --- .../server/index/account/AccountIndexer.java | 8 ++++++ .../index/account/AccountIndexerImpl.java | 26 +++++++++---------- .../server/index/group/GroupIndexer.java | 8 ++++++ .../server/index/group/GroupIndexerImpl.java | 26 +++++++++---------- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/java/com/google/gerrit/server/index/account/AccountIndexer.java b/java/com/google/gerrit/server/index/account/AccountIndexer.java index dd24714690..91fa1d977c 100644 --- a/java/com/google/gerrit/server/index/account/AccountIndexer.java +++ b/java/com/google/gerrit/server/index/account/AccountIndexer.java @@ -25,4 +25,12 @@ public interface AccountIndexer { * @param id account id to index. */ void index(Account.Id id) throws IOException; + + /** + * Synchronously reindex an account if it is stale. + * + * @param id account id to index. + * @return whether the account was reindexed + */ + boolean reindexIfStale(Account.Id id) throws IOException; } diff --git a/java/com/google/gerrit/server/index/account/AccountIndexerImpl.java b/java/com/google/gerrit/server/index/account/AccountIndexerImpl.java index 8ca4a34142..d055a46d3c 100644 --- a/java/com/google/gerrit/server/index/account/AccountIndexerImpl.java +++ b/java/com/google/gerrit/server/index/account/AccountIndexerImpl.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.Optional; -import java.util.concurrent.Callable; import java.util.concurrent.Future; import org.eclipse.jgit.lib.Config; @@ -102,6 +101,15 @@ public class AccountIndexerImpl implements AccountIndexer { autoReindexIfStale(id); } + @Override + public boolean reindexIfStale(Account.Id id) throws IOException { + if (stalenessChecker.isStale(id)) { + index(id); + return true; + } + return false; + } + private static boolean autoReindexIfStale(Config cfg) { return cfg.getBoolean("index", null, "autoReindexIfStale", true); } @@ -110,7 +118,7 @@ public class AccountIndexerImpl implements AccountIndexer { if (autoReindexIfStale) { // Don't retry indefinitely; if this fails the account will be stale. @SuppressWarnings("unused") - Future possiblyIgnoredError = reindexIfStale(id); + Future possiblyIgnoredError = reindexIfStaleAsync(id); } } @@ -124,19 +132,11 @@ public class AccountIndexerImpl implements AccountIndexer { * @return future for reindexing the account; returns true if the account was stale. */ @SuppressWarnings("deprecation") - public com.google.common.util.concurrent.CheckedFuture reindexIfStale( + private com.google.common.util.concurrent.CheckedFuture reindexIfStaleAsync( Account.Id id) { - Callable task = - () -> { - if (stalenessChecker.isStale(id)) { - index(id); - return true; - } - return false; - }; - return Futures.makeChecked( - Futures.nonCancellationPropagating(batchExecutor.submit(task)), IndexUtils.MAPPER); + Futures.nonCancellationPropagating(batchExecutor.submit(() -> reindexIfStale(id))), + IndexUtils.MAPPER); } private void fireAccountIndexedEvent(int id) { diff --git a/java/com/google/gerrit/server/index/group/GroupIndexer.java b/java/com/google/gerrit/server/index/group/GroupIndexer.java index 0925cf4148..503fd6b5fd 100644 --- a/java/com/google/gerrit/server/index/group/GroupIndexer.java +++ b/java/com/google/gerrit/server/index/group/GroupIndexer.java @@ -25,4 +25,12 @@ public interface GroupIndexer { * @param uuid group UUID to index. */ void index(AccountGroup.UUID uuid) throws IOException; + + /** + * Synchronously reindex a group if it is stale. + * + * @param uuid group UUID to index. + * @return whether the group was reindexed + */ + boolean reindexIfStale(AccountGroup.UUID uuid) throws IOException; } diff --git a/java/com/google/gerrit/server/index/group/GroupIndexerImpl.java b/java/com/google/gerrit/server/index/group/GroupIndexerImpl.java index b101dcb296..ca4abbc08d 100644 --- a/java/com/google/gerrit/server/index/group/GroupIndexerImpl.java +++ b/java/com/google/gerrit/server/index/group/GroupIndexerImpl.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.Optional; -import java.util.concurrent.Callable; import java.util.concurrent.Future; import org.eclipse.jgit.lib.Config; @@ -102,6 +101,15 @@ public class GroupIndexerImpl implements GroupIndexer { autoReindexIfStale(uuid); } + @Override + public boolean reindexIfStale(AccountGroup.UUID uuid) throws IOException { + if (stalenessChecker.isStale(uuid)) { + index(uuid); + return true; + } + return false; + } + private static boolean autoReindexIfStale(Config cfg) { return cfg.getBoolean("index", null, "autoReindexIfStale", true); } @@ -110,7 +118,7 @@ public class GroupIndexerImpl implements GroupIndexer { if (autoReindexIfStale) { // Don't retry indefinitely; if this fails the group will be stale. @SuppressWarnings("unused") - Future possiblyIgnoredError = reindexIfStale(uuid); + Future possiblyIgnoredError = reindexIfStaleAsync(uuid); } } @@ -124,19 +132,11 @@ public class GroupIndexerImpl implements GroupIndexer { * @return future for reindexing the group; returns true if the group was stale. */ @SuppressWarnings("deprecation") - public com.google.common.util.concurrent.CheckedFuture reindexIfStale( + private com.google.common.util.concurrent.CheckedFuture reindexIfStaleAsync( AccountGroup.UUID uuid) { - Callable task = - () -> { - if (stalenessChecker.isStale(uuid)) { - index(uuid); - return true; - } - return false; - }; - return Futures.makeChecked( - Futures.nonCancellationPropagating(batchExecutor.submit(task)), IndexUtils.MAPPER); + Futures.nonCancellationPropagating(batchExecutor.submit(() -> reindexIfStale(uuid))), + IndexUtils.MAPPER); } private void fireGroupIndexedEvent(String uuid) {