abstractdog commented on code in PR #547:
URL: https://github.com/apache/tez/pull/547#discussion_r4693596306
##########
tez-runtime-library/src/main/java/org/apache/tez/runtime/library/common/sort/impl/IFile.java:
##########
@@ -353,32 +373,48 @@ public Writer(Serialization keySerialization,
Serialization valSerialization, FS
}
}
- void setupOutputStream(CompressionCodec codec) throws IOException {
+ /**
+ * @param streamLength upper bound of the bytes written to the codec or
checksum stream (the
+ * header bypasses them), or -1 if unknown
+ */
Review Comment:
if `streamLength` param is covered by javadoc, then `codec` and
`codeBufferSize `should also be covered
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1601,6 +1608,130 @@ private static String createRandomString(int size) {
return sb.toString();
}
+ /**
+ * A spill sizes each partition's stream to the partition without reading
the codec's buffer size
+ * per stream, and writes the bytes of an unsized writer: a final spill of
many partitions, spills
+ * of several buffers each, and a merge of many spills.
+ */
+ @Test
+ public void testSpillStreamsSizedToPartitions() throws IOException,
InterruptedException {
+ setupInit(true, ReportPartitionStats.DISABLED);
+ int numPartitions = 1009;
+ UnorderedPartitionedKVWriter kvWriter = createSnappyWriter(numPartitions,
16 << 20, -1, true);
+ write(kvWriter, 50_000);
+ ReadCountingConfiguration conf = (ReadCountingConfiguration) kvWriter.conf;
+ // The final spill runs on this thread; it borrows the pooled compressor
instead of creating one.
+ CodecPool.returnCompressor(CodecPool.getCompressor(kvWriter.codec));
+ int reads = conf.reads.get();
+ long allocated = getCurrentThreadAllocatedBytes();
+ kvWriter.close();
+ allocated = getCurrentThreadAllocatedBytes() - allocated;
+ assertEquals(1, kvWriter.numSpills.get());
+ assertEquals(reads, conf.reads.get());
+ // The configured buffers would take 320 KiB per partition: 256 KiB codec
+ 64 KiB write buffer.
+ assertTrue(allocated < numPartitions * 32L * 1024, "allocated " +
allocated);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+
+ numPartitions = 10;
+ kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.spillLimit > 1);
+ for (int i = 0; i < kvWriter.numSpills.get(); i++) {
+ assertPartitionsWrittenUnsized(kvWriter,
kvWriter.outputFileHandler.getSpillFileForWrite(i, 0),
+ kvWriter.outputFileHandler.getSpillIndexFileForWrite(i, 0),
numPartitions);
+ }
+
+ kvWriter = createSnappyWriter(numPartitions, 2048, -1, true);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.numSpills.get() > 100);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+ }
+
+ /** Spills of a writer without final merge take all its buffers. */
+ 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,
+ TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID_DEFAULT));
+ Configuration conf = new ReadCountingConfiguration(
+ createConfiguration(outputContext, IntWritable.class,
LongWritable.class, true, maxSingleBufferSizeBytes),
+ CommonConfigurationKeys.IO_COMPRESSION_CODEC_SNAPPY_BUFFERSIZE_KEY);
+ conf.set(TezRuntimeConfiguration.TEZ_RUNTIME_COMPRESS_CODEC,
SnappyCodec.class.getName());
+
conf.setBoolean(TezRuntimeConfiguration.TEZ_RUNTIME_ENABLE_FINAL_MERGE_IN_OUTPUT,
finalMerge);
+ if (!finalMerge) {
+
conf.setInt(TezRuntimeConfiguration.TEZ_RUNTIME_UNORDERED_PARTITIONED_KVWRITER_BUFFER_MERGE_PERCENT,
100);
+ }
+ return new UnorderedPartitionedKVWriterForTest(outputContext, conf,
numPartitions, availableMemory);
+ }
+
+ private static void write(UnorderedPartitionedKVWriter kvWriter, int
numRecords) throws IOException {
+ IntWritable key = new IntWritable();
+ LongWritable value = new LongWritable();
+ for (int i = 0; i < numRecords; i++) {
+ key.set(i);
+ value.set(i);
+ kvWriter.write(key, value);
+ }
+ }
+
+ private static long getCurrentThreadAllocatedBytes() {
+ return ((com.sun.management.ThreadMXBean)
ManagementFactory.getThreadMXBean())
Review Comment:
use import instead of fully qualified name
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1601,6 +1608,130 @@ private static String createRandomString(int size) {
return sb.toString();
}
+ /**
+ * A spill sizes each partition's stream to the partition without reading
the codec's buffer size
+ * per stream, and writes the bytes of an unsized writer: a final spill of
many partitions, spills
+ * of several buffers each, and a merge of many spills.
+ */
+ @Test
+ public void testSpillStreamsSizedToPartitions() throws IOException,
InterruptedException {
+ setupInit(true, ReportPartitionStats.DISABLED);
+ int numPartitions = 1009;
+ UnorderedPartitionedKVWriter kvWriter = createSnappyWriter(numPartitions,
16 << 20, -1, true);
+ write(kvWriter, 50_000);
+ ReadCountingConfiguration conf = (ReadCountingConfiguration) kvWriter.conf;
+ // The final spill runs on this thread; it borrows the pooled compressor
instead of creating one.
+ CodecPool.returnCompressor(CodecPool.getCompressor(kvWriter.codec));
+ int reads = conf.reads.get();
+ long allocated = getCurrentThreadAllocatedBytes();
+ kvWriter.close();
+ allocated = getCurrentThreadAllocatedBytes() - allocated;
+ assertEquals(1, kvWriter.numSpills.get());
+ assertEquals(reads, conf.reads.get());
+ // The configured buffers would take 320 KiB per partition: 256 KiB codec
+ 64 KiB write buffer.
+ assertTrue(allocated < numPartitions * 32L * 1024, "allocated " +
allocated);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+
+ numPartitions = 10;
+ kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.spillLimit > 1);
+ for (int i = 0; i < kvWriter.numSpills.get(); i++) {
+ assertPartitionsWrittenUnsized(kvWriter,
kvWriter.outputFileHandler.getSpillFileForWrite(i, 0),
+ kvWriter.outputFileHandler.getSpillIndexFileForWrite(i, 0),
numPartitions);
+ }
+
+ kvWriter = createSnappyWriter(numPartitions, 2048, -1, true);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.numSpills.get() > 100);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+ }
+
+ /** Spills of a writer without final merge take all its buffers. */
+ 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,
+ TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID_DEFAULT));
+ Configuration conf = new ReadCountingConfiguration(
+ createConfiguration(outputContext, IntWritable.class,
LongWritable.class, true, maxSingleBufferSizeBytes),
+ CommonConfigurationKeys.IO_COMPRESSION_CODEC_SNAPPY_BUFFERSIZE_KEY);
+ conf.set(TezRuntimeConfiguration.TEZ_RUNTIME_COMPRESS_CODEC,
SnappyCodec.class.getName());
+
conf.setBoolean(TezRuntimeConfiguration.TEZ_RUNTIME_ENABLE_FINAL_MERGE_IN_OUTPUT,
finalMerge);
+ if (!finalMerge) {
+
conf.setInt(TezRuntimeConfiguration.TEZ_RUNTIME_UNORDERED_PARTITIONED_KVWRITER_BUFFER_MERGE_PERCENT,
100);
+ }
+ return new UnorderedPartitionedKVWriterForTest(outputContext, conf,
numPartitions, availableMemory);
+ }
+
+ private static void write(UnorderedPartitionedKVWriter kvWriter, int
numRecords) throws IOException {
+ IntWritable key = new IntWritable();
+ LongWritable value = new LongWritable();
+ for (int i = 0; i < numRecords; i++) {
+ key.set(i);
+ value.set(i);
+ kvWriter.write(key, value);
+ }
+ }
+
+ private static long getCurrentThreadAllocatedBytes() {
+ return ((com.sun.management.ThreadMXBean)
ManagementFactory.getThreadMXBean())
+ .getCurrentThreadAllocatedBytes();
+ }
+
+ /** Each partition of the output holds the bytes an unsized writer writes
for its records. */
+ private static void
assertPartitionsWrittenUnsized(UnorderedPartitionedKVWriter kvWriter, Path
outputPath,
Review Comment:
maybe change "unsized" terminology here too,
`assertPartitionsMatchConfiguredWriter`
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/sort/impl/TestIFile.java:
##########
@@ -821,6 +824,54 @@ public void testLz4CompressedDataIsLargerThanOriginal()
throws IOException {
comp.compress(buf, 0, 32);
}
+ /**
+ * A writer given an upper bound of its raw length writes the bytes of an
unsized writer, from a
+ * record to several codec blocks, and with values longer than the write
buffer.
+ */
+ @Test
+ public void testSizedWriterWritesUnsizedBytes() throws IOException {
Review Comment:
`testWriterSizedToDataWritesConfiguredBytes`, it would be inline with other
test class named `testOutputStreamSizedToData`
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1601,6 +1608,130 @@ private static String createRandomString(int size) {
return sb.toString();
}
+ /**
+ * A spill sizes each partition's stream to the partition without reading
the codec's buffer size
+ * per stream, and writes the bytes of an unsized writer: a final spill of
many partitions, spills
+ * of several buffers each, and a merge of many spills.
+ */
+ @Test
+ public void testSpillStreamsSizedToPartitions() throws IOException,
InterruptedException {
+ setupInit(true, ReportPartitionStats.DISABLED);
+ int numPartitions = 1009;
+ UnorderedPartitionedKVWriter kvWriter = createSnappyWriter(numPartitions,
16 << 20, -1, true);
+ write(kvWriter, 50_000);
+ ReadCountingConfiguration conf = (ReadCountingConfiguration) kvWriter.conf;
+ // The final spill runs on this thread; it borrows the pooled compressor
instead of creating one.
+ CodecPool.returnCompressor(CodecPool.getCompressor(kvWriter.codec));
+ int reads = conf.reads.get();
+ long allocated = getCurrentThreadAllocatedBytes();
+ kvWriter.close();
+ allocated = getCurrentThreadAllocatedBytes() - allocated;
+ assertEquals(1, kvWriter.numSpills.get());
+ assertEquals(reads, conf.reads.get());
+ // The configured buffers would take 320 KiB per partition: 256 KiB codec
+ 64 KiB write buffer.
+ assertTrue(allocated < numPartitions * 32L * 1024, "allocated " +
allocated);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+
+ numPartitions = 10;
+ kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.spillLimit > 1);
+ for (int i = 0; i < kvWriter.numSpills.get(); i++) {
+ assertPartitionsWrittenUnsized(kvWriter,
kvWriter.outputFileHandler.getSpillFileForWrite(i, 0),
+ kvWriter.outputFileHandler.getSpillIndexFileForWrite(i, 0),
numPartitions);
+ }
+
+ kvWriter = createSnappyWriter(numPartitions, 2048, -1, true);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.numSpills.get() > 100);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+ }
+
+ /** Spills of a writer without final merge take all its buffers. */
+ 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,
+ TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID_DEFAULT));
+ Configuration conf = new ReadCountingConfiguration(
+ createConfiguration(outputContext, IntWritable.class,
LongWritable.class, true, maxSingleBufferSizeBytes),
+ CommonConfigurationKeys.IO_COMPRESSION_CODEC_SNAPPY_BUFFERSIZE_KEY);
+ conf.set(TezRuntimeConfiguration.TEZ_RUNTIME_COMPRESS_CODEC,
SnappyCodec.class.getName());
+
conf.setBoolean(TezRuntimeConfiguration.TEZ_RUNTIME_ENABLE_FINAL_MERGE_IN_OUTPUT,
finalMerge);
+ if (!finalMerge) {
+
conf.setInt(TezRuntimeConfiguration.TEZ_RUNTIME_UNORDERED_PARTITIONED_KVWRITER_BUFFER_MERGE_PERCENT,
100);
+ }
+ return new UnorderedPartitionedKVWriterForTest(outputContext, conf,
numPartitions, availableMemory);
+ }
+
+ private static void write(UnorderedPartitionedKVWriter kvWriter, int
numRecords) throws IOException {
+ IntWritable key = new IntWritable();
+ LongWritable value = new LongWritable();
+ for (int i = 0; i < numRecords; i++) {
+ key.set(i);
+ value.set(i);
+ kvWriter.write(key, value);
+ }
+ }
+
+ private static long getCurrentThreadAllocatedBytes() {
+ return ((com.sun.management.ThreadMXBean)
ManagementFactory.getThreadMXBean())
+ .getCurrentThreadAllocatedBytes();
+ }
+
+ /** Each partition of the output holds the bytes an unsized writer writes
for its records. */
+ private static void
assertPartitionsWrittenUnsized(UnorderedPartitionedKVWriter kvWriter, Path
outputPath,
+ Path indexPath, int numPartitions) throws IOException {
+ byte[] output = new byte[(int) localFs.getFileStatus(outputPath).getLen()];
+ try (FSDataInputStream in = localFs.open(outputPath)) {
+ in.readFully(output);
+ }
+ TezSpillRecord spillRecord = new TezSpillRecord(indexPath, kvWriter.conf);
+ DataInputBuffer key = new DataInputBuffer();
+ DataInputBuffer value = new DataInputBuffer();
+ for (int i = 0; i < numPartitions; i++) {
+ TezIndexRecord indexRecord = spillRecord.getIndex(i);
+ if (indexRecord.getPartLength() == 0) {
+ continue;
+ }
+ int start = (int) indexRecord.getStartOffset();
+ 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();
Review Comment:
`sizeConfigured`
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1601,6 +1608,130 @@ private static String createRandomString(int size) {
return sb.toString();
}
+ /**
+ * A spill sizes each partition's stream to the partition without reading
the codec's buffer size
+ * per stream, and writes the bytes of an unsized writer: a final spill of
many partitions, spills
+ * of several buffers each, and a merge of many spills.
+ */
+ @Test
+ public void testSpillStreamsSizedToPartitions() throws IOException,
InterruptedException {
+ setupInit(true, ReportPartitionStats.DISABLED);
+ int numPartitions = 1009;
+ UnorderedPartitionedKVWriter kvWriter = createSnappyWriter(numPartitions,
16 << 20, -1, true);
+ write(kvWriter, 50_000);
+ ReadCountingConfiguration conf = (ReadCountingConfiguration) kvWriter.conf;
+ // The final spill runs on this thread; it borrows the pooled compressor
instead of creating one.
+ CodecPool.returnCompressor(CodecPool.getCompressor(kvWriter.codec));
+ int reads = conf.reads.get();
+ long allocated = getCurrentThreadAllocatedBytes();
+ kvWriter.close();
+ allocated = getCurrentThreadAllocatedBytes() - allocated;
+ assertEquals(1, kvWriter.numSpills.get());
+ assertEquals(reads, conf.reads.get());
+ // The configured buffers would take 320 KiB per partition: 256 KiB codec
+ 64 KiB write buffer.
+ assertTrue(allocated < numPartitions * 32L * 1024, "allocated " +
allocated);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+
+ numPartitions = 10;
+ kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.spillLimit > 1);
Review 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");
```
##########
tez-runtime-library/src/main/java/org/apache/tez/runtime/library/common/writers/UnorderedPartitionedKVWriter.java:
##########
@@ -119,7 +121,7 @@ public class UnorderedPartitionedKVWriter extends
BaseUnorderedPartitionedKVWrit
private final FileSystem rfs;
@VisibleForTesting
- final List<SpillInfo> spillInfoList = Collections.synchronizedList(new
ArrayList<SpillInfo>());
+ final Queue<SpillInfo> spillInfoList = new ConcurrentLinkedQueue<>();
Review 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
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/sort/impl/TestIFile.java:
##########
@@ -821,6 +824,54 @@ public void testLz4CompressedDataIsLargerThanOriginal()
throws IOException {
comp.compress(buf, 0, 32);
}
+ /**
+ * A writer given an upper bound of its raw length writes the bytes of an
unsized writer, from a
+ * record to several codec blocks, and with values longer than the write
buffer.
+ */
+ @Test
+ public void testSizedWriterWritesUnsizedBytes() throws IOException {
+ CompressionCodec snappy = ReflectionUtils.newInstance(SnappyCodec.class,
new Configuration(false));
+ int codecBufferSize = CodecUtils.getOutputBufferSize(snappy);
+ Random random = new Random(0);
+ // {records, value length bound}: one record, one block, several blocks,
values above the write buffer
+ for (int[] shape : new int[][] {{1, 50}, {100, 50}, {10_000, 50}, {5,
200_000}}) {
+ List<byte[][]> records = new ArrayList<>();
+ long keyValueBytes = 0;
+ for (int i = 0; i < shape[0]; i++) {
+ byte[] key = new byte[1 + random.nextInt(20)];
+ byte[] value = new byte[random.nextInt(shape[1])];
+ random.nextBytes(key);
+ random.nextBytes(value);
+ records.add(new byte[][] {key, value});
+ keyValueBytes += key.length + value.length;
+ }
+ long maxRawLength = Writer.maxRawLength(records.size(), keyValueBytes);
+
+ ByteArrayOutputStream unsized = new ByteArrayOutputStream();
+ ByteArrayOutputStream sized = new ByteArrayOutputStream();
Review Comment:
`unsized`/`sized` looks a bit odd, it's actually
`sizeConfigured`/`sizedToData`, I believe these can be renamed or code commented
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/utils/TestCodecUtils.java:
##########
@@ -203,6 +211,90 @@ public void testDefaultBufferSize() {
CodecUtils.getDefaultBufferSize(conf, new Lz4Codec()));
}
+ /**
+ * A stream sized to its data writes the bytes of the configured stream,
from one byte to several
+ * configured blocks of compressible and of incompressible data.
+ */
+ @ParameterizedTest
+ @ValueSource(classes = {SnappyCodec.class, Lz4Codec.class})
+ public void testOutputStreamSizedToData(Class<? extends CompressionCodec>
codecClass) throws IOException {
+ Configuration conf = new Configuration(false);
+ conf.setInt(CodecUtils.getBufferSizeProperty(codecClass.getName()), 64 *
1024);
+ CompressionCodec codec = ReflectionUtils.newInstance(codecClass, conf);
+ int bufferSize = CodecUtils.getOutputBufferSize(codec);
+ assertEquals(64 * 1024, bufferSize);
Review Comment:
maybe message, that can also contain the actual codec name, given this is
the coverage for `CodecUtils.getOutputBufferSize`
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1601,6 +1608,130 @@ private static String createRandomString(int size) {
return sb.toString();
}
+ /**
+ * A spill sizes each partition's stream to the partition without reading
the codec's buffer size
+ * per stream, and writes the bytes of an unsized writer: a final spill of
many partitions, spills
+ * of several buffers each, and a merge of many spills.
+ */
+ @Test
+ public void testSpillStreamsSizedToPartitions() throws IOException,
InterruptedException {
+ setupInit(true, ReportPartitionStats.DISABLED);
+ int numPartitions = 1009;
+ UnorderedPartitionedKVWriter kvWriter = createSnappyWriter(numPartitions,
16 << 20, -1, true);
+ write(kvWriter, 50_000);
+ ReadCountingConfiguration conf = (ReadCountingConfiguration) kvWriter.conf;
+ // The final spill runs on this thread; it borrows the pooled compressor
instead of creating one.
+ CodecPool.returnCompressor(CodecPool.getCompressor(kvWriter.codec));
+ int reads = conf.reads.get();
+ long allocated = getCurrentThreadAllocatedBytes();
+ kvWriter.close();
+ allocated = getCurrentThreadAllocatedBytes() - allocated;
+ assertEquals(1, kvWriter.numSpills.get());
+ assertEquals(reads, conf.reads.get());
+ // The configured buffers would take 320 KiB per partition: 256 KiB codec
+ 64 KiB write buffer.
+ assertTrue(allocated < numPartitions * 32L * 1024, "allocated " +
allocated);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+
+ numPartitions = 10;
+ kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.spillLimit > 1);
+ for (int i = 0; i < kvWriter.numSpills.get(); i++) {
+ assertPartitionsWrittenUnsized(kvWriter,
kvWriter.outputFileHandler.getSpillFileForWrite(i, 0),
+ kvWriter.outputFileHandler.getSpillIndexFileForWrite(i, 0),
numPartitions);
+ }
+
+ kvWriter = createSnappyWriter(numPartitions, 2048, -1, true);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.numSpills.get() > 100);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+ }
+
+ /** Spills of a writer without final merge take all its buffers. */
+ 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,
Review Comment:
remove `defaultConf` usage as suggested in another PR:
https://github.com/apache/tez/pull/550/changes#r4665629676
##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1601,6 +1608,130 @@ private static String createRandomString(int size) {
return sb.toString();
}
+ /**
+ * A spill sizes each partition's stream to the partition without reading
the codec's buffer size
+ * per stream, and writes the bytes of an unsized writer: a final spill of
many partitions, spills
+ * of several buffers each, and a merge of many spills.
+ */
+ @Test
+ public void testSpillStreamsSizedToPartitions() throws IOException,
InterruptedException {
+ setupInit(true, ReportPartitionStats.DISABLED);
+ int numPartitions = 1009;
+ UnorderedPartitionedKVWriter kvWriter = createSnappyWriter(numPartitions,
16 << 20, -1, true);
+ write(kvWriter, 50_000);
+ ReadCountingConfiguration conf = (ReadCountingConfiguration) kvWriter.conf;
+ // The final spill runs on this thread; it borrows the pooled compressor
instead of creating one.
+ CodecPool.returnCompressor(CodecPool.getCompressor(kvWriter.codec));
+ int reads = conf.reads.get();
+ long allocated = getCurrentThreadAllocatedBytes();
+ kvWriter.close();
+ allocated = getCurrentThreadAllocatedBytes() - allocated;
+ assertEquals(1, kvWriter.numSpills.get());
+ assertEquals(reads, conf.reads.get());
+ // The configured buffers would take 320 KiB per partition: 256 KiB codec
+ 64 KiB write buffer.
+ assertTrue(allocated < numPartitions * 32L * 1024, "allocated " +
allocated);
+ assertPartitionsWrittenUnsized(kvWriter, kvWriter.finalOutPath,
kvWriter.finalIndexPath, numPartitions);
+
+ numPartitions = 10;
+ kvWriter = createSnappyWriter(numPartitions, 10 << 10, 1 << 10, false);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.spillLimit > 1);
+ for (int i = 0; i < kvWriter.numSpills.get(); i++) {
+ assertPartitionsWrittenUnsized(kvWriter,
kvWriter.outputFileHandler.getSpillFileForWrite(i, 0),
+ kvWriter.outputFileHandler.getSpillIndexFileForWrite(i, 0),
numPartitions);
+ }
+
+ kvWriter = createSnappyWriter(numPartitions, 2048, -1, true);
+ write(kvWriter, 5000);
+ kvWriter.close();
+ assertTrue(kvWriter.numSpills.get() > 100);
Review 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());
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]