Skip to content
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

Register Datadog classloader as parallel capable #448

Merged
merged 2 commits into from
Aug 20, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

/** Classloader used to run the core datadog agent. */
public class DatadogClassLoader extends URLClassLoader {
static {
ClassLoader.registerAsParallelCapable();
}

// Calling java.lang.instrument.Instrumentation#appendToBootstrapClassLoaderSearch
// adds a jar to the bootstrap class lookup, but not to the resource lookup.
// As a workaround, we keep a reference to the bootstrap jar
Expand Down Expand Up @@ -45,6 +49,10 @@ public BootstrapClassLoaderProxy getBootstrapProxy() {
* <p>This class is thread safe.
*/
public static final class BootstrapClassLoaderProxy extends URLClassLoader {
static {
ClassLoader.registerAsParallelCapable();
}

public BootstrapClassLoaderProxy(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package datadog.trace.bootstrap

import spock.lang.Specification
import spock.lang.Timeout

import java.util.concurrent.Phaser
import java.util.concurrent.TimeUnit

class DatadogClassLoaderTest extends Specification {
@Timeout(value = 60, unit = TimeUnit.SECONDS)
def "DD classloader does not lock classloading around instance" () {
setup:
def className1 = 'some/class/Name1'
def className2 = 'some/class/Name2'
final URL loc = getClass().getProtectionDomain().getCodeSource().getLocation()
final DatadogClassLoader ddLoader = new DatadogClassLoader(loc, loc, (ClassLoader) null)
final Phaser threadHoldLockPhase = new Phaser(2)
final Phaser acquireLockFromMainThreadPhase = new Phaser(2)

when:
final Thread thread1 = new Thread() {
@Override
void run() {
synchronized (ddLoader.getClassLoadingLock(className1)) {
threadHoldLockPhase.arrive()
acquireLockFromMainThreadPhase.arriveAndAwaitAdvance()
}
}
}
thread1.start()

final Thread thread2 = new Thread() {
@Override
void run() {
threadHoldLockPhase.arriveAndAwaitAdvance()
synchronized (ddLoader.getClassLoadingLock(className2)) {
acquireLockFromMainThreadPhase.arrive()
}
}
}
thread2.start()
thread1.join()
thread2.join()
boolean applicationDidNotDeadlock = true

then:
applicationDidNotDeadlock
}
}