This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git
The following commit(s) were added to refs/heads/main by this push:
new a45886ef fix(java): ThreadLocalFury and ThreadPoolFury prioritize
using the user classloader (#1907)
a45886ef is described below
commit a45886efe81bf223f6c33574c69e7769c8ab0aba
Author: Aliothmoon <[email protected]>
AuthorDate: Sat Oct 26 01:52:51 2024 +0800
fix(java): ThreadLocalFury and ThreadPoolFury prioritize using the user
classloader (#1907)
## What does this PR do?
ThreadLocalFury and ThreadPoolFury prioritize using the user-specified
ClassLoader
<!-- Describe the purpose of this PR. -->
## Related issues
[1884](https://github.com/apache/fury/issues/1884)
[1878](https://github.com/apache/fury/issues/1878)
<!--
Is there any related issue? Please attach here.
- #xxxx0
- #xxxx1
- #xxxx2
-->
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
---------
Co-authored-by: Shawn Yang <[email protected]>
---
.../main/java/org/apache/fury/ThreadLocalFury.java | 9 ++-
.../apache/fury/pool/FuryPooledObjectFactory.java | 6 ++
.../classloader/ThreadSafeFuryClassLoaderTest.java | 73 ++++++++++++++++++++++
3 files changed, 87 insertions(+), 1 deletion(-)
diff --git a/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java
b/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java
index 3e2abb86..032a2506 100644
--- a/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java
+++ b/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java
@@ -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<>());
@@ -58,7 +60,11 @@ public class ThreadLocalFury extends AbstractThreadSafeFury {
() -> {
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;
});
@@ -258,6 +264,7 @@ public class ThreadLocalFury extends AbstractThreadSafeFury
{
@Override
public void setClassLoader(ClassLoader classLoader, StagingType stagingType)
{
+ this.classLoader = classLoader;
bindingThreadLocal.get().setClassLoader(classLoader, stagingType);
}
diff --git
a/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java
b/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java
index 17e56a63..9f8e2a28 100644
---
a/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java
+++
b/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java
@@ -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();
@@ -111,6 +116,7 @@ public class FuryPooledObjectFactory {
// may be used to clear some classloader
classLoader = Fury.class.getClassLoader();
}
+ this.classLoader = classLoader;
classLoaderLocal.set(classLoader);
getOrAddCache(classLoader);
}
diff --git
a/java/fury-core/src/test/java/org/apache/fury/classloader/ThreadSafeFuryClassLoaderTest.java
b/java/fury-core/src/test/java/org/apache/fury/classloader/ThreadSafeFuryClassLoaderTest.java
new file mode 100644
index 00000000..f01ff024
--- /dev/null
+++
b/java/fury-core/src/test/java/org/apache/fury/classloader/ThreadSafeFuryClassLoaderTest.java
@@ -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();
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]