#2027 Allow JDK's ThreadLocalRandom to be used for agents running Java7+

This commit is contained in:
HyunGil Jeong
2016-08-31 19:07:38 +09:00
parent 1be76c3798
commit 26cb056ba9
5 changed files with 160 additions and 2 deletions
@@ -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();
}
}
@@ -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) {
@@ -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();
}
}
@@ -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();
}
@@ -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<? extends ThreadLocalRandomFactory> threadLocalRandomFactoryClass =
(Class<? extends ThreadLocalRandomFactory>) 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();
}
}