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

    https://github.com/apache/spark/pull/11791#discussion_r56875075
  
    --- Diff: 
core/src/main/scala/org/apache/spark/storage/memory/MemoryStore.scala ---
    @@ -500,3 +601,79 @@ private[storage] class PartiallyUnrolledIterator(
         iter = null
       }
     }
    +
    +/**
    + * A wrapper which allows an open [[OutputStream]] to be redirected to a 
different sink.
    + */
    +private class RedirectableOutputStream extends OutputStream {
    +  private[this] var os: OutputStream = _
    +  def setOutputStream(s: OutputStream): Unit = { os = s }
    +  override def write(b: Int): Unit = os.write(b)
    +  override def write(b: Array[Byte]): Unit = os.write(b)
    +  override def write(b: Array[Byte], off: Int, len: Int): Unit = 
os.write(b, off, len)
    +  override def flush(): Unit = os.flush()
    +  override def close(): Unit = os.close()
    +}
    +
    +/**
    + * The result of a failed [[MemoryStore.putIteratorAsBytes()]] call.
    + *
    + * @param memoryStore the MemoryStore, used for freeing memory.
    + * @param blockManager the BlockManager, used for deserializing values.
    + * @param blockId the block id.
    + * @param serializationStream a serialization stream which writes to 
[[redirectableOutputStream]].
    + * @param redirectableOutputStream an OutputStream which can be redirected 
to a different sink.
    + * @param unrollMemory the amount of unroll memory used by the values in 
`unrolled`.
    + * @param unrolled a byte buffer containing the partially-serialized 
values.
    + * @param rest         the rest of the original iterator passed to
    + *                     [[MemoryStore.putIteratorAsValues()]].
    + */
    +private[storage] class PartiallySerializedBlock(
    +    memoryStore: MemoryStore,
    +    blockManager: BlockManager,
    +    blockId: BlockId,
    +    serializationStream: SerializationStream,
    +    redirectableOutputStream: RedirectableOutputStream,
    +    unrollMemory: Long,
    +    unrolled: ChunkedByteBuffer,
    +    rest: Iterator[Any]) {
    +
    +  /**
    +   * Called to dispose of this block and free its memory.
    +   */
    +  def discard(): Unit = {
    +    try {
    +      serializationStream.close()
    +    } finally {
    +      memoryStore.releaseUnrollMemoryForThisTask(unrollMemory)
    +    }
    +  }
    +
    +  /**
    +   * Finish writing this block to the given output stream by first writing 
the serialized values
    +   * and then serializing the values from the original input iterator.
    +   */
    +  def finishWritingToStream(os: OutputStream): Unit = {
    +    ByteStreams.copy(unrolled.toInputStream(), os)
    +    redirectableOutputStream.setOutputStream(os)
    +    while (rest.hasNext) {
    +      serializationStream.writeObject(rest.next())
    +    }
    +    serializationStream.close()
    +  }
    +
    +  /**
    +   * Returns an iterator over the values in this block by first 
deserializing the serialized
    +   * values and then consuming the rest of the original input iterator.
    +   *
    +   * If the caller does not plan to fully consume the resulting iterator 
then they must call
    +   * `close()` on it to free its resources.
    +   */
    +  def valuesIterator: PartiallyUnrolledIterator = {
    +    new PartiallyUnrolledIterator(
    +      memoryStore,
    +      unrollMemory,
    +      unrolled = blockManager.dataDeserialize(blockId, unrolled),
    --- End diff --
    
    It's kind of a bummer that we need to pass `blockManager` in here when all 
that we want to do is figure out which type of decompression and 
deserialization needs to be performed. It'd be nice to loosen coupling here in 
a future followup patch.


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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to