Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.concurrent.atomic.LongAdder;

import com.google.common.annotations.VisibleForTesting;
import org.apache.tez.runtime.library.api.IOInterruptedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,6 +39,7 @@ public class CelebornTezWriter {
private final int attemptNumber;
private final int numMappers;
private final int numPartitions;
private final LongAdder[] mapStatusLengths;

public CelebornTezWriter(
int shuffleId,
Expand All @@ -51,17 +53,37 @@ public CelebornTezWriter(
String lifecycleManagerHost,
int lifecycleManagerPort,
UserIdentifier userIdentifier) {
shuffleClient =
this(
shuffleId,
mapId,
attemptNumber,
taskAttemptId,
numMappers,
numPartitions,
conf,
ShuffleClient.get(
appUniqueId, lifecycleManagerHost, lifecycleManagerPort, conf, userIdentifier, null);
appUniqueId, lifecycleManagerHost, lifecycleManagerPort, conf, userIdentifier, null));
}

@VisibleForTesting
CelebornTezWriter(
int shuffleId,
int mapId,
int attemptNumber,
long taskAttemptId,
int numMappers,
int numPartitions,
CelebornConf conf,
ShuffleClient shuffleClient) {
this.shuffleClient = shuffleClient;
// TEZ_SHUFFLE_ID
this.shuffleId = shuffleId;
this.mapId = mapId;
this.attemptNumber = attemptNumber;
this.numMappers = numMappers;
this.numPartitions = numPartitions;

LongAdder[] mapStatusLengths = new LongAdder[numPartitions];
mapStatusLengths = new LongAdder[numPartitions];
for (int i = 0; i < numPartitions; i++) {
mapStatusLengths[i] = new LongAdder();
}
Expand All @@ -84,6 +106,28 @@ public CelebornTezWriter(
}
}

@VisibleForTesting
CelebornTezWriter(
int shuffleId,
int mapId,
int attemptNumber,
int numMappers,
int numPartitions,
ShuffleClient shuffleClient,
DataPusher dataPusher) {
this.shuffleId = shuffleId;
this.mapId = mapId;
this.attemptNumber = attemptNumber;
this.numMappers = numMappers;
this.numPartitions = numPartitions;
this.shuffleClient = shuffleClient;
this.dataPusher = dataPusher;
mapStatusLengths = new LongAdder[numPartitions];
for (int i = 0; i < numPartitions; i++) {
mapStatusLengths[i] = new LongAdder();
}
}

public void pushData(int partitionId, byte[] dataBuf, int size) throws IOException {
try {
dataPusher.addTask(partitionId, dataBuf, size);
Expand All @@ -104,12 +148,26 @@ public void mergeData(int partitionId, byte[] dataBuf, int size) throws IOExcept
size,
numMappers,
numPartitions);
mapStatusLengths[partitionId].add(bytesWritten);
}

public int getNumPartitions() {
return numPartitions;
}

public long[] getPartitionStats() {
// Return a stable snapshot because the underlying counters are updated by the push thread.
return snapshotPartitionStats(mapStatusLengths);
}

static long[] snapshotPartitionStats(LongAdder[] mapStatusLengths) {
long[] partitionStats = new long[mapStatusLengths.length];
for (int i = 0; i < mapStatusLengths.length; i++) {
partitionStats[i] = mapStatusLengths[i].sum();
}
return partitionStats;
}

public void close() throws IOException {
logger.info(
"Call mapper end shuffleId:{} mapId:{} attemptId:{} numMappers:{}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class CelebornUnorderedPartitionedKVWriter extends KeyValuesWriter {
final TezRuntimeConfiguration.ReportPartitionStats reportPartitionStats;

private CelebornSortBasedPusher pusher;
private final CelebornTezWriter celebornTezWriter;

public CelebornUnorderedPartitionedKVWriter(
OutputContext outputContext,
Expand All @@ -84,6 +85,7 @@ public CelebornUnorderedPartitionedKVWriter(
CelebornConf celebornConf) {
this.outputContext = outputContext;
this.conf = conf;
this.celebornTezWriter = celebornTezWriter;
try {
this.localFs = (RawLocalFileSystem) FileSystem.getLocal(conf).getRaw();
} catch (IOException e) {
Expand Down Expand Up @@ -175,7 +177,7 @@ public void close() throws IOException {
private void updateTezCountersAndNotify() {
numRecordsPerPartition = pusher.getRecordsPerPartition();
if (sizePerPartition != null) {
sizePerPartition = pusher.getBytesPerPartition();
sizePerPartition = celebornTezWriter.getPartitionStats();
}
outputContext.notifyProgress();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.hadoop.io.RawComparator;
Expand Down Expand Up @@ -53,7 +52,6 @@ public class CelebornSortBasedPusher<K, V> extends OutputStream {
private byte[] serializedKV;
private final int maxPushDataSize;
private Map<Integer, AtomicInteger> recordsPerPartition = new HashMap<>();
private Map<Integer, AtomicLong> bytesPerPartition = new HashMap<>();
private final boolean needSort;

public CelebornSortBasedPusher(
Expand Down Expand Up @@ -107,14 +105,10 @@ public void insert(K key, V value, int partition) {
int dataLen = insertRecordInternal(key, value, partition);
if (numOutputs == 1 && !needSort) {
recordsPerPartition.putIfAbsent(0, new AtomicInteger());
bytesPerPartition.putIfAbsent(0, new AtomicLong());
recordsPerPartition.get(0).incrementAndGet();
bytesPerPartition.get(0).incrementAndGet();
} else {
recordsPerPartition.computeIfAbsent(partition, p -> new AtomicInteger());
bytesPerPartition.computeIfAbsent(partition, p -> new AtomicLong());
recordsPerPartition.get(partition).incrementAndGet();
bytesPerPartition.get(partition).incrementAndGet();
}
if (logger.isDebugEnabled()) {
logger.debug(
Expand Down Expand Up @@ -321,16 +315,7 @@ public int[] getRecordsPerPartition() {
}

public long[] getBytesPerPartition() {
long[] values = new long[numOutputs];
for (int i = 0; i < numOutputs; i++) {
AtomicLong bytes = bytesPerPartition.get(i);
if (bytes != null) {
values[i] = bytes.get();
} else {
values[i] = 0;
}
}
return values;
return celebornTezWriter.getPartitionStats();
}

static class SerializedKV {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class CelebornSorter extends ExternalSorter {

private static final Logger LOG = LoggerFactory.getLogger(CelebornSorter.class);
private CelebornSortBasedPusher celebornSortBasedPusher;
private final CelebornTezWriter celebornTezWriter;

private int[] numRecordsPerPartition;

Expand All @@ -53,6 +54,7 @@ public CelebornSorter(
throws IOException {
super(outputContext, conf, numOutputs, initialMemoryAvailable);

this.celebornTezWriter = celebornTezWriter;
this.numRecordsPerPartition = new int[numOutputs];

final float spillper =
Expand Down Expand Up @@ -82,6 +84,12 @@ public void flush() throws IOException {
celebornSortBasedPusher.close();
}

@Override
public long[] getPartitionStats() {
// ExternalSorter uses null to indicate that partition statistics reporting is disabled.
return partitionStats == null ? null : celebornTezWriter.getPartitionStats();
}

@Override
public final List<Event> close() throws IOException {
return super.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* 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.celeborn.client;

import static org.junit.Assert.assertArrayEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.concurrent.atomic.LongAdder;

import org.junit.Test;
import org.mockito.InOrder;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add focused coverage for the two Tez consumers changed in this patch? This suite verifies the counters inside CelebornTezWriter, but it would still pass if CelebornSorter or CelebornUnorderedPartitionedKVWriter failed to expose the snapshot, or if disabled partition-stat reporting returned a non-null array. Exercising enabled and disabled TEZ_RUNTIME_REPORT_PARTITION_STATS through both paths would protect the user-visible behavior this PR is fixing.

@SparksFyz SparksFyz Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SteNicholas Thanks for the review. I added focused tests for both CelebornSorter and CelebornUnorderedPartitionedKVWriter, covering explicit PRECISE, the default MEMORY_OPTIMIZED, and disabled (NONE) partition-stat reporting. The tests verify that enabled reporting exposes the snapshot after CelebornTezWriter is closed, while disabled reporting returns null without querying the writer.


import org.apache.celeborn.client.write.DataPusher;
import org.apache.celeborn.common.CelebornConf;
import org.apache.celeborn.common.write.PushState;

public class CelebornTezWriterSuiteJ {

private static final int SHUFFLE_ID = 1;
private static final int MAP_ID = 2;
private static final int ATTEMPT_ID = 3;
private static final int NUM_MAPPERS = 4;
private static final int NUM_PARTITIONS = 3;

@Test
public void testSnapshotPartitionStats() {
LongAdder[] mapStatusLengths = new LongAdder[3];
for (int i = 0; i < mapStatusLengths.length; i++) {
mapStatusLengths[i] = new LongAdder();
}
mapStatusLengths[0].add(1016);
mapStatusLengths[1].add(2016);
mapStatusLengths[0].add(3016);

long[] partitionStats = CelebornTezWriter.snapshotPartitionStats(mapStatusLengths);
assertArrayEquals(new long[] {4032, 2016, 0}, partitionStats);

mapStatusLengths[2].add(4016);
assertArrayEquals(new long[] {4032, 2016, 0}, partitionStats);
assertArrayEquals(
new long[] {4032, 2016, 4016}, CelebornTezWriter.snapshotPartitionStats(mapStatusLengths));
}

@Test
public void testMergeDataUsesBytesWrittenByShuffleClient() throws Exception {
CelebornConf conf = createConf();
ShuffleClient shuffleClient = createShuffleClient(conf);
when(shuffleClient.mergeData(
anyInt(),
anyInt(),
anyInt(),
anyInt(),
any(byte[].class),
anyInt(),
anyInt(),
anyInt(),
anyInt()))
.thenAnswer(
invocation -> {
int partitionId = invocation.getArgument(3);
int length = invocation.getArgument(6);
return partitionId * 1000 + length;
});

CelebornTezWriter writer = createWriter(conf, shuffleClient);
writer.mergeData(0, new byte[10], 10);
writer.mergeData(1, new byte[20], 20);
writer.mergeData(0, new byte[30], 30);

assertArrayEquals(new long[] {40, 1020, 0}, writer.getPartitionStats());
writer.close();
}

@Test
public void testAsyncPushIsIncludedInPartitionStatsAfterClose() throws Exception {
CelebornConf conf = createConf();
ShuffleClient shuffleClient = createShuffleClient(conf);
when(shuffleClient.pushData(
anyInt(),
anyInt(),
anyInt(),
anyInt(),
any(byte[].class),
anyInt(),
anyInt(),
anyInt(),
anyInt()))
.thenAnswer(
invocation -> {
int partitionId = invocation.getArgument(3);
int length = invocation.getArgument(6);
return partitionId * 1000 + length;
});

CelebornTezWriter writer = createWriter(conf, shuffleClient);
writer.pushData(2, new byte[50], 50);
writer.close();

assertArrayEquals(new long[] {0, 0, 2050}, writer.getPartitionStats());
InOrder inOrder = inOrder(shuffleClient);
inOrder
.verify(shuffleClient)
.pushData(
eq(SHUFFLE_ID),
eq(MAP_ID),
eq(ATTEMPT_ID),
eq(2),
any(byte[].class),
eq(0),
eq(50),
eq(NUM_MAPPERS),
eq(NUM_PARTITIONS));
inOrder.verify(shuffleClient).pushMergedData(SHUFFLE_ID, MAP_ID, ATTEMPT_ID);
inOrder
.verify(shuffleClient)
.mapperEnd(SHUFFLE_ID, MAP_ID, ATTEMPT_ID, NUM_MAPPERS, NUM_PARTITIONS);
}

@Test
public void testCloseWaitsForDataPusherBeforeFinalizingMapper() throws Exception {
ShuffleClient shuffleClient = mock(ShuffleClient.class);
DataPusher dataPusher = mock(DataPusher.class);
CelebornTezWriter writer =
new CelebornTezWriter(
SHUFFLE_ID, MAP_ID, ATTEMPT_ID, NUM_MAPPERS, NUM_PARTITIONS, shuffleClient, dataPusher);

writer.close();

InOrder inOrder = inOrder(dataPusher, shuffleClient);
inOrder.verify(dataPusher).waitOnTermination();
inOrder.verify(shuffleClient).pushMergedData(SHUFFLE_ID, MAP_ID, ATTEMPT_ID);
inOrder
.verify(shuffleClient)
.mapperEnd(SHUFFLE_ID, MAP_ID, ATTEMPT_ID, NUM_MAPPERS, NUM_PARTITIONS);
}

private static CelebornConf createConf() {
CelebornConf conf = new CelebornConf();
conf.set(CelebornConf.CLIENT_PUSH_QUEUE_CAPACITY().key(), "2");
conf.set(CelebornConf.CLIENT_PUSH_BUFFER_MAX_SIZE().key(), "1024");
return conf;
}

private static ShuffleClient createShuffleClient(CelebornConf conf) throws Exception {
ShuffleClient shuffleClient = mock(ShuffleClient.class);
when(shuffleClient.getPushState(anyString())).thenReturn(new PushState(conf));
when(shuffleClient.getPartitionLocation(anyInt(), anyInt(), anyInt())).thenReturn(null);
doAnswer(invocation -> null)
.when(shuffleClient)
.computeBatchCRC(
anyInt(), anyInt(), anyInt(), anyInt(), any(byte[].class), anyInt(), anyInt());
return shuffleClient;
}

private static CelebornTezWriter createWriter(CelebornConf conf, ShuffleClient shuffleClient) {
return new CelebornTezWriter(
SHUFFLE_ID, MAP_ID, ATTEMPT_ID, 4L, NUM_MAPPERS, NUM_PARTITIONS, conf, shuffleClient);
}
}
Loading
Loading