You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@uniffle.apache.org by "zuston (via GitHub)" <gi...@apache.org> on 2025/08/28 09:08:45 UTC
[PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
zuston opened a new pull request, #2597:
URL: https://github.com/apache/uniffle/pull/2597
### What changes were proposed in this pull request?
This is an experimental feature to introduce the fory serializer to replace the villina spark serializer to speed up.
### Why are the changes needed?
for #2596
### Does this PR introduce _any_ user-facing change?
Yes.
`spark.rss.client.shuffle.serializer=FORY`
### How was this patch tested?
Unit test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307189119
##########
client-spark/common/src/main/scala/org/apache/spark/serializer/ForySerializer.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.spark.serializer
+
+import org.apache.fory.Fory
+import org.apache.fory.config.{CompatibleMode, Language}
+import org.apache.spark.internal.Logging
+
+import java.io.{InputStream, OutputStream, Serializable}
+import java.nio.ByteBuffer
+import scala.reflect.ClassTag
+
+class ForySerializer extends org.apache.spark.serializer.Serializer
+ with Logging
+ with Serializable {
+
+ override def newInstance(): SerializerInstance = new ForySerializerInstance()
+
+ override def supportsRelocationOfSerializedObjects: Boolean = true
+
+}
+
+class ForySerializerInstance extends org.apache.spark.serializer.SerializerInstance {
+
+ // Thread-local Fury instance for thread safety
+ private val fury: ThreadLocal[Fory] = ThreadLocal.withInitial(() => {
+ val f = Fory.builder()
+ .withLanguage(Language.JAVA)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build()
Review Comment:
Thanks for pointing this. I also found similar problem when using the `case class` without `scalaSerializer` like
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307124010
##########
client-spark/common/src/main/scala/org/apache/spark/serializer/ForySerializer.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.spark.serializer
+
+import org.apache.fory.Fory
+import org.apache.fory.config.{CompatibleMode, Language}
+import org.apache.spark.internal.Logging
+
+import java.io.{InputStream, OutputStream, Serializable}
+import java.nio.ByteBuffer
+import scala.reflect.ClassTag
+
+class ForySerializer extends org.apache.spark.serializer.Serializer
+ with Logging
+ with Serializable {
+
+ override def newInstance(): SerializerInstance = new ForySerializerInstance()
+
+ override def supportsRelocationOfSerializedObjects: Boolean = true
+
+}
+
+class ForySerializerInstance extends org.apache.spark.serializer.SerializerInstance {
+
+ // Thread-local Fury instance for thread safety
+ private val fury: ThreadLocal[Fory] = ThreadLocal.withInitial(() => {
+ val f = Fory.builder()
+ .withLanguage(Language.JAVA)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build()
+ f
+ })
+
+ override def serialize[T: ClassTag](t: T): ByteBuffer = {
+ val bytes = fury.get().serialize(t.asInstanceOf[AnyRef])
+ ByteBuffer.wrap(bytes)
+ }
+
+ override def deserialize[T: ClassTag](bytes: ByteBuffer): T = {
+ val array = if (bytes.hasArray) {
Review Comment:
You can pass bytebuffer to fory directly without an extra copy
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3233159317
Shuffle data should already be binary, is there anything that needs being serialized?
Have you ever benchmark your job to see whether there is bottleneck on serialization?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3233227564
> Only if you are using spark rdd with raw java objects, there will be serialization bottleneck. Such cases are similiar to datastream in flink. We've observed several times of e2e performance speed up for multiple cases.
Thanks for your sharing. Do you mean that there is no need to optimize performance of vanilla spark SQL shuffle serialization ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307184888
##########
client-spark/common/pom.xml:
##########
@@ -89,6 +89,96 @@
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-core</artifactId>
+ <version>0.12.0</version>
Review Comment:
Pity to say that Spark still uses the scala2.x
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "jerqi (via GitHub)" <gi...@apache.org>.
jerqi commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3240762777
The serialization of Spark happens in the shuffle write shuffle stage.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307122000
##########
client-spark/common/src/main/scala/org/apache/spark/serializer/ForySerializer.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.spark.serializer
+
+import org.apache.fory.Fory
+import org.apache.fory.config.{CompatibleMode, Language}
+import org.apache.spark.internal.Logging
+
+import java.io.{InputStream, OutputStream, Serializable}
+import java.nio.ByteBuffer
+import scala.reflect.ClassTag
+
+class ForySerializer extends org.apache.spark.serializer.Serializer
+ with Logging
+ with Serializable {
+
+ override def newInstance(): SerializerInstance = new ForySerializerInstance()
+
+ override def supportsRelocationOfSerializedObjects: Boolean = true
+
+}
+
+class ForySerializerInstance extends org.apache.spark.serializer.SerializerInstance {
+
+ // Thread-local Fury instance for thread safety
+ private val fury: ThreadLocal[Fory] = ThreadLocal.withInitial(() => {
+ val f = Fory.builder()
+ .withLanguage(Language.JAVA)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build()
Review Comment:
you should also register scala serializers and enable scala serialization optimization:
```scala
val f = Fory.builder()
.withLanguage(Language.JAVA)
.withRefTracking(true)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.requireClassRegistration(false)
.withScalaOptimizationEnabled(true)
.build()
ScalaSerializers.registerSerializers(f)
```
See more details on https://fory.apache.org/docs/docs/guide/scala_guide#fory-creation
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3233199861
Big thanks for your quick and patient review. @chaokunyang
> Shuffle data should already be binary, is there anything that needs being serialized?
If using the vanilla spark, record is a object class and then serialized into bytes to push to remote shuffle-server. If using the gluten/auron/datafusion-comet, there is no need to serialize.
> Have you ever benchmark your job to see whether there is bottleneck on serialization?
Haven't. This PR is still in initial phase
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3232744941
cc @chaokunyang . If you have time, could you help review this integration with Fory?
So far, this implementation hasn’t shown significant improvements. I would greatly appreciate any guidance you could provide on using Fory.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3233213379
Only if you are using spark rdd with raw java objects, there will be serialization bottleneck. Such cases are similiar to datastream in flink. We've observed several times of e2e performance speed up for multiple cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3233276105
> Data record in Spark SQL are alreay binary, there is no serialization happened. I suggest benchmark first before optimizing.
It seems that serialization is still happening. https://github.com/apache/spark/blob/2de0248071035aa94818386c2402169f6670d2d4/core/src/main/scala/org/apache/spark/shuffle/ShuffleWriteProcessor.scala#L57
The product2 contains the Key/Value that will be serializated. refer: https://github.com/apache/spark/blob/47991b074a5a277e1fb75be3a5cc207f400b0b0c/core/src/main/java/org/apache/spark/shuffle/sort/UnsafeShuffleWriter.java#L243
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3232844747
## Test Results
3 067 files - 23 3 067 suites - 23 6h 7m 44s ⏱️ - 41m 52s
1 207 tests + 9 1 123 ✅ - 74 1 💤 ±0 0 ❌ ±0 83 🔥 + 83
15 225 runs +59 15 044 ✅ - 107 15 💤 ±0 0 ❌ ±0 166 🔥 +166
For more details on these errors, see [this check](https://github.com/apache/uniffle/runs/49083293067).
Results for commit 32f2ad51. ± Comparison against base commit d5e689c3.
<details>
<summary>This pull request <b>removes</b> 4 and <b>adds</b> 13 tests. <i>Note that renamed tests count towards both.</i></summary>
```
org.apache.spark.shuffle.RssSpark2ShuffleUtilsTest ‑ testCreateFetchFailedException
org.apache.spark.shuffle.RssSpark2ShuffleUtilsTest ‑ testGetActiveSparkContext
org.apache.spark.shuffle.RssSpark2ShuffleUtilsTest ‑ testIsStageResubmitSupported
org.apache.uniffle.test.GetReaderTest ‑ test{File}
```
```
org.apache.spark.serializer.ForySerializerTest ‑ ForyDeserializationStream should handle stream operations after close
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializationStream should handle empty stream
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializationStream should handle stream operations after close
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializationStream should serialize and deserialize simple objects
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializer should create new instance
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializer should support relocation of serialized objects
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializerInstance should handle byte arrays
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializerInstance should handle large strings
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializerInstance should handle null values
org.apache.spark.serializer.ForySerializerTest ‑ ForySerializerInstance should serialize and deserialize simple case class
…
```
</details>
[test-results]:data:application/gzip;base64,H4sIAEIqsGgC/03MSw6DIBSF4a0Yxh1cLi/bzTTIIyFVaRBGTfdeMEodno/L/yE+zG4jj4GBVLeBbCXk67Yl6RziWgURJK9U33O7oAjqnM+tGNOMIvvbK7ybdfA6zBWgg0sppipj+5TKumcFojhmzwrgvNuR7UeX7L7PKpWykonLEnKdhKFHbQVV6CyA4qC055Lbu9Z2MtRpN1LHJk6+P59eE7MXAQAA
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#issuecomment-3233250339
Data record in Spark SQL are alreay binary, there is no serialization happened. I suggest benchmark first before optimizing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307115444
##########
client-spark/common/pom.xml:
##########
@@ -89,6 +89,96 @@
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-core</artifactId>
+ <version>0.12.0</version>
Review Comment:
Please also introduce fory-scala dependency: https://mvnrepository.com/artifact/org.apache.fory/fory-scala
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307144902
##########
client-spark/common/src/main/scala/org/apache/spark/serializer/ForySerializer.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.spark.serializer
+
+import org.apache.fory.Fory
+import org.apache.fory.config.{CompatibleMode, Language}
+import org.apache.spark.internal.Logging
+
+import java.io.{InputStream, OutputStream, Serializable}
+import java.nio.ByteBuffer
+import scala.reflect.ClassTag
+
+class ForySerializer extends org.apache.spark.serializer.Serializer
+ with Logging
+ with Serializable {
+
+ override def newInstance(): SerializerInstance = new ForySerializerInstance()
+
+ override def supportsRelocationOfSerializedObjects: Boolean = true
+
+}
+
+class ForySerializerInstance extends org.apache.spark.serializer.SerializerInstance {
+
+ // Thread-local Fury instance for thread safety
+ private val fury: ThreadLocal[Fory] = ThreadLocal.withInitial(() => {
+ val f = Fory.builder()
+ .withLanguage(Language.JAVA)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build()
+ f
+ })
+
+ override def serialize[T: ClassTag](t: T): ByteBuffer = {
+ val bytes = fury.get().serialize(t.asInstanceOf[AnyRef])
+ ByteBuffer.wrap(bytes)
+ }
+
+ override def deserialize[T: ClassTag](bytes: ByteBuffer): T = {
+ val array = if (bytes.hasArray) {
+ val offset = bytes.arrayOffset() + bytes.position()
+ val length = bytes.remaining()
+ java.util.Arrays.copyOfRange(bytes.array(), offset, offset + length)
+ } else {
+ val array = new Array[Byte](bytes.remaining())
+ bytes.get(array)
+ array
+ }
+ fury.get().deserialize(array).asInstanceOf[T]
+ }
+
+ override def deserialize[T: ClassTag](bytes: ByteBuffer, loader: ClassLoader): T = {
+ // Fury handles class loading internally, so we can use the standard deserialize method
+ deserialize[T](bytes)
+ }
+
+ override def serializeStream(s: OutputStream): SerializationStream = {
+ new ForySerializationStream(fury.get(), s)
+ }
+
+ override def deserializeStream(s: InputStream): DeserializationStream = {
+ new ForyDeserializationStream(fury.get(), s)
+ }
+}
+
+class ForySerializationStream(fury: Fory, outputStream: OutputStream)
+ extends org.apache.spark.serializer.SerializationStream {
+
+ private val out = outputStream
+ private var closed = false
+
+ override def writeObject[T: ClassTag](t: T): SerializationStream = {
+ if (closed) {
+ throw new IllegalStateException("Stream is closed")
+ }
+
+ val bytes = fury.serialize(t.asInstanceOf[AnyRef])
+ // Write length first, then data
+ writeInt(bytes.length)
+ out.write(bytes)
+ this
+ }
+
+ private def writeInt(value: Int): Unit = {
+ out.write((value >>> 24) & 0xFF)
Review Comment:
Just use:
```
public void writeInt64(MemoryBuffer buffer, long value) {
LongSerializer.writeInt64(buffer, value, longEncoding);
}
public long readInt64(MemoryBuffer buffer) {
return LongSerializer.readInt64(buffer, longEncoding);
}
```
This will be faster and simple, it also compress data
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307141150
##########
client-spark/common/src/main/scala/org/apache/spark/serializer/ForySerializer.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.spark.serializer
+
+import org.apache.fory.Fory
+import org.apache.fory.config.{CompatibleMode, Language}
+import org.apache.spark.internal.Logging
+
+import java.io.{InputStream, OutputStream, Serializable}
+import java.nio.ByteBuffer
+import scala.reflect.ClassTag
+
+class ForySerializer extends org.apache.spark.serializer.Serializer
+ with Logging
+ with Serializable {
+
+ override def newInstance(): SerializerInstance = new ForySerializerInstance()
+
+ override def supportsRelocationOfSerializedObjects: Boolean = true
+
+}
+
+class ForySerializerInstance extends org.apache.spark.serializer.SerializerInstance {
+
+ // Thread-local Fury instance for thread safety
+ private val fury: ThreadLocal[Fory] = ThreadLocal.withInitial(() => {
+ val f = Fory.builder()
+ .withLanguage(Language.JAVA)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build()
+ f
+ })
+
+ override def serialize[T: ClassTag](t: T): ByteBuffer = {
+ val bytes = fury.get().serialize(t.asInstanceOf[AnyRef])
+ ByteBuffer.wrap(bytes)
+ }
+
+ override def deserialize[T: ClassTag](bytes: ByteBuffer): T = {
+ val array = if (bytes.hasArray) {
+ val offset = bytes.arrayOffset() + bytes.position()
+ val length = bytes.remaining()
+ java.util.Arrays.copyOfRange(bytes.array(), offset, offset + length)
+ } else {
+ val array = new Array[Byte](bytes.remaining())
+ bytes.get(array)
+ array
+ }
+ fury.get().deserialize(array).asInstanceOf[T]
+ }
+
+ override def deserialize[T: ClassTag](bytes: ByteBuffer, loader: ClassLoader): T = {
+ // Fury handles class loading internally, so we can use the standard deserialize method
+ deserialize[T](bytes)
+ }
+
+ override def serializeStream(s: OutputStream): SerializationStream = {
+ new ForySerializationStream(fury.get(), s)
+ }
+
+ override def deserializeStream(s: InputStream): DeserializationStream = {
+ new ForyDeserializationStream(fury.get(), s)
+ }
+}
+
+class ForySerializationStream(fury: Fory, outputStream: OutputStream)
+ extends org.apache.spark.serializer.SerializationStream {
+
+ private val out = outputStream
+ private var closed = false
+
+ override def writeObject[T: ClassTag](t: T): SerializationStream = {
+ if (closed) {
+ throw new IllegalStateException("Stream is closed")
+ }
+
+ val bytes = fury.serialize(t.asInstanceOf[AnyRef])
Review Comment:
Maybe hold a Fory `MemoryBuffer` as an instance field in the class, and serialize object into that buffer, then you can get heap buffer from that buffer, and write it into `out`. In this way, yo u can reduce a copy
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org
Re: [PR] [#2596] feat(spark): Introduce fory serializer [uniffle]
Posted by "zuston (via GitHub)" <gi...@apache.org>.
zuston commented on code in PR #2597:
URL: https://github.com/apache/uniffle/pull/2597#discussion_r2307189119
##########
client-spark/common/src/main/scala/org/apache/spark/serializer/ForySerializer.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.spark.serializer
+
+import org.apache.fory.Fory
+import org.apache.fory.config.{CompatibleMode, Language}
+import org.apache.spark.internal.Logging
+
+import java.io.{InputStream, OutputStream, Serializable}
+import java.nio.ByteBuffer
+import scala.reflect.ClassTag
+
+class ForySerializer extends org.apache.spark.serializer.Serializer
+ with Logging
+ with Serializable {
+
+ override def newInstance(): SerializerInstance = new ForySerializerInstance()
+
+ override def supportsRelocationOfSerializedObjects: Boolean = true
+
+}
+
+class ForySerializerInstance extends org.apache.spark.serializer.SerializerInstance {
+
+ // Thread-local Fury instance for thread safety
+ private val fury: ThreadLocal[Fory] = ThreadLocal.withInitial(() => {
+ val f = Fory.builder()
+ .withLanguage(Language.JAVA)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build()
Review Comment:
Thanks for pointing this. I also found similar problem when using the `case class` without `scalaSerializer` like
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org