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 1 commit
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 All @@ -24,6 +28,12 @@ public DatadogClassLoader(URL bootstrapJarLocation, URL agentJarLocation, ClassL
bootstrapProxy = new BootstrapClassLoaderProxy(new URL[] {bootstrapJarLocation}, null);
}

/** Public for testing only */
@Override
public Object getClassLoadingLock(String className) {
return super.getClassLoadingLock(className);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this method already had enough visibility to not needing this. If you remove this method tests still pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Looks like a protected method is visible to spock tests. Pretty useful for avoiding this problem of escalating exposure for testing. Removing this method.


@Override
public URL getResource(String resourceName) {
final URL bootstrapResource = bootstrapProxy.getResource(resourceName);
Expand All @@ -45,6 +55,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,39 @@
package datadog.trace.bootstrap

import spock.lang.Specification

import java.util.concurrent.Phaser

class DatadogClassLoaderTest extends Specification {
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 thread = new Thread() {
@Override
void run() {
synchronized (ddLoader.getClassLoadingLock(className1)) {
threadHoldLockPhase.arrive()
acquireLockFromMainThreadPhase.arriveAndAwaitAdvance()
}
}
}
thread.start()

threadHoldLockPhase.arriveAndAwaitAdvance()
synchronized (ddLoader.getClassLoadingLock(className2)) {
acquireLockFromMainThreadPhase.arrive()
}
thread.join()
boolean applicationDidNotDeadlock = true

then:
applicationDidNotDeadlock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failure of this test would result in test runs just hanging forever. Would it be possible to implement this so test actually exits on failure? E.g. using two additional threads and making 'main' thread just wait reasonable amount of time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I added another thread and used spock's timeout annotation to fail this test after 1 minute.

}
}