From bf6d935ac80b4a3d9a2cf571f6688682c0899af9 Mon Sep 17 00:00:00 2001 From: huangxiaoping <1754789345@qq.com> Date: Thu, 19 Nov 2026 18:11:06 +0800 Subject: [PATCH] [CELEBORN-2567][MR] Handle records exceeding remaining sort buffer --- client-mr/mr/pom.xml | 5 + .../mapred/CelebornSortBasedPusher.java | 63 +++-- .../mapred/CelebornSortBasedPusherSuiteJ.java | 264 ++++++++++++++++++ 3 files changed, 307 insertions(+), 25 deletions(-) create mode 100644 client-mr/mr/src/test/java/org/apache/hadoop/mapred/CelebornSortBasedPusherSuiteJ.java diff --git a/client-mr/mr/pom.xml b/client-mr/mr/pom.xml index 06d72c2d66a..857ceff115c 100644 --- a/client-mr/mr/pom.xml +++ b/client-mr/mr/pom.xml @@ -59,6 +59,11 @@ javax.servlet javax.servlet-api + + org.mockito + mockito-core + test + diff --git a/client-mr/mr/src/main/java/org/apache/hadoop/mapred/CelebornSortBasedPusher.java b/client-mr/mr/src/main/java/org/apache/hadoop/mapred/CelebornSortBasedPusher.java index 25354bf62de..2b03007e019 100644 --- a/client-mr/mr/src/main/java/org/apache/hadoop/mapred/CelebornSortBasedPusher.java +++ b/client-mr/mr/src/main/java/org/apache/hadoop/mapred/CelebornSortBasedPusher.java @@ -50,6 +50,7 @@ public class CelebornSortBasedPusher extends OutputStream { private final Counters.Counter mapOutputRecordCounter; private final Map> partitionedKVs; private int writePos; + private int recordStart = -1; private byte[] serializedKV; private final int maxPushDataSize; @@ -256,26 +257,28 @@ private void sortKVs() { } private int insertRecordInternal(K key, V value, int partition) throws IOException { - int offset = writePos; - int keyLen; - int valLen; - kSer.serialize(key); - keyLen = writePos - offset; - vSer.serialize(value); - valLen = writePos - keyLen - offset; - List serializedKVs = - partitionedKVs.computeIfAbsent(partition, v -> new ArrayList<>()); - serializedKVs.add(new SerializedKV(offset, keyLen, valLen)); - if (logger.isDebugEnabled()) { - logger.debug( - "Pusher insert into buffer partition:{} offset:{} keyLen:{} valueLen:{} size:{}", - partition, - offset, - keyLen, - valLen, - partitionedKVs.size()); + recordStart = writePos; + try { + kSer.serialize(key); + int keyLen = writePos - recordStart; + vSer.serialize(value); + int valLen = writePos - keyLen - recordStart; + List serializedKVs = + partitionedKVs.computeIfAbsent(partition, v -> new ArrayList<>()); + serializedKVs.add(new SerializedKV(recordStart, keyLen, valLen)); + if (logger.isDebugEnabled()) { + logger.debug( + "Pusher insert into buffer partition:{} offset:{} keyLen:{} valueLen:{} size:{}", + partition, + recordStart, + keyLen, + valLen, + partitionedKVs.size()); + } + return keyLen + valLen; + } finally { + recordStart = -1; } - return keyLen + valLen; } public void checkException() throws IOException { @@ -286,13 +289,23 @@ public void checkException() throws IOException { @Override public void write(int b) throws IOException { - if (writePos < maxIOBufferSize) { - serializedKV[writePos] = (byte) b; - writePos++; - } else { - logger.warn("Sort push memory high, write pos {} max size {}", writePos, maxIOBufferSize); - throw new IOException("Sort pusher memory exhausted."); + if (writePos == maxIOBufferSize) { + if (recordStart <= 0 || partitionedKVs.isEmpty()) { + logger.warn("Sort push memory high, write pos {} max size {}", writePos, maxIOBufferSize); + throw new IOException("Sort pusher memory exhausted."); + } + // Only complete records have entries in partitionedKVs. Preserve the current partial + // record while they are sorted and sent, then continue serializing it at offset zero. + int partialLength = writePos - recordStart; + int partialOffset = recordStart; + sortKVs(); + sendKVAndUpdateWritePos(); + System.arraycopy(serializedKV, partialOffset, serializedKV, 0, partialLength); + writePos = partialLength; + recordStart = 0; } + serializedKV[writePos] = (byte) b; + writePos++; } @Override diff --git a/client-mr/mr/src/test/java/org/apache/hadoop/mapred/CelebornSortBasedPusherSuiteJ.java b/client-mr/mr/src/test/java/org/apache/hadoop/mapred/CelebornSortBasedPusherSuiteJ.java new file mode 100644 index 00000000000..51e947a25ea --- /dev/null +++ b/client-mr/mr/src/test/java/org/apache/hadoop/mapred/CelebornSortBasedPusherSuiteJ.java @@ -0,0 +1,264 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.mapred; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.hadoop.io.RawComparator; +import org.apache.hadoop.io.WritableComparator; +import org.apache.hadoop.io.WritableUtils; +import org.apache.hadoop.io.serializer.Serializer; +import org.junit.Test; + +import org.apache.celeborn.client.ShuffleClient; +import org.apache.celeborn.common.CelebornConf; + +public class CelebornSortBasedPusherSuiteJ { + private static class BytesSerializer implements Serializer { + private OutputStream out; + + @Override + public void open(OutputStream out) { + this.out = out; + } + + @Override + public void serialize(byte[] value) throws IOException { + out.write(value); + } + + @Override + public void close() {} + } + + private static class BytesComparator implements RawComparator { + @Override + public int compare(byte[] a, byte[] b) { + return compare(a, 0, a.length, b, 0, b.length); + } + + @Override + public int compare(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) { + return WritableComparator.compareBytes(a, aOffset, aLength, b, bOffset, bLength); + } + } + + private static class Record { + final int partition; + final byte[] key; + final byte[] value; + + Record(int partition, byte[] key, byte[] value) { + this.partition = partition; + this.key = key; + this.value = value; + } + } + + private final List pushed = new ArrayList<>(); + private final ShuffleClient client = mock(ShuffleClient.class); + private final Counters.Counter bytes = mock(Counters.Counter.class); + private final Counters.Counter records = mock(Counters.Counter.class); + + private CelebornSortBasedPusher newPusher() throws IOException { + when(client.pushData( + anyInt(), + anyInt(), + anyInt(), + anyInt(), + any(byte[].class), + anyInt(), + anyInt(), + anyInt(), + anyInt())) + .thenAnswer( + invocation -> { + int partition = invocation.getArgument(3); + byte[] data = invocation.getArgument(4); + DataInputStream in = + new DataInputStream(new ByteArrayInputStream(data, 4, data.length - 4)); + while (true) { + int keyLength = WritableUtils.readVInt(in); + int valueLength = WritableUtils.readVInt(in); + if (keyLength == -1 && valueLength == -1) { + break; + } + byte[] key = new byte[keyLength]; + byte[] value = new byte[valueLength]; + in.readFully(key); + in.readFully(value); + pushed.add(new Record(partition, key, value)); + } + assertEquals(0, in.available()); + return data.length; + }); + return new CelebornSortBasedPusher<>( + 1, + 2, + 0, + 0, + new BytesSerializer(), + new BytesSerializer(), + 10, + 9, + new BytesComparator(), + bytes, + records, + client, + new CelebornConf()); + } + + private void assertRecord(int index, int partition, byte[] key, byte[] value) { + Record record = pushed.get(index); + assertEquals(partition, record.partition); + assertArrayEquals(key, record.key); + assertArrayEquals(value, record.value); + } + + @Test + public void testBufferFillsDuringValueSerialization() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + byte[] firstKey = {3}; + byte[] firstValue = {1, 2, 3, 4, 5, 6}; + byte[] nextKey = {2}; + byte[] nextValue = {7, 8, 9, 10}; + pusher.insert(firstKey, firstValue, 0); + pusher.insert(nextKey, nextValue, 1); + pusher.checkException(); + pusher.flush(); + pusher.checkException(); + assertEquals(2, pushed.size()); + assertRecord(0, 0, firstKey, firstValue); + assertRecord(1, 1, nextKey, nextValue); + org.mockito.Mockito.verify(bytes).increment(7); + org.mockito.Mockito.verify(bytes).increment(5); + org.mockito.Mockito.verify(records, org.mockito.Mockito.times(2)).increment(1); + } + + @Test + public void testBufferFillsDuringKeySerialization() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + byte[] firstKey = {1}; + byte[] firstValue = {2, 3, 4, 5, 6, 7}; + byte[] nextKey = {8, 9, 10, 11}; + byte[] nextValue = {12}; + pusher.insert(firstKey, firstValue, 0); + pusher.insert(nextKey, nextValue, 1); + pusher.checkException(); + pusher.flush(); + pusher.checkException(); + assertEquals(2, pushed.size()); + assertRecord(0, 0, firstKey, firstValue); + assertRecord(1, 1, nextKey, nextValue); + } + + @Test + public void testCompleteRecordsRemainSortedWhenBufferFills() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + pusher.insert(new byte[] {3}, new byte[] {30, 31}, 0); + pusher.insert(new byte[] {1}, new byte[] {10, 11, 12}, 0); + pusher.insert(new byte[] {2, 2, 2, 2}, new byte[] {20}, 0); + pusher.checkException(); + pusher.flush(); + pusher.checkException(); + assertEquals(3, pushed.size()); + assertRecord(0, 0, new byte[] {1}, new byte[] {10, 11, 12}); + assertRecord(1, 0, new byte[] {3}, new byte[] {30, 31}); + assertRecord(2, 0, new byte[] {2, 2, 2, 2}, new byte[] {20}); + } + + @Test + public void testRecordExactlyFillsBuffer() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + pusher.insert(new byte[] {1}, new byte[9], 0); + pusher.checkException(); + pusher.insert(new byte[] {2}, new byte[] {3}, 1); + pusher.checkException(); + pusher.flush(); + pusher.checkException(); + assertEquals(2, pushed.size()); + assertRecord(0, 0, new byte[] {1}, new byte[9]); + assertRecord(1, 1, new byte[] {2}, new byte[] {3}); + } + + @Test + public void testRecordLargerThanBufferFails() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + pusher.insert(new byte[] {1}, new byte[10], 0); + try { + pusher.checkException(); + fail("An oversized record should fail"); + } catch (IOException e) { + assertTrue(e.getCause().getMessage().contains("Sort pusher memory exhausted")); + } + assertTrue(pushed.isEmpty()); + } + + @Test + public void testOversizedRecordAfterCompleteRecord() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + pusher.insert(new byte[] {1}, new byte[] {2, 3, 4, 5, 6, 7}, 0); + pusher.insert(new byte[] {8}, new byte[10], 1); + try { + pusher.checkException(); + fail("An oversized record should fail"); + } catch (IOException e) { + assertTrue(e.getCause().getMessage().contains("Sort pusher memory exhausted")); + } + assertEquals(1, pushed.size()); + assertRecord(0, 0, new byte[] {1}, new byte[] {2, 3, 4, 5, 6, 7}); + } + + @Test + public void testPushFailureDuringSerializationIsReported() throws Exception { + CelebornSortBasedPusher pusher = newPusher(); + org.mockito.Mockito.doThrow(new IOException("push failed")) + .when(client) + .pushData( + anyInt(), + anyInt(), + anyInt(), + anyInt(), + any(byte[].class), + anyInt(), + anyInt(), + anyInt(), + anyInt()); + pusher.insert(new byte[] {1}, new byte[] {2, 3, 4, 5, 6, 7}, 0); + pusher.insert(new byte[] {8}, new byte[] {9, 10, 11, 12}, 1); + try { + pusher.checkException(); + fail("A push failure should be reported"); + } catch (IOException e) { + assertEquals("push failed", e.getCause().getMessage()); + } + } +}