reswqa commented on code in PR #22833:
URL: https://github.com/apache/flink/pull/22833#discussion_r1244602826


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/BufferAccumulator.java:
##########
@@ -44,9 +44,21 @@ public interface BufferAccumulator extends AutoCloseable {
     /**
      * Receives the records from tiered store producer, these records will be 
accumulated and
      * transformed into finished buffers.
+     *
+     * <p>Note that when isBroadcast is true, for a broadcast-only partition, 
the subpartitionId
+     * value will always be 0. Conversely, for a non-broadcast-only partition 
(such as an all-to-all

Review Comment:
   > for a non-broadcast-only partition (such as an all-to-all partition).
   
   I'm a bit confused: Isn't  broadcast only partition is also a `all-to-all` 
partition?
   



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulatorTest.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferPool;
+import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.TieredStorageTestUtils.generateRandomData;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link SortBufferAccumulator}. */
+class SortBufferAccumulatorTest {
+
+    private static final int NUM_TOTAL_BUFFERS = 1000;
+
+    private static final int BUFFER_SIZE_BYTES = 1024;
+
+    private static final float NUM_BUFFERS_TRIGGER_FLUSH_RATIO = 0.6f;
+
+    private NetworkBufferPool globalPool;
+
+    @BeforeEach
+    void before() {
+        globalPool = new NetworkBufferPool(NUM_TOTAL_BUFFERS, 
BUFFER_SIZE_BYTES);
+    }
+
+    @AfterEach
+    void after() {
+        globalPool.destroy();
+    }
+
+    @Test
+    void testAccumulateRecordsAndGenerateBuffers() throws IOException {
+        int numBuffers = 10;
+        int numRecords = 1000;
+        int indexEntrySize = 16;
+        TieredStorageSubpartitionId subpartitionId = new 
TieredStorageSubpartitionId(0);
+        Random random = new Random(1234);
+        TieredStorageMemoryManager tieredStorageMemoryManager =
+                createStorageMemoryManager(numBuffers);
+
+        int numExpectBytes = 0;
+        int numExpectBuffers = 0;
+        AtomicInteger numReceivedFinishedBuffer = new AtomicInteger(0);
+
+        try (SortBufferAccumulator bufferAccumulator =
+                new SortBufferAccumulator(1, 2, BUFFER_SIZE_BYTES, 
tieredStorageMemoryManager)) {
+            bufferAccumulator.setup(
+                    ((subpartition, buffers) ->
+                            buffers.forEach(
+                                    buffer -> {
+                                        
numReceivedFinishedBuffer.incrementAndGet();
+                                        buffer.recycleBuffer();
+                                    })));
+            int currentBufferWriteBytes = 0;
+            boolean isBroadcastForPreviousRecord = false;
+            for (int i = 0; i < numRecords; i++) {
+                int numBytes = random.nextInt(BUFFER_SIZE_BYTES) + 1;
+                ByteBuffer record = generateRandomData(numBytes, random);
+                boolean isBroadcast = random.nextBoolean();
+                bufferAccumulator.receive(
+                        record, subpartitionId, Buffer.DataType.DATA_BUFFER, 
isBroadcast);
+                if (numExpectBytes + numBytes + indexEntrySize > 
BUFFER_SIZE_BYTES
+                        || i > 0 && isBroadcastForPreviousRecord != 
isBroadcast) {
+                    numExpectBuffers++;
+                    numExpectBytes = 0;
+                }
+
+                isBroadcastForPreviousRecord = isBroadcast;
+                numExpectBytes += numBytes + indexEntrySize;
+                currentBufferWriteBytes += numBytes + indexEntrySize;

Review Comment:
   What is the purpose of this `currentBufferWriteBytes `?



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulatorTest.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferPool;
+import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.TieredStorageTestUtils.generateRandomData;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link SortBufferAccumulator}. */
+class SortBufferAccumulatorTest {
+
+    private static final int NUM_TOTAL_BUFFERS = 1000;
+
+    private static final int BUFFER_SIZE_BYTES = 1024;
+
+    private static final float NUM_BUFFERS_TRIGGER_FLUSH_RATIO = 0.6f;
+
+    private NetworkBufferPool globalPool;
+
+    @BeforeEach
+    void before() {
+        globalPool = new NetworkBufferPool(NUM_TOTAL_BUFFERS, 
BUFFER_SIZE_BYTES);
+    }
+
+    @AfterEach
+    void after() {
+        globalPool.destroy();
+    }
+
+    @Test
+    void testAccumulateRecordsAndGenerateBuffers() throws IOException {
+        int numBuffers = 10;
+        int numRecords = 1000;
+        int indexEntrySize = 16;
+        TieredStorageSubpartitionId subpartitionId = new 
TieredStorageSubpartitionId(0);
+        Random random = new Random(1234);
+        TieredStorageMemoryManager tieredStorageMemoryManager =
+                createStorageMemoryManager(numBuffers);
+
+        int numExpectBytes = 0;
+        int numExpectBuffers = 0;
+        AtomicInteger numReceivedFinishedBuffer = new AtomicInteger(0);
+
+        try (SortBufferAccumulator bufferAccumulator =
+                new SortBufferAccumulator(1, 2, BUFFER_SIZE_BYTES, 
tieredStorageMemoryManager)) {
+            bufferAccumulator.setup(
+                    ((subpartition, buffers) ->
+                            buffers.forEach(
+                                    buffer -> {
+                                        
numReceivedFinishedBuffer.incrementAndGet();
+                                        buffer.recycleBuffer();
+                                    })));
+            int currentBufferWriteBytes = 0;
+            boolean isBroadcastForPreviousRecord = false;
+            for (int i = 0; i < numRecords; i++) {
+                int numBytes = random.nextInt(BUFFER_SIZE_BYTES) + 1;
+                ByteBuffer record = generateRandomData(numBytes, random);
+                boolean isBroadcast = random.nextBoolean();
+                bufferAccumulator.receive(
+                        record, subpartitionId, Buffer.DataType.DATA_BUFFER, 
isBroadcast);
+                if (numExpectBytes + numBytes + indexEntrySize > 
BUFFER_SIZE_BYTES
+                        || i > 0 && isBroadcastForPreviousRecord != 
isBroadcast) {
+                    numExpectBuffers++;
+                    numExpectBytes = 0;
+                }
+
+                isBroadcastForPreviousRecord = isBroadcast;
+                numExpectBytes += numBytes + indexEntrySize;
+                currentBufferWriteBytes += numBytes + indexEntrySize;
+                if (currentBufferWriteBytes >= BUFFER_SIZE_BYTES) {
+                    currentBufferWriteBytes = currentBufferWriteBytes % 
BUFFER_SIZE_BYTES;
+                }
+            }
+        }
+
+        assertThat(numExpectBytes < BUFFER_SIZE_BYTES).isTrue();
+        assertThat(numReceivedFinishedBuffer.get())
+                .isEqualTo(
+                        numExpectBuffers
+                                + numExpectBytes / BUFFER_SIZE_BYTES
+                                + (numExpectBytes % BUFFER_SIZE_BYTES == 0 ? 0 
: 1));
+    }
+
+    @Test
+    void testNoBuffersForSort() throws IOException {
+        int numBuffers = 10;
+        int bufferSize = 1024;
+        Random random = new Random(1111);
+        TieredStorageSubpartitionId subpartitionId = new 
TieredStorageSubpartitionId(0);
+        TieredStorageMemoryManager memoryManager = 
createStorageMemoryManager(numBuffers);
+
+        try (SortBufferAccumulator sortBufferAccumulator =
+                new SortBufferAccumulator(1, 1, bufferSize, memoryManager)) {
+            sortBufferAccumulator.setup((subpartitionIndex, buffers) -> {});
+            assertThatThrownBy(
+                            () -> {

Review Comment:
   Can be replaced with expression lambda.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulator.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferBuilder;
+import org.apache.flink.runtime.io.network.buffer.BufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.apache.commons.lang3.tuple.Pair;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.function.BiConsumer;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * The sort-based implementation of the {@link BufferAccumulator}. The {@link 
BufferAccumulator}
+ * receives the records from {@link TieredStorageProducerClient} and the 
records will accumulate and
+ * transform to finished buffers. The accumulated buffers will be transferred 
to the corresponding
+ * tier dynamically.
+ *
+ * <p>The {@link SortBufferAccumulator} can help use less buffers to 
accumulate data, which
+ * decouples the buffer usage with the number of parallelism. The number of 
buffers used by the
+ * {@link SortBufferAccumulator} will be numBuffers at most. Once the {@link 
SortBuffer} is full, or
+ * switching from broadcast to non-broadcast(or vice versa), the buffer in the 
sort buffer will be
+ * flushed to the tiers.
+ *
+ * <p>Note that this class need not be thread-safe, because it should only be 
accessed from the main
+ * thread.
+ */
+public class SortBufferAccumulator implements BufferAccumulator {
+
+    /** The number of the subpartitions. */
+    private final int numSubpartitions;
+
+    /** The total number of the buffers used by the {@link 
SortBufferAccumulator}. */
+    private final int numBuffers;
+
+    /** The byte size of one single buffer. */
+    private final int bufferSizeBytes;
+
+    /** The empty buffers without storing data. */
+    private final LinkedList<MemorySegment> freeSegments = new LinkedList<>();
+
+    /** The memory manager of the tiered storage. */
+    private final TieredStorageMemoryManager storeMemoryManager;

Review Comment:
   ```suggestion
       private final TieredStorageMemoryManager memoryManager;
   ```



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulatorTest.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferPool;
+import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.TieredStorageTestUtils.generateRandomData;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link SortBufferAccumulator}. */
+class SortBufferAccumulatorTest {
+
+    private static final int NUM_TOTAL_BUFFERS = 1000;
+
+    private static final int BUFFER_SIZE_BYTES = 1024;
+
+    private static final float NUM_BUFFERS_TRIGGER_FLUSH_RATIO = 0.6f;
+
+    private NetworkBufferPool globalPool;
+
+    @BeforeEach
+    void before() {
+        globalPool = new NetworkBufferPool(NUM_TOTAL_BUFFERS, 
BUFFER_SIZE_BYTES);
+    }
+
+    @AfterEach
+    void after() {
+        globalPool.destroy();
+    }
+
+    @Test
+    void testAccumulateRecordsAndGenerateBuffers() throws IOException {
+        int numBuffers = 10;
+        int numRecords = 1000;
+        int indexEntrySize = 16;
+        TieredStorageSubpartitionId subpartitionId = new 
TieredStorageSubpartitionId(0);
+        Random random = new Random(1234);
+        TieredStorageMemoryManager tieredStorageMemoryManager =
+                createStorageMemoryManager(numBuffers);
+
+        int numExpectBytes = 0;
+        int numExpectBuffers = 0;
+        AtomicInteger numReceivedFinishedBuffer = new AtomicInteger(0);
+
+        try (SortBufferAccumulator bufferAccumulator =
+                new SortBufferAccumulator(1, 2, BUFFER_SIZE_BYTES, 
tieredStorageMemoryManager)) {

Review Comment:
   This seems to potentially rely on an assumption: there is only one buffer 
for sort, and when it is full, it will flush. It is best to add some comments 
to explain, otherwise the following code is not very intuitive.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBuffer.java:
##########
@@ -0,0 +1,455 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+
+import org.apache.commons.lang3.tuple.Pair;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * The {@link SortBuffer} is used to accumulate the records into {@link 
Buffer}s. The {@link
+ * SortBuffer} allows for writing data in arbitrary subpartition orders but 
supports reading of data
+ * in the order grouped by subpartitions. Note that the {@link SortBuffer} 
only supports reading
+ * after the write process finished, and can not support reading while writing.
+ */
+public class SortBuffer {

Review Comment:
   There are too many codes that duplicated with `SortBasedDataBuffer`, I 
wonder can we reuse it to some extent?



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulator.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferBuilder;
+import org.apache.flink.runtime.io.network.buffer.BufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.apache.commons.lang3.tuple.Pair;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.function.BiConsumer;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * The sort-based implementation of the {@link BufferAccumulator}. The {@link 
BufferAccumulator}
+ * receives the records from {@link TieredStorageProducerClient} and the 
records will accumulate and
+ * transform to finished buffers. The accumulated buffers will be transferred 
to the corresponding
+ * tier dynamically.
+ *
+ * <p>The {@link SortBufferAccumulator} can help use less buffers to 
accumulate data, which
+ * decouples the buffer usage with the number of parallelism. The number of 
buffers used by the
+ * {@link SortBufferAccumulator} will be numBuffers at most. Once the {@link 
SortBuffer} is full, or
+ * switching from broadcast to non-broadcast(or vice versa), the buffer in the 
sort buffer will be
+ * flushed to the tiers.
+ *
+ * <p>Note that this class need not be thread-safe, because it should only be 
accessed from the main
+ * thread.
+ */
+public class SortBufferAccumulator implements BufferAccumulator {
+
+    /** The number of the subpartitions. */
+    private final int numSubpartitions;
+
+    /** The total number of the buffers used by the {@link 
SortBufferAccumulator}. */
+    private final int numBuffers;
+
+    /** The byte size of one single buffer. */
+    private final int bufferSizeBytes;
+
+    /** The empty buffers without storing data. */
+    private final LinkedList<MemorySegment> freeSegments = new LinkedList<>();
+
+    /** The memory manager of the tiered storage. */
+    private final TieredStorageMemoryManager storeMemoryManager;
+
+    /** The number of buffers for sorting used in the {@link SortBuffer}. */
+    private int numBuffersForSort;

Review Comment:
   Can this field be a local variable?



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulatorTest.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferPool;
+import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.TieredStorageTestUtils.generateRandomData;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link SortBufferAccumulator}. */
+class SortBufferAccumulatorTest {
+
+    private static final int NUM_TOTAL_BUFFERS = 1000;
+
+    private static final int BUFFER_SIZE_BYTES = 1024;
+
+    private static final float NUM_BUFFERS_TRIGGER_FLUSH_RATIO = 0.6f;
+
+    private NetworkBufferPool globalPool;
+
+    @BeforeEach
+    void before() {
+        globalPool = new NetworkBufferPool(NUM_TOTAL_BUFFERS, 
BUFFER_SIZE_BYTES);
+    }
+
+    @AfterEach
+    void after() {
+        globalPool.destroy();
+    }
+
+    @Test
+    void testAccumulateRecordsAndGenerateBuffers() throws IOException {

Review Comment:
   IIUC, This test was not covered the situation of largeRecord.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulatorTest.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.storage;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferPool;
+import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.TieredStorageTestUtils.generateRandomData;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link SortBufferAccumulator}. */
+class SortBufferAccumulatorTest {
+
+    private static final int NUM_TOTAL_BUFFERS = 1000;
+
+    private static final int BUFFER_SIZE_BYTES = 1024;
+
+    private static final float NUM_BUFFERS_TRIGGER_FLUSH_RATIO = 0.6f;
+
+    private NetworkBufferPool globalPool;
+
+    @BeforeEach
+    void before() {
+        globalPool = new NetworkBufferPool(NUM_TOTAL_BUFFERS, 
BUFFER_SIZE_BYTES);
+    }
+
+    @AfterEach
+    void after() {
+        globalPool.destroy();
+    }
+
+    @Test
+    void testAccumulateRecordsAndGenerateBuffers() throws IOException {
+        int numBuffers = 10;
+        int numRecords = 1000;
+        int indexEntrySize = 16;
+        TieredStorageSubpartitionId subpartitionId = new 
TieredStorageSubpartitionId(0);
+        Random random = new Random(1234);
+        TieredStorageMemoryManager tieredStorageMemoryManager =
+                createStorageMemoryManager(numBuffers);
+
+        int numExpectBytes = 0;
+        int numExpectBuffers = 0;
+        AtomicInteger numReceivedFinishedBuffer = new AtomicInteger(0);
+
+        try (SortBufferAccumulator bufferAccumulator =
+                new SortBufferAccumulator(1, 2, BUFFER_SIZE_BYTES, 
tieredStorageMemoryManager)) {
+            bufferAccumulator.setup(
+                    ((subpartition, buffers) ->
+                            buffers.forEach(
+                                    buffer -> {
+                                        
numReceivedFinishedBuffer.incrementAndGet();
+                                        buffer.recycleBuffer();
+                                    })));
+            int currentBufferWriteBytes = 0;
+            boolean isBroadcastForPreviousRecord = false;
+            for (int i = 0; i < numRecords; i++) {
+                int numBytes = random.nextInt(BUFFER_SIZE_BYTES) + 1;
+                ByteBuffer record = generateRandomData(numBytes, random);
+                boolean isBroadcast = random.nextBoolean();
+                bufferAccumulator.receive(
+                        record, subpartitionId, Buffer.DataType.DATA_BUFFER, 
isBroadcast);
+                if (numExpectBytes + numBytes + indexEntrySize > 
BUFFER_SIZE_BYTES
+                        || i > 0 && isBroadcastForPreviousRecord != 
isBroadcast) {
+                    numExpectBuffers++;
+                    numExpectBytes = 0;

Review Comment:
   This name is a bit strange, it feels like all the bytes expected, but in 
reality, it's not.



-- 
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]

Reply via email to