Skip to content

use reflection to get thread ID #4764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/

/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.configuration;

import org.jetbrains.annotations.NotNull;
import org.opengrok.indexer.util.ThreadUtil;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand All @@ -47,7 +48,7 @@ public OpenGrokThreadFactory(String name) {
@Override
public Thread newThread(@NotNull Runnable runnable) {
Thread thread = Executors.defaultThreadFactory().newThread(Objects.requireNonNull(runnable, "runnable"));
thread.setName(PREFIX + threadPrefix + thread.getId());
thread.setName(PREFIX + threadPrefix + ThreadUtil.getThreadId(thread));
return thread;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.util;
Expand Down Expand Up @@ -412,7 +412,7 @@ public static void registerErrorHandler() {
LOGGER.log(Level.FINE, "Installing default uncaught exception handler");
Thread.setDefaultUncaughtExceptionHandler((t, e) ->
LOGGER.log(Level.SEVERE, String.format("Uncaught exception in thread %s with ID %d: %s",
t.getName(), t.getId(), e.getMessage()), e));
t.getName(), ThreadUtil.getThreadId(t), e.getMessage()), e));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ThreadUtil {
private ThreadUtil() {
// private to enforce static
}

/**
* Retrieve thread ID, preferring the non-deprecated <code>threadId</code> method.
* This can be replaced with direct call after target Java version is switched to Java 21 or higher.
* @param thread thread object
* @return thread id
*/
public static long getThreadId(Thread thread) {
Class<? extends Thread> clazz = thread.getClass();
Method method;
try {
method = clazz.getMethod("threadId");
} catch (NoSuchMethodException e) {
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
}
try {
return (long) method.invoke(thread);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
7 changes: 4 additions & 3 deletions suggester/src/main/java/org/opengrok/suggest/Suggester.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.suggest;

Expand All @@ -34,6 +34,7 @@
import org.apache.lucene.util.BytesRef;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.opengrok.suggest.util.ThreadUtil;
import org.opengrok.suggest.query.SuggesterPrefixQuery;
import org.opengrok.suggest.query.SuggesterQuery;
import org.opengrok.suggest.util.Progress;
Expand Down Expand Up @@ -161,15 +162,15 @@ public Suggester(
runnable -> {
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
// This should match the naming in OpenGrokThreadFactory class.
thread.setName("OpenGrok-suggester-lookup-" + thread.getId());
thread.setName("OpenGrok-suggester-lookup-" + ThreadUtil.getThreadId(thread));
return thread;
});

this.initRebuildExecutor = Executors.newFixedThreadPool(rebuildParallelismLevel,
runnable -> {
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
// This should match the naming in OpenGrokThreadFactory class.
thread.setName("OpenGrok-suggester-rebuild-" + thread.getId());
thread.setName("OpenGrok-suggester-rebuild-" + ThreadUtil.getThreadId(thread));
return thread;
});

Expand Down
57 changes: 57 additions & 0 deletions suggester/src/main/java/org/opengrok/suggest/util/ThreadUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.suggest.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ThreadUtil {
private ThreadUtil() {
// private to enforce static
}

/**
* Retrieve thread ID, preferring the non-deprecated <code>threadId</code> method.
* This can be replaced with direct call after target Java version is switched to Java 21 or higher.
* @param thread thread object
* @return thread id
*/
public static long getThreadId(Thread thread) {
Class<? extends Thread> clazz = thread.getClass();
Method method;
try {
method = clazz.getMethod("threadId");
} catch (NoSuchMethodException e) {
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
}
try {
return (long) method.invoke(thread);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
Loading