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
ADataBuffer
implementation which sorts all appended records only by subpartition index. Records of the same subpartition keep the appended order.It maintains a list of
MemorySegment
s 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.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 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 BufferRecycler
bufferRecycler
BufferRecycler
used to recyclefreeSegments
.protected LinkedList<MemorySegment>
freeSegments
A list ofMemorySegment
s used to store data in memory.protected static 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.protected boolean
isFinished
Whether this sort buffer is finished.protected boolean
isReleased
Whether this sort buffer is released.protected long[]
lastIndexEntryAddresses
Addresses of the last record's index entry for each subpartition.protected long
numTotalBytesRead
Total number of bytes already read from this sort buffer.protected long
readIndexEntryAddress
Index entry address of the current record or event to be read.protected int
readOrderIndex
Used to index the current available subpartition to read data from.protected int
recordRemainingBytes
Record bytes remaining after last copy, which must be read first in next copy.ArrayList<MemorySegment>
segments
A segment list as a joint buffer which stores all records and index entries.protected int[]
subpartitionReadOrder
Data of different subpartitions in this sort buffer will be read in this order.
-
Constructor Summary
Constructors Modifier Constructor Description protected
SortBuffer(LinkedList<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 boolean
append(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 int
copyRecordOrEvent(MemorySegment targetSegment, int targetSegmentOffset, int sourceSegmentIndex, int sourceSegmentOffset, int recordLength)
void
finish()
Finishes thisDataBuffer
which means no record can be appended anymore.protected int
getSegmentIndexFromPointer(long value)
protected int
getSegmentOffsetFromPointer(long value)
boolean
hasRemaining()
Returns true if not all data appended to thisDataBuffer
is consumed.boolean
isFinished()
Whether thisDataBuffer
is finished or not.boolean
isReleased()
Whether thisDataBuffer
is released or not.long
numTotalBytes()
Returns the total number of bytes written to thisDataBuffer
.long
numTotalRecords()
Returns the total number of records written to thisDataBuffer
.void
release()
Releases thisDataBuffer
which releases all resources.protected void
updateReadSubpartitionAndIndexEntryAddress()
-
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<MemorySegment> freeSegments
A list ofMemorySegment
s used to store data in memory.
-
bufferRecycler
protected final BufferRecycler bufferRecycler
BufferRecycler
used to recyclefreeSegments
.
-
segments
public final ArrayList<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<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:
append
in interfaceDataBuffer
- Throws:
IOException
-
copyRecordOrEvent
protected int copyRecordOrEvent(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:DataBuffer
Returns the total number of records written to thisDataBuffer
.- Specified by:
numTotalRecords
in interfaceDataBuffer
-
numTotalBytes
public long numTotalBytes()
Description copied from interface:DataBuffer
Returns the total number of bytes written to thisDataBuffer
.- Specified by:
numTotalBytes
in interfaceDataBuffer
-
hasRemaining
public boolean hasRemaining()
Description copied from interface:DataBuffer
Returns true if not all data appended to thisDataBuffer
is consumed.- Specified by:
hasRemaining
in interfaceDataBuffer
-
finish
public void finish()
Description copied from interface:DataBuffer
Finishes thisDataBuffer
which means no record can be appended anymore.- Specified by:
finish
in interfaceDataBuffer
-
isFinished
public boolean isFinished()
Description copied from interface:DataBuffer
Whether thisDataBuffer
is finished or not.- Specified by:
isFinished
in interfaceDataBuffer
-
release
public void release()
Description copied from interface:DataBuffer
Releases thisDataBuffer
which releases all resources.- Specified by:
release
in interfaceDataBuffer
-
isReleased
public boolean isReleased()
Description copied from interface:DataBuffer
Whether thisDataBuffer
is released or not.- Specified by:
isReleased
in interfaceDataBuffer
-
-