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

fix(java): ThreadLocalFury and ThreadPoolFury prioritize using the user classloader #1907

Merged
merged 5 commits into from
Oct 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class ThreadLocalFury extends AbstractThreadSafeFury {
private Consumer<Fury> factoryCallback;
private final Map<LoaderBinding, Object> allFury;

private ClassLoader classLoader;

public ThreadLocalFury(Function<ClassLoader, Fury> furyFactory) {
factoryCallback = f -> {};
allFury = Collections.synchronizedMap(new WeakHashMap<>());
Expand All @@ -58,7 +60,11 @@ public ThreadLocalFury(Function<ClassLoader, Fury> furyFactory) {
() -> {
LoaderBinding binding = new LoaderBinding(furyFactory);
binding.setBindingCallback(factoryCallback);
binding.setClassLoader(Thread.currentThread().getContextClassLoader());
ClassLoader cl =
classLoader == null
? Thread.currentThread().getContextClassLoader()
: classLoader;
binding.setClassLoader(cl);
allFury.put(binding, null);
return binding;
});
Expand Down Expand Up @@ -258,6 +264,7 @@ public void setClassLoader(ClassLoader classLoader) {

@Override
public void setClassLoader(ClassLoader classLoader, StagingType stagingType) {
this.classLoader = classLoader;
bindingThreadLocal.get().setClassLoader(classLoader, stagingType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ public class FuryPooledObjectFactory {
*/
final Cache<ClassLoader, ClassLoaderFuryPooled> classLoaderFuryPooledCache;

private volatile ClassLoader classLoader = null;

/** ThreadLocal: ClassLoader. */
private final ThreadLocal<ClassLoader> classLoaderLocal =
ThreadLocal.withInitial(
() -> {
if (classLoader != null) {
return classLoader;
}
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = Fury.class.getClassLoader();
Expand Down Expand Up @@ -111,6 +116,7 @@ public void setClassLoader(ClassLoader classLoader, LoaderBinding.StagingType st
// may be used to clear some classloader
classLoader = Fury.class.getClassLoader();
}
this.classLoader = classLoader;
classLoaderLocal.set(classLoader);
getOrAddCache(classLoader);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.fury.classloader;

import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.config.Language;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ThreadSafeFuryClassLoaderTest {

static class MyClassLoader extends ClassLoader {}

@Test
void testFuryThreadLocalUseProvidedClassLoader() throws InterruptedException {
final MyClassLoader myClassLoader = new MyClassLoader();
final ThreadSafeFury fury =
Fury.builder()
.withClassLoader(myClassLoader)
.withLanguage(Language.JAVA)
.requireClassRegistration(false)
.buildThreadLocalFury();
fury.setClassLoader(myClassLoader);

Thread thread =
new Thread(
() -> {
final ClassLoader t = fury.getClassLoader();
Assert.assertEquals(t, myClassLoader);
});
thread.start();
thread.join();
}

@Test
void testFuryPoolUseProvidedClassLoader() throws InterruptedException {
final MyClassLoader myClassLoader = new MyClassLoader();
final ThreadSafeFury fury =
Fury.builder()
.withClassLoader(myClassLoader)
.withLanguage(Language.JAVA)
.requireClassRegistration(false)
.buildThreadSafeFuryPool(1, 1);
fury.setClassLoader(myClassLoader);

Thread thread =
new Thread(
() -> {
final ClassLoader t = fury.getClassLoader();
Assert.assertEquals(t, myClassLoader);
});
thread.start();
thread.join();
}
}
Loading