Skip to content

Commit

Permalink
feat(java): add set serializer for concurrent set (#1616)
Browse files Browse the repository at this point in the history
<!--
**Thanks for contributing to Fury.**

**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).**

Contribution Checklist

- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).

- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->

## What does this PR do?

<!-- Describe the purpose of this PR. -->

we use `Sets.newConcurrentHashSet()` to create set, with lastest guava
version it use `ConcurrentHashMapKeySetView` and old version it use
`Collections.newSetFromMap(map)`.

remove `CopyOnWriteArrayListSerializer` from native-image.properties.
#1614 forget delete.

## Related issues

<!--
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/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
MrChang0 authored May 11, 2024
1 parent 4de1623 commit 9441191
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.graalvm;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.fury.Fury;
import org.apache.fury.util.Preconditions;

public class CollectionExample {
static Fury fury;

static {
fury = Fury.builder().requireClassRegistration(true).build();
}

static void test(Fury fury) {
final Map<String, String> unmodifiableMap = Map.of("k1", "v1", "k2", "v2");
Preconditions.checkArgument(
unmodifiableMap.equals(fury.deserialize(fury.serialize(unmodifiableMap))));
System.out.println(unmodifiableMap);
final List<Integer> arrayasList = Arrays.asList(1, 2, 3);
Preconditions.checkArgument(arrayasList.equals(fury.deserialize(fury.serialize(arrayasList))));
System.out.println(arrayasList);
final Set<String> setFromMap = Collections.newSetFromMap(new ConcurrentHashMap<>());
setFromMap.add("a");
setFromMap.add("b");
setFromMap.add("c");
Preconditions.checkArgument(setFromMap.equals(fury.deserialize(fury.serialize(setFromMap))));
System.out.println(setFromMap);
}

public static void main(String[] args) {
test(fury);
System.out.println("Collection succeed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public static void main(String[] args) throws Throwable {
CompatibleThreadSafeExample.main(args);
ProxyExample.main(args);
Benchmark.main(args);
CollectionExample.main(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,22 @@ public static void registerDefaultSerializers(Fury fury) {
fury.registerSerializer(Object[].class, new ObjectArraySerializer<>(fury, Object[].class));
fury.registerSerializer(Class[].class, new ObjectArraySerializer<>(fury, Class[].class));
fury.registerSerializer(byte[].class, new ByteArraySerializer(fury));
fury.registerSerializer(Byte[].class, new ObjectArraySerializer<>(fury, Byte[].class));
fury.registerSerializer(char[].class, new CharArraySerializer(fury));
fury.registerSerializer(
Character[].class, new ObjectArraySerializer<>(fury, Character[].class));
fury.registerSerializer(short[].class, new ShortArraySerializer(fury));
fury.registerSerializer(Short[].class, new ObjectArraySerializer<>(fury, Short[].class));
fury.registerSerializer(int[].class, new IntArraySerializer(fury));
fury.registerSerializer(Integer[].class, new ObjectArraySerializer<>(fury, Integer[].class));
fury.registerSerializer(long[].class, new LongArraySerializer(fury));
fury.registerSerializer(Long[].class, new ObjectArraySerializer<>(fury, Long[].class));
fury.registerSerializer(float[].class, new FloatArraySerializer(fury));
fury.registerSerializer(Float[].class, new ObjectArraySerializer<>(fury, Float[].class));
fury.registerSerializer(double[].class, new DoubleArraySerializer(fury));
fury.registerSerializer(Double[].class, new ObjectArraySerializer<>(fury, Double[].class));
fury.registerSerializer(boolean[].class, new BooleanArraySerializer(fury));
fury.registerSerializer(Boolean[].class, new ObjectArraySerializer<>(fury, Boolean[].class));
fury.registerSerializer(String[].class, new StringArraySerializer(fury));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.fury.Fury;
Expand All @@ -46,6 +49,7 @@
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.Platform;
import org.apache.fury.reflect.ReflectionUtils;
import org.apache.fury.resolver.ClassInfo;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.serializer.ReplaceResolveSerializer;
import org.apache.fury.serializer.Serializer;
Expand Down Expand Up @@ -411,6 +415,64 @@ public ConcurrentSkipListSet newCollection(MemoryBuffer buffer) {
}
}

public static final class SetFromMapSerializer extends CollectionSerializer<Set<?>> {

private static final long MAP_FIELD_OFFSET;

static {
try {
Field mapField = Class.forName("java.util.Collections$SetFromMap").getDeclaredField("m");
MAP_FIELD_OFFSET = Platform.objectFieldOffset(mapField);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

public SetFromMapSerializer(Fury fury, Class<Set<?>> type) {
super(fury, type, true);
}

@Override
public Collection newCollection(MemoryBuffer buffer) {
final ClassInfo mapClassInfo = fury.getClassResolver().readClassInfo(buffer);
final AbstractMapSerializer mapSerializer =
(AbstractMapSerializer) fury.getClassResolver().getSerializer(mapClassInfo.getCls());
Map map = mapSerializer.newMap(buffer);
final int numElements = mapSerializer.getAndClearNumElements();
setNumElements(numElements);
final Set set = Collections.newSetFromMap(map);
fury.getRefResolver().reference(set);
return set;
}

@Override
public Collection onCollectionWrite(MemoryBuffer buffer, Set<?> value) {
final Map<?, Boolean> map = (Map<?, Boolean>) Platform.getObject(value, MAP_FIELD_OFFSET);
final ClassInfo classInfo = fury.getClassResolver().getClassInfo(map.getClass());
fury.getClassResolver().writeClass(buffer, classInfo);
// newMap will read num size first.
buffer.writeVarUint32Small7(value.size());
return value;
}
}

public static final class ConcurrentHashMapKeySetView
extends CollectionSerializer<ConcurrentHashMap.KeySetView> {

public ConcurrentHashMapKeySetView(Fury fury, Class<ConcurrentHashMap.KeySetView> type) {
super(fury, type);
}

@Override
public ConcurrentHashMap.KeySetView newCollection(MemoryBuffer buffer) {
int numElements = buffer.readVarUint32Small7();
setNumElements(numElements);
ConcurrentHashMap.KeySetView keySetView = ConcurrentHashMap.newKeySet(numElements);
fury.getRefResolver().reference(keySetView);
return keySetView;
}
}

public static final class VectorSerializer extends CollectionSerializer<Vector> {

public VectorSerializer(Fury fury, Class<Vector> cls) {
Expand Down Expand Up @@ -649,5 +711,10 @@ public static void registerDefaultSerializers(Fury fury) {
fury.registerSerializer(
CopyOnWriteArrayList.class,
new CopyOnWriteArrayListSerializer(fury, CopyOnWriteArrayList.class));
final Class setFromMapClass = Collections.newSetFromMap(new HashMap<>()).getClass();
fury.registerSerializer(setFromMapClass, new SetFromMapSerializer(fury, setFromMapClass));
fury.registerSerializer(
ConcurrentHashMap.KeySetView.class,
new ConcurrentHashMapKeySetView(fury, ConcurrentHashMap.KeySetView.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Args=--initialize-at-build-time=org.apache.fury.memory.MemoryBuffer,\
org.apache.fury.serializer.collection.UnmodifiableSerializers$Offset,\
org.apache.fury.serializer.collection.SynchronizedSerializers$Offset,\
org.apache.fury.serializer.collection.CollectionSerializers$ArraysAsListSerializer,\
org.apache.fury.serializer.collection.CollectionSerializers$CopyOnWriteArrayListSerializer,\
org.apache.fury.serializer.collection.CollectionSerializers$SetFromMapSerializer,\
org.apache.fury.serializer.collection.MapSerializers$EnumMapSerializer,\
org.apache.fury.serializer.JdkProxySerializer,\
org.apache.fury.serializer.StringSerializer$Offset,\
Expand Down Expand Up @@ -136,6 +136,7 @@ Args=--initialize-at-build-time=org.apache.fury.memory.MemoryBuffer,\
org.apache.fury.memory.BoundsChecking,\
org.apache.fury.shaded.org.codehaus.janino.Java$Package,\
org.apache.fury.serializer.ArraySerializers,\
org.apache.fury.serializer.ArraySerializers$ObjectArraySerializer,\
org.apache.fury.shaded.org.codehaus.janino.Java$AccessModifier,\
org.apache.fury.util.ClassLoaderUtils$ParentClassLoader,\
com.google.common.collect.RegularImmutableSortedSet,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -67,6 +68,7 @@
import org.apache.fury.type.GenericType;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.collections.Maps;

public class CollectionSerializersTest extends FuryTestBase {

Expand Down Expand Up @@ -274,6 +276,36 @@ public void testCopyOnWriteArrayList() {
CollectionSerializers.CopyOnWriteArrayListSerializer.class);
}

@Test(dataProvider = "enableCodegen")
public void testSetFromMap(boolean enableCodegen) {
final Fury fury =
Fury.builder()
.withLanguage(Language.JAVA)
.requireClassRegistration(false)
.withCodegen(enableCodegen)
.build();
final Set<String> set = Collections.newSetFromMap(Maps.newConcurrentMap());
set.add("a");
set.add("b");
set.add("c");
Assert.assertEquals(set, serDe(fury, set));
Assert.assertEquals(
getJavaFury().getClassResolver().getSerializerClass(set.getClass()),
CollectionSerializers.SetFromMapSerializer.class);
}

@Test
public void testConcurrentMapKeySetViewMap() {
final ConcurrentHashMap.KeySetView<Object, Boolean> set = ConcurrentHashMap.newKeySet();
set.add("a");
set.add("b");
set.add("c");
Assert.assertEquals(set, serDe(getJavaFury(), set));
Assert.assertEquals(
getJavaFury().getClassResolver().getSerializerClass(set.getClass()),
CollectionSerializers.ConcurrentHashMapKeySetView.class);
}

@Test
public void testSerializeJavaBlockingQueue() {
Fury fury =
Expand Down

0 comments on commit 9441191

Please sign in to comment.