Class SortBuffer
- java.lang.Object
-
- org.apache.flink.runtime.io.network.partition.SortBuffer
-
- All Implemented Interfaces:
DataBuffer
- Direct Known Subclasses:
SortBasedDataBuffer,TieredStorageSortBuffer
@NotThreadSafe public abstract class SortBuffer extends Object implements DataBuffer
ADataBufferimplementation which sorts all appended records only by subpartition index. Records of the same subpartition keep the appended order.It maintains a list of
MemorySegments as a joint buffer. Data will be appended to the joint buffer sequentially. When writing a record, an index entry will be appended first. An index entry consists of 4 fields: 4 bytes for record length, 4 bytes forBuffer.DataTypeand 8 bytes for address pointing to the next index entry of the same subpartition which will be used to index the next record to read when coping data from thisDataBuffer. For simplicity, no index entry can span multiple segments. The corresponding record data is seated right after its index entry and different from the index entry, records have variable length thus may span multiple segments.
-
-
Field Summary
Fields Modifier and Type Field Description protected BufferRecyclerbufferRecyclerBufferRecyclerused to recyclefreeSegments.protected LinkedList<org.apache.flink.core.memory.MemorySegment>freeSegmentsA list ofMemorySegments used to store data in memory.protected static intINDEX_ENTRY_SIZESize of an index entry: 4 bytes for record length, 4 bytes for data type and 8 bytes for pointer to next entry.protected booleanisFinishedWhether this sort buffer is finished.protected booleanisReleasedWhether this sort buffer is released.protected long[]lastIndexEntryAddressesAddresses of the last record's index entry for each subpartition.protected longnumTotalBytesReadTotal number of bytes already read from this sort buffer.protected longreadIndexEntryAddressIndex entry address of the current record or event to be read.protected intreadOrderIndexUsed to index the current available subpartition to read data from.protected intrecordRemainingBytesRecord bytes remaining after last copy, which must be read first in next copy.ArrayList<org.apache.flink.core.memory.MemorySegment>segmentsA segment list as a joint buffer which stores all records and index entries.protected int[]subpartitionReadOrderData of different subpartitions in this sort buffer will be read in this order.
-
Constructor Summary
Constructors Modifier Constructor Description protectedSortBuffer(LinkedList<org.apache.flink.core.memory.MemorySegment> freeSegments, BufferRecycler bufferRecycler, int numSubpartitions, int bufferSize, int numGuaranteedBuffers, int[] customReadOrder)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanappend(ByteBuffer source, int targetSubpartition, Buffer.DataType dataType)No partial record will be written to thisSortBasedDataBuffer, which means that either all data of target record will be written or nothing will be written.protected intcopyRecordOrEvent(org.apache.flink.core.memory.MemorySegment targetSegment, int targetSegmentOffset, int sourceSegmentIndex, int sourceSegmentOffset, int recordLength)voidfinish()Finishes thisDataBufferwhich means no record can be appended anymore.protected intgetSegmentIndexFromPointer(long value)protected intgetSegmentOffsetFromPointer(long value)booleanhasRemaining()Returns true if not all data appended to thisDataBufferis consumed.booleanisFinished()Whether thisDataBufferis finished or not.booleanisReleased()Whether thisDataBufferis released or not.longnumTotalBytes()Returns the total number of bytes written to thisDataBuffer.longnumTotalRecords()Returns the total number of records written to thisDataBuffer.voidrelease()Releases thisDataBufferwhich releases all resources.protected voidupdateReadSubpartitionAndIndexEntryAddress()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.flink.runtime.io.network.partition.DataBuffer
getNextBuffer
-
-
-
-
Field Detail
-
INDEX_ENTRY_SIZE
protected static final int INDEX_ENTRY_SIZE
Size of an index entry: 4 bytes for record length, 4 bytes for data type and 8 bytes for pointer to next entry.- See Also:
- Constant Field Values
-
freeSegments
protected final LinkedList<org.apache.flink.core.memory.MemorySegment> freeSegments
A list ofMemorySegments used to store data in memory.
-
bufferRecycler
protected final BufferRecycler bufferRecycler
BufferRecyclerused to recyclefreeSegments.
-
segments
public final ArrayList<org.apache.flink.core.memory.MemorySegment> segments
A segment list as a joint buffer which stores all records and index entries.
-
lastIndexEntryAddresses
protected final long[] lastIndexEntryAddresses
Addresses of the last record's index entry for each subpartition.
-
numTotalBytesRead
protected long numTotalBytesRead
Total number of bytes already read from this sort buffer.
-
isFinished
protected boolean isFinished
Whether this sort buffer is finished. One can only read a finished sort buffer.
-
isReleased
protected boolean isReleased
Whether this sort buffer is released. A released sort buffer can not be used.
-
subpartitionReadOrder
protected final int[] subpartitionReadOrder
Data of different subpartitions in this sort buffer will be read in this order.
-
readIndexEntryAddress
protected long readIndexEntryAddress
Index entry address of the current record or event to be read.
-
recordRemainingBytes
protected int recordRemainingBytes
Record bytes remaining after last copy, which must be read first in next copy.
-
readOrderIndex
protected int readOrderIndex
Used to index the current available subpartition to read data from.
-
-
Constructor Detail
-
SortBuffer
protected SortBuffer(LinkedList<org.apache.flink.core.memory.MemorySegment> freeSegments, BufferRecycler bufferRecycler, int numSubpartitions, int bufferSize, int numGuaranteedBuffers, @Nullable int[] customReadOrder)
-
-
Method Detail
-
append
public boolean append(ByteBuffer source, int targetSubpartition, Buffer.DataType dataType) throws IOException
No partial record will be written to thisSortBasedDataBuffer, which means that either all data of target record will be written or nothing will be written.- Specified by:
appendin interfaceDataBuffer- Throws:
IOException
-
copyRecordOrEvent
protected int copyRecordOrEvent(org.apache.flink.core.memory.MemorySegment targetSegment, int targetSegmentOffset, int sourceSegmentIndex, int sourceSegmentOffset, int recordLength)
-
updateReadSubpartitionAndIndexEntryAddress
protected void updateReadSubpartitionAndIndexEntryAddress()
-
getSegmentIndexFromPointer
protected int getSegmentIndexFromPointer(long value)
-
getSegmentOffsetFromPointer
protected int getSegmentOffsetFromPointer(long value)
-
numTotalRecords
public long numTotalRecords()
Description copied from interface:DataBufferReturns the total number of records written to thisDataBuffer.- Specified by:
numTotalRecordsin interfaceDataBuffer
-
numTotalBytes
public long numTotalBytes()
Description copied from interface:DataBufferReturns the total number of bytes written to thisDataBuffer.- Specified by:
numTotalBytesin interfaceDataBuffer
-
hasRemaining
public boolean hasRemaining()
Description copied from interface:DataBufferReturns true if not all data appended to thisDataBufferis consumed.- Specified by:
hasRemainingin interfaceDataBuffer
-
finish
public void finish()
Description copied from interface:DataBufferFinishes thisDataBufferwhich means no record can be appended anymore.- Specified by:
finishin interfaceDataBuffer
-
isFinished
public boolean isFinished()
Description copied from interface:DataBufferWhether thisDataBufferis finished or not.- Specified by:
isFinishedin interfaceDataBuffer
-
release
public void release()
Description copied from interface:DataBufferReleases thisDataBufferwhich releases all resources.- Specified by:
releasein interfaceDataBuffer
-
isReleased
public boolean isReleased()
Description copied from interface:DataBufferWhether thisDataBufferis released or not.- Specified by:
isReleasedin interfaceDataBuffer
-
-