Skip to content

Commit 73b37cd

Browse files
authored
unify thread naming (#4015)
fixes #4014
1 parent 78b6302 commit 73b37cd

File tree

11 files changed

+92
-47
lines changed

11 files changed

+92
-47
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.configuration;
24+
25+
import org.jetbrains.annotations.NotNull;
26+
27+
import java.util.concurrent.Executors;
28+
import java.util.concurrent.ThreadFactory;
29+
30+
/**
31+
* ThreadFactory to be used throughout OpenGrok to make sure all threads have common prefix.
32+
*/
33+
public class OpenGrokThreadFactory implements ThreadFactory {
34+
private final String threadPrefix;
35+
36+
public OpenGrokThreadFactory(String name) {
37+
if (!name.endsWith("-")) {
38+
threadPrefix = name + "-";
39+
} else {
40+
threadPrefix = name;
41+
}
42+
}
43+
44+
@Override
45+
public Thread newThread(@NotNull Runnable runnable) {
46+
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
47+
thread.setName("OpenGrok-" + threadPrefix + thread.getId());
48+
return thread;
49+
}
50+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import org.apache.lucene.store.AlreadyClosedException;
6363
import org.apache.lucene.store.Directory;
6464
import org.apache.lucene.store.FSDirectory;
65-
import org.apache.lucene.util.NamedThreadFactory;
6665
import org.jetbrains.annotations.Nullable;
6766
import org.jetbrains.annotations.VisibleForTesting;
6867
import org.opengrok.indexer.authorization.AuthorizationFramework;
@@ -196,11 +195,7 @@ public ExecutorService getSearchExecutor() {
196195
private ExecutorService newSearchExecutor() {
197196
return Executors.newFixedThreadPool(
198197
this.getMaxSearchThreadCount(),
199-
runnable -> {
200-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
201-
thread.setName("search-" + thread.getId());
202-
return thread;
203-
});
198+
new OpenGrokThreadFactory("search"));
204199
}
205200

206201
public void shutdownSearchExecutor() {
@@ -218,7 +213,7 @@ public ExecutorService getRevisionExecutor() {
218213

219214
private ExecutorService newRevisionExecutor() {
220215
return Executors.newFixedThreadPool(this.getMaxRevisionThreadCount(),
221-
new NamedThreadFactory("get-revision"));
216+
new OpenGrokThreadFactory("get-revision"));
222217
}
223218

224219
public void shutdownRevisionExecutor() throws InterruptedException {

opengrok-indexer/src/main/java/org/opengrok/indexer/history/GitRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.jetbrains.annotations.NotNull;
8282
import org.jetbrains.annotations.Nullable;
8383
import org.opengrok.indexer.configuration.CommandTimeoutType;
84+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
8485
import org.opengrok.indexer.configuration.RuntimeEnvironment;
8586
import org.opengrok.indexer.logger.LoggerFactory;
8687
import org.opengrok.indexer.util.ForbiddenSymlinkException;
@@ -784,7 +785,7 @@ private void rebuildTagList(File directory) {
784785
*/
785786
@Override
786787
protected void buildTagList(File directory, CommandTimeoutType cmdType) {
787-
final ExecutorService executor = Executors.newSingleThreadExecutor();
788+
final ExecutorService executor = Executors.newSingleThreadExecutor(new OpenGrokThreadFactory("git-tags"));
788789
final Future<?> future = executor.submit(() -> rebuildTagList(directory));
789790
executor.shutdown();
790791

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryGuru.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.jetbrains.annotations.VisibleForTesting;
5151
import org.opengrok.indexer.configuration.CommandTimeoutType;
5252
import org.opengrok.indexer.configuration.Configuration.RemoteSCM;
53+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
5354
import org.opengrok.indexer.configuration.PathAccepter;
5455
import org.opengrok.indexer.configuration.RuntimeEnvironment;
5556
import org.opengrok.indexer.logger.LoggerFactory;
@@ -984,11 +985,7 @@ public void invalidateRepositories(Collection<? extends RepositoryInfo> repos, C
984985
parallelismLevel = env.getRepositoryInvalidationParallelism();
985986
}
986987
final ExecutorService executor = Executors.newFixedThreadPool(parallelismLevel,
987-
runnable -> {
988-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
989-
thread.setName("invalidate-repos-" + thread.getId());
990-
return thread;
991-
});
988+
new OpenGrokThreadFactory("invalidate-repos-"));
992989

993990
for (RepositoryInfo repositoryInfo : repos) {
994991
executor.submit(() -> {

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexerParallelizer.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.opengrok.indexer.analysis.Ctags;
3232
import org.opengrok.indexer.analysis.CtagsValidator;
33+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
3334
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3435
import org.opengrok.indexer.util.BoundedBlockingObjectPool;
3536
import org.opengrok.indexer.util.CtagsUtil;
@@ -241,43 +242,32 @@ private void createLazyCtagsPool() {
241242

242243
private void createLazyCtagsWatcherExecutor() {
243244
lzCtagsWatcherExecutor = LazilyInstantiate.using(() ->
244-
new ScheduledThreadPoolExecutor(1, runnable -> {
245-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
246-
thread.setName("ctags-watcher-" + thread.getId());
247-
return thread;
248-
}));
245+
new ScheduledThreadPoolExecutor(1,
246+
new OpenGrokThreadFactory("ctags-watcher")));
249247
}
250248

251249
private void createLazyXrefWatcherExecutor() {
252250
lzXrefWatcherExecutor = LazilyInstantiate.using(() ->
253-
new ScheduledThreadPoolExecutor(1, runnable -> {
254-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
255-
thread.setName("xref-watcher-" + thread.getId());
256-
return thread;
257-
}));
251+
new ScheduledThreadPoolExecutor(1,
252+
new OpenGrokThreadFactory("xref-watcher")));
258253
}
259254

260255
private void createLazyFixedExecutor() {
261256
lzFixedExecutor = LazilyInstantiate.using(() ->
262-
Executors.newFixedThreadPool(indexingParallelism));
257+
Executors.newFixedThreadPool(indexingParallelism,
258+
new OpenGrokThreadFactory("index-worker")));
263259
}
264260

265261
private void createLazyHistoryExecutor() {
266262
lzHistoryExecutor = LazilyInstantiate.using(() ->
267-
Executors.newFixedThreadPool(env.getHistoryParallelism(), runnable -> {
268-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
269-
thread.setName("history-" + thread.getId());
270-
return thread;
271-
}));
263+
Executors.newFixedThreadPool(env.getHistoryParallelism(),
264+
new OpenGrokThreadFactory("history")));
272265
}
273266

274267
private void createLazyHistoryFileExecutor() {
275268
lzHistoryFileExecutor = LazilyInstantiate.using(() ->
276-
Executors.newFixedThreadPool(env.getHistoryFileParallelism(), runnable -> {
277-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
278-
thread.setName("history-file-" + thread.getId());
279-
return thread;
280-
}));
269+
Executors.newFixedThreadPool(env.getHistoryFileParallelism(),
270+
new OpenGrokThreadFactory("history-file")));
281271
}
282272

283273
private class CtagsObjectFactory implements ObjectFactory<Ctags> {

opengrok-indexer/src/main/java/org/opengrok/indexer/util/BoundedBlockingObjectPool.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* https://dzone.com/articles/generic-and-concurrent-object : "Feel free to use
66
* it, change it, add more implementations. Happy coding!"
77
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
8+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
89
*/
910

1011
package org.opengrok.indexer.util;
@@ -16,6 +17,8 @@
1617
import java.util.concurrent.TimeUnit;
1718
import java.util.logging.Level;
1819
import java.util.logging.Logger;
20+
21+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
1922
import org.opengrok.indexer.logger.LoggerFactory;
2023

2124
/**
@@ -37,7 +40,7 @@ public final class BoundedBlockingObjectPool<T> extends AbstractObjectPool<T>
3740
private final LinkedBlockingDeque<T> objects;
3841
private final ObjectValidator<T> validator;
3942
private final ObjectFactory<T> objectFactory;
40-
private final ExecutorService executor = Executors.newCachedThreadPool();
43+
private final ExecutorService executor = Executors.newCachedThreadPool(new OpenGrokThreadFactory("bounded"));
4144
private volatile boolean puttingLast;
4245
private volatile boolean shutdownCalled;
4346

opengrok-web/src/main/java/org/opengrok/web/api/ApiTaskManager.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.web.api;
2424

2525
import jakarta.ws.rs.core.Response;
26-
import org.apache.lucene.util.NamedThreadFactory;
26+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
2727
import org.opengrok.indexer.logger.LoggerFactory;
2828
import org.opengrok.web.api.v1.controller.StatusController;
2929

@@ -76,7 +76,11 @@ public void setContextPath(String path) {
7676
}
7777

7878
static String getQueueName(String name) {
79-
return name.replaceAll("^/", "").replace("/", "-");
79+
String suffix = name.replaceAll("^/", "").replace("/", "-");
80+
if (!suffix.startsWith("-")) {
81+
suffix = "-" + suffix;
82+
}
83+
return "api_task" + suffix;
8084
}
8185

8286
/**
@@ -121,7 +125,7 @@ public void addPool(String name, int threadCount) {
121125
}
122126

123127
queues.put(queueName, Executors.newFixedThreadPool(threadCount,
124-
new NamedThreadFactory(getQueueName(queueName))));
128+
new OpenGrokThreadFactory(queueName)));
125129
}
126130

127131
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.lucene.search.Query;
3232
import org.apache.lucene.util.BytesRef;
3333
import org.opengrok.indexer.Metrics;
34+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
3435
import org.opengrok.suggest.Suggester;
3536
import org.opengrok.suggest.Suggester.NamedIndexDir;
3637
import org.opengrok.suggest.Suggester.NamedIndexReader;
@@ -78,7 +79,8 @@ public class SuggesterServiceImpl implements SuggesterService {
7879

7980
private Suggester suggester;
8081

81-
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
82+
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,
83+
new OpenGrokThreadFactory("suggester-scheduler"));
8284

8385
private ScheduledFuture<?> future;
8486

opengrok-web/src/test/java/org/opengrok/web/api/ApiTaskManagerTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.web.api;
2424

@@ -32,6 +32,7 @@
3232

3333
import static org.awaitility.Awaitility.await;
3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
3536
import static org.junit.jupiter.api.Assertions.assertNotNull;
3637
import static org.junit.jupiter.api.Assertions.assertNull;
3738
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -47,9 +48,9 @@ void testSingleton() {
4748
@Test
4849
void testQueueName() {
4950
String name = "foo";
50-
assertEquals(name, ApiTaskManager.getQueueName(name));
51+
assertTrue(ApiTaskManager.getQueueName(name).endsWith(name));
5152
name = "/foo";
52-
assertEquals(name.substring(1), ApiTaskManager.getQueueName(name));
53+
assertFalse(ApiTaskManager.getQueueName(name).contains("/"));
5354
}
5455

5556
private Object doNothing() {

plugins/src/main/java/opengrok/auth/plugin/util/WebHook.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package opengrok.auth.plugin.util;
2424

25+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
26+
2527
import java.io.Serializable;
2628
import java.util.concurrent.CompletableFuture;
2729
import java.util.concurrent.Executors;
@@ -56,10 +58,9 @@ public String getContent() {
5658
}
5759

5860
public Future<String> post() {
59-
CompletableFuture<String> completableFuture
60-
= new CompletableFuture<>();
61+
CompletableFuture<String> completableFuture = new CompletableFuture<>();
6162

62-
Executors.newCachedThreadPool().submit(() -> {
63+
Executors.newCachedThreadPool(new OpenGrokThreadFactory("webhook-")).submit(() -> {
6364
int status = RestfulClient.postIt(getURI(), getContent());
6465
completableFuture.complete(String.valueOf(status));
6566
return null;

0 commit comments

Comments
 (0)