From 26cb056ba992fb28b8807f00a594aba158e4991e Mon Sep 17 00:00:00 2001 From: HyunGil Jeong Date: Tue, 30 Aug 2016 17:15:04 +0900 Subject: [PATCH] #2027 Allow JDK's ThreadLocalRandom to be used for agents running Java7+ --- .../util/jdk/JdkThreadLocalRandomFactory.java | 30 ++++++++ .../pinpoint/bootstrap/context/SpanId.java | 4 +- .../jdk/PinpointThreadLocalRandomFactory.java | 30 ++++++++ .../util/jdk/ThreadLocalRandomFactory.java | 26 +++++++ .../util/jdk/ThreadLocalRandomUtils.java | 72 +++++++++++++++++++ 5 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 bootstrap-core-optional/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/JdkThreadLocalRandomFactory.java create mode 100644 bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/PinpointThreadLocalRandomFactory.java create mode 100644 bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomFactory.java create mode 100644 bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomUtils.java diff --git a/bootstrap-core-optional/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/JdkThreadLocalRandomFactory.java b/bootstrap-core-optional/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/JdkThreadLocalRandomFactory.java new file mode 100644 index 000000000..33460ee6c --- /dev/null +++ b/bootstrap-core-optional/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/JdkThreadLocalRandomFactory.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 Naver Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.bootstrap.util.jdk; + +import java.util.Random; + +/** + * @author HyunGil Jeong + */ +public class JdkThreadLocalRandomFactory implements ThreadLocalRandomFactory { + + @Override + public Random current() { + return java.util.concurrent.ThreadLocalRandom.current(); + } +} diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/SpanId.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/SpanId.java index 3f537b180..047962f1f 100644 --- a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/SpanId.java +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/SpanId.java @@ -18,7 +18,7 @@ package com.navercorp.pinpoint.bootstrap.context; import java.util.Random; -import com.navercorp.pinpoint.bootstrap.util.jdk.ThreadLocalRandom; +import com.navercorp.pinpoint.bootstrap.util.jdk.ThreadLocalRandomUtils; /** * @author emeroad @@ -38,7 +38,7 @@ public class SpanId { // Changed to ThreadLocalRandom because unique value per thread will be enough. // If you need to change Random implementation, modify this method. private static Random getRandom() { - return ThreadLocalRandom.current(); + return ThreadLocalRandomUtils.current(); } private static long createSpanId(Random seed) { diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/PinpointThreadLocalRandomFactory.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/PinpointThreadLocalRandomFactory.java new file mode 100644 index 000000000..f823dcdcd --- /dev/null +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/PinpointThreadLocalRandomFactory.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 Naver Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.bootstrap.util.jdk; + +import java.util.Random; + +/** + * @author HyunGil Jeong + */ +public class PinpointThreadLocalRandomFactory implements ThreadLocalRandomFactory { + + @Override + public Random current() { + return ThreadLocalRandom.current(); + } +} diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomFactory.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomFactory.java new file mode 100644 index 000000000..e87626845 --- /dev/null +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomFactory.java @@ -0,0 +1,26 @@ +/* + * Copyright 2016 Naver Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.bootstrap.util.jdk; + +import java.util.Random; + +/** + * @author HyunGil Jeong + */ +public interface ThreadLocalRandomFactory { + Random current(); +} diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomUtils.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomUtils.java new file mode 100644 index 000000000..4cea47822 --- /dev/null +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/jdk/ThreadLocalRandomUtils.java @@ -0,0 +1,72 @@ +/* + * Copyright 2016 Naver Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.bootstrap.util.jdk; + +import com.navercorp.pinpoint.bootstrap.logging.PLogger; +import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory; +import com.navercorp.pinpoint.common.util.JvmUtils; +import com.navercorp.pinpoint.common.util.JvmVersion; + +import java.util.Random; + +/** + * @author HyunGil Jeong + */ +public class ThreadLocalRandomUtils { + + private static final PLogger LOGGER = PLoggerFactory.getLogger(ThreadLocalRandomUtils.class); + + private static final ThreadLocalRandomFactory THREAD_LOCAL_RANDOM_FACTORY = createThreadLocalRandomFactory(); + + // Jdk 7+ + private static final String DEFAULT_THREAD_LOCAL_RANDOM_FACTORY = "com.navercorp.pinpoint.bootstrap.util.jdk.JdkThreadLocalRandomFactory"; + + private ThreadLocalRandomUtils() { + throw new IllegalAccessError(); + } + + private static final ThreadLocalRandomFactory createThreadLocalRandomFactory() { + final JvmVersion jvmVersion = JvmUtils.getVersion(); + if (jvmVersion == JvmVersion.JAVA_6) { + return new PinpointThreadLocalRandomFactory(); + } else if (jvmVersion.onOrAfter(JvmVersion.JAVA_7)) { + try { + final Class threadLocalRandomFactoryClass = + (Class) Class.forName(DEFAULT_THREAD_LOCAL_RANDOM_FACTORY, true, null); + return threadLocalRandomFactoryClass.newInstance(); + } catch (ClassNotFoundException e) { + logError(e); + } catch (InstantiationException e) { + logError(e); + } catch (IllegalAccessException e) { + logError(e); + } + return new PinpointThreadLocalRandomFactory(); + } else { + throw new RuntimeException("Unsupported jvm version " + jvmVersion); + } + + } + + private static void logError(Exception e) { + LOGGER.warn("Error creating JdkThreadLocalRandomFactory", e); + } + + public static Random current() { + return THREAD_LOCAL_RANDOM_FACTORY.current(); + } +}