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

Disable recordCreateAt from RemoteClassLoader #258

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/hudson/remoting/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ protected Channel(@Nonnull ChannelBuilder settings, @Nonnull CommandTransport tr

if(internalExport(IChannel.class, this, false)!=1)
throw new AssertionError(); // export number 1 is reserved for the channel itself
remoteChannel = RemoteInvocationHandler.wrap(this,1,IChannel.class,true,false,false);
remoteChannel = RemoteInvocationHandler.wrap(this, 1, IChannel.class, true, false, false, true);

this.remoteCapability = transport.getRemoteCapability();
this.pipeWriter = new PipeWriter(createPipeWriterExecutor());
Expand Down Expand Up @@ -727,7 +727,7 @@ private ExecutorService createPipeWriterExecutor() {
*/
@Override
public <T> T export(Class<T> type, T instance) {
return export(type, instance, true, true);
return export(type, instance, true, true, true);
}

/**
Expand All @@ -740,11 +740,12 @@ public <T> T export(Class<T> type, T instance) {
* used by {@link RemoteClassLoader}.
*
* To create proxies for objects inside remoting, pass in false.
* @param recordCreatedAt as in {@link Command#Command(boolean)}
* @return Reference to the exported instance wrapped by {@link RemoteInvocationHandler}.
* {@code null} if the input instance is {@code null}.
*/
@Nullable
/*package*/ <T> T export(Class<T> type, @CheckForNull T instance, boolean userProxy, boolean userScope) {
/*package*/ <T> T export(Class<T> type, @CheckForNull T instance, boolean userProxy, boolean userScope, boolean recordCreatedAt) {
if(instance==null) {
return null;
}
Expand All @@ -763,7 +764,7 @@ public <T> T export(Class<T> type, T instance) {
// either local side will auto-unexport, or the remote side will unexport when it's GC-ed
boolean autoUnexportByCaller = exportedObjects.isRecording();
final int id = internalExport(type, instance, autoUnexportByCaller);
return RemoteInvocationHandler.wrap(null, id, type, userProxy, autoUnexportByCaller, userScope);
return RemoteInvocationHandler.wrap(null, id, type, userProxy, autoUnexportByCaller, userScope, recordCreatedAt);
}

/*package*/ <T> int internalExport(Class<T> clazz, T instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class ImportedClassLoaderTable {
*/
@Nonnull
public synchronized ClassLoader get(int oid) {
return get(RemoteInvocationHandler.wrap(channel,oid,IClassLoader.class,false,false,false));
return get(RemoteInvocationHandler.wrap(channel, oid, IClassLoader.class, false, false, false, false));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/remoting/RemoteClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public static IClassLoader export(@Nonnull ClassLoader cl, @Nonnull Channel loca
// Remote classloader operates in the System scope (JENKINS-45294).
// It's probably YOLO, but otherwise the termination calls may be unable
// to execute correctly.
return local.export(IClassLoader.class, new ClassLoaderProxy(cl,local), false, false);
return local.export(IClassLoader.class, new ClassLoaderProxy(cl,local), false, false, false);
}

public static void pin(ClassLoader cl, Channel local) {
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/hudson/remoting/RemoteInvocationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final class RemoteInvocationHandler implements InvocationHandler, Serializable {
/**
* Diagnostic information that remembers where the proxy was created.
*/
private final Throwable origin;
private final @CheckForNull Throwable origin;

/**
* Indicates that the handler operates in the user space.
Expand All @@ -127,33 +127,38 @@ final class RemoteInvocationHandler implements InvocationHandler, Serializable {
*/
private final boolean userSpace;

/** @see Command#Command(boolean) */
private final boolean recordCreatedAt;

/**
* Creates a proxy that wraps an existing OID on the remote.
*/
RemoteInvocationHandler(Channel channel, int id, boolean userProxy,
private RemoteInvocationHandler(Channel channel, int id, boolean userProxy,
boolean autoUnexportByCaller, boolean userSpace,
Class proxyType) {
Class proxyType, boolean recordCreatedAt) {
this.channel = channel == null ? null : channel.ref();
this.oid = id;
this.userProxy = userProxy;
this.origin = new Exception("Proxy "+toString()+" was created for "+proxyType);
this.origin = recordCreatedAt ? new Exception("Proxy " + toString() + " was created for " + proxyType) : null;
this.autoUnexportByCaller = autoUnexportByCaller;
this.userSpace = userSpace;
this.recordCreatedAt = recordCreatedAt;
}

/**
* Wraps an OID to the typed wrapper.
*
* @param userProxy If {@code true} (recommended), all commands will be wrapped into {@link UserRequest}s.
* @param userSpace If {@code true} (recommended), the requests will be executed in a user scope
* @param recordCreatedAt as in {@link Command#Command(boolean)}
*/
@Nonnull
static <T> T wrap(Channel channel, int id, Class<T> type, boolean userProxy, boolean autoUnexportByCaller, boolean userSpace) {
static <T> T wrap(Channel channel, int id, Class<T> type, boolean userProxy, boolean autoUnexportByCaller, boolean userSpace, boolean recordCreatedAt) {
ClassLoader cl = type.getClassLoader();
// if the type is a JDK-defined type, classloader should be for IReadResolve
if(cl==null || cl==ClassLoader.getSystemClassLoader())
cl = IReadResolve.class.getClassLoader();
RemoteInvocationHandler handler = new RemoteInvocationHandler(channel, id, userProxy, autoUnexportByCaller, userSpace, type);
RemoteInvocationHandler handler = new RemoteInvocationHandler(channel, id, userProxy, autoUnexportByCaller, userSpace, type, recordCreatedAt);
if (channel != null) {
if (!autoUnexportByCaller) {
UNEXPORTER.watch(handler);
Expand Down Expand Up @@ -270,8 +275,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl

boolean async = method.isAnnotationPresent(Asynchronous.class);
RPCRequest req = userSpace
? new UserRPCRequest(oid, method, args, userProxy ? dc.getClassLoader() : null)
: new RPCRequest(oid, method, args, userProxy ? dc.getClassLoader() : null);
? new UserRPCRequest(oid, method, args, userProxy ? dc.getClassLoader() : null, recordCreatedAt)
: new RPCRequest(oid, method, args, userProxy ? dc.getClassLoader() : null, recordCreatedAt);
try {
if(userProxy) {
if (async) channelOrFail().callAsync(req);
Expand Down Expand Up @@ -353,7 +358,7 @@ private static class PhantomReferenceImpl extends PhantomReference<RemoteInvocat
/**
* The origin from where the object was created.
*/
private Throwable origin;
private @CheckForNull Throwable origin;
/**
* The reference to the channel on which to unexport.
*/
Expand Down Expand Up @@ -879,11 +884,8 @@ static class RPCRequest extends Request<Serializable,Throwable> implements Deleg
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "We're fine with the default null on the recipient side")
private transient ClassLoader classLoader;

public RPCRequest(int oid, Method m, Object[] arguments) {
this(oid,m,arguments,null);
}

public RPCRequest(int oid, Method m, Object[] arguments, ClassLoader cl) {
private RPCRequest(int oid, Method m, Object[] arguments, ClassLoader cl, boolean recordCreatedAt) {
super(recordCreatedAt);
this.oid = oid;
this.arguments = arguments;
declaringClassName = m.getDeclaringClass().getName();
Expand Down Expand Up @@ -989,12 +991,12 @@ public String toString() {
* this can be used to send a method call to user-level objects, and
* classes for the parameters and the return value are sent remotely if needed.
*/
static class UserRPCRequest extends RPCRequest {
private static class UserRPCRequest extends RPCRequest {

private static final long serialVersionUID = -9185841650347902580L;

public UserRPCRequest(int oid, Method m, Object[] arguments, ClassLoader cl) {
super(oid, m, arguments, cl);
UserRPCRequest(int oid, Method m, Object[] arguments, ClassLoader cl, boolean recordCreatedAt) {
super(oid, m, arguments, cl, recordCreatedAt);
}

// Same implementation as UserRequest
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/hudson/remoting/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ public abstract class Request<RSP extends Serializable,EXC extends Throwable> ex
@Deprecated
/*package*/ volatile transient Future<?> lastIo;

@SuppressFBWarnings(value="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification="That is why we synchronize on the class.")
Request() {
this(true);
}

@SuppressFBWarnings(value="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification="That is why we synchronize on the class.")
Request(boolean recordCreatedAt) {
super(recordCreatedAt);
synchronized(Request.class) {
id = nextId++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/hudson/remoting/ChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private static class RMIObjectExportedCallable<TInterface> implements Callable<T
@Override
public TInterface call() throws Exception {
// UserProxy is used only for the user space, otherwise it will be wrapped into UserRequest
return Channel.current().export(clazz, object, userSpace, userSpace);
return Channel.current().export(clazz, object, userSpace, userSpace, true);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/hudson/remoting/PipeWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PipeWriterTest extends RmiTestBase implements Serializable, PipeWri
protected void setUp() throws Exception {
super.setUp();
// Checker operates using the user-space RMI
checker = channel.export(PipeWriterTestChecker.class, this, false, true);
checker = channel.export(PipeWriterTestChecker.class, this, false, true, true);
}

/**
Expand Down