Github user mridulm commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17295#discussion_r106779317
  
    --- Diff: core/src/main/scala/org/apache/spark/storage/DiskStore.scala ---
    @@ -73,55 +86,219 @@ private[spark] class DiskStore(conf: SparkConf, 
diskManager: DiskBlockManager) e
       }
     
       def putBytes(blockId: BlockId, bytes: ChunkedByteBuffer): Unit = {
    -    put(blockId) { fileOutputStream =>
    -      val channel = fileOutputStream.getChannel
    -      Utils.tryWithSafeFinally {
    -        bytes.writeFully(channel)
    -      } {
    -        channel.close()
    -      }
    +    put(blockId) { channel =>
    +      bytes.writeFully(channel)
         }
       }
     
    -  def getBytes(blockId: BlockId): ChunkedByteBuffer = {
    +  def getBytes(blockId: BlockId): BlockData = {
         val file = diskManager.getFile(blockId.name)
    -    val channel = new RandomAccessFile(file, "r").getChannel
    -    Utils.tryWithSafeFinally {
    -      // For small files, directly read rather than memory map
    -      if (file.length < minMemoryMapBytes) {
    -        val buf = ByteBuffer.allocate(file.length.toInt)
    -        channel.position(0)
    -        while (buf.remaining() != 0) {
    -          if (channel.read(buf) == -1) {
    -            throw new IOException("Reached EOF before filling buffer\n" +
    -              
s"offset=0\nfile=${file.getAbsolutePath}\nbuf.remaining=${buf.remaining}")
    +    val blockSize = getSize(blockId)
    +
    +    securityManager.getIOEncryptionKey() match {
    +      case Some(key) =>
    +        // Encrypted blocks cannot be memory mapped; return a special 
object that does decryption
    +        // and provides InputStream / FileRegion implementations for 
reading the data.
    +        new EncryptedBlockData(file, blockSize, conf, key)
    +
    +      case _ =>
    +        val channel = new FileInputStream(file).getChannel()
    +        if (blockSize < minMemoryMapBytes) {
    +          // For small files, directly read rather than memory map.
    +          Utils.tryWithSafeFinally {
    +            val buf = ByteBuffer.allocate(blockSize.toInt)
    +            while (buf.remaining() > 0) {
    +              channel.read(buf)
    --- End diff --
    
    We need to handle case where read() returns EOF (-1) in case of data 
corruption, file removal from underneath, etc : we will end up in infinite loop 
otherwise.
    
    I might have missed more places where this pattern exists in this change.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to