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 ofBufferinstances from theNetworkBufferPool.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 theNetworkBufferPoolto 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-blockingrequestBufferBuilderBlocking(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
NetworkBufferPoolas long as it hasn't reachedmaxNumberOfMemorySegmentsor one subpartition reached the quota.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.flink.runtime.io.AvailabilityProvider
AvailabilityProvider.AvailabilityHelper
-
Nested classes/interfaces inherited from interface org.apache.flink.runtime.io.network.buffer.BufferRecycler
BufferRecycler.DummyBufferRecycler
-
-
Field Summary
-
Fields inherited from interface org.apache.flink.runtime.io.AvailabilityProvider
AVAILABLE
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddBufferListener(BufferListener listener)Adds a buffer availability listener to the buffer provider.intbestEffortGetNumOfUsedBuffers()Returns the number of used buffers of this buffer pool.CompletableFuture<?>getAvailableFuture()intgetEstimatedNumberOfRequestedMemorySegments()Estimates the number of requested buffers.intgetMaxNumberOfMemorySegments()Returns the maximum number of memory segments this buffer pool should use.intgetMaxOverdraftBuffersPerGate()Returns the max overdraft buffer size of per gate.intgetNumberOfAvailableMemorySegments()Returns the number memory segments, which are currently held by this buffer pool.intgetNumberOfRequestedMemorySegments()intgetNumberOfRequiredMemorySegments()Returns the number of guaranteed (minimum number of) memory segments of this buffer pool.intgetNumBuffers()Returns the current size of this buffer pool.booleanisDestroyed()Checks whether this buffer pool has been destroyed.voidlazyDestroy()Destroy is called after the produce or consume phase of a task finishes.voidrecycle(org.apache.flink.core.memory.MemorySegment segment)Recycles theMemorySegmentto its originalBufferPoolinstance.BufferrequestBuffer()Returns aBufferinstance from the buffer provider, if one is available.BufferBuilderrequestBufferBuilder()Returns aBufferBuilderinstance from the buffer provider.BufferBuilderrequestBufferBuilder(int targetChannel)Returns aBufferBuilderinstance from the buffer provider.BufferBuilderrequestBufferBuilderBlocking()Returns aBufferBuilderinstance from the buffer provider.BufferBuilderrequestBufferBuilderBlocking(int targetChannel)Returns aBufferBuilderinstance from the buffer provider.org.apache.flink.core.memory.MemorySegmentrequestMemorySegment()Returns aMemorySegmentinstance from the buffer provider.org.apache.flink.core.memory.MemorySegmentrequestMemorySegmentBlocking()Returns aMemorySegmentinstance from the buffer provider.voidreserveSegments(int numberOfSegmentsToReserve)Reserves the target number of segments to this pool.voidsetMaxOverdraftBuffersPerGate(int maxOverdraftBuffersPerGate)Sets the max overdraft buffer size of per gate.voidsetNumBuffers(int numBuffers)Sets the current size of this buffer pool.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.flink.runtime.io.AvailabilityProvider
isApproximatelyAvailable, isAvailable
-
-
-
-
Method Detail
-
reserveSegments
public void reserveSegments(int numberOfSegmentsToReserve) throws IOExceptionDescription copied from interface:BufferPoolReserves the target number of segments to this pool. Will throw an exception if it can not allocate enough segments.- Specified by:
reserveSegmentsin interfaceBufferPool- Throws:
IOException
-
isDestroyed
public boolean isDestroyed()
Description copied from interface:BufferPoolChecks whether this buffer pool has been destroyed.- Specified by:
isDestroyedin interfaceBufferPool- Specified by:
isDestroyedin interfaceBufferProvider
-
getNumberOfRequiredMemorySegments
public int getNumberOfRequiredMemorySegments()
Description copied from interface:BufferPoolReturns the number of guaranteed (minimum number of) memory segments of this buffer pool.- Specified by:
getNumberOfRequiredMemorySegmentsin interfaceBufferPool
-
getMaxNumberOfMemorySegments
public int getMaxNumberOfMemorySegments()
Description copied from interface:BufferPoolReturns the maximum number of memory segments this buffer pool should use.- Specified by:
getMaxNumberOfMemorySegmentsin interfaceBufferPool- 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 upongetNumberOfRequiredMemorySegments()
-
getNumberOfRequestedMemorySegments
@VisibleForTesting public int getNumberOfRequestedMemorySegments()
-
getNumberOfAvailableMemorySegments
public int getNumberOfAvailableMemorySegments()
Description copied from interface:BufferPoolReturns the number memory segments, which are currently held by this buffer pool.- Specified by:
getNumberOfAvailableMemorySegmentsin interfaceBufferPool
-
getNumBuffers
public int getNumBuffers()
Description copied from interface:BufferPoolReturns the current size of this buffer pool.The size of the buffer pool can change dynamically at runtime.
- Specified by:
getNumBuffersin interfaceBufferPool
-
bestEffortGetNumOfUsedBuffers
public int bestEffortGetNumOfUsedBuffers()
Description copied from interface:BufferPoolReturns the number of used buffers of this buffer pool.- Specified by:
bestEffortGetNumOfUsedBuffersin interfaceBufferPool
-
requestBuffer
public Buffer requestBuffer()
Description copied from interface:BufferProviderReturns aBufferinstance from the buffer provider, if one is available.- Specified by:
requestBufferin interfaceBufferProvider- Returns:
nullif no buffer is available or the buffer provider has been destroyed.
-
requestBufferBuilder
public BufferBuilder requestBufferBuilder()
Description copied from interface:BufferProviderReturns aBufferBuilderinstance from the buffer provider. This equals toBufferProvider.requestBufferBuilder(int)with unknown target channel.- Specified by:
requestBufferBuilderin interfaceBufferProvider- Returns:
nullif no buffer is available or the buffer provider has been destroyed.
-
requestBufferBuilder
public BufferBuilder requestBufferBuilder(int targetChannel)
Description copied from interface:BufferProviderReturns aBufferBuilderinstance from the buffer provider.- Specified by:
requestBufferBuilderin interfaceBufferProvider- Parameters:
targetChannel- to which the request will be accounted to.- Returns:
nullif no buffer is available or the buffer provider has been destroyed.
-
requestBufferBuilderBlocking
public BufferBuilder requestBufferBuilderBlocking() throws InterruptedException
Description copied from interface:BufferProviderReturns aBufferBuilderinstance from the buffer provider. This equals toBufferProvider.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:
requestBufferBuilderBlockingin interfaceBufferProvider- Throws:
InterruptedException
-
requestMemorySegmentBlocking
public org.apache.flink.core.memory.MemorySegment requestMemorySegmentBlocking() throws InterruptedExceptionDescription copied from interface:BufferProviderReturns aMemorySegmentinstance 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:
requestMemorySegmentBlockingin interfaceBufferProvider- Throws:
InterruptedException
-
requestBufferBuilderBlocking
public BufferBuilder requestBufferBuilderBlocking(int targetChannel) throws InterruptedException
Description copied from interface:BufferProviderReturns aBufferBuilderinstance 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:
requestBufferBuilderBlockingin interfaceBufferProvider- 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:BufferProviderReturns aMemorySegmentinstance from the buffer provider.- Specified by:
requestMemorySegmentin interfaceBufferProvider- Returns:
nullif 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:BufferRecyclerRecycles theMemorySegmentto its originalBufferPoolinstance.- Specified by:
recyclein interfaceBufferRecycler- 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:
lazyDestroyin interfaceBufferPool
-
addBufferListener
public boolean addBufferListener(BufferListener listener)
Description copied from interface:BufferProviderAdds 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:
addBufferListenerin interfaceBufferProvider
-
setNumBuffers
public void setNumBuffers(int numBuffers)
Description copied from interface:BufferPoolSets the current size of this buffer pool.The size needs to be greater or equal to the guaranteed number of memory segments.
- Specified by:
setNumBuffersin interfaceBufferPool
-
setMaxOverdraftBuffersPerGate
public void setMaxOverdraftBuffersPerGate(int maxOverdraftBuffersPerGate)
Description copied from interface:BufferPoolSets the max overdraft buffer size of per gate.- Specified by:
setMaxOverdraftBuffersPerGatein interfaceBufferPool
-
getMaxOverdraftBuffersPerGate
public int getMaxOverdraftBuffersPerGate()
Description copied from interface:BufferPoolReturns the max overdraft buffer size of per gate.- Specified by:
getMaxOverdraftBuffersPerGatein interfaceBufferPool
-
getAvailableFuture
public CompletableFuture<?> getAvailableFuture()
- Specified by:
getAvailableFuturein interfaceAvailabilityProvider- Returns:
- a future that is completed if the respective provider is available.
-
-