From f542960855ba5112047cc553ec9aa5b0c50763c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Ar=C3=A8s?= Date: Wed, 18 Mar 2015 15:48:41 -0400 Subject: [PATCH 1/2] Rework intra line diff to interrupt threads instead of killing them Now that MyersDiff is interruptible[1], interrupt threads instead of killing them when MyersDiff goes into infinite loop. Replace IntraLineWorkerPool that was created to allow killing threads by a standard CachedThreadPool. [1] https://git.eclipse.org/r/44041 Change-Id: I39ceef66f503fb9f0b6036fc32b671818772c258 --- Documentation/config-gerrit.txt | 9 - .../java/com/google/gerrit/pgm/Daemon.java | 4 +- .../gerrit/server/patch/DiffExecutor.java | 31 +++ .../server/patch/DiffExecutorModule.java | 39 ++++ .../gerrit/server/patch/IntraLineLoader.java | 50 +++-- .../server/patch/IntraLineWorkerPool.java | 182 ------------------ .../gerrit/testutil/InMemoryModule.java | 15 ++ .../gerrit/httpd/WebAppInitializer.java | 4 +- 8 files changed, 119 insertions(+), 215 deletions(-) create mode 100644 gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java create mode 100644 gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutorModule.java delete mode 100644 gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineWorkerPool.java diff --git a/Documentation/config-gerrit.txt b/Documentation/config-gerrit.txt index 9f34b07235..1e65ae2fe7 100644 --- a/Documentation/config-gerrit.txt +++ b/Documentation/config-gerrit.txt @@ -720,15 +720,6 @@ See also link:cmd-flush-caches.html[gerrit flush-caches]. ==== [[cache_options]]Cache Options -[[cache.diff_intraline.maxIdleWorkers]]cache.diff_intraline.maxIdleWorkers:: -+ -Number of idle worker threads to maintain for the intraline difference -computations. There is no upper bound on how many concurrent requests -can occur at once, if additional threads are started to handle a peak -load, only this many will remain idle afterwards. -+ -Default is 1.5x number of available CPUs. - [[cache.diff_intraline.timeout]]cache.diff_intraline.timeout:: + Maximum number of milliseconds to wait for intraline difference data diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java index 63cdc93d50..4e811bf4c2 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java @@ -64,7 +64,7 @@ import com.google.gerrit.server.index.IndexModule.IndexType; import com.google.gerrit.server.mail.SignedTokenEmailTokenVerifier; import com.google.gerrit.server.mail.SmtpEmailSender; import com.google.gerrit.server.mime.MimeUtil2Module; -import com.google.gerrit.server.patch.IntraLineWorkerPool; +import com.google.gerrit.server.patch.DiffExecutorModule; import com.google.gerrit.server.plugins.PluginGuiceEnvironment; import com.google.gerrit.server.plugins.PluginRestApiModule; import com.google.gerrit.server.schema.DataSourceProvider; @@ -317,7 +317,7 @@ public class Daemon extends SiteProgram { modules.add(new WorkQueue.Module()); modules.add(new ChangeHookRunner.Module()); modules.add(new ReceiveCommitsExecutorModule()); - modules.add(new IntraLineWorkerPool.Module()); + modules.add(new DiffExecutorModule()); modules.add(new MimeUtil2Module()); modules.add(cfgInjector.getInstance(GerritGlobalModule.class)); modules.add(new InternalAccountDirectory.Module()); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java new file mode 100644 index 0000000000..23589e3e3a --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java @@ -0,0 +1,31 @@ +// Copyright (C) 2015 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.patch; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.util.concurrent.ExecutorService; + +/** + * Marker on {@link ExecutorService} used by + * {@link IntraLineLoader}. + */ +@Retention(RUNTIME) +@BindingAnnotation +public @interface DiffExecutor { +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutorModule.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutorModule.java new file mode 100644 index 0000000000..9eaea3a62f --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutorModule.java @@ -0,0 +1,39 @@ +// Copyright (C) 2015 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.patch; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.Singleton; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** Module providing the {@link DiffExecutor}. */ +public class DiffExecutorModule extends AbstractModule { + + @Override + protected void configure() { + } + + @Provides + @Singleton + @DiffExecutor + public ExecutorService createDiffExecutor() { + return Executors.newCachedThreadPool(new ThreadFactoryBuilder() + .setNameFormat("Diff-%d").setDaemon(true).build()); + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineLoader.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineLoader.java index dd67c20e90..ffcce14654 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineLoader.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineLoader.java @@ -15,6 +15,7 @@ package com.google.gerrit.server.patch; +import com.google.common.base.Throwables; import com.google.common.cache.CacheLoader; import com.google.gerrit.server.config.ConfigUtil; import com.google.gerrit.server.config.GerritServerConfig; @@ -29,7 +30,12 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.regex.Pattern; class IntraLineLoader extends CacheLoader { @@ -41,12 +47,13 @@ class IntraLineLoader extends CacheLoader { private static final Pattern CONTROL_BLOCK_START_RE = Pattern .compile("[{:][ \\t]*$"); - private final IntraLineWorkerPool workerPool; + private final ExecutorService diffExecutor; private final long timeoutMillis; @Inject - IntraLineLoader(IntraLineWorkerPool pool, @GerritServerConfig Config cfg) { - workerPool = pool; + IntraLineLoader(@DiffExecutor ExecutorService diffExecutor, + @GerritServerConfig Config cfg) { + this.diffExecutor = diffExecutor; timeoutMillis = ConfigUtil.getTimeUnit(cfg, "cache", PatchListCacheImpl.INTRA_NAME, "timeout", TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS), @@ -54,27 +61,30 @@ class IntraLineLoader extends CacheLoader { } @Override - public IntraLineDiff load(IntraLineDiffKey key) throws Exception { - IntraLineWorkerPool.Worker w = workerPool.acquire(); - IntraLineWorkerPool.Worker.Result r = w.computeWithTimeout(key, timeoutMillis); - - if (r == IntraLineWorkerPool.Worker.Result.TIMEOUT) { - // Don't keep this thread. We have to murder it unsafely, which - // means its unable to be reused in the future. Return back a - // null result, indicating the cache cannot load this key. - // + public IntraLineDiff load(final IntraLineDiffKey key) throws Exception { + Future result = diffExecutor.submit(new Callable() { + @Override + public IntraLineDiff call() throws Exception { + return IntraLineLoader.compute(key); + } + }); + try { + return result.get(timeoutMillis, TimeUnit.MILLISECONDS); + } catch (InterruptedException | TimeoutException e) { + log.warn(timeoutMillis + " ms timeout reached for IntraLineDiff" + + " in project " + key.getProject().get() + + " on commit " + key.getCommit().name() + + " for path " + key.getPath() + + " comparing " + key.getBlobA().name() + + ".." + key.getBlobB().name()); + result.cancel(true); return new IntraLineDiff(IntraLineDiff.Status.TIMEOUT); - } - workerPool.release(w); - - if (r.error != null) { + } catch (ExecutionException e) { // If there was an error computing the result, carry it // up to the caller so the cache knows this key is invalid. - // - throw r.error; + Throwables.propagateIfInstanceOf(e.getCause(), Exception.class); + throw new Exception(e.getMessage(), e.getCause()); } - - return r.diff; } static IntraLineDiff compute(IntraLineDiffKey key) throws Exception { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineWorkerPool.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineWorkerPool.java deleted file mode 100644 index 49ae9503ac..0000000000 --- a/gerrit-server/src/main/java/com/google/gerrit/server/patch/IntraLineWorkerPool.java +++ /dev/null @@ -1,182 +0,0 @@ -// Copyright (C) 2009 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.patch; - -import static com.google.gerrit.server.patch.IntraLineLoader.log; - -import com.google.gerrit.server.config.GerritServerConfig; -import com.google.inject.AbstractModule; -import com.google.inject.Inject; -import com.google.inject.Singleton; - -import org.eclipse.jgit.lib.Config; - -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -@Singleton -public class IntraLineWorkerPool { - public static class Module extends AbstractModule { - @Override - protected void configure() { - bind(IntraLineWorkerPool.class); - } - } - - private final BlockingQueue workerPool; - - @Inject - public IntraLineWorkerPool(@GerritServerConfig Config cfg) { - int workers = cfg.getInt( - "cache", PatchListCacheImpl.INTRA_NAME, "maxIdleWorkers", - Runtime.getRuntime().availableProcessors() * 3 / 2); - workerPool = new ArrayBlockingQueue<>(workers, true /* fair */); - } - - Worker acquire() { - Worker w = workerPool.poll(); - if (w == null) { - // If no worker is immediately available, start a new one. - // Maximum parallelism is controlled by the web server. - w = new Worker(); - w.start(); - } - return w; - } - - void release(Worker w) { - if (!workerPool.offer(w)) { - // If the idle worker pool is full, terminate the worker. - w.shutdownGracefully(); - } - } - - static class Worker extends Thread { - private static final AtomicInteger count = new AtomicInteger(1); - - private final ArrayBlockingQueue input; - private final ArrayBlockingQueue result; - - Worker() { - input = new ArrayBlockingQueue<>(1); - result = new ArrayBlockingQueue<>(1); - - setName("IntraLineDiff-" + count.getAndIncrement()); - setDaemon(true); - } - - Result computeWithTimeout(IntraLineDiffKey key, long timeoutMillis) - throws Exception { - if (!input.offer(new Input(key))) { - log.error("Cannot enqueue task to thread " + getName()); - return Result.TIMEOUT; - } - - Result r = result.poll(timeoutMillis, TimeUnit.MILLISECONDS); - if (r != null) { - return r; - } else { - log.warn(timeoutMillis + " ms timeout reached for IntraLineDiff" - + " in project " + key.getProject().get() - + " on commit " + key.getCommit().name() - + " for path " + key.getPath() - + " comparing " + key.getBlobA().name() - + ".." + key.getBlobB().name() - + ". Killing " + getName()); - forcefullyKillThreadInAnUglyWay(); - return Result.TIMEOUT; - } - } - - @SuppressWarnings("deprecation") - private void forcefullyKillThreadInAnUglyWay() { - try { - stop(); - } catch (Throwable error) { - // Ignore any reason the thread won't stop. - log.error("Cannot stop runaway thread " + getName(), error); - } - } - - private void shutdownGracefully() { - if (!input.offer(Input.END_THREAD)) { - log.error("Cannot gracefully stop thread " + getName()); - } - } - - @Override - public void run() { - try { - for (;;) { - Input in; - try { - in = input.take(); - } catch (InterruptedException e) { - log.error("Unexpected interrupt on " + getName()); - continue; - } - - if (in == Input.END_THREAD) { - return; - } - - Result r; - try { - r = new Result(IntraLineLoader.compute(in.key)); - } catch (Exception error) { - r = new Result(error); - } - - if (!result.offer(r)) { - log.error("Cannot return result from " + getName()); - } - } - } catch (ThreadDeath iHaveBeenShot) { - // Handle thread death by gracefully returning to the caller, - // allowing the thread to be destroyed. - } - } - - private static class Input { - static final Input END_THREAD = new Input(null); - - final IntraLineDiffKey key; - - Input(IntraLineDiffKey key) { - this.key = key; - } - } - - static class Result { - static final Result TIMEOUT = new Result((IntraLineDiff) null); - - final IntraLineDiff diff; - final Exception error; - - Result(IntraLineDiff diff) { - this.diff = diff; - this.error = null; - } - - Result(Exception error) { - this.diff = null; - this.error = error; - } - } - } -} diff --git a/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java b/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java index f33cfb23de..f3ad75bea2 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java +++ b/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkState; import static com.google.inject.Scopes.SINGLETON; import com.google.common.net.InetAddresses; +import com.google.common.util.concurrent.MoreExecutors; import com.google.gerrit.common.ChangeHooks; import com.google.gerrit.common.DisabledChangeHooks; import com.google.gerrit.reviewdb.client.AuthType; @@ -48,6 +49,7 @@ import com.google.gerrit.server.index.ChangeSchemas; import com.google.gerrit.server.index.IndexModule.IndexType; import com.google.gerrit.server.mail.SignedTokenEmailTokenVerifier; import com.google.gerrit.server.mail.SmtpEmailSender; +import com.google.gerrit.server.patch.DiffExecutor; import com.google.gerrit.server.schema.DataSourceType; import com.google.gerrit.server.schema.SchemaCreator; import com.google.gerrit.server.securestore.DefaultSecureStore; @@ -75,6 +77,7 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.concurrent.ExecutorService; public class InMemoryModule extends FactoryModule { public static Config newDefaultConfig() { @@ -162,6 +165,18 @@ public class InMemoryModule extends FactoryModule { return CanonicalWebUrlProvider.class; } }); + //Replacement of DiffExecutorModule to not use thread pool in the tests + install(new AbstractModule() { + @Override + protected void configure() { + } + @Provides + @Singleton + @DiffExecutor + public ExecutorService createDiffExecutor() { + return MoreExecutors.newDirectExecutorService(); + } + }); install(new DefaultCacheFactory.Module()); install(new SmtpEmailSender.Module()); install(new SignedTokenEmailTokenVerifier.Module()); diff --git a/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java b/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java index 865dc670db..003b9cabbf 100644 --- a/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java +++ b/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java @@ -47,7 +47,7 @@ import com.google.gerrit.server.index.IndexModule; import com.google.gerrit.server.mail.SignedTokenEmailTokenVerifier; import com.google.gerrit.server.mail.SmtpEmailSender; import com.google.gerrit.server.mime.MimeUtil2Module; -import com.google.gerrit.server.patch.IntraLineWorkerPool; +import com.google.gerrit.server.patch.DiffExecutorModule; import com.google.gerrit.server.plugins.PluginGuiceEnvironment; import com.google.gerrit.server.plugins.PluginRestApiModule; import com.google.gerrit.server.schema.DataSourceModule; @@ -281,7 +281,7 @@ public class WebAppInitializer extends GuiceServletContextListener modules.add(new WorkQueue.Module()); modules.add(new ChangeHookRunner.Module()); modules.add(new ReceiveCommitsExecutorModule()); - modules.add(new IntraLineWorkerPool.Module()); + modules.add(new DiffExecutorModule()); modules.add(new MimeUtil2Module()); modules.add(cfgInjector.getInstance(GerritGlobalModule.class)); modules.add(new InternalAccountDirectory.Module()); From 2bc8681d8b9b2d82cd1e828ff0ceb3866de79839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Ar=C3=A8s?= Date: Mon, 9 Mar 2015 15:20:28 -0400 Subject: [PATCH 2/2] Work around MyersDiff infinite loop in PatchListLoader This infinite loop is happening for some files when the PatchListLoader is computing the differences between 2 commits. It first showed up[1] in the mergeability checks done in background and was easy to work around by killing the thread using Javamelody and abandoning the faulty commits. The issue showed up again, when upgrading from 2.9.x to 2.10: the online reindexer getting stuck because of that infinite loop and this time, no easy work around. Use a similar approach that was done in intraline diff to work around the MyersDiff infinite loop. Instead of returning a timeout error message when the infinite loop is detected, fallback to a diff algorithm that does not use MyersDiff. Returning a timeout error was not an option because the failing operation is not always triggered by a user. From the user perspective, the only difference when the infinite loop is detected is that the files in the commit will not be compared in-depth, which will result in bigger edit regions. [1]https://groups.google.com/d/msg/repo-discuss/ZtiCilM3wFA/LijfZ4YkLHsJ Change-Id: Ib00de070dd8df1722d4ade0a83c0ffa8eaa37f8e --- Documentation/config-gerrit.txt | 19 ++++++ .../gerrit/server/patch/DiffExecutor.java | 2 +- .../server/patch/PatchListCacheImpl.java | 2 +- .../gerrit/server/patch/PatchListLoader.java | 62 ++++++++++++++++++- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Documentation/config-gerrit.txt b/Documentation/config-gerrit.txt index 1e65ae2fe7..c93a01b009 100644 --- a/Documentation/config-gerrit.txt +++ b/Documentation/config-gerrit.txt @@ -720,6 +720,25 @@ See also link:cmd-flush-caches.html[gerrit flush-caches]. ==== [[cache_options]]Cache Options +[[cache.diff.timeout]]cache.diff.timeout:: ++ +Maximum number of milliseconds to wait for diff data before giving up and +falling back on a simpler diff algorithm that will not be able to break down +modified regions into smaller ones. This is a work around for an infinite loop +bug in the default difference algorithm implementation. ++ +Values should use common unit suffixes to express their setting: ++ +* ms, milliseconds +* s, sec, second, seconds +* m, min, minute, minutes +* h, hr, hour, hours + ++ +If a unit suffix is not specified, `milliseconds` is assumed. ++ +Default is 5 seconds. + [[cache.diff_intraline.timeout]]cache.diff_intraline.timeout:: + Maximum number of milliseconds to wait for intraline difference data diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java index 23589e3e3a..564ca58453 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/patch/DiffExecutor.java @@ -23,7 +23,7 @@ import java.util.concurrent.ExecutorService; /** * Marker on {@link ExecutorService} used by - * {@link IntraLineLoader}. + * {@link IntraLineLoader} and {@link PatchListLoader}. */ @Retention(RUNTIME) @BindingAnnotation diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListCacheImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListCacheImpl.java index 7b7c73108a..6c769f742a 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListCacheImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListCacheImpl.java @@ -35,7 +35,7 @@ import java.util.concurrent.ExecutionException; /** Provides a cached list of {@link PatchListEntry}. */ @Singleton public class PatchListCacheImpl implements PatchListCache { - private static final String FILE_NAME = "diff"; + static final String FILE_NAME = "diff"; static final String INTRA_NAME = "diff_intraline"; public static Module module() { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListLoader.java b/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListLoader.java index bfb8c005e3..272f8c3373 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListLoader.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/patch/PatchListLoader.java @@ -16,11 +16,13 @@ package com.google.gerrit.server.patch; import com.google.common.base.Function; +import com.google.common.base.Throwables; import com.google.common.cache.CacheLoader; import com.google.common.collect.FluentIterable; import com.google.gerrit.reviewdb.client.AccountDiffPreference.Whitespace; import com.google.gerrit.reviewdb.client.Patch; import com.google.gerrit.reviewdb.client.RefNames; +import com.google.gerrit.server.config.ConfigUtil; import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.MergeUtil; @@ -69,6 +71,12 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class PatchListLoader extends CacheLoader { static final Logger log = LoggerFactory.getLogger(PatchListLoader.class); @@ -76,13 +84,23 @@ public class PatchListLoader extends CacheLoader { private final GitRepositoryManager repoManager; private final PatchListCache patchListCache; private final ThreeWayMergeStrategy mergeStrategy; + private final ExecutorService diffExecutor; + private final long timeoutMillis; + @Inject - PatchListLoader(GitRepositoryManager mgr, PatchListCache plc, - @GerritServerConfig Config cfg) { + PatchListLoader(GitRepositoryManager mgr, + PatchListCache plc, + @GerritServerConfig Config cfg, + @DiffExecutor ExecutorService de) { repoManager = mgr; patchListCache = plc; mergeStrategy = MergeUtil.getMergeStrategy(cfg); + diffExecutor = de; + timeoutMillis = + ConfigUtil.getTimeUnit(cfg, "cache", PatchListCacheImpl.FILE_NAME, + "timeout", TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS), + TimeUnit.MILLISECONDS); } @Override @@ -163,7 +181,7 @@ public class PatchListLoader extends CacheLoader { DiffEntry diffEntry = diffEntries.get(i); if (paths == null || paths.contains(diffEntry.getNewPath()) || paths.contains(diffEntry.getOldPath())) { - FileHeader fh = df.toFileHeader(diffEntry); + FileHeader fh = toFileHeader(key, df, diffEntry); entries.add(newEntry(aTree, fh)); } } @@ -172,6 +190,44 @@ public class PatchListLoader extends CacheLoader { } } + private FileHeader toFileHeader(PatchListKey key, + final DiffFormatter diffFormatter, final DiffEntry diffEntry) + throws IOException { + + Future result = diffExecutor.submit(new Callable() { + @Override + public FileHeader call() throws IOException { + return diffFormatter.toFileHeader(diffEntry); + } + }); + + try { + return result.get(timeoutMillis, TimeUnit.MILLISECONDS); + } catch (InterruptedException | TimeoutException e) { + log.warn(timeoutMillis + " ms timeout reached for Diff loader" + + " in project " + key.projectKey.get() + + " on commit " + key.getNewId() + + " on path " + diffEntry.getNewPath() + + " comparing " + diffEntry.getOldId() + + ".." + diffEntry.getNewId()); + result.cancel(true); + return toFileHeaderWithoutMyersDiff(diffFormatter, diffEntry); + } catch (ExecutionException e) { + // If there was an error computing the result, carry it + // up to the caller so the cache knows this key is invalid. + Throwables.propagateIfInstanceOf(e.getCause(), IOException.class); + throw new IOException(e.getMessage(), e.getCause()); + } + } + + private FileHeader toFileHeaderWithoutMyersDiff(DiffFormatter diffFormatter, + DiffEntry diffEntry) throws IOException { + HistogramDiff histogramDiff = new HistogramDiff(); + histogramDiff.setFallbackAlgorithm(null); + diffFormatter.setDiffAlgorithm(histogramDiff); + return diffFormatter.toFileHeader(diffEntry); + } + private PatchListEntry newCommitMessage(final RawTextComparator cmp, final ObjectReader reader, final RevCommit aCommit, final RevCommit bCommit) throws IOException {