Skip to content

Commit a35e9f3

Browse files
ahornaceVladimir Kotal
authored andcommitted
Make suggestions time threshold configurable
1 parent d8d61bf commit a35e9f3

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/org/opensolaris/opengrok/configuration/SuggesterConfig.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class SuggesterConfig {
4949
public static final boolean SHOW_TIME_DEFAULT = false;
5050
public static final String REBUILD_CRON_CONFIG_DEFAULT = "0 0 * * *"; // every day at midnight
5151
public static final int SUGGESTER_BUILD_TERMINATION_TIME_DEFAULT = 1800; // half an hour should be enough
52+
public static final int TIME_THRESHOLD_DEFAULT = 2000; // 2 sec
5253

5354
public static final Set<String> allowedProjectsDefault = null;
5455
public static final Set<String> allowedFieldsDefault = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
@@ -127,6 +128,12 @@ public class SuggesterConfig {
127128
*/
128129
private int suggesterBuildTerminationTimeSec;
129130

131+
/**
132+
* Time threshold for suggestions in milliseconds. If the computation exceeds this time,
133+
* it will be stopped and partial results will be returned.
134+
*/
135+
private int timeThreshold;
136+
130137
public SuggesterConfig() {
131138
setEnabled(ENABLED_DEFAULT);
132139
setMaxResults(MAX_RESULTS_DEFAULT);
@@ -139,6 +146,7 @@ public SuggesterConfig() {
139146
setShowScores(SHOW_SCORES_DEFAULT);
140147
setShowProjects(SHOW_PROJECTS_DEFAULT);
141148
setShowTime(SHOW_TIME_DEFAULT);
149+
setTimeThreshold(TIME_THRESHOLD_DEFAULT);
142150
// do not use setter because indexer invocation with --man will fail
143151
rebuildCronConfig = REBUILD_CRON_CONFIG_DEFAULT;
144152
setSuggesterBuildTerminationTimeSec(SUGGESTER_BUILD_TERMINATION_TIME_DEFAULT);
@@ -265,6 +273,17 @@ public void setSuggesterBuildTerminationTimeSec(final int suggesterBuildTerminat
265273
this.suggesterBuildTerminationTimeSec = suggesterBuildTerminationTimeSec;
266274
}
267275

276+
public int getTimeThreshold() {
277+
return timeThreshold;
278+
}
279+
280+
public void setTimeThreshold(final int timeThreshold) {
281+
if (timeThreshold < 0) {
282+
throw new IllegalArgumentException("Time threshold for suggestions cannot be negative");
283+
}
284+
this.timeThreshold = timeThreshold;
285+
}
286+
268287
@Override
269288
public boolean equals(Object o) {
270289
if (this == o) {

src/org/opensolaris/opengrok/web/api/v1/suggester/provider/service/impl/SuggesterServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ private void initSuggester() {
278278
suggester = new Suggester(suggesterDir,
279279
suggesterConfig.getMaxResults(),
280280
Duration.ofSeconds(suggesterConfig.getSuggesterBuildTerminationTimeSec()),
281-
suggesterConfig.isAllowMostPopular(), env.isProjectsEnabled(), suggesterConfig.getAllowedFields());
281+
suggesterConfig.isAllowMostPopular(),
282+
env.isProjectsEnabled(),
283+
suggesterConfig.getAllowedFields(),
284+
suggesterConfig.getTimeThreshold());
282285

283286
new Thread(() -> {
284287
suggester.init(getAllProjectIndexDirs());

suggester/src/main/java/org/opengrok/suggest/Suggester.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public final class Suggester implements Closeable {
6363

6464
private static final String PROJECTS_DISABLED_KEY = "";
6565

66-
private static final int MAX_TIME_MS = 1000;
67-
6866
private static final Logger logger = Logger.getLogger(Suggester.class.getName());
6967

7068
private final Map<String, SuggesterProjectData> projectData = new ConcurrentHashMap<>();
@@ -83,6 +81,8 @@ public final class Suggester implements Closeable {
8381

8482
private final Set<String> allowedFields;
8583

84+
private final int timeThreshold;
85+
8686
private final ExecutorService executorService = Executors.newWorkStealingPool();
8787

8888
/**
@@ -100,7 +100,8 @@ public Suggester(
100100
final Duration awaitTerminationTime,
101101
final boolean allowMostPopular,
102102
final boolean projectsEnabled,
103-
final Set<String> allowedFields
103+
final Set<String> allowedFields,
104+
final int timeThreshold
104105
) {
105106
if (suggesterDir == null) {
106107
throw new IllegalArgumentException("Suggester needs to have directory specified");
@@ -117,6 +118,7 @@ public Suggester(
117118
this.allowMostPopular = allowMostPopular;
118119
this.projectsEnabled = projectsEnabled;
119120
this.allowedFields = new HashSet<>(allowedFields);
121+
this.timeThreshold = timeThreshold;
120122
}
121123

122124
/**
@@ -343,7 +345,7 @@ private List<LookupResultItem> complexLookup(
343345
}
344346

345347
try {
346-
executorService.invokeAll(searchTasks, MAX_TIME_MS, TimeUnit.MILLISECONDS);
348+
executorService.invokeAll(searchTasks, timeThreshold, TimeUnit.MILLISECONDS);
347349
} catch (InterruptedException e) {
348350
logger.log(Level.WARNING, "Interrupted while invoking suggester search", e);
349351
Thread.currentThread().interrupt();

suggester/src/test/java/org/opengrok/suggest/SuggesterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ private Suggester.NamedIndexReader getNamedIndexReader() throws IOException {
9191

9292
@Test(expected = IllegalArgumentException.class)
9393
public void testNullSuggesterDir() {
94-
new Suggester(null, 10, Duration.ofMinutes(5), false, true, null);
94+
new Suggester(null, 10, Duration.ofMinutes(5), false, true, null, Integer.MAX_VALUE);
9595
}
9696

9797
@Test(expected = IllegalArgumentException.class)
9898
public void testNullDuration() throws IOException {
9999
Path tempFile = Files.createTempFile("opengrok", "test");
100100
try {
101-
new Suggester(tempFile.toFile(), 10, null, false, true, null);
101+
new Suggester(tempFile.toFile(), 10, null, false, true, null, Integer.MAX_VALUE);
102102
} finally {
103103
tempFile.toFile().delete();
104104
}
@@ -108,7 +108,7 @@ public void testNullDuration() throws IOException {
108108
public void testNegativeDuration() throws IOException {
109109
Path tempFile = Files.createTempFile("opengrok", "test");
110110
try {
111-
new Suggester(tempFile.toFile(), 10, Duration.ofMinutes(-4), false, true, null);
111+
new Suggester(tempFile.toFile(), 10, Duration.ofMinutes(-4), false, true, null, Integer.MAX_VALUE);
112112
} finally {
113113
tempFile.toFile().delete();
114114
}
@@ -125,7 +125,7 @@ private SuggesterTestData initSuggester() throws IOException {
125125
Path tempSuggesterDir = Files.createTempDirectory("opengrok");
126126

127127
Suggester s = new Suggester(tempSuggesterDir.toFile(), 10, Duration.ofMinutes(1), true,
128-
true, Collections.singleton("test"));
128+
true, Collections.singleton("test"), Integer.MAX_VALUE);
129129

130130
s.init(Collections.singleton(new Suggester.NamedIndexDir("test", tempIndexDir)));
131131

@@ -197,7 +197,7 @@ public void testIndexChangedWhileOffline() throws IOException {
197197
addText(t.getIndexDirectory(), "a1 a2");
198198

199199
t.s = new Suggester(t.suggesterDir.toFile(), 10, Duration.ofMinutes(1), false,
200-
true, Collections.singleton("test"));
200+
true, Collections.singleton("test"), Integer.MAX_VALUE);
201201

202202
t.s.init(Collections.singleton(t.getNamedIndexDir()));
203203

0 commit comments

Comments
 (0)