Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Status

Current stateReleased

Discussion threadhttps://lists.apache.org/thread.html/r11750db945277d944f408eaebbbdc9d595d587fcfb67b015c716404e%40%3Cdev.flink.apache.org%3E

...

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Table of Contents

Motivation

Hash-based blocking shuffle and sort-merge based blocking shuffle are two main blocking shuffle implementations wildly adopted by existing distributed data processing frameworks. Hash-based implementation writes data sent to different reducer tasks into separate files concurrently while sort-merge based approach writes those data together into a single file and merges those small files into bigger ones. Compared to sort-merge based approach, hash-based approach has several weak points when it comes to running large scale batch jobs:

  1. Stability: For high parallelism (tens of thousands) batch job, current hash-based blocking shuffle implementation writes too many files concurrently which gives high pressure to the file system, for example, maintenance of too many file metas, exhaustion of inodes or file descriptors. All of these can be potential stability issues. Sort-Merge based blocking shuffle don’t have the problem because for one result partition, only one file is written at the same time.
  2. Performance: Large amounts of small shuffle files and random IO can influence shuffle performance a lot especially for hdd (for ssd, sequential read is also important because of read ahead and cache)HDD. For batch jobs processing massive data, small amount of data per subpartition is common because of high parallelism. Besides, data skew is another cause of small subpartition files. By merging writing data of all subpartitions together in one file and leveraging IO scheduling, more sequential read can be achieved.
  3. Resource: For current hash-based implementation, each subpartition needs at least one buffer. For large scale batch shuffles, the memory consumption can be huge. For example, we need at least 320M network memory per result partition if parallelism is set to 10000 and because of the huge network consumption, it is hard to config the network memory for large scale batch job and  sometimes parallelism can not be increased just because of insufficient network memory  which leads to bad user experience.

By introducing the sort-merge based approach blocking shuffle implementation to Flink, we can improve Flink’s capability of running large scale batch jobs.

Public Interfaces

Several new config options will be added to control the behavior of the sort-merge based blocking shuffle and by disable sort-merge based blocking shuffle by default, the default behavior of blocking shuffle stays unchanged.

...

A fixed number of network buffers per result partition makes the memory consumption decoupled with parallelism which is more friendly for large scale batch jobs.

Proposed Changes

Image Removed

Image Added

  1. Each result partition holds a We have SortBuffer, serialized records and events will be appended to the SortBuffer until the it is full or EOF reached.
  2. Then the PartitionedFileWriter will spill all data in the SortBuffer as one PartitionedFile in subpartition index order and at the same time partition offset information will be also saved.
  3. MergePolicy will collect information of all spilled PartitionedFiles and select a subset or all files to be merged according to the number of files and the file size.
  4. PartitionedFileMerger then merges all the selected PartitionedFiles into one PartitionedFile.
  5. After the SortMergeResultPartition is finished, the consumer task can request the partition data, a SortMergePartitionReader will be created to read the corresponding data.

SortBuffer: Data of different channels can be appended to a SortBuffer and after the SortBuffer is finished, the appended data can be copied from it in channel index order.

...


@Nullable
@Override
public BufferAndBacklog getNextBuffer() throws IOException;

@Override
public void notifyDataAvailable();

@Override
public void recycle(MemorySegment segment);

@Override
public void releaseAllResources();

@Override
public boolean isAvailable(int numCreditsAvailable);
}

The interface of SortBuffer and PartitionedFileMerger is flexible enough and new requirements like sortsorting by record can be also implemented easily if needed.

Further Optimization

As we discussed above, writing data of all subpartitions together in one file makes it more friendly for sequential read and write which can already improve the IO performance a lot. Besides, we can even further improve the IO performance by scheduling the reading and writing IO requests (especially helpful for reading). When shuffling data, the sequential read is restricted by the amount of data of each subpartition, the size of the read buffer and the available credits of the consumer task. The data read pattern can be summarized as reading a chunk of data from different subpartitions in parallel. After data of all subpartitions is spilled to one file in subpartition index order, we can rearrange the data read requests and always serve the data in subpartition order and read as much data in one request. By scheduling the read requests, more sequential reads can be achieved and in the best cases, a data file can be read totally in a sequential way.

Data compression has been implemented for the default hash-based blocking shuffle, which improves the tpcTPC-ds DS benchmark performance by about 30%. We can also implement data compression for sort-merge based blocking shuffle.

For the result partition using the broadcast partitioner, we can copy the serialized record only once to the SortBuffer and write only one copy of the data to disk which can reduce CPU usage and file IO a lot.

If there are multiple disks, load balance is important for good performance. The simplest way to achieve load balance is rebalance disk selection.

For large scale batch jobs, a large number of network connections will be established, which may incur stability issues. We can restrict the number of concurrent partition requests to relieve the issue. Besides, restricting concurrent partition requests can increase the number of network buffers can be used per remote channel, that is, more credits per channel which is helpful for the shuffle reader to read sequentially. (As we mentioned above, the number of available credits can influence sequential read because we can not read more buffers than the consumer can process)

Implementing a stand-alone shuffle service can further improve the shuffle IO performance because it is a centralized service and can collect more information which can lead to more optimized actions. For example, better node-level load balance, better disk-level load balance, further file merging, node-level IO scheduling and shared read/write buffer and thread pool. It can be introduced in a separated FLIP.

Implementation and Test Plan

Basic shuffle logic and data compression will be implemented first, which can make the sort-merge based blocking shuffle available for usage. Main components include 1) SortBuffer and a hash-based data clustering implementation; 2) PartitionedFile together with the corresponding writer (PartitionedFileWriter) and reader (PartitionedFileReader); 3) SortMergeResultPartition and the subpartition data reader SortMergePartitionReader. We will introduce this components separately. For data compression, by reusing the facilities implemented for the existing BoundedBlockingResultPartition, only very small change is needed. Tests will include both unit tests, IT cases and real job test on a cluster. Besides, IO scheduling can improve the shuffle performance a lot so we also need to implement it.

File merge and other optimizations can be implemented as the second step. Main components include MergePolicy, PartitionedFileMerge, IOScheduler and PartitionRequestManager. Tests will include both unit tests, IT cases and real job test on a cluster.

Compatibility, Deprecation, and Migration Plan

The default behavior of Flink stays unchanged. Nothing need to do when migrating to new Flink version.

Appendix

Our goal is to cluster data belonging to the same subpartition together and sort is a nature approach. However, we do not need a generic sort implementation. Given that the subpartition index is a sequence of continuous integers from 0, bucket sort combining linked list is a simpler and more efficient way. Each subpartition takes a bucket and each bucket points to the first record in the binary SortBuffer. Each record also has a pointer pointing to the next record belonging to the same subpartition. The following picture shows how it works:

...