Class LocalBufferPool

java.lang.Object
org.apache.flink.runtime.io.network.buffer.LocalBufferPool
All Implemented Interfaces:
AvailabilityProvider, BufferPool, BufferProvider, BufferRecycler

public class LocalBufferPool extends Object implements BufferPool
A buffer pool used to manage a number of Buffer instances from the NetworkBufferPool.

Buffer requests are mediated to the network buffer pool to ensure deadlock free operation of the network stack by limiting the number of buffers per local buffer pool. It also implements the default mechanism for buffer recycling, which ensures that every buffer is ultimately returned to the network buffer pool.

The size of this pool can be dynamically changed at runtime (setNumBuffers(int). It will then lazily return the required number of buffers to the NetworkBufferPool to match its new size.

New buffers can be requested only when numberOfRequestedMemorySegments < currentPoolSize + maxOverdraftBuffersPerGate. In other words, all buffers exceeding the currentPoolSize will be dynamically regarded as overdraft buffers.

Availability is defined as returning a non-overdraft segment on a subsequent requestBuffer()/ requestBufferBuilder() and heaving a non-blocking requestBufferBuilderBlocking(int). In particular,

  • There is at least one availableMemorySegments.
  • No subpartitions has reached maxBuffersPerChannel.

To ensure this contract, the implementation eagerly fetches additional memory segments from NetworkBufferPool as long as it hasn't reached maxNumberOfMemorySegments or one subpartition reached the quota.

  • Method Details

    • reserveSegments

      public void reserveSegments(int numberOfSegmentsToReserve) throws IOException
      Description copied from interface: BufferPool
      Reserves the target number of segments to this pool. Will throw an exception if it can not allocate enough segments.
      Specified by:
      reserveSegments in interface BufferPool
      Throws:
      IOException
    • isDestroyed

      public boolean isDestroyed()
      Description copied from interface: BufferPool
      Checks whether this buffer pool has been destroyed.
      Specified by:
      isDestroyed in interface BufferPool
      Specified by:
      isDestroyed in interface BufferProvider
    • getNumberOfRequiredMemorySegments

      public int getNumberOfRequiredMemorySegments()
      Description copied from interface: BufferPool
      Returns the number of guaranteed (minimum number of) memory segments of this buffer pool.
      Specified by:
      getNumberOfRequiredMemorySegments in interface BufferPool
    • getMaxNumberOfMemorySegments

      public int getMaxNumberOfMemorySegments()
      Description copied from interface: BufferPool
      Returns the maximum number of memory segments this buffer pool should use.
      Specified by:
      getMaxNumberOfMemorySegments in interface BufferPool
      Returns:
      maximum number of memory segments to use or -1 if unlimited
    • getEstimatedNumberOfRequestedMemorySegments

      public int getEstimatedNumberOfRequestedMemorySegments()
      Estimates the number of requested buffers.
      Returns:
      the same value as getMaxNumberOfMemorySegments() for bounded pools. For unbounded pools it returns an approximation based upon getNumberOfRequiredMemorySegments()
    • getNumberOfRequestedMemorySegments

      @VisibleForTesting public int getNumberOfRequestedMemorySegments()
    • getNumberOfAvailableMemorySegments

      public int getNumberOfAvailableMemorySegments()
      Description copied from interface: BufferPool
      Returns the number memory segments, which are currently held by this buffer pool.
      Specified by:
      getNumberOfAvailableMemorySegments in interface BufferPool
    • getNumBuffers

      public int getNumBuffers()
      Description copied from interface: BufferPool
      Returns the current size of this buffer pool.

      The size of the buffer pool can change dynamically at runtime.

      Specified by:
      getNumBuffers in interface BufferPool
    • bestEffortGetNumOfUsedBuffers

      public int bestEffortGetNumOfUsedBuffers()
      Description copied from interface: BufferPool
      Returns the number of used buffers of this buffer pool.
      Specified by:
      bestEffortGetNumOfUsedBuffers in interface BufferPool
    • requestBuffer

      public Buffer requestBuffer()
      Description copied from interface: BufferProvider
      Returns a Buffer instance from the buffer provider, if one is available.
      Specified by:
      requestBuffer in interface BufferProvider
      Returns:
      null if no buffer is available or the buffer provider has been destroyed.
    • requestBufferBuilder

      public BufferBuilder requestBufferBuilder()
      Description copied from interface: BufferProvider
      Returns a BufferBuilder instance from the buffer provider. This equals to BufferProvider.requestBufferBuilder(int) with unknown target channel.
      Specified by:
      requestBufferBuilder in interface BufferProvider
      Returns:
      null if no buffer is available or the buffer provider has been destroyed.
    • requestBufferBuilder

      public BufferBuilder requestBufferBuilder(int targetChannel)
      Description copied from interface: BufferProvider
      Returns a BufferBuilder instance from the buffer provider.
      Specified by:
      requestBufferBuilder in interface BufferProvider
      Parameters:
      targetChannel - to which the request will be accounted to.
      Returns:
      null if no buffer is available or the buffer provider has been destroyed.
    • requestBufferBuilderBlocking

      public BufferBuilder requestBufferBuilderBlocking() throws InterruptedException
      Description copied from interface: BufferProvider
      Returns a BufferBuilder instance from the buffer provider. This equals to BufferProvider.requestBufferBuilderBlocking(int) with unknown target channel.

      If there is no buffer available, the call will block until one becomes available again or the buffer provider has been destroyed.

      Specified by:
      requestBufferBuilderBlocking in interface BufferProvider
      Throws:
      InterruptedException
    • requestMemorySegmentBlocking

      public org.apache.flink.core.memory.MemorySegment requestMemorySegmentBlocking() throws InterruptedException
      Description copied from interface: BufferProvider
      Returns a MemorySegment instance from the buffer provider.

      If there is no memory segment available, the call will block until one becomes available again or the buffer provider has been destroyed.

      Specified by:
      requestMemorySegmentBlocking in interface BufferProvider
      Throws:
      InterruptedException
    • requestBufferBuilderBlocking

      public BufferBuilder requestBufferBuilderBlocking(int targetChannel) throws InterruptedException
      Description copied from interface: BufferProvider
      Returns a BufferBuilder instance from the buffer provider.

      If there is no buffer available, the call will block until one becomes available again or the buffer provider has been destroyed.

      Specified by:
      requestBufferBuilderBlocking in interface BufferProvider
      Parameters:
      targetChannel - to which the request will be accounted to.
      Throws:
      InterruptedException
    • requestMemorySegment

      public org.apache.flink.core.memory.MemorySegment requestMemorySegment()
      Description copied from interface: BufferProvider
      Returns a MemorySegment instance from the buffer provider.
      Specified by:
      requestMemorySegment in interface BufferProvider
      Returns:
      null if no memory segment is available or the buffer provider has been destroyed.
    • recycle

      public void recycle(org.apache.flink.core.memory.MemorySegment segment)
      Description copied from interface: BufferRecycler
      Recycles the MemorySegment to its original BufferPool instance.
      Specified by:
      recycle in interface BufferRecycler
      Parameters:
      segment - The memory segment to be recycled.
    • lazyDestroy

      public void lazyDestroy()
      Destroy is called after the produce or consume phase of a task finishes.
      Specified by:
      lazyDestroy in interface BufferPool
    • addBufferListener

      public boolean addBufferListener(BufferListener listener)
      Description copied from interface: BufferProvider
      Adds a buffer availability listener to the buffer provider.

      The operation fails with return value false, when there is a buffer available or the buffer provider has been destroyed.

      Specified by:
      addBufferListener in interface BufferProvider
    • setNumBuffers

      public void setNumBuffers(int numBuffers)
      Description copied from interface: BufferPool
      Sets the current size of this buffer pool.

      The size needs to be greater or equal to the guaranteed number of memory segments.

      Specified by:
      setNumBuffers in interface BufferPool
    • setMaxOverdraftBuffersPerGate

      public void setMaxOverdraftBuffersPerGate(int maxOverdraftBuffersPerGate)
      Description copied from interface: BufferPool
      Sets the max overdraft buffer size of per gate.
      Specified by:
      setMaxOverdraftBuffersPerGate in interface BufferPool
    • getMaxOverdraftBuffersPerGate

      public int getMaxOverdraftBuffersPerGate()
      Description copied from interface: BufferPool
      Returns the max overdraft buffer size of per gate.
      Specified by:
      getMaxOverdraftBuffersPerGate in interface BufferPool
    • getAvailableFuture

      public CompletableFuture<?> getAvailableFuture()
      Specified by:
      getAvailableFuture in interface AvailabilityProvider
      Returns:
      a future that is completed if the respective provider is available.
    • toString

      public String toString()
      Overrides:
      toString in class Object