Added parentId to ThreadNamingRunnable
This commit is contained in:
parent
3659847859
commit
d68c5160ed
@ -196,7 +196,7 @@ class NioClientSocketPipelineSink extends AbstractChannelSink {
|
||||
try {
|
||||
bossExecutor.execute(
|
||||
new IoWorkerRunnable(new ThreadRenamingRunnable(
|
||||
this, "NewIO", "ClientBoss", String.valueOf(id), null)));
|
||||
this, "NewIO", "ClientBoss", null, String.valueOf(id), null)));
|
||||
success = true;
|
||||
} finally {
|
||||
if (!success) {
|
||||
|
@ -169,7 +169,8 @@ class NioDatagramWorker implements Runnable {
|
||||
try {
|
||||
// Start the main selector loop. See run() for details.
|
||||
executor.execute(new ThreadRenamingRunnable(
|
||||
this, "NewIO", "DatagramWorker", bossId + "-" + id, null));
|
||||
this, "NewIO", "DatagramWorker",
|
||||
String.valueOf(bossId), String.valueOf(id), null));
|
||||
success = true;
|
||||
} finally {
|
||||
if (!success) {
|
||||
|
@ -156,7 +156,7 @@ class NioServerSocketPipelineSink extends AbstractChannelSink {
|
||||
bossExecutor.execute(
|
||||
new IoWorkerRunnable(new ThreadRenamingRunnable(
|
||||
new Boss(channel),
|
||||
"NewIO", "ServerBoss", String.valueOf(id),
|
||||
"NewIO", "ServerBoss", null, String.valueOf(id),
|
||||
channel.toString())));
|
||||
bossStarted = true;
|
||||
} catch (Throwable t) {
|
||||
|
@ -112,7 +112,7 @@ class NioWorker implements Runnable {
|
||||
new IoWorkerRunnable(new ThreadRenamingRunnable(
|
||||
this, "NewIO",
|
||||
server? "ServerWorker" : "ClientWorker",
|
||||
bossId + "-" + id, null)));
|
||||
String.valueOf(bossId), String.valueOf(id), null)));
|
||||
success = true;
|
||||
} finally {
|
||||
if (!success) {
|
||||
|
@ -136,7 +136,7 @@ class OioClientSocketPipelineSink extends AbstractChannelSink {
|
||||
new ThreadRenamingRunnable(
|
||||
new OioWorker(channel),
|
||||
"OldIO", "ClientWorker",
|
||||
id + "-" + channel.getId(),
|
||||
String.valueOf(id), String.valueOf(channel.getId()),
|
||||
channel.toString())));
|
||||
workerStarted = true;
|
||||
} catch (Throwable t) {
|
||||
|
@ -108,7 +108,7 @@ class OioDatagramPipelineSink extends AbstractChannelSink {
|
||||
new OioDatagramWorker(channel),
|
||||
"OldIO",
|
||||
"DatagramWorker",
|
||||
id + "-" + channel.getId(),
|
||||
String.valueOf(id), String.valueOf(channel.getId()),
|
||||
channel.toString())));
|
||||
workerStarted = true;
|
||||
} catch (Throwable t) {
|
||||
@ -154,13 +154,13 @@ class OioDatagramPipelineSink extends AbstractChannelSink {
|
||||
// Start the business.
|
||||
workerExecutor.execute(new IoWorkerRunnable(new ThreadRenamingRunnable(
|
||||
new OioDatagramWorker(channel),
|
||||
service, category, id + "-" + channel.getId(), comment)));
|
||||
service, category, String.valueOf(id), String.valueOf(channel.getId()), comment)));
|
||||
} else {
|
||||
// Worker started by bind() - just rename.
|
||||
Thread workerThread = channel.workerThread;
|
||||
if (workerThread != null) {
|
||||
ThreadRenamingRunnable.renameThread(
|
||||
workerThread, service, category, id + "-" + channel.getId(), comment);
|
||||
workerThread, service, category, String.valueOf(id), String.valueOf(channel.getId()), comment);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -177,7 +177,8 @@ class OioDatagramWorker implements Runnable {
|
||||
if (workerThread != null) {
|
||||
ThreadRenamingRunnable.renameThread(
|
||||
workerThread, "OldIO", "DatagramWorker",
|
||||
((OioDatagramChannelFactory) channel.getFactory()).id + "-" + channel.getId(),
|
||||
String.valueOf(((OioDatagramChannelFactory) channel.getFactory()).id),
|
||||
String.valueOf(channel.getId()),
|
||||
channel.toString());
|
||||
}
|
||||
|
||||
|
@ -151,8 +151,8 @@ class OioServerSocketPipelineSink extends AbstractChannelSink {
|
||||
new IoWorkerRunnable(
|
||||
new ThreadRenamingRunnable(
|
||||
new Boss(channel),
|
||||
"OldIO", "ServerBoss", String.valueOf(id),
|
||||
channel.toString())));
|
||||
"OldIO", "ServerBoss", null,
|
||||
String.valueOf(id), channel.toString())));
|
||||
bossStarted = true;
|
||||
} catch (Throwable t) {
|
||||
future.setFailure(t);
|
||||
@ -218,7 +218,9 @@ class OioServerSocketPipelineSink extends AbstractChannelSink {
|
||||
new IoWorkerRunnable(
|
||||
new ThreadRenamingRunnable(
|
||||
new OioWorker(acceptedChannel),
|
||||
"OldIO", "ServerWorker", id + "-" + acceptedChannel.getId(),
|
||||
"OldIO", "ServerWorker",
|
||||
String.valueOf(id),
|
||||
String.valueOf(acceptedChannel.getId()),
|
||||
acceptedChannel.toString())));
|
||||
} catch (Exception e) {
|
||||
logger.warn(
|
||||
|
@ -211,7 +211,7 @@ public class HashedWheelTimer implements Timer {
|
||||
|
||||
workerThread = threadFactory.newThread(new ThreadRenamingRunnable(
|
||||
worker,
|
||||
"HashedWheelTimer", null,
|
||||
"HashedWheelTimer", null, null,
|
||||
String.valueOf(id.incrementAndGet()), null));
|
||||
|
||||
// Misuse check
|
||||
|
@ -30,13 +30,13 @@ public interface ThreadNameDeterminer {
|
||||
*/
|
||||
ThreadNameDeterminer PROPOSED = new ThreadNameDeterminer() {
|
||||
public String determineThreadName(String current, String service,
|
||||
String category, String id, String comment) throws Exception {
|
||||
String category, String parentId, String id, String comment) throws Exception {
|
||||
|
||||
String newName =
|
||||
(format("", service, " ") +
|
||||
format("", category, " ") +
|
||||
format("#", id, " ") +
|
||||
format("(", comment, ")")).trim();
|
||||
(format("", " ", service) +
|
||||
format("", " ", category) +
|
||||
format("#", " ", parentId, id) +
|
||||
format("(", ")", comment)).trim();
|
||||
if (newName.length() == 0) {
|
||||
return null;
|
||||
} else {
|
||||
@ -44,12 +44,22 @@ public interface ThreadNameDeterminer {
|
||||
}
|
||||
}
|
||||
|
||||
private String format(String prefix, String s, String postfix) {
|
||||
if (s.length() == 0) {
|
||||
return "";
|
||||
} else {
|
||||
return prefix + s + postfix;
|
||||
private String format(String prefix, String postfix, String... components) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (String c: components) {
|
||||
if (c.length() == 0) {
|
||||
continue;
|
||||
}
|
||||
buf.append(c);
|
||||
buf.append(':');
|
||||
}
|
||||
|
||||
if (buf.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
buf.setLength(buf.length() - 1); // Remove trailing ':'
|
||||
return prefix + buf + postfix;
|
||||
}
|
||||
};
|
||||
|
||||
@ -59,7 +69,7 @@ public interface ThreadNameDeterminer {
|
||||
*/
|
||||
ThreadNameDeterminer CURRENT = new ThreadNameDeterminer() {
|
||||
public String determineThreadName(String current, String service,
|
||||
String category, String id, String comment) throws Exception {
|
||||
String category, String parentId, String id, String comment) throws Exception {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@ -68,9 +78,10 @@ public interface ThreadNameDeterminer {
|
||||
* Overrides the thread name proposed by {@link ThreadRenamingRunnable}.
|
||||
*
|
||||
* @param current the current thread name
|
||||
* @param service the service name (e.g. <tt>"New I/O"</tt> or <tt>"Old I/O"</tt>)
|
||||
* @param category the category name (e.g. <tt>"server boss"</tt> or <tt>"client worker"</tt>)
|
||||
* @param id the thread ID (e.g. <tt>"1"</tt> or <tt>"1-3"</tt>)
|
||||
* @param service the service name (e.g. <tt>"NewIO"</tt> or <tt>"OldIO"</tt>)
|
||||
* @param category the category name (e.g. <tt>"ServerBoss"</tt> or <tt>"ClientWorker"</tt>)
|
||||
* @param parentId the parent thread ID (e.g. <tt>"1"</tt>)
|
||||
* @param id the thread ID (e.g. <tt>"3"</tt>)
|
||||
* @param comment the optional comment which might help debugging
|
||||
*
|
||||
* @return the actual new thread name.
|
||||
@ -79,5 +90,5 @@ public interface ThreadNameDeterminer {
|
||||
*/
|
||||
String determineThreadName(
|
||||
String current,
|
||||
String service, String category, String id, String comment) throws Exception;
|
||||
String service, String category, String parentId, String id, String comment) throws Exception;
|
||||
}
|
||||
|
@ -42,8 +42,7 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
|
||||
private static final Pattern SERVICE_PATTERN = Pattern.compile("[a-zA-Z0-9]*");
|
||||
private static final Pattern CATEGORY_PATTERN = SERVICE_PATTERN;
|
||||
private static final Pattern ID_PATTERN = Pattern.compile("^[-_a-zA-Z0-9]*$");
|
||||
|
||||
private static final Pattern ID_PATTERN = SERVICE_PATTERN;
|
||||
|
||||
private static volatile ThreadNameDeterminer threadNameDeterminer =
|
||||
ThreadNameDeterminer.PROPOSED;
|
||||
@ -78,16 +77,17 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
*
|
||||
* @return {@code true} if and only if the thread was renamed
|
||||
*/
|
||||
public static boolean renameThread(Thread thread, String service, String category, String id, String comment) {
|
||||
public static boolean renameThread(Thread thread, String service, String category, String parentId, String id, String comment) {
|
||||
if (thread == null) {
|
||||
throw new NullPointerException("thread");
|
||||
}
|
||||
|
||||
validateNameComponents(service, category, id);
|
||||
validateNameComponents(service, category, parentId, id);
|
||||
|
||||
// Normalize the parameters.
|
||||
service = service != null? service : "";
|
||||
category = category != null? category : "";
|
||||
parentId = parentId != null? parentId : "";
|
||||
id = id != null? id : "";
|
||||
comment = comment != null? comment : "";
|
||||
|
||||
@ -96,7 +96,7 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
String newThreadName = null;
|
||||
try {
|
||||
newThreadName = getThreadNameDeterminer().determineThreadName(
|
||||
oldThreadName, service, category, id, comment);
|
||||
oldThreadName, service, category, parentId, id, comment);
|
||||
} catch (Throwable t) {
|
||||
logger.warn("Failed to determine the thread name", t);
|
||||
}
|
||||
@ -121,7 +121,7 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
return renamed;
|
||||
}
|
||||
|
||||
private static void validateNameComponents(String service, String category, String id) {
|
||||
private static void validateNameComponents(String service, String category, String parentId, String id) {
|
||||
if (service != null && !SERVICE_PATTERN.matcher(service).matches()) {
|
||||
throw new IllegalArgumentException(
|
||||
"service: " + service +
|
||||
@ -134,6 +134,12 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
" (expected: " + CATEGORY_PATTERN.pattern() + ')');
|
||||
}
|
||||
|
||||
if (parentId != null && !ID_PATTERN.matcher(parentId).matches()) {
|
||||
throw new IllegalArgumentException(
|
||||
"parentId: " + parentId +
|
||||
" (expected: " + ID_PATTERN.pattern() + ')');
|
||||
}
|
||||
|
||||
if (id != null && !ID_PATTERN.matcher(id).matches()) {
|
||||
throw new IllegalArgumentException(
|
||||
"id: " + id +
|
||||
@ -144,6 +150,7 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
private final Runnable runnable;
|
||||
private final String service;
|
||||
private final String category;
|
||||
private final String parentId;
|
||||
private final String id;
|
||||
private final String comment;
|
||||
|
||||
@ -154,15 +161,16 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
*/
|
||||
public ThreadRenamingRunnable(
|
||||
Runnable runnable,
|
||||
String service, String category, String id, String comment) {
|
||||
String service, String category, String parentId, String id, String comment) {
|
||||
if (runnable == null) {
|
||||
throw new NullPointerException("runnable");
|
||||
}
|
||||
|
||||
validateNameComponents(service, category, id);
|
||||
validateNameComponents(service, category, parentId, id);
|
||||
this.runnable = runnable;
|
||||
this.service = service;
|
||||
this.category = category;
|
||||
this.parentId = parentId;
|
||||
this.id = id;
|
||||
this.comment = comment;
|
||||
}
|
||||
@ -173,7 +181,7 @@ public class ThreadRenamingRunnable implements Runnable {
|
||||
|
||||
// Change the thread name before starting the actual runnable.
|
||||
final boolean renamed = renameThread(
|
||||
Thread.currentThread(), service, category, id, comment);
|
||||
Thread.currentThread(), service, category, parentId, id, comment);
|
||||
|
||||
// Run the actual runnable and revert the name back when it ends.
|
||||
try {
|
||||
|
@ -34,7 +34,7 @@ public class ThreadRenamingRunnableTest {
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void shouldNotAllowNullRunnable() throws Exception {
|
||||
new ThreadRenamingRunnable(null, "a", "b", "c", "d");
|
||||
new ThreadRenamingRunnable(null, "a", "b", "c", "d", "e");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -46,7 +46,7 @@ public class ThreadRenamingRunnableTest {
|
||||
public void run() {
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
}
|
||||
}, null, null, null, null));
|
||||
}, null, null, null, null, null));
|
||||
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
}
|
||||
@ -60,7 +60,7 @@ public class ThreadRenamingRunnableTest {
|
||||
public void run() {
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
}
|
||||
}, "", "", "", ""));
|
||||
}, "", "", "", "", ""));
|
||||
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
}
|
||||
@ -72,10 +72,10 @@ public class ThreadRenamingRunnableTest {
|
||||
e.execute(new ThreadRenamingRunnable(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
assertEquals("a b #c (d)", Thread.currentThread().getName());
|
||||
assertEquals("a b #c:d (e)", Thread.currentThread().getName());
|
||||
assertFalse(oldThreadName.equals(Thread.currentThread().getName()));
|
||||
}
|
||||
}, "a", "b", "c", "d"));
|
||||
}, "a", "b", "c", "d", "e"));
|
||||
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
}
|
||||
@ -107,7 +107,7 @@ public class ThreadRenamingRunnableTest {
|
||||
public void run() {
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
}
|
||||
}, "a", "b", "c", "d"));
|
||||
}, "a", "b", "c", "d", "e"));
|
||||
} finally {
|
||||
System.setSecurityManager(null);
|
||||
assertEquals(oldThreadName, Thread.currentThread().getName());
|
||||
|
Loading…
Reference in New Issue
Block a user