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

    https://github.com/apache/spark/pull/15043#discussion_r79067270
  
    --- Diff: 
core/src/test/scala/org/apache/spark/storage/PartiallySerializedBlockSuite.scala
 ---
    @@ -0,0 +1,215 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.storage
    +
    +import java.nio.ByteBuffer
    +
    +import scala.reflect.ClassTag
    +
    +import org.mockito.Mockito
    +import org.mockito.Mockito.atLeastOnce
    +import org.mockito.invocation.InvocationOnMock
    +import org.mockito.stubbing.Answer
    +import org.scalatest.{BeforeAndAfterEach, PrivateMethodTester}
    +
    +import org.apache.spark.{SparkConf, SparkFunSuite, TaskContext, 
TaskContextImpl}
    +import org.apache.spark.memory.MemoryMode
    +import org.apache.spark.serializer.{JavaSerializer, SerializationStream, 
SerializerManager}
    +import org.apache.spark.storage.memory.{MemoryStore, 
PartiallySerializedBlock, RedirectableOutputStream}
    +import org.apache.spark.util.{ByteBufferInputStream, 
ByteBufferOutputStream}
    +import org.apache.spark.util.io.{ChunkedByteBuffer, 
ChunkedByteBufferOutputStream}
    +
    +class PartiallySerializedBlockSuite
    +    extends SparkFunSuite
    +    with BeforeAndAfterEach
    +    with PrivateMethodTester {
    +
    +  private val blockId = new TestBlockId("test")
    +  private val conf = new SparkConf()
    +  private val memoryStore = Mockito.mock(classOf[MemoryStore], 
Mockito.RETURNS_SMART_NULLS)
    +  private val serializerManager = new SerializerManager(new 
JavaSerializer(conf), conf)
    +
    +  private val getSerializationStream = 
PrivateMethod[SerializationStream]('serializationStream)
    +  private val getRedirectableOutputStream =
    +    PrivateMethod[RedirectableOutputStream]('redirectableOutputStream)
    +
    +  override protected def beforeEach(): Unit = {
    +    super.beforeEach()
    +    Mockito.reset(memoryStore)
    +  }
    +
    +  private def partiallyUnroll[T: ClassTag](
    +      iter: Iterator[T],
    +      numItemsToBuffer: Int): PartiallySerializedBlock[T] = {
    +
    +    val bbos: ChunkedByteBufferOutputStream = {
    +      val spy = Mockito.spy(new ChunkedByteBufferOutputStream(128, 
ByteBuffer.allocate))
    +      Mockito.doAnswer(new Answer[ChunkedByteBuffer] {
    +        override def answer(invocationOnMock: InvocationOnMock): 
ChunkedByteBuffer = {
    +          
Mockito.spy(invocationOnMock.callRealMethod().asInstanceOf[ChunkedByteBuffer])
    +        }
    +      }).when(spy).toChunkedByteBuffer
    +      spy
    +    }
    +
    +    val serializer = 
serializerManager.getSerializer(implicitly[ClassTag[T]]).newInstance()
    +    val redirectableOutputStream = Mockito.spy(new 
RedirectableOutputStream)
    +    redirectableOutputStream.setOutputStream(bbos)
    +    val serializationStream = 
Mockito.spy(serializer.serializeStream(redirectableOutputStream))
    +
    +    (1 to numItemsToBuffer).foreach { _ =>
    +      assert(iter.hasNext)
    +      serializationStream.writeObject[T](iter.next())
    +    }
    +
    +    val unrollMemory = bbos.size
    +    new PartiallySerializedBlock[T](
    +      memoryStore,
    +      serializerManager,
    +      blockId,
    +      serializationStream = serializationStream,
    +      redirectableOutputStream,
    +      unrollMemory = unrollMemory,
    +      memoryMode = MemoryMode.ON_HEAP,
    --- End diff --
    
    It should only affect the memory accounting for unroll memory. If you're 
caching a block at `MEMORY_SER` storage level and are using off-heap caching 
then it's possible for the unrolled memory to be off-heap (so the 
ChunkedByteBufferOutputStream will be using a DirectBuffer allocator). In this 
case we need to count this as off-heap unroll memory so that Spark's off-heap 
allocations can respect the configured off-heap memory limit.
    
    Given that off-heap caching (and thus off-heap unrolling) is a relatively 
new experimental feature, it's entirely possible that there are accounting bugs 
within this path. I'm going to try to expand this test suite to also exercise 
that case just to be 100% sure that we're accounting properly.


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