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 ofBuffer
instances 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 theNetworkBufferPool
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-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
NetworkBufferPool
as long as it hasn't reachedmaxNumberOfMemorySegments
or 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 boolean
addBufferListener(BufferListener listener)
Adds a buffer availability listener to the buffer provider.int
bestEffortGetNumOfUsedBuffers()
Returns the number of used buffers of this buffer pool.CompletableFuture<?>
getAvailableFuture()
int
getEstimatedNumberOfRequestedMemorySegments()
Estimates the number of requested buffers.int
getMaxNumberOfMemorySegments()
Returns the maximum number of memory segments this buffer pool should use.int
getMaxOverdraftBuffersPerGate()
Returns the max overdraft buffer size of per gate.int
getNumberOfAvailableMemorySegments()
Returns the number memory segments, which are currently held by this buffer pool.int
getNumberOfRequestedMemorySegments()
int
getNumberOfRequiredMemorySegments()
Returns the number of guaranteed (minimum number of) memory segments of this buffer pool.int
getNumBuffers()
Returns the current size of this buffer pool.boolean
isDestroyed()
Checks whether this buffer pool has been destroyed.void
lazyDestroy()
Destroy is called after the produce or consume phase of a task finishes.void
recycle(MemorySegment segment)
Recycles theMemorySegment
to its originalBufferPool
instance.Buffer
requestBuffer()
Returns aBuffer
instance from the buffer provider, if one is available.BufferBuilder
requestBufferBuilder()
Returns aBufferBuilder
instance from the buffer provider.BufferBuilder
requestBufferBuilder(int targetChannel)
Returns aBufferBuilder
instance from the buffer provider.BufferBuilder
requestBufferBuilderBlocking()
Returns aBufferBuilder
instance from the buffer provider.BufferBuilder
requestBufferBuilderBlocking(int targetChannel)
Returns aBufferBuilder
instance from the buffer provider.MemorySegment
requestMemorySegment()
Returns aMemorySegment
instance from the buffer provider.MemorySegment
requestMemorySegmentBlocking()
Returns aMemorySegment
instance from the buffer provider.void
reserveSegments(int numberOfSegmentsToReserve)
Reserves the target number of segments to this pool.void
setMaxOverdraftBuffersPerGate(int maxOverdraftBuffersPerGate)
Sets the max overdraft buffer size of per gate.void
setNumBuffers(int numBuffers)
Sets the current size of this buffer pool.String
toString()
-
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 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 interfaceBufferPool
- Throws:
IOException
-
isDestroyed
public boolean isDestroyed()
Description copied from interface:BufferPool
Checks whether this buffer pool has been destroyed.- Specified by:
isDestroyed
in interfaceBufferPool
- Specified by:
isDestroyed
in interfaceBufferProvider
-
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 interfaceBufferPool
-
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 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:BufferPool
Returns the number memory segments, which are currently held by this buffer pool.- Specified by:
getNumberOfAvailableMemorySegments
in interfaceBufferPool
-
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 interfaceBufferPool
-
bestEffortGetNumOfUsedBuffers
public int bestEffortGetNumOfUsedBuffers()
Description copied from interface:BufferPool
Returns the number of used buffers of this buffer pool.- Specified by:
bestEffortGetNumOfUsedBuffers
in interfaceBufferPool
-
requestBuffer
public Buffer requestBuffer()
Description copied from interface:BufferProvider
Returns aBuffer
instance from the buffer provider, if one is available.- Specified by:
requestBuffer
in interfaceBufferProvider
- Returns:
null
if no buffer is available or the buffer provider has been destroyed.
-
requestBufferBuilder
public BufferBuilder requestBufferBuilder()
Description copied from interface:BufferProvider
Returns aBufferBuilder
instance from the buffer provider. This equals toBufferProvider.requestBufferBuilder(int)
with unknown target channel.- Specified by:
requestBufferBuilder
in interfaceBufferProvider
- 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 aBufferBuilder
instance from the buffer provider.- Specified by:
requestBufferBuilder
in interfaceBufferProvider
- 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 aBufferBuilder
instance 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:
requestBufferBuilderBlocking
in interfaceBufferProvider
- Throws:
InterruptedException
-
requestMemorySegmentBlocking
public MemorySegment requestMemorySegmentBlocking() throws InterruptedException
Description copied from interface:BufferProvider
Returns aMemorySegment
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 interfaceBufferProvider
- Throws:
InterruptedException
-
requestBufferBuilderBlocking
public BufferBuilder requestBufferBuilderBlocking(int targetChannel) throws InterruptedException
Description copied from interface:BufferProvider
Returns aBufferBuilder
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 interfaceBufferProvider
- Parameters:
targetChannel
- to which the request will be accounted to.- Throws:
InterruptedException
-
requestMemorySegment
public MemorySegment requestMemorySegment()
Description copied from interface:BufferProvider
Returns aMemorySegment
instance from the buffer provider.- Specified by:
requestMemorySegment
in interfaceBufferProvider
- Returns:
null
if no memory segment is available or the buffer provider has been destroyed.
-
recycle
public void recycle(MemorySegment segment)
Description copied from interface:BufferRecycler
Recycles theMemorySegment
to its originalBufferPool
instance.- Specified by:
recycle
in 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:
lazyDestroy
in interfaceBufferPool
-
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 interfaceBufferProvider
-
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 interfaceBufferPool
-
setMaxOverdraftBuffersPerGate
public void setMaxOverdraftBuffersPerGate(int maxOverdraftBuffersPerGate)
Description copied from interface:BufferPool
Sets the max overdraft buffer size of per gate.- Specified by:
setMaxOverdraftBuffersPerGate
in interfaceBufferPool
-
getMaxOverdraftBuffersPerGate
public int getMaxOverdraftBuffersPerGate()
Description copied from interface:BufferPool
Returns the max overdraft buffer size of per gate.- Specified by:
getMaxOverdraftBuffersPerGate
in interfaceBufferPool
-
getAvailableFuture
public CompletableFuture<?> getAvailableFuture()
- Specified by:
getAvailableFuture
in interfaceAvailabilityProvider
- Returns:
- a future that is completed if the respective provider is available.
-
-