Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ private[spark] class ChunkedByteBuffer(var chunks: Array[ByteBuffer]) {
*/
def writeFully(channel: WritableByteChannel): Unit = {
for (bytes <- getChunks()) {
while (bytes.remaining() > 0) {
val curChunkLimit = bytes.limit()
while (bytes.hasRemaining) {
val ioSize = Math.min(bytes.remaining(), bufferWriteChunkSize)
bytes.limit(bytes.position() + ioSize)
channel.write(bytes)
bytes.limit(curChunkLimit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rewrite this using:

try {
	val ioSize = Math.min(bytes.remaining(), bufferWriteChunkSize)
	bytes.limit(bytes.position() + ioSize)
	channel.write(bytes)
} finally {
	bytes.limit(curChunkLimit)
}

to be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. When channel write throw IOException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have commit this modified

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package org.apache.spark.io
import java.nio.ByteBuffer

import com.google.common.io.ByteStreams
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line behind 22 to separate spark and third-party group.


import org.apache.spark.SparkFunSuite
import org.apache.spark.{SparkEnv, SparkFunSuite}
import org.apache.spark.internal.config
import org.apache.spark.network.util.ByteArrayWritableChannel
import org.apache.spark.util.io.ChunkedByteBuffer

Expand Down Expand Up @@ -56,6 +56,15 @@ class ChunkedByteBufferSuite extends SparkFunSuite {
assert(chunkedByteBuffer.getChunks().head.position() === 0)
}

test("SPARK-24107: writeFully() write buffer which is larger than bufferWriteChunkSize") {
val bufferWriteChunkSize = Option(SparkEnv.get).map(_.conf.get(config.BUFFER_WRITE_CHUNK_SIZE))
.getOrElse(config.BUFFER_WRITE_CHUNK_SIZE.defaultValue.get).toInt
Copy link
Member

@maropu maropu Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about setting this value via spark.buffer.write.chunkSize? e.g.,

sc.conf.set("spark.default.parallelism", "4")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I have added. Please check

val chunkedByteBuffer = new ChunkedByteBuffer(Array(ByteBuffer.allocate(bufferWriteChunkSize + 8)))
val byteArrayWritableChannel = new ByteArrayWritableChannel(chunkedByteBuffer.size.toInt)
chunkedByteBuffer.writeFully(byteArrayWritableChannel)
assert(byteArrayWritableChannel.length() === chunkedByteBuffer.size)
}

test("toArray()") {
val empty = ByteBuffer.wrap(Array.empty[Byte])
val bytes = ByteBuffer.wrap(Array.tabulate(8)(_.toByte))
Expand Down