Skip to content

Commit f1379d6

Browse files
author
Vladimir Kotal
authored
Suggester rebuild terminate on undeploy (#3396)
fixes #3395
1 parent 05aaab8 commit f1379d6

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public final class Suggester implements Closeable {
7171

7272
private static final String PROJECTS_DISABLED_KEY = "";
7373

74-
private static final Logger logger = Logger.getLogger(Suggester.class.getName());
74+
private static final Logger LOGGER = Logger.getLogger(Suggester.class.getName());
7575

7676
private final Map<String, SuggesterProjectData> projectData = new ConcurrentHashMap<>();
7777

@@ -94,6 +94,7 @@ public final class Suggester implements Closeable {
9494
private final int rebuildParallelismLevel;
9595

9696
private volatile boolean rebuilding;
97+
private volatile boolean terminating;
9798
private final Lock rebuildLock = new ReentrantLock();
9899
private final Condition rebuildDone = rebuildLock.newCondition();
99100

@@ -164,7 +165,7 @@ public Suggester(
164165
*/
165166
public void init(final Collection<NamedIndexDir> luceneIndexes) {
166167
if (luceneIndexes == null || luceneIndexes.isEmpty()) {
167-
logger.log(Level.INFO, "No index directories found, exiting...");
168+
LOGGER.log(Level.INFO, "No index directories found, exiting...");
168169
return;
169170
}
170171
if (!projectsEnabled && luceneIndexes.size() > 1) {
@@ -173,11 +174,15 @@ public void init(final Collection<NamedIndexDir> luceneIndexes) {
173174

174175
synchronized (lock) {
175176
Instant start = Instant.now();
176-
logger.log(Level.INFO, "Initializing suggester");
177+
LOGGER.log(Level.INFO, "Initializing suggester");
177178

178179
ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel);
179180

180181
for (NamedIndexDir indexDir : luceneIndexes) {
182+
if (terminating) {
183+
LOGGER.log(Level.INFO, "Terminating suggester initialization");
184+
return;
185+
}
181186
submitInitIfIndexExists(executor, indexDir);
182187
}
183188

@@ -202,18 +207,22 @@ private void submitInitIfIndexExists(final ExecutorService executorService, fina
202207
if (indexExists(indexDir.path)) {
203208
executorService.submit(getInitRunnable(indexDir));
204209
} else {
205-
logger.log(Level.FINE, "Index in {0} directory does not exist, skipping...", indexDir);
210+
LOGGER.log(Level.FINE, "Index in {0} directory does not exist, skipping...", indexDir);
206211
}
207212
} catch (IOException e) {
208-
logger.log(Level.WARNING, "Could not check if index exists", e);
213+
LOGGER.log(Level.WARNING, "Could not check if index exists", e);
209214
}
210215
}
211216

212217
private Runnable getInitRunnable(final NamedIndexDir indexDir) {
213218
return () -> {
214219
try {
220+
if (terminating) {
221+
return;
222+
}
223+
215224
Instant start = Instant.now();
216-
logger.log(Level.FINE, "Initializing {0}", indexDir);
225+
LOGGER.log(Level.FINE, "Initializing {0}", indexDir);
217226

218227
SuggesterProjectData wfst = new SuggesterProjectData(FSDirectory.open(indexDir.path),
219228
getSuggesterDir(indexDir.name), allowMostPopular, allowedFields);
@@ -225,9 +234,9 @@ private Runnable getInitRunnable(final NamedIndexDir indexDir) {
225234
}
226235

227236
Duration d = Duration.between(start, Instant.now());
228-
logger.log(Level.FINE, "Finished initialization of {0}, took {1}", new Object[] {indexDir, d});
237+
LOGGER.log(Level.FINE, "Finished initialization of {0}, took {1}", new Object[] {indexDir, d});
229238
} catch (Exception e) {
230-
logger.log(Level.SEVERE, "Could not initialize suggester data for " + indexDir, e);
239+
LOGGER.log(Level.SEVERE, "Could not initialize suggester data for " + indexDir, e);
231240
}
232241
};
233242
}
@@ -254,11 +263,11 @@ private void shutdownAndAwaitTermination(final ExecutorService executorService,
254263
executorService.awaitTermination(awaitTerminationTime.toMillis(), TimeUnit.MILLISECONDS);
255264
Duration duration = Duration.between(start, Instant.now());
256265
timer.record(duration);
257-
logger.log(Level.INFO, "{0} (took {1})", new Object[]{logMessageOnSuccess,
266+
LOGGER.log(Level.INFO, "{0} (took {1})", new Object[]{logMessageOnSuccess,
258267
DurationFormatUtils.formatDurationWords(duration.toMillis(),
259268
true, true)});
260269
} catch (InterruptedException e) {
261-
logger.log(Level.SEVERE, "Interrupted while building suggesters", e);
270+
LOGGER.log(Level.SEVERE, "Interrupted while building suggesters", e);
262271
Thread.currentThread().interrupt();
263272
}
264273
}
@@ -269,7 +278,7 @@ private void shutdownAndAwaitTermination(final ExecutorService executorService,
269278
*/
270279
public void rebuild(final Collection<NamedIndexDir> indexDirs) {
271280
if (indexDirs == null || indexDirs.isEmpty()) {
272-
logger.log(Level.INFO, "Not rebuilding suggester data because no index directories were specified");
281+
LOGGER.log(Level.INFO, "Not rebuilding suggester data because no index directories were specified");
273282
return;
274283
}
275284

@@ -278,8 +287,12 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
278287
rebuildLock.unlock();
279288

280289
synchronized (lock) {
290+
if (terminating) {
291+
return;
292+
}
293+
281294
Instant start = Instant.now();
282-
logger.log(Level.INFO, "Rebuilding the following suggesters: {0}", indexDirs);
295+
LOGGER.log(Level.INFO, "Rebuilding the following suggesters: {0}", indexDirs);
283296

284297
ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel);
285298

@@ -325,14 +338,18 @@ public void waitForRebuild(long timeout, TimeUnit unit) throws InterruptedExcept
325338
private Runnable getRebuildRunnable(final SuggesterProjectData data) {
326339
return () -> {
327340
try {
341+
if (terminating) {
342+
return;
343+
}
344+
328345
Instant start = Instant.now();
329-
logger.log(Level.FINE, "Rebuilding {0}", data);
346+
LOGGER.log(Level.FINE, "Rebuilding {0}", data);
330347
data.rebuild();
331348

332349
Duration d = Duration.between(start, Instant.now());
333-
logger.log(Level.FINE, "Rebuild of {0} finished, took {1}", new Object[] {data, d});
350+
LOGGER.log(Level.FINE, "Rebuild of {0} finished, took {1}", new Object[] {data, d});
334351
} catch (Exception e) {
335-
logger.log(Level.SEVERE, "Could not rebuild suggester", e);
352+
LOGGER.log(Level.SEVERE, "Could not rebuild suggester", e);
336353
}
337354
};
338355
}
@@ -347,12 +364,12 @@ public void remove(final Iterable<String> names) {
347364
}
348365

349366
synchronized (lock) {
350-
logger.log(Level.INFO, "Removing following suggesters: {0}", names);
367+
LOGGER.log(Level.INFO, "Removing following suggesters: {0}", names);
351368

352369
for (String suggesterName : names) {
353370
SuggesterProjectData collection = projectData.get(suggesterName);
354371
if (collection == null) {
355-
logger.log(Level.WARNING, "Unknown suggester {0}", suggesterName);
372+
LOGGER.log(Level.WARNING, "Unknown suggester {0}", suggesterName);
356373
continue;
357374
}
358375
collection.remove();
@@ -403,7 +420,7 @@ private Suggestions prefixLookup(
403420
List<LookupResultItem> results = readers.parallelStream().flatMap(namedIndexReader -> {
404421
SuggesterProjectData data = projectData.get(namedIndexReader.name);
405422
if (data == null) {
406-
logger.log(Level.FINE, "{0} not yet initialized", namedIndexReader.name);
423+
LOGGER.log(Level.FINE, "{0} not yet initialized", namedIndexReader.name);
407424
partialResult.value = true;
408425
return Stream.empty();
409426
}
@@ -442,7 +459,7 @@ private Suggestions complexLookup(
442459
try {
443460
futures = executorService.invokeAll(searchTasks, timeThreshold, TimeUnit.MILLISECONDS);
444461
} catch (InterruptedException e) {
445-
logger.log(Level.WARNING, "Interrupted while invoking suggester search", e);
462+
LOGGER.log(Level.WARNING, "Interrupted while invoking suggester search", e);
446463
Thread.currentThread().interrupt();
447464
return new Suggestions(Collections.emptyList(), true);
448465
}
@@ -461,7 +478,7 @@ private Suggestions complexLookup(
461478
try {
462479
searchTask.wait();
463480
} catch (InterruptedException e) {
464-
logger.log(Level.WARNING, "Interrupted while waiting for task: {0}", searchTask);
481+
LOGGER.log(Level.WARNING, "Interrupted while waiting for task: {0}", searchTask);
465482
Thread.currentThread().interrupt();
466483
}
467484
}
@@ -501,7 +518,7 @@ public void onSearch(final Iterable<String> projects, final Query query) {
501518
}
502519
}
503520
} catch (Exception e) {
504-
logger.log(Level.FINE,
521+
LOGGER.log(Level.FINE,
505522
String.format("Could not update search count map%s",
506523
projectsEnabled ? " for projects: " + projects : ""), e);
507524
}
@@ -550,7 +567,7 @@ public boolean increaseSearchCount(final String project, final Term term, final
550567
}
551568

552569
if (data == null) {
553-
logger.log(Level.WARNING, "Cannot update search count because of missing suggester data{}",
570+
LOGGER.log(Level.WARNING, "Cannot update search count because of missing suggester data{}",
554571
projectsEnabled ? " for project " + project : "");
555572
return false;
556573
}
@@ -574,7 +591,7 @@ public List<Entry<BytesRef, Integer>> getSearchCounts(
574591
) {
575592
SuggesterProjectData data = projectData.get(project);
576593
if (data == null) {
577-
logger.log(Level.FINE, "Cannot retrieve search counts because suggester data for project {0} was not found",
594+
LOGGER.log(Level.FINE, "Cannot retrieve search counts because suggester data for project {0} was not found",
578595
project);
579596
return Collections.emptyList();
580597
}
@@ -588,11 +605,12 @@ public List<Entry<BytesRef, Integer>> getSearchCounts(
588605
@Override
589606
public void close() {
590607
executorService.shutdownNow();
608+
terminating = true;
591609
projectData.values().forEach(f -> {
592610
try {
593611
f.close();
594612
} catch (IOException e) {
595-
logger.log(Level.WARNING, "Could not close suggester data " + f, e);
613+
LOGGER.log(Level.WARNING, "Could not close suggester data " + f, e);
596614
}
597615
});
598616
}
@@ -626,7 +644,7 @@ public Void call() {
626644

627645
SuggesterProjectData data = projectData.get(namedIndexReader.name);
628646
if (data == null) {
629-
logger.log(Level.FINE, "{0} not yet initialized", namedIndexReader.name);
647+
LOGGER.log(Level.FINE, "{0} not yet initialized", namedIndexReader.name);
630648
return null;
631649
}
632650
boolean gotLock = data.tryLock();

0 commit comments

Comments
 (0)