Skip to content

Commit

Permalink
feat(java): remove soft/weak ref values from thread safe fury (#1639)
Browse files Browse the repository at this point in the history
## What does this PR do?

remove soft/weak ref values from thread safe fury

## Related issues

Closes #1632


## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-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.
-->
  • Loading branch information
chaokunyang committed May 20, 2024
1 parent cf13c99 commit 03dba11
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public Object deserializeJavaObjectAndClass(FuryReadableChannel channel) {

@Override
public void setClassLoader(ClassLoader classLoader) {
setClassLoader(classLoader, StagingType.SOFT_STAGING);
setClassLoader(classLoader, StagingType.STRONG_STAGING);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ public FuryPooledObjectFactory(
this.furyFactory = furyFactory;
this.factoryCallback = factoryCallback;
classLoaderFuryPooledCache =
CacheBuilder.newBuilder()
.weakKeys()
.softValues()
.expireAfterAccess(expireTime, timeUnit)
.build();
CacheBuilder.newBuilder().expireAfterAccess(expireTime, timeUnit).build();
}

public Fury getFury() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ClassLoader getClassLoader() {
* {@link #setClassLoader(ClassLoader, StagingType)} and {@link #clearClassLoader} should be used.
*/
public void setClassLoader(ClassLoader classLoader) {
setClassLoader(classLoader, StagingType.SOFT_STAGING);
setClassLoader(classLoader, StagingType.STRONG_STAGING);
}

/**
Expand Down Expand Up @@ -127,8 +127,8 @@ public void setClassLoader(ClassLoader classLoader, StagingType stagingType) {
fury = furyFactory.apply(classLoader);
bindingCallback.accept(fury);
furySoftMap.put(classLoader, new SoftReference<>(fury));
this.fury = fury;
}
this.fury = fury;
break;
}
case STRONG_STAGING:
Expand All @@ -138,8 +138,8 @@ public void setClassLoader(ClassLoader classLoader, StagingType stagingType) {
fury = furyFactory.apply(classLoader);
bindingCallback.accept(fury);
furyMap.put(classLoader, fury);
this.fury = fury;
}
this.fury = fury;
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,30 +279,38 @@ public void testClassGC() throws Exception {
}

private WeakHashMap<Class<?>, Boolean> generateClassForGC() {
ThreadSafeFury fury = Fury.builder().requireClassRegistration(false).buildThreadSafeFury();
ThreadSafeFury fury1 = Fury.builder().requireClassRegistration(false).buildThreadSafeFury();
ThreadSafeFury fury2 =
Fury.builder().requireClassRegistration(false).buildThreadSafeFuryPool(1, 2);
String className = "DuplicateStruct";
WeakHashMap<Class<?>, Boolean> map = new WeakHashMap<>();
{
Class<?> structClass1 = Struct.createStructClass(className, 1, false);
Object struct1 = Struct.createPOJO(structClass1);
byte[] bytes = fury.serialize(struct1);
Assert.assertEquals(fury.deserialize(bytes), struct1);
map.put(structClass1, true);
System.out.printf(
"structClass1 %s %s\n",
structClass1.hashCode(), structClass1.getClassLoader().hashCode());
for (ThreadSafeFury fury : new ThreadSafeFury[] {fury1, fury2}) {
fury.setClassLoader(structClass1.getClassLoader());
byte[] bytes = fury.serialize(struct1);
Assert.assertEquals(fury.deserialize(bytes), struct1);
map.put(structClass1, true);
System.out.printf(
"structClass1 %s %s\n",
structClass1.hashCode(), structClass1.getClassLoader().hashCode());
fury.clearClassLoader(structClass1.getClassLoader());
}
}
{
Class<?> structClass2 = Struct.createStructClass(className, 2, false);
map.put(structClass2, true);
System.out.printf(
"structClass2 %s %s\n ",
structClass2.hashCode(), structClass2.getClassLoader().hashCode());
fury.setClassLoader(structClass2.getClassLoader());
Object struct2 = Struct.createPOJO(structClass2);
byte[] bytes2 = fury.serialize(struct2);
Assert.assertEquals(fury.deserialize(bytes2), struct2);
fury.clearClassLoader(structClass2.getClassLoader());
for (ThreadSafeFury fury : new ThreadSafeFury[] {fury1, fury2}) {
fury.setClassLoader(structClass2.getClassLoader());
Object struct2 = Struct.createPOJO(structClass2);
byte[] bytes2 = fury.serialize(struct2);
Assert.assertEquals(fury.deserialize(bytes2), struct2);
fury.clearClassLoader(structClass2.getClassLoader());
}
}
return map;
}
Expand Down

0 comments on commit 03dba11

Please sign in to comment.