@@ -37,7 +37,7 @@ import org.apache.spark.network.crypto.{AuthClientBootstrap, AuthServerBootstrap
3737import org .apache .spark .network .netty .SparkTransportConf
3838import org .apache .spark .network .server ._
3939import org .apache .spark .rpc ._
40- import org .apache .spark .serializer .{JavaSerializer , JavaSerializerInstance }
40+ import org .apache .spark .serializer .{JavaSerializer , JavaSerializerInstance , SerializationStream }
4141import org .apache .spark .util .{ByteBufferInputStream , ByteBufferOutputStream , ThreadUtils , Utils }
4242
4343private [netty] class NettyRpcEnv (
@@ -253,6 +253,13 @@ private[netty] class NettyRpcEnv(
253253 javaSerializerInstance.serialize(content)
254254 }
255255
256+ /**
257+ * Returns [[SerializationStream ]] that forwards the serialized bytes to `out`.
258+ */
259+ private [netty] def serializeStream (out : OutputStream ): SerializationStream = {
260+ javaSerializerInstance.serializeStream(out)
261+ }
262+
256263 private [netty] def deserialize [T : ClassTag ](client : TransportClient , bytes : ByteBuffer ): T = {
257264 NettyRpcEnv .currentClient.withValue(client) {
258265 deserialize { () =>
@@ -530,19 +537,23 @@ private[netty] class NettyRpcEndpointRef(
530537 */
531538private [netty] class RequestMessage (
532539 val senderAddress : RpcAddress ,
533- val receiver : NettyRpcEndpointRef , val content : Any ) {
540+ val receiver : NettyRpcEndpointRef ,
541+ val content : Any ) {
534542
535- /** Manually serialize [[RequestMessage ]] to minimize the size of bytes . */
543+ /** Manually serialize [[RequestMessage ]] to minimize the size. */
536544 def serialize (nettyEnv : NettyRpcEnv ): ByteBuffer = {
537545 val bos = new ByteBufferOutputStream ()
538546 val out = new DataOutputStream (bos)
539547 try {
540548 writeRpcAddress(out, senderAddress)
541549 writeRpcAddress(out, receiver.address)
542550 out.writeUTF(receiver.name)
543- val contentBytes = nettyEnv.serialize(content)
544- assert(contentBytes.hasArray)
545- out.write(contentBytes.array, contentBytes.arrayOffset, contentBytes.remaining)
551+ val s = nettyEnv.serializeStream(out)
552+ try {
553+ s.writeObject(content)
554+ } finally {
555+ s.close()
556+ }
546557 } finally {
547558 out.close()
548559 }
0 commit comments