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

    https://github.com/apache/spark/pull/1722#discussion_r15722869
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/collection/ExternalAppendOnlyMap.scala
 ---
    @@ -389,27 +404,51 @@ class ExternalAppendOnlyMap[K, V, C](
        * An iterator that returns (K, C) pairs in sorted order from an on-disk 
map
        */
       private class DiskMapIterator(file: File, blockId: BlockId, batchSizes: 
ArrayBuffer[Long])
    -    extends Iterator[(K, C)] {
    -    private val fileStream = new FileInputStream(file)
    -    private val bufferedStream = new BufferedInputStream(fileStream, 
fileBufferSize)
    +    extends Iterator[(K, C)]
    +  {
    +    private val batchOffsets = batchSizes.scanLeft(0L)(_ + _)  // Size 
will be batchSize.length + 1
    +    assert(file.length() == batchOffsets(batchOffsets.length - 1))
    +
    +    private var batchIndex = 0  // Which batch we're in
    +    private var fileStream: FileInputStream = null
     
         // An intermediate stream that reads from exactly one batch
         // This guards against pre-fetching and other arbitrary behavior of 
higher level streams
    -    private var batchStream = nextBatchStream()
    -    private var compressedStream = 
blockManager.wrapForCompression(blockId, batchStream)
    -    private var deserializeStream = ser.deserializeStream(compressedStream)
    +    private var deserializeStream = nextBatchStream()
         private var nextItem: (K, C) = null
         private var objectsRead = 0
     
         /**
          * Construct a stream that reads only from the next batch.
          */
    -    private def nextBatchStream(): InputStream = {
    -      if (batchSizes.length > 0) {
    -        ByteStreams.limit(bufferedStream, batchSizes.remove(0))
    +    private def nextBatchStream(): DeserializationStream = {
    +      // Note that batchOffsets.length = numBatches + 1 since we did a 
scan above; check whether
    +      // we're still in a valid batch.
    +      if (batchIndex < batchOffsets.length - 1) {
    +        if (deserializeStream != null) {
    +          deserializeStream.close()
    +          fileStream.close()
    +          deserializeStream = null
    +          fileStream = null
    +        }
    +
    +        val start = batchOffsets(batchIndex)
    +        fileStream = new FileInputStream(file)
    +        fileStream.getChannel.position(start)
    +        batchIndex += 1
    +
    +        val end = batchOffsets(batchIndex)
    +
    +        assert(end >= start, "start = " + start + ", end = " + end +
    +          ", batchOffsets = " + batchOffsets.mkString("[", ", ", "]"))
    +
    +        val bufferedStream = new 
BufferedInputStream(ByteStreams.limit(fileStream, end - start))
    +        val compressedStream = blockManager.wrapForCompression(blockId, 
bufferedStream)
    +        ser.deserializeStream(compressedStream)
    --- End diff --
    
    One delta w.r.t. your patch, @mridulm: you used to do `ser = 
serializer.newInstance` before this, but this should not be necessary; our 
serializers support reading even multiple streams concurrently (though 
confusingly not writing them as far as I see; they can share an output buffer 
there). I removed that because creating a new instance is actually kind of 
expensive for Kryo.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to