Limit number of reductions made by Prolog
Allow site administrators to place an upper bound on how much CPU time will be spent evaluating rules for a single change. This covers not just the project rules, but also all filters up to All-Projects. Reduction counting and not wall-clock time is used to be fair to rules that are running on a temporarily overloaded system. It also permits builtin predicates that are expensive like commit_edits/2 to count only once per result made to Prolog, and not charge time waiting for JGit to populate the PatchListCache. The default is configured to be 100,000 reductions. This value was experimentally determined on a Intel Core i7 running at 2.3 GHz and Java 1.6.0_37. On that system reaching the limit of reductions and aborting an infinite loop in a user predicate takes about 14 ms. Any rules taking 14 ms to run will add ~350 ms to the latency of the All > Open changes screen. Because of this, Administrators already have sufficient incentives to keep rule running time low. Change-Id: I2cd5660fc556e6aeeb61606a850ce45c98157c96
This commit is contained in:

committed by
David Pursehouse

parent
80babfe985
commit
ed001d7f69
@@ -16,6 +16,7 @@ package com.google.gerrit.rules;
|
||||
|
||||
import com.google.gerrit.server.AnonymousUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.patch.PatchListCache;
|
||||
import com.google.gerrit.server.patch.PatchSetInfoFactory;
|
||||
@@ -26,9 +27,11 @@ import com.google.inject.Singleton;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
import com.googlecode.prolog_cafe.lang.BufferingPrologControl;
|
||||
import com.googlecode.prolog_cafe.lang.Predicate;
|
||||
import com.googlecode.prolog_cafe.lang.Prolog;
|
||||
import com.googlecode.prolog_cafe.lang.PrologMachineCopy;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -65,6 +68,8 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
|
||||
private final Args args;
|
||||
private final Map<StoredValue<Object>, Object> storedValues;
|
||||
private int reductionLimit;
|
||||
private int reductionsRemaining;
|
||||
private List<Runnable> cleanup;
|
||||
|
||||
@Inject
|
||||
@@ -74,6 +79,8 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
setEnabled(EnumSet.allOf(Prolog.Feature.class), false);
|
||||
args = a;
|
||||
storedValues = new HashMap<>();
|
||||
reductionLimit = a.reductionLimit;
|
||||
reductionsRemaining = reductionLimit;
|
||||
cleanup = new LinkedList<>();
|
||||
}
|
||||
|
||||
@@ -81,6 +88,28 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
return args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEngineStopped() {
|
||||
if (super.isEngineStopped()) {
|
||||
return true;
|
||||
} else if (--reductionsRemaining <= 0) {
|
||||
throw new ReductionLimitException(reductionLimit);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPredicate(Predicate goal) {
|
||||
super.setPredicate(goal);
|
||||
reductionLimit = args.reductionLimit(goal);
|
||||
reductionsRemaining = reductionLimit;
|
||||
}
|
||||
|
||||
/** @return number of reductions during execution. */
|
||||
public int getReductions() {
|
||||
return reductionLimit - reductionsRemaining;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup a stored value in the interpreter's hash manager.
|
||||
*
|
||||
@@ -154,6 +183,8 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
private final PatchSetInfoFactory patchSetInfoFactory;
|
||||
private final IdentifiedUser.GenericFactory userFactory;
|
||||
private final Provider<AnonymousUser> anonymousUser;
|
||||
private final int reductionLimit;
|
||||
private final int compileLimit;
|
||||
|
||||
@Inject
|
||||
Args(ProjectCache projectCache,
|
||||
@@ -161,13 +192,29 @@ public class PrologEnvironment extends BufferingPrologControl {
|
||||
PatchListCache patchListCache,
|
||||
PatchSetInfoFactory patchSetInfoFactory,
|
||||
IdentifiedUser.GenericFactory userFactory,
|
||||
Provider<AnonymousUser> anonymousUser) {
|
||||
Provider<AnonymousUser> anonymousUser,
|
||||
@GerritServerConfig Config config) {
|
||||
this.projectCache = projectCache;
|
||||
this.repositoryManager = repositoryManager;
|
||||
this.patchListCache = patchListCache;
|
||||
this.patchSetInfoFactory = patchSetInfoFactory;
|
||||
this.userFactory = userFactory;
|
||||
this.anonymousUser = anonymousUser;
|
||||
|
||||
int limit = config.getInt("rules", null, "reductionLimit", 100000);
|
||||
reductionLimit = limit <= 0 ? Integer.MAX_VALUE : limit;
|
||||
|
||||
limit = config.getInt("rules", null, "compileReductionLimit",
|
||||
(int) Math.min(10L * limit, Integer.MAX_VALUE));
|
||||
compileLimit = limit <= 0 ? Integer.MAX_VALUE : limit;
|
||||
}
|
||||
|
||||
private int reductionLimit(Predicate goal) {
|
||||
if ("com.googlecode.prolog_cafe.builtin.PRED_consult_stream_2"
|
||||
.equals(goal.getClass().getName())) {
|
||||
return compileLimit;
|
||||
}
|
||||
return reductionLimit;
|
||||
}
|
||||
|
||||
public ProjectCache getProjectCache() {
|
||||
|
@@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2014 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.rules;
|
||||
|
||||
/** Thrown by {@link PrologEnvironment} if a script runs too long. */
|
||||
public class ReductionLimitException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
ReductionLimitException(int limit) {
|
||||
super(String.format("exceeded reduction limit of %d", limit));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user