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

    https://github.com/apache/spark/pull/1722#discussion_r15725667
  
    --- 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 --
    
    So that is something I was not sure of : particularly with kryo (not java).
    We were seeing the input buffer getting stepped on from various threads - 
this was specifically in context of 2G fixes though, where we had to modify the 
way the buffer was created anyway. I dont know if the initialization changes 
something else.


---
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