From c04e3c0987aec7bda06260caddef8add7f62955d Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Fri, 19 Nov 2021 17:36:49 -0600 Subject: [PATCH] add typed Indexed property to fix ThreadNum --- .../engine/core/lifecycle/IndexedThreadFactory.java | 13 ++++++++----- .../java/io/nosqlbench/nb/api/metadata/Indexed.java | 5 +++++ .../shared/nondeterministic/to_int/ThreadNum.java | 5 +++++ .../shared/nondeterministic/to_long/ThreadNum.java | 5 +++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 nb-api/src/main/java/io/nosqlbench/nb/api/metadata/Indexed.java diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/IndexedThreadFactory.java b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/IndexedThreadFactory.java index e510db5c7..e6f59939e 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/IndexedThreadFactory.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/IndexedThreadFactory.java @@ -18,6 +18,8 @@ package io.nosqlbench.engine.core.lifecycle; +import io.nosqlbench.nb.api.metadata.Indexed; + import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; @@ -36,7 +38,7 @@ public class IndexedThreadFactory implements ThreadFactory { this.uncaughtExceptionHandler = exceptionHandler; } - public class IndexedThread extends Thread { + public class IndexedThread extends Thread implements Indexed { private final int threadIndex; private String metricName = "default-name-" + Thread.currentThread().getName(); @@ -46,10 +48,6 @@ public class IndexedThreadFactory implements ThreadFactory { this.threadIndex = threadIndex; } - public int getThreadIndex() { - return threadIndex; - } - public void setMetricName(String metricName) { this.metricName = metricName; } @@ -57,6 +55,11 @@ public class IndexedThreadFactory implements ThreadFactory { public String getMetricName() { return metricName; } + + @Override + public int getIndex() { + return threadIndex; + } } @Override diff --git a/nb-api/src/main/java/io/nosqlbench/nb/api/metadata/Indexed.java b/nb-api/src/main/java/io/nosqlbench/nb/api/metadata/Indexed.java new file mode 100644 index 000000000..10b71df53 --- /dev/null +++ b/nb-api/src/main/java/io/nosqlbench/nb/api/metadata/Indexed.java @@ -0,0 +1,5 @@ +package io.nosqlbench.nb.api.metadata; + +public interface Indexed { + int getIndex(); +} diff --git a/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_int/ThreadNum.java b/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_int/ThreadNum.java index 6957e708e..7fd61fbe1 100644 --- a/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_int/ThreadNum.java +++ b/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_int/ThreadNum.java @@ -18,6 +18,7 @@ package io.nosqlbench.virtdata.library.basics.shared.nondeterministic.to_int; +import io.nosqlbench.nb.api.metadata.Indexed; import io.nosqlbench.virtdata.api.annotations.Categories; import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; @@ -41,6 +42,10 @@ public class ThreadNum implements LongToIntFunction { private final ThreadLocal threadLocalInt = new ThreadLocal() { @Override protected Integer initialValue() { + if (Thread.currentThread() instanceof Indexed ) { + return ((Indexed)Thread.currentThread()).getIndex(); + } + Matcher matcher = pattern.matcher(Thread.currentThread().getName()); if (matcher.matches()) { return Integer.valueOf(matcher.group(1)); diff --git a/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_long/ThreadNum.java b/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_long/ThreadNum.java index 29033874f..f2b0951c2 100644 --- a/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_long/ThreadNum.java +++ b/virtdata-lib-basics/src/main/java/io/nosqlbench/virtdata/library/basics/shared/nondeterministic/to_long/ThreadNum.java @@ -18,6 +18,7 @@ package io.nosqlbench.virtdata.library.basics.shared.nondeterministic.to_long; +import io.nosqlbench.nb.api.metadata.Indexed; import io.nosqlbench.virtdata.api.annotations.Categories; import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; @@ -34,6 +35,10 @@ public class ThreadNum implements LongUnaryOperator { private final ThreadLocal threadLocalInt = new ThreadLocal() { @Override protected Long initialValue() { + if (Thread.currentThread() instanceof Indexed) { + return (long) ((Indexed)Thread.currentThread()).getIndex(); + } + Matcher matcher = pattern.matcher(Thread.currentThread().getName()); if (matcher.matches()) { return Long.valueOf(matcher.group(1));