Repository navigation
Conversation
…tream to the partition - A spill opens a codec stream per partition: SnappyCodec and Lz4Codec parse the buffer size from the Configuration and allocate and zero a 256 KiB buffer for each, and IFile.Writer a 64 KiB write buffer, while a partition holds a few KB (Hive LLAP, TPC-DS 10 TB q75 / q64: writer setup in spills is 3.2 / 3.5 % of LLAP CPU, Integer.parseInt 1.5 / 1.5 % and these buffers 1.0 / 1.4 % of it) - CodecUtils reads the buffer size once per output and builds these codecs' BlockCompressorStream itself: a stream known to fit one block gets a block of its length and a buffer of its compression bound, which writes the same bytes; IFile.Writer sizes its write buffer to the stream too - The spill passes each partition's raw length bound from the buffers' record and byte counts, the final merge the sum of the spilled raw lengths; other codecs and writers keep codec.createOutputStream - JMH, one spill of 1,009 partitions (Snappy, 22 B / 2.2 KB / 22 KB per partition): -76 % / -48 % / -10 %, 336 MB allocated -> 5 / 11 / 62 MB; LZ4 -59 % / -41 % / -12 %
|
💔 -1 overall
This message was automatically generated. |
- spillInfoList is a ConcurrentLinkedQueue: spill threads add without a lock, and the loops that run after close() waits for the spills iterate without one - Tests: drop the checks that repeat the byte equality with the unsized writer or stream (read-backs, decompression, the stream buffer read by reflection) - lz4-java at Hadoop's version in test scope, so the Lz4Codec cases run instead of skipping
|
💔 -1 overall
This message was automatically generated. |
please include a 220KB and maybe 1MB benchmark result, it would be fair to see that the patch is not supposed to introduce any regression |
abstractdog
left a comment
There was a problem hiding this comment.
nice improvement @deniskuzZ , only minor comments
| /** | ||
| * @param streamLength upper bound of the bytes written to the codec or checksum stream (the | ||
| * header bypasses them), or -1 if unknown | ||
| */ |
There was a problem hiding this comment.
if streamLength param is covered by javadoc, then codec and codeBufferSize should also be covered
|
|
||
| @VisibleForTesting | ||
| final List<SpillInfo> spillInfoList = Collections.synchronizedList(new ArrayList<SpillInfo>()); | ||
| final Queue<SpillInfo> spillInfoList = new ConcurrentLinkedQueue<>(); |
There was a problem hiding this comment.
this change looks unrelated to the original goal, but it's okay, and makes the code much cleaner (removing synchronized blocks), please just add a small not in the PR, it doesn't need a follow-up ticket
| } | ||
|
|
||
| private static long getCurrentThreadAllocatedBytes() { | ||
| return ((com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean()) |
There was a problem hiding this comment.
use import instead of fully qualified name
| conf.setInt(CodecUtils.getBufferSizeProperty(codecClass.getName()), 64 * 1024); | ||
| CompressionCodec codec = ReflectionUtils.newInstance(codecClass, conf); | ||
| int bufferSize = CodecUtils.getOutputBufferSize(codec); | ||
| assertEquals(64 * 1024, bufferSize); |
There was a problem hiding this comment.
maybe message, that can also contain the actual codec name, given this is the coverage for CodecUtils.getOutputBufferSize
| ByteArrayOutputStream unsized = new ByteArrayOutputStream(); | ||
| ByteArrayOutputStream sized = new ByteArrayOutputStream(); |
There was a problem hiding this comment.
unsized/sized looks a bit odd, it's actually sizeConfigured/sizedToData, I believe these can be renamed or code commented
| * record to several codec blocks, and with values longer than the write buffer. | ||
| */ | ||
| @Test | ||
| public void testSizedWriterWritesUnsizedBytes() throws IOException { |
There was a problem hiding this comment.
testWriterSizedToDataWritesConfiguredBytes, it would be inline with other test class named testOutputStreamSizedToData
| private UnorderedPartitionedKVWriter createSnappyWriter(int numPartitions, long availableMemory, | ||
| int maxSingleBufferSizeBytes, boolean finalMerge) throws IOException { | ||
| OutputContext outputContext = createMockOutputContext(new TezCounters(), ApplicationId.newInstance(10000000, 1), | ||
| UUID.randomUUID().toString(), defaultConf.get(TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID, |
There was a problem hiding this comment.
remove defaultConf usage as suggested in another PR: https://github.com/apache/tez/pull/550/changes#r4665629676
| kvWriter = createSnappyWriter(numPartitions, 2048, -1, true); | ||
| write(kvWriter, 5000); | ||
| kvWriter.close(); | ||
| assertTrue(kvWriter.numSpills.get() > 100); |
There was a problem hiding this comment.
add clarity here:
assertTrue(kvWriter.numSpills.get() > 100,
"expected a merge of many spills (~119: two 1 KiB buffers of 24 B records), got " + kvWriter.numSpills.get());
| kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false); | ||
| write(kvWriter, 5000); | ||
| kvWriter.close(); | ||
| assertTrue(kvWriter.spillLimit > 1); |
There was a problem hiding this comment.
spillLimit is fixed by the constructor and doesn't depend on timing, so > 1 is looser than it needs to be
assertEquals(10, kvWriter.spillLimit, "each spill should take all 10 buffers");
| byte[] segment = Arrays.copyOfRange(output, start, start + (int) indexRecord.getPartLength()); | ||
| Reader reader = new Reader(new ByteArrayInputStream(segment), segment.length, kvWriter.codec, null, null, | ||
| false, 0, -1); | ||
| ByteArrayOutputStream unsized = new ByteArrayOutputStream(); |
A spill of
UnorderedPartitionedKVWriteropens anIFile.Writerper partition, and for Snappy and LZ4 each one goesthrough
codec.createOutputStream: the codec parses its buffer size from theConfiguration(under the codec's conflock), and
BlockCompressorStreamallocates and zeroes a 256 KB buffer plus the compressed buffer, while a partitionoften holds a few KB. With 1,009 partitions that is ~336 MB allocated per spill. In Hive on Tez (LLAP), TPC-DS 10 TB,
the part of this setup that the patch removes is 2.5 % (q75) and 2.8 % (q64) of LLAP CPU (profile in TEZ-4762).
The patch sizes each partition's stream to its data:
CodecUtils.getOutputBufferSize(codec)reads the buffer size once per output (-1 for codecs other than Snappy andLZ4), and
CodecUtils.createOutputStream(codec, out, compressor, bufferSize, dataLength)builds theirBlockCompressorStreamdirectly, with noConfigurationlookup and no lock. A stream known to fit in one block gets ablock of its length and a buffer of its compression bound, and writes the same bytes as the configured stream;
otherwise the configured stream is used.
IFile.Writertakes the codec buffer size and an upper bound of its raw length (Writer.maxRawLength(records, keyValueBytes)), and sizes its write buffer to the stream too.UnorderedPartitionedKVWriterpasses each partition's bound from the buffers' record and byte counts on a spill, andthe sum of the spilled raw lengths on the final merge. Other codecs and other writers keep
codec.createOutputStream.Microbenchmark (JMH, one spill of 1,009 partitions, 22 B records, JDK 21, Apple M-series, avgt of 10; before = the
configured stream, as on master):
Tests:
TestCodecUtils(a sized stream writes the same bytes as the configured one and round-trips, also when thelength is underestimated),
TestIFile(a sized writer writes the bytes of an unsized one),TestUnorderedPartitionedKVWriter(a spill's streams are sized to their partitions, bounded allocation, and everypartition is written as the unsized path would); with the existing tests of the three classes, 401 run, 0 failures.
Nine mutations of the sizing (no sizing, buffer without the compression bound, block one byte short, bound without the
vints, spill unsized, spill reading the conf, merge ignoring the spills, write buffer unsized, spill bound from the first
buffer) each fail at least one of them.