Class SortBuffer
java.lang.Object
org.apache.flink.runtime.io.network.partition.SortBuffer
- All Implemented Interfaces:
DataBuffer
- Direct Known Subclasses:
SortBasedDataBuffer,TieredStorageSortBuffer
A
DataBuffer implementation 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 for Buffer.DataType and 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 this DataBuffer. 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
FieldsModifier and TypeFieldDescriptionprotected final BufferRecyclerBufferRecyclerused to recyclefreeSegments.protected final LinkedList<org.apache.flink.core.memory.MemorySegment>A list ofMemorySegments used to store data in memory.protected static final intSize of an index entry: 4 bytes for record length, 4 bytes for data type and 8 bytes for pointer to next entry.protected booleanWhether this sort buffer is finished.protected booleanWhether this sort buffer is released.protected final long[]Addresses of the last record's index entry for each subpartition.protected longTotal number of bytes already read from this sort buffer.protected longIndex entry address of the current record or event to be read.protected intUsed to index the current available subpartition to read data from.protected intRecord bytes remaining after last copy, which must be read first in next copy.final ArrayList<org.apache.flink.core.memory.MemorySegment>A segment list as a joint buffer which stores all records and index entries.protected final int[]Data of different subpartitions in this sort buffer will be read in this order. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSortBuffer(LinkedList<org.apache.flink.core.memory.MemorySegment> freeSegments, BufferRecycler bufferRecycler, int numSubpartitions, int bufferSize, int numGuaranteedBuffers, int[] customReadOrder) -
Method Summary
Modifier and TypeMethodDescriptionbooleanappend(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) booleanReturns true if not all data appended to thisDataBufferis consumed.booleanWhether thisDataBufferis finished or not.booleanWhether thisDataBufferis released or not.longReturns the total number of bytes written to thisDataBuffer.longReturns the total number of records written to thisDataBuffer.voidrelease()Releases thisDataBufferwhich releases all resources.protected voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.flink.runtime.io.network.partition.DataBuffer
getNextBuffer
-
Field Details
-
INDEX_ENTRY_SIZE
protected static final int INDEX_ENTRY_SIZESize of an index entry: 4 bytes for record length, 4 bytes for data type and 8 bytes for pointer to next entry.- See Also:
-
freeSegments
A list ofMemorySegments used to store data in memory. -
bufferRecycler
BufferRecyclerused to recyclefreeSegments. -
segments
A segment list as a joint buffer which stores all records and index entries. -
lastIndexEntryAddresses
protected final long[] lastIndexEntryAddressesAddresses of the last record's index entry for each subpartition. -
numTotalBytesRead
protected long numTotalBytesReadTotal number of bytes already read from this sort buffer. -
isFinished
protected boolean isFinishedWhether this sort buffer is finished. One can only read a finished sort buffer. -
isReleased
protected boolean isReleasedWhether this sort buffer is released. A released sort buffer can not be used. -
subpartitionReadOrder
protected final int[] subpartitionReadOrderData of different subpartitions in this sort buffer will be read in this order. -
readIndexEntryAddress
protected long readIndexEntryAddressIndex entry address of the current record or event to be read. -
recordRemainingBytes
protected int recordRemainingBytesRecord bytes remaining after last copy, which must be read first in next copy. -
readOrderIndex
protected int readOrderIndexUsed to index the current available subpartition to read data from.
-
-
Constructor Details
-
SortBuffer
protected SortBuffer(LinkedList<org.apache.flink.core.memory.MemorySegment> freeSegments, BufferRecycler bufferRecycler, int numSubpartitions, int bufferSize, int numGuaranteedBuffers, @Nullable int[] customReadOrder)
-
-
Method Details
-
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
-