You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "TanYuxin-tyx (via GitHub)" <gi...@apache.org> on 2023/06/07 08:42:06 UTC

[GitHub] [flink] TanYuxin-tyx opened a new pull request, #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

TanYuxin-tyx opened a new pull request, #22728:
URL: https://github.com/apache/flink/pull/22728

   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   *Implement the memory tier producer for the tiered storage.*
   
   
   ## Brief change log
     - *Implement the memory tier producer for the tiered storage.*
   
   
   ## Verifying this change
   
   This change added tests.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (no)
     - If yes, how is the feature documented? (not documented)
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227898774


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {
+        NettyPayload segmentNettyPayload = NettyPayload.newSegment(segmentId);
+        addFinishedBuffer(segmentNettyPayload);
+    }
+
+    void release() {
+        if (nettyConnectionWriter != null) {

Review Comment:
   Made this `volatile`.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] reswqa commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "reswqa (via GitHub)" <gi...@apache.org>.
reswqa commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227924653


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgentTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierSubpartitionProducerAgent}. */
+class MemoryTierSubpartitionProducerAgentTest {
+
+    @Test
+    void testConnectionEstablished() {
+        MemoryTierSubpartitionProducerAgent subpartitionProducerAgent =
+                createSubpartitionProducerAgent();
+
+        assertThatThrownBy(
+                        () ->
+                                subpartitionProducerAgent.addFinishedBuffer(
+                                        BufferBuilderTestUtils.buildSomeBuffer()))
+                .isInstanceOf(NullPointerException.class);
+        subpartitionProducerAgent.connectionEstablished(
+                new TestingNettyConnectionWriter.Builder().build());
+        subpartitionProducerAgent.addFinishedBuffer(BufferBuilderTestUtils.buildSomeBuffer());

Review Comment:
   Maybe assert this line of code no error.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgentTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierSubpartitionProducerAgent}. */
+class MemoryTierSubpartitionProducerAgentTest {
+
+    @Test
+    void testConnectionEstablished() {
+        MemoryTierSubpartitionProducerAgent subpartitionProducerAgent =
+                createSubpartitionProducerAgent();
+
+        assertThatThrownBy(
+                        () ->
+                                subpartitionProducerAgent.addFinishedBuffer(
+                                        BufferBuilderTestUtils.buildSomeBuffer()))
+                .isInstanceOf(NullPointerException.class);
+        subpartitionProducerAgent.connectionEstablished(
+                new TestingNettyConnectionWriter.Builder().build());
+        subpartitionProducerAgent.addFinishedBuffer(BufferBuilderTestUtils.buildSomeBuffer());
+    }
+
+    @Test
+    void testAddFinishedBuffer() {
+        MemoryTierSubpartitionProducerAgent subpartitionProducerAgent =
+                createSubpartitionProducerAgent();
+
+        AtomicReference<NettyPayload> received = new AtomicReference<>();
+        TestingNettyConnectionWriter connectionWriter =
+                new TestingNettyConnectionWriter.Builder()
+                        .setWriteBufferFunction(
+                                buffer -> {
+                                    received.set(buffer);
+                                    return null;
+                                })
+                        .build();
+        subpartitionProducerAgent.connectionEstablished(connectionWriter);
+
+        Buffer sentBuffer = BufferBuilderTestUtils.buildSomeBuffer();
+        subpartitionProducerAgent.addFinishedBuffer(sentBuffer);
+        Optional<Buffer> receivedBuffer = received.get().getBuffer();
+
+        assertThat(receivedBuffer.isPresent()).isTrue();
+        assertThat(receivedBuffer.get()).isEqualTo(sentBuffer);

Review Comment:
   `AssertJ` has its own assertion for `Optional`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] reswqa merged pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "reswqa (via GitHub)" <gi...@apache.org>.
reswqa merged PR #22728:
URL: https://github.com/apache/flink/pull/22728


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on PR #22728:
URL: https://github.com/apache/flink/pull/22728#issuecomment-1586756251

   @flinkbot run azure


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222624491


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.local.memory;

Review Comment:
   Removed the `local` dir.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227899184


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcast) {
+        return createMemoryTierProducerAgent(isBroadcast, new TieredStorageResourceRegistry());
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(
+            boolean isBroadcast, TieredStorageResourceRegistry resourceRegistry) {
+        TestingTieredStorageMemoryManager memoryManager =
+                new TestingTieredStorageMemoryManager.Builder()
+                        .setGetMaxNonReclaimableBuffersFunction(ignore -> Integer.MAX_VALUE)
+                        .build();
+        TestingTieredStorageNettyService nettyService =
+                new TestingTieredStorageNettyService.Builder().build();
+        TestingNettyServiceProducer nettyServiceProducer =
+                new TestingNettyServiceProducer.Builder().build();
+        nettyService.registerProducer(PARTITION_ID, nettyServiceProducer);
+
+        return new MemoryTierProducerAgent(
+                PARTITION_ID,
+                NUM_SUBPARTITIONS,
+                BUFFER_SIZE,
+                SEGMENT_SIZE_BYTES,
+                isBroadcast,

Review Comment:
   Fixed.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcast) {

Review Comment:
   Fixed.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227899936


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();

Review Comment:
   Fixed this by adding a new argument to `createMemoryTierProducerAgent`.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcast) {

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227918036


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/NoopMasterAgent.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.tier;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+
+/** The noop implementation for {@link TierMasterAgent}. */

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222628468


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierMasterAgent.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+
+/** The memory tier implementation for {@link TierMasterAgent}. */
+public class MemoryTierMasterAgent implements TierMasterAgent {
+    @Override
+    public void addPartition(TieredStoragePartitionId partitionId) {
+        // Nothing to do.

Review Comment:
   Fixed.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierMasterAgent.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+
+/** The memory tier implementation for {@link TierMasterAgent}. */
+public class MemoryTierMasterAgent implements TierMasterAgent {

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226178941


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.tier.memory;
+
+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.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    private NettyConnectionWriter nettyConnectionWriter;

Review Comment:
   Fixed. Added `@Nullable` and the comments.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] reswqa commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "reswqa (via GitHub)" <gi...@apache.org>.
reswqa commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227508536


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/netty/TestingNettyConnectionWriter.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.netty;
+
+import javax.annotation.Nullable;
+
+import java.util.function.Supplier;
+
+/** Test implementation for {@link NettyConnectionWriter}. */
+public class TestingNettyConnectionWriter implements NettyConnectionWriter {
+
+    private final Runnable writeBufferFunction;
+
+    private final Supplier<NettyConnectionId> nettyConnectionIdSupplier;
+
+    private final Supplier<Integer> numQueuedBuffersSupplier;
+
+    private final Runnable closeFunction;
+
+    private TestingNettyConnectionWriter(
+            Runnable writeBufferFunction,
+            Supplier<NettyConnectionId> nettyConnectionIdSupplier,
+            Supplier<Integer> numQueuedBuffersSupplier,
+            Runnable closeFunction) {
+        this.writeBufferFunction = writeBufferFunction;
+        this.nettyConnectionIdSupplier = nettyConnectionIdSupplier;
+        this.numQueuedBuffersSupplier = numQueuedBuffersSupplier;
+        this.closeFunction = closeFunction;
+    }
+
+    @Override
+    public void writeBuffer(NettyPayload nettyPayload) {
+        writeBufferFunction.run();

Review Comment:
   Why not use a `Function` instead use a `Runnable` here.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/NoopMasterAgent.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.tier;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+
+/** The noop implementation for {@link TierMasterAgent}. */
+public class NoopMasterAgent implements TierMasterAgent {

Review Comment:
   ```suggestion
   public class NoOpMasterAgent implements TierMasterAgent {
   ```



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    /**
+     * Record the writ bytes to each subpartition. When starting a new segment, the value will be
+     * reset to 0.
+     */
+    private final int[] currentSubpartitionWriteBuffers;
+
+    /**
+     * Whether a subpartition's netty connection has been established. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyConnectionEstablished;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSizeBytes,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                segmentSizeBytes >= bufferSizeBytes, "One segment contains at least one buffer.");

Review Comment:
   ```suggestion
                   segmentSizeBytes >= bufferSizeBytes, "One segment should contains at least one buffer.");
   ```



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {
+        NettyPayload segmentNettyPayload = NettyPayload.newSegment(segmentId);
+        addFinishedBuffer(segmentNettyPayload);
+    }
+
+    void release() {
+        if (nettyConnectionWriter != null) {
+            nettyConnectionWriter.close(null);
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private void addFinishedBuffer(NettyPayload nettyPayload) {
+        finishedBufferIndex++;
+        checkNotNull(nettyConnectionWriter).writeBuffer(nettyPayload);
+        if (nettyConnectionWriter.numQueuedBuffers() <= 1
+                && nettyService.getClass() == TieredStorageNettyServiceImpl.class) {
+            ((TieredStorageNettyServiceImpl) nettyService)

Review Comment:
   I wonder why `notifyResultSubpartitionViewSendBuffer` is not a interface method. If method about `subpartitionView` is not belong to the protocol of `TieredStorageNettyService`, we can consider let `TieredStorageNettyServiceImpl` implementing another interface at the same time.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {

Review Comment:
   I'd prefer rename this to `updateSegmentId` or `increaseSegmentId`.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcast) {

Review Comment:
   ```suggestion
       private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcastOnly) {
   ```



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();

Review Comment:
   ```suggestion
           assertThat(isClosed).isTrue();
   ```



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();

Review Comment:
   This strongly rely on `BUFFER_SIZE = SEGMENT_SIZE_BYTES = 1024`, this will make this test case a bit obscure. I suggest setting the default `SEGMENT_SIZE_BYTES` in the class to be greater than `BUFFER_SIZE`(e.g. `BUFFER_SIZE * 2`) and introducing `createMemoryTierProducerAgent (boolean isBroadcast, int segmentSizeBytes)`. As for this test, we can call `createMemoryTierProducerAgent(false, BUFFER_SIZE)`



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/NoopMasterAgent.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.tier;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+
+/** The noop implementation for {@link TierMasterAgent}. */

Review Comment:
   ```suggestion
   /** The empty implementation for {@link TierMasterAgent}. */
   ```



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    /**
+     * Record the writ bytes to each subpartition. When starting a new segment, the value will be
+     * reset to 0.
+     */
+    private final int[] currentSubpartitionWriteBuffers;
+
+    /**
+     * Whether a subpartition's netty connection has been established. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyConnectionEstablished;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSizeBytes,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                segmentSizeBytes >= bufferSizeBytes, "One segment contains at least one buffer.");
+        checkArgument(
+                !isBroadcastOnly,

Review Comment:
   I'm not sure if memory tier can used when partition reuse enable.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.NoopMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */

Review Comment:
   ```suggestion
   /** The implementation of {@link TierFactory} for memory tier. */
   ```



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/netty/TestingNettyConnectionWriter.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.netty;
+
+import javax.annotation.Nullable;
+
+import java.util.function.Supplier;
+
+/** Test implementation for {@link NettyConnectionWriter}. */
+public class TestingNettyConnectionWriter implements NettyConnectionWriter {
+
+    private final Runnable writeBufferFunction;
+
+    private final Supplier<NettyConnectionId> nettyConnectionIdSupplier;
+
+    private final Supplier<Integer> numQueuedBuffersSupplier;
+
+    private final Runnable closeFunction;
+
+    private TestingNettyConnectionWriter(
+            Runnable writeBufferFunction,
+            Supplier<NettyConnectionId> nettyConnectionIdSupplier,
+            Supplier<Integer> numQueuedBuffersSupplier,
+            Runnable closeFunction) {
+        this.writeBufferFunction = writeBufferFunction;
+        this.nettyConnectionIdSupplier = nettyConnectionIdSupplier;
+        this.numQueuedBuffersSupplier = numQueuedBuffersSupplier;
+        this.closeFunction = closeFunction;
+    }
+
+    @Override
+    public void writeBuffer(NettyPayload nettyPayload) {
+        writeBufferFunction.run();
+    }
+
+    @Override
+    public NettyConnectionId getNettyConnectionId() {
+        return nettyConnectionIdSupplier.get();
+    }
+
+    @Override
+    public int numQueuedBuffers() {
+        return numQueuedBuffersSupplier.get();
+    }
+
+    @Override
+    public void close(@Nullable Throwable error) {
+        closeFunction.run();

Review Comment:
   Why not use a Function instead use a Runnable here.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(

Review Comment:
   Maybe we should also test another condition:
   ```
   memoryManager.getMaxNonReclaimableBuffers(this)
                                           - memoryManager.numOwnerRequestedBuffer(this))
                                   > numBuffersPerSegment;
   ```



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);

Review Comment:
   Please using `try-with-resource` pattern.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcast) {
+        return createMemoryTierProducerAgent(isBroadcast, new TieredStorageResourceRegistry());
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(
+            boolean isBroadcast, TieredStorageResourceRegistry resourceRegistry) {
+        TestingTieredStorageMemoryManager memoryManager =
+                new TestingTieredStorageMemoryManager.Builder()
+                        .setGetMaxNonReclaimableBuffersFunction(ignore -> Integer.MAX_VALUE)
+                        .build();
+        TestingTieredStorageNettyService nettyService =
+                new TestingTieredStorageNettyService.Builder().build();
+        TestingNettyServiceProducer nettyServiceProducer =
+                new TestingNettyServiceProducer.Builder().build();
+        nettyService.registerProducer(PARTITION_ID, nettyServiceProducer);
+
+        return new MemoryTierProducerAgent(
+                PARTITION_ID,
+                NUM_SUBPARTITIONS,
+                BUFFER_SIZE,
+                SEGMENT_SIZE_BYTES,
+                isBroadcast,

Review Comment:
   ```suggestion
                   isBroadcastOnly,
   ```



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {
+        NettyPayload segmentNettyPayload = NettyPayload.newSegment(segmentId);
+        addFinishedBuffer(segmentNettyPayload);
+    }
+
+    void release() {
+        if (nettyConnectionWriter != null) {

Review Comment:
   I wonder how we can ensure that the visibility of `nettyConnectionWriter` here. 🤔 



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);

Review Comment:
   Please using `try-with-resource` pattern.
   
   



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {

Review Comment:
   It seems like we haven't tested this class yet.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227920487


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    /**
+     * Record the writ bytes to each subpartition. When starting a new segment, the value will be
+     * reset to 0.
+     */
+    private final int[] currentSubpartitionWriteBuffers;
+
+    /**
+     * Whether a subpartition's netty connection has been established. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyConnectionEstablished;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSizeBytes,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                segmentSizeBytes >= bufferSizeBytes, "One segment contains at least one buffer.");
+        checkArgument(
+                !isBroadcastOnly,

Review Comment:
   When enabling partition-reuse, the memory tier will not be used, which is controlled in the scheduler.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227894974


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/netty/TestingNettyConnectionWriter.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.netty;
+
+import javax.annotation.Nullable;
+
+import java.util.function.Supplier;
+
+/** Test implementation for {@link NettyConnectionWriter}. */
+public class TestingNettyConnectionWriter implements NettyConnectionWriter {
+
+    private final Runnable writeBufferFunction;
+
+    private final Supplier<NettyConnectionId> nettyConnectionIdSupplier;
+
+    private final Supplier<Integer> numQueuedBuffersSupplier;
+
+    private final Runnable closeFunction;
+
+    private TestingNettyConnectionWriter(
+            Runnable writeBufferFunction,
+            Supplier<NettyConnectionId> nettyConnectionIdSupplier,
+            Supplier<Integer> numQueuedBuffersSupplier,
+            Runnable closeFunction) {
+        this.writeBufferFunction = writeBufferFunction;
+        this.nettyConnectionIdSupplier = nettyConnectionIdSupplier;
+        this.numQueuedBuffersSupplier = numQueuedBuffersSupplier;
+        this.closeFunction = closeFunction;
+    }
+
+    @Override
+    public void writeBuffer(NettyPayload nettyPayload) {
+        writeBufferFunction.run();
+    }
+
+    @Override
+    public NettyConnectionId getNettyConnectionId() {
+        return nettyConnectionIdSupplier.get();
+    }
+
+    @Override
+    public int numQueuedBuffers() {
+        return numQueuedBuffersSupplier.get();
+    }
+
+    @Override
+    public void close(@Nullable Throwable error) {
+        closeFunction.run();

Review Comment:
   Fixed.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    /**
+     * Record the writ bytes to each subpartition. When starting a new segment, the value will be
+     * reset to 0.
+     */
+    private final int[] currentSubpartitionWriteBuffers;
+
+    /**
+     * Whether a subpartition's netty connection has been established. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyConnectionEstablished;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSizeBytes,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                segmentSizeBytes >= bufferSizeBytes, "One segment contains at least one buffer.");

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227897864


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {
+        NettyPayload segmentNettyPayload = NettyPayload.newSegment(segmentId);
+        addFinishedBuffer(segmentNettyPayload);
+    }
+
+    void release() {
+        if (nettyConnectionWriter != null) {
+            nettyConnectionWriter.close(null);
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private void addFinishedBuffer(NettyPayload nettyPayload) {
+        finishedBufferIndex++;
+        checkNotNull(nettyConnectionWriter).writeBuffer(nettyPayload);
+        if (nettyConnectionWriter.numQueuedBuffers() <= 1
+                && nettyService.getClass() == TieredStorageNettyServiceImpl.class) {
+            ((TieredStorageNettyServiceImpl) nettyService)

Review Comment:
   Currently, we can only transfer the type of the `nettyService`. `TieredStorageNettyServiceImpl ` implementing the new interface can not resolve this,  because we can not make the `createProducerAgent` to be dependent on an implementation of the `nettyService`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] reswqa commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "reswqa (via GitHub)" <gi...@apache.org>.
reswqa commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227583507


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();

Review Comment:
   This strongly rely on `BUFFER_SIZE = SEGMENT_SIZE_BYTES = 1024`, it will make this test case a bit obscure. I suggest setting the default `SEGMENT_SIZE_BYTES` in the class to be greater than `BUFFER_SIZE`(e.g. `BUFFER_SIZE * 2`) and introducing `createMemoryTierProducerAgent (boolean isBroadcast, int segmentSizeBytes)`. As for this test, we can call `createMemoryTierProducerAgent(false, BUFFER_SIZE)`



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222629980


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent {
+
+    private final int numBytesPerSegment;
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager storageMemoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each sub partition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Each element of the list is all views of the subpartition corresponding to its index, which
+     * are stored in the form of a map that maps consumer id to its subpartition view.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int numBytesPerSegment,
+            TieredStorageMemoryManager storageMemoryManager,
+            boolean isBroadcastOnly,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                numBytesPerSegment >= bufferSize, "One segment contains at least one buffer.");
+
+        this.numBytesPerSegment = numBytesPerSegment;
+        this.numBuffersPerSegment = numBytesPerSegment / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.storageMemoryManager = storageMemoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, createNettyServiceProducer());
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (storageMemoryManager.getMaxNonReclaimableBuffers(this)
+                                        - storageMemoryManager.numOwnerRequestedBuffer(this))
+                                > numBuffersPerSegment;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentBufferContext(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > numBytesPerSegment) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void close() {
+        // Nothing to do
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private NettyServiceProducer createNettyServiceProducer() {
+        return new NettyServiceProducer() {
+            @Override
+            public void connectionEstablished(
+                    TieredStorageSubpartitionId subpartitionId,
+                    NettyConnectionWriter nettyConnectionWriter) {
+                MemoryTierProducerAgent.this.register(subpartitionId, nettyConnectionWriter);
+            }
+
+            @Override
+            public void connectionBroken(NettyConnectionId connectionId) {
+                // nothing to do;
+            }
+        };
+    }
+
+    private void register(

Review Comment:
   This method is removed, the logic is moved to the method `connectionEstablished `.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222629034


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent {
+
+    private final int numBytesPerSegment;
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager storageMemoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each sub partition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Each element of the list is all views of the subpartition corresponding to its index, which
+     * are stored in the form of a map that maps consumer id to its subpartition view.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int numBytesPerSegment,
+            TieredStorageMemoryManager storageMemoryManager,
+            boolean isBroadcastOnly,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                numBytesPerSegment >= bufferSize, "One segment contains at least one buffer.");
+
+        this.numBytesPerSegment = numBytesPerSegment;
+        this.numBuffersPerSegment = numBytesPerSegment / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.storageMemoryManager = storageMemoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, createNettyServiceProducer());
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (storageMemoryManager.getMaxNonReclaimableBuffers(this)
+                                        - storageMemoryManager.numOwnerRequestedBuffer(this))
+                                > numBuffersPerSegment;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentBufferContext(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > numBytesPerSegment) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void close() {
+        // Nothing to do
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private NettyServiceProducer createNettyServiceProducer() {
+        return new NettyServiceProducer() {
+            @Override
+            public void connectionEstablished(
+                    TieredStorageSubpartitionId subpartitionId,
+                    NettyConnectionWriter nettyConnectionWriter) {
+                MemoryTierProducerAgent.this.register(subpartitionId, nettyConnectionWriter);
+            }
+
+            @Override
+            public void connectionBroken(NettyConnectionId connectionId) {
+                // nothing to do;
+            }
+        };
+    }

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226171618


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }

Review Comment:
   Fixed, replaced this with `checkState`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226177202


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void connectionEstablished(
+            TieredStorageSubpartitionId subpartitionId,
+            NettyConnectionWriter nettyConnectionWriter) {
+        if (isBroadcastOnly) {
+            throw new RuntimeException(
+                    "Broadcast-only result partition is not allowed to use the memory tier.");
+        }
+        this.subpartitionProducerAgents[subpartitionId.getSubpartitionId()].registerNettyService(
+                nettyConnectionWriter);
+        nettyServiceRegistered[subpartitionId.getSubpartitionId()] = true;
+    }
+
+    @Override
+    public void connectionBroken(NettyConnectionId connectionId) {
+        // noop
+    }
+
+    @Override
+    public void close() {
+        // noop

Review Comment:
   Added comments about this to describe the reason why we do not wait for the netty queue to be consumed when closing the agent.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227901327


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {

Review Comment:
   Rename it as `updateSegmentId `



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.NoopMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227894217


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/NoopMasterAgent.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.tier;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+
+/** The noop implementation for {@link TierMasterAgent}. */
+public class NoopMasterAgent implements TierMasterAgent {

Review Comment:
   Fixed.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/netty/TestingNettyConnectionWriter.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.netty;
+
+import javax.annotation.Nullable;
+
+import java.util.function.Supplier;
+
+/** Test implementation for {@link NettyConnectionWriter}. */
+public class TestingNettyConnectionWriter implements NettyConnectionWriter {
+
+    private final Runnable writeBufferFunction;
+
+    private final Supplier<NettyConnectionId> nettyConnectionIdSupplier;
+
+    private final Supplier<Integer> numQueuedBuffersSupplier;
+
+    private final Runnable closeFunction;
+
+    private TestingNettyConnectionWriter(
+            Runnable writeBufferFunction,
+            Supplier<NettyConnectionId> nettyConnectionIdSupplier,
+            Supplier<Integer> numQueuedBuffersSupplier,
+            Runnable closeFunction) {
+        this.writeBufferFunction = writeBufferFunction;
+        this.nettyConnectionIdSupplier = nettyConnectionIdSupplier;
+        this.numQueuedBuffersSupplier = numQueuedBuffersSupplier;
+        this.closeFunction = closeFunction;
+    }
+
+    @Override
+    public void writeBuffer(NettyPayload nettyPayload) {
+        writeBufferFunction.run();

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222624235


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */
+public class MemoryTierFactory implements TierFactory {
+
+    private final int numBytesPerSegment;
+
+    public MemoryTierFactory(int numBytesPerSegment) {
+        this.numBytesPerSegment = numBytesPerSegment;
+    }
+
+    @Override
+    public TierMasterAgent createMasterAgent(
+            TieredStorageResourceRegistry tieredStorageResourceRegistry) {
+        return new MemoryTierMasterAgent();
+    }
+
+    @Override
+    public TierProducerAgent createProducerAgent(
+            int tierIndex,
+            int numSubpartitions,
+            int bufferSize,
+            ResultPartitionID resultPartitionID,
+            String dataFileBasePath,
+            float minReservedDiskSpaceFraction,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager storageMemoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {

Review Comment:
   1. Removed `tierIndex`.
   2. Moved the `bufferSize ` and `minReservedDiskSpaceFraction ` to the constructor.
   3. Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226172397


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }

Review Comment:
   Rename this to `currentSubpartitionWriteBuffers`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226179405


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.tier.memory;
+
+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.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void registerNettyService(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addEndOfSegmentEvent(ByteBuffer event) {
+        MemorySegment data = MemorySegmentFactory.wrap(event.array());
+        Buffer buffer =
+                new NetworkBuffer(
+                        data, FreeingBufferRecycler.INSTANCE, END_OF_SEGMENT, data.size());
+        NettyPayload nettyPayload =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(nettyPayload);
+    }

Review Comment:
   Fixed. Only the `MemoryTierProducerAgent ` need to be aware of the end of segment now.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] flinkbot commented on pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #22728:
URL: https://github.com/apache/flink/pull/22728#issuecomment-1580222205

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "b5400c9e324684b727f178c276601934ba947879",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "b5400c9e324684b727f178c276601934ba947879",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * b5400c9e324684b727f178c276601934ba947879 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] xintongsong commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "xintongsong (via GitHub)" <gi...@apache.org>.
xintongsong commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1225133162


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/NoopMasterAgent.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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.tier;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+
+/** The noop implementation for {@link TierMasterAgent}. */
+public class NoopMasterAgent implements TierMasterAgent {

Review Comment:
   This can be singleton.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void connectionEstablished(
+            TieredStorageSubpartitionId subpartitionId,
+            NettyConnectionWriter nettyConnectionWriter) {
+        if (isBroadcastOnly) {
+            throw new RuntimeException(
+                    "Broadcast-only result partition is not allowed to use the memory tier.");
+        }

Review Comment:
   Same here. Checkstate in constructor.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.tier.memory;
+
+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.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void registerNettyService(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addEndOfSegmentEvent(ByteBuffer event) {
+        MemorySegment data = MemorySegmentFactory.wrap(event.array());
+        Buffer buffer =
+                new NetworkBuffer(
+                        data, FreeingBufferRecycler.INSTANCE, END_OF_SEGMENT, data.size());
+        NettyPayload nettyPayload =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(nettyPayload);
+    }

Review Comment:
   It's weird that both `MemoryTierProducerAgent` and `MemoryTierSubpartitionProducerAgent` need to be aware of the details end-of-segment event. Can we prepare the event outside the subpartition agent?



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }

Review Comment:
   For broadcast only result partition, the memory tier shouldn't be created at all. So this should be a check-state, and it should be mored to the constructor.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.

Review Comment:
   JavaDoc



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;

Review Comment:
   Why do we need both of them?



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void connectionEstablished(
+            TieredStorageSubpartitionId subpartitionId,
+            NettyConnectionWriter nettyConnectionWriter) {
+        if (isBroadcastOnly) {
+            throw new RuntimeException(
+                    "Broadcast-only result partition is not allowed to use the memory tier.");
+        }
+        this.subpartitionProducerAgents[subpartitionId.getSubpartitionId()].registerNettyService(
+                nettyConnectionWriter);
+        nettyServiceRegistered[subpartitionId.getSubpartitionId()] = true;
+    }
+
+    @Override
+    public void connectionBroken(NettyConnectionId connectionId) {
+        // noop
+    }
+
+    @Override
+    public void close() {
+        // noop

Review Comment:
   I think buffers in memory tier should be released at the closing time, not release resources. The former means the upstream task has finished, while the latter means the result partition can be released.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void connectionEstablished(
+            TieredStorageSubpartitionId subpartitionId,
+            NettyConnectionWriter nettyConnectionWriter) {
+        if (isBroadcastOnly) {
+            throw new RuntimeException(
+                    "Broadcast-only result partition is not allowed to use the memory tier.");
+        }
+        this.subpartitionProducerAgents[subpartitionId.getSubpartitionId()].registerNettyService(
+                nettyConnectionWriter);
+        nettyServiceRegistered[subpartitionId.getSubpartitionId()] = true;
+    }
+
+    @Override
+    public void connectionBroken(NettyConnectionId connectionId) {
+        // noop
+    }
+
+    @Override
+    public void close() {
+        // noop
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private void releaseResources() {
+        Arrays.stream(subpartitionProducerAgents)
+                .forEach(MemoryTierSubpartitionProducerAgent::release);
+    }
+
+    private void appendEndOfSegmentEvent(int targetChannel) {
+        try {
+            getSubpartitionProducerAgent(targetChannel)
+                    .addEndOfSegmentEvent(
+                            EventSerializer.toSerializedEvent(EndOfSegmentEvent.INSTANCE));
+        } catch (IOException e) {
+            ExceptionUtils.rethrow(e, "Failed to append end of segment event,");
+        }
+    }
+
+    private void append(Buffer finishedBuffer, int targetChannel) {
+        getSubpartitionProducerAgent(targetChannel).addFinishedBuffer(finishedBuffer);
+    }
+
+    private boolean isSubpartitionRegistered(TieredStorageSubpartitionId subpartitionId) {
+        return nettyServiceRegistered[subpartitionId.getSubpartitionId()];
+    }
+
+    private MemoryTierSubpartitionProducerAgent getSubpartitionProducerAgent(int targetChannel) {
+        return subpartitionProducerAgents[targetChannel];
+    }

Review Comment:
   This seems to be unnecessary.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.NoopMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */
+public class MemoryTierFactory implements TierFactory {
+
+    private final int segmentSizeBytes;
+
+    private final int bufferSize;

Review Comment:
   Better to name with explicit unit.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.tier.memory;
+
+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.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void registerNettyService(NettyConnectionWriter nettyConnectionWriter) {

Review Comment:
   Name is inconsistent.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }

Review Comment:
   The name `numSubpartitionEmitBytes` is misleading. This is the emitted bytes only for the current segment of the subpartition.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.tier.memory;
+
+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.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    private NettyConnectionWriter nettyConnectionWriter;

Review Comment:
   Lack `@Nullable` and semantic explaination.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226175631


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void connectionEstablished(
+            TieredStorageSubpartitionId subpartitionId,
+            NettyConnectionWriter nettyConnectionWriter) {
+        if (isBroadcastOnly) {
+            throw new RuntimeException(
+                    "Broadcast-only result partition is not allowed to use the memory tier.");
+        }

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226170359


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/NoopMasterAgent.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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.tier;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+
+/** The noop implementation for {@link TierMasterAgent}. */
+public class NoopMasterAgent implements TierMasterAgent {

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226178506


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.tier.memory;
+
+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.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import java.nio.ByteBuffer;
+
+import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.END_OF_SEGMENT;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void registerNettyService(NettyConnectionWriter nettyConnectionWriter) {

Review Comment:
   Fixed. Rename it to `connectionEstablished`



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] xintongsong commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "xintongsong (via GitHub)" <gi...@apache.org>.
xintongsong commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222376428


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */
+public class MemoryTierFactory implements TierFactory {
+
+    private final int numBytesPerSegment;
+
+    public MemoryTierFactory(int numBytesPerSegment) {
+        this.numBytesPerSegment = numBytesPerSegment;
+    }
+
+    @Override
+    public TierMasterAgent createMasterAgent(
+            TieredStorageResourceRegistry tieredStorageResourceRegistry) {
+        return new MemoryTierMasterAgent();
+    }
+
+    @Override
+    public TierProducerAgent createProducerAgent(
+            int tierIndex,
+            int numSubpartitions,
+            int bufferSize,
+            ResultPartitionID resultPartitionID,
+            String dataFileBasePath,
+            float minReservedDiskSpaceFraction,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager storageMemoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {

Review Comment:
   1. `tierIndex` is not used
   2. `bufferSize` and `minReservedDiskSpaceFraction` are not per-agent configs. Might be better to set them in factory constructors.
   3. Should convert `ResultPartitionID` to `TieredStoragePartitionId` outside the class.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */
+public class MemoryTierFactory implements TierFactory {
+
+    private final int numBytesPerSegment;

Review Comment:
   Minor: `segmentSizeBytes`



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent {
+
+    private final int numBytesPerSegment;
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager storageMemoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each sub partition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Each element of the list is all views of the subpartition corresponding to its index, which
+     * are stored in the form of a map that maps consumer id to its subpartition view.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int numBytesPerSegment,
+            TieredStorageMemoryManager storageMemoryManager,
+            boolean isBroadcastOnly,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                numBytesPerSegment >= bufferSize, "One segment contains at least one buffer.");
+
+        this.numBytesPerSegment = numBytesPerSegment;
+        this.numBuffersPerSegment = numBytesPerSegment / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.storageMemoryManager = storageMemoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, createNettyServiceProducer());
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (storageMemoryManager.getMaxNonReclaimableBuffers(this)
+                                        - storageMemoryManager.numOwnerRequestedBuffer(this))
+                                > numBuffersPerSegment;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentBufferContext(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > numBytesPerSegment) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void close() {
+        // Nothing to do
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private NettyServiceProducer createNettyServiceProducer() {
+        return new NettyServiceProducer() {
+            @Override
+            public void connectionEstablished(
+                    TieredStorageSubpartitionId subpartitionId,
+                    NettyConnectionWriter nettyConnectionWriter) {
+                MemoryTierProducerAgent.this.register(subpartitionId, nettyConnectionWriter);
+            }
+
+            @Override
+            public void connectionBroken(NettyConnectionId connectionId) {
+                // nothing to do;
+            }
+        };
+    }

Review Comment:
   Instead of creating a new anonymous class, we should make `MemoryTierProducerAgent` implement the `NettyServiceProducer` interface.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.local.memory;

Review Comment:
   `.local` might not be necessary.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierMasterAgent.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+
+/** The memory tier implementation for {@link TierMasterAgent}. */
+public class MemoryTierMasterAgent implements TierMasterAgent {

Review Comment:
   We may introduce a reusable `NoopMasterAgent`.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierMasterAgent.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+
+/** The memory tier implementation for {@link TierMasterAgent}. */
+public class MemoryTierMasterAgent implements TierMasterAgent {
+    @Override
+    public void addPartition(TieredStoragePartitionId partitionId) {
+        // Nothing to do.

Review Comment:
   Minor: `noop`



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent {
+
+    private final int numBytesPerSegment;
+
+    private final int numBuffersPerSegment;
+
+    private final TieredStorageMemoryManager storageMemoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each sub partition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Each element of the list is all views of the subpartition corresponding to its index, which
+     * are stored in the form of a map that maps consumer id to its subpartition view.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int numBytesPerSegment,
+            TieredStorageMemoryManager storageMemoryManager,
+            boolean isBroadcastOnly,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(
+                numBytesPerSegment >= bufferSize, "One segment contains at least one buffer.");
+
+        this.numBytesPerSegment = numBytesPerSegment;
+        this.numBuffersPerSegment = numBytesPerSegment / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.storageMemoryManager = storageMemoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, createNettyServiceProducer());
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (storageMemoryManager.getMaxNonReclaimableBuffers(this)
+                                        - storageMemoryManager.numOwnerRequestedBuffer(this))
+                                > numBuffersPerSegment;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentBufferContext(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > numBytesPerSegment) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void close() {
+        // Nothing to do
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private NettyServiceProducer createNettyServiceProducer() {
+        return new NettyServiceProducer() {
+            @Override
+            public void connectionEstablished(
+                    TieredStorageSubpartitionId subpartitionId,
+                    NettyConnectionWriter nettyConnectionWriter) {
+                MemoryTierProducerAgent.this.register(subpartitionId, nettyConnectionWriter);
+            }
+
+            @Override
+            public void connectionBroken(NettyConnectionId connectionId) {
+                // nothing to do;
+            }
+        };
+    }
+
+    private void register(

Review Comment:
   Method name is confusing. We should keep consistent terminology with `connectionEstablished`.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227899184


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(boolean isBroadcast) {
+        return createMemoryTierProducerAgent(isBroadcast, new TieredStorageResourceRegistry());
+    }
+
+    private static MemoryTierProducerAgent createMemoryTierProducerAgent(
+            boolean isBroadcast, TieredStorageResourceRegistry resourceRegistry) {
+        TestingTieredStorageMemoryManager memoryManager =
+                new TestingTieredStorageMemoryManager.Builder()
+                        .setGetMaxNonReclaimableBuffersFunction(ignore -> Integer.MAX_VALUE)
+                        .build();
+        TestingTieredStorageNettyService nettyService =
+                new TestingTieredStorageNettyService.Builder().build();
+        TestingNettyServiceProducer nettyServiceProducer =
+                new TestingNettyServiceProducer.Builder().build();
+        nettyService.registerProducer(PARTITION_ID, nettyServiceProducer);
+
+        return new MemoryTierProducerAgent(
+                PARTITION_ID,
+                NUM_SUBPARTITIONS,
+                BUFFER_SIZE,
+                SEGMENT_SIZE_BYTES,
+                isBroadcast,

Review Comment:
   Fixed.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isTrue();
+        assertThat(
+                        memoryTierProducerAgent.tryWrite(
+                                SUBPARTITION_ID, BufferBuilderTestUtils.buildSomeBuffer()))
+                .isFalse();
+    }
+
+    @Test
+    void testBroadcastOnlyPartitionCanNotUseMemoryTier() {
+        assertThatThrownBy(() -> createMemoryTierProducerAgent(true))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("not allowed to use the memory tier");
+    }
+
+    @Test
+    void testRelease() {
+        TieredStorageResourceRegistry resourceRegistry = new TieredStorageResourceRegistry();
+        MemoryTierProducerAgent memoryTierProducerAgent =
+                createMemoryTierProducerAgent(false, resourceRegistry);
+
+        AtomicBoolean isClosed = new AtomicBoolean(false);
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID,
+                new TestingNettyConnectionWriter.Builder()
+                        .setCloseFunction(() -> isClosed.set(true))
+                        .build());
+
+        resourceRegistry.clearResourceFor(PARTITION_ID);
+        assertThat(isClosed.get()).isTrue();

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227898419


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(

Review Comment:
   Added a new test of `testStartSegmentFailedWithInsufficientMemory`



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227899472


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {

Review Comment:
   Added a new test class.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226170927


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.NoopMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */
+public class MemoryTierFactory implements TierFactory {
+
+    private final int segmentSizeBytes;
+
+    private final int bufferSize;

Review Comment:
   Fixed.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1222628106


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/local/memory/MemoryTierFactory.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.tier.local.memory;
+
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierConsumerAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierFactory;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierMasterAgent;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+
+/** The memory tier factory implementation for {@link TierFactory}. */
+public class MemoryTierFactory implements TierFactory {
+
+    private final int numBytesPerSegment;

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227900712


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);

Review Comment:
   Fixed.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isFalse();
+        memoryTierProducerAgent.connectionEstablished(
+                SUBPARTITION_ID, new TestingNettyConnectionWriter.Builder().build());
+        assertThat(memoryTierProducerAgent.tryStartNewSegment(SUBPARTITION_ID, 0)).isTrue();
+    }
+
+    @Test
+    void testTryWrite() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227901327


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {

Review Comment:
   Rename it as `updateSegmentId `



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on PR #22728:
URL: https://github.com/apache/flink/pull/22728#issuecomment-1590374279

   Thanks @xintongsong  and @reswqa for helping review the code.


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] reswqa commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "reswqa (via GitHub)" <gi...@apache.org>.
reswqa commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227904564


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierSubpartitionProducerAgent.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyPayload;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyServiceImpl;
+
+import javax.annotation.Nullable;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** The subpartition producer agent for the memory tier. */
+class MemoryTierSubpartitionProducerAgent {
+
+    private final int subpartitionId;
+
+    private final TieredStorageNettyService nettyService;
+
+    /**
+     * The {@link NettyConnectionWriter} is used to write buffers to the netty connection.
+     *
+     * <p>Note that this field can be null before the netty connection is established.
+     */
+    @Nullable private NettyConnectionWriter nettyConnectionWriter;
+
+    private int finishedBufferIndex;
+
+    MemoryTierSubpartitionProducerAgent(
+            int subpartitionId, TieredStorageNettyService nettyService) {
+        this.subpartitionId = subpartitionId;
+        this.nettyService = nettyService;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by MemoryTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    void connectionEstablished(NettyConnectionWriter nettyConnectionWriter) {
+        this.nettyConnectionWriter = nettyConnectionWriter;
+    }
+
+    void addFinishedBuffer(Buffer buffer) {
+        NettyPayload toAddBuffer =
+                NettyPayload.newBuffer(buffer, finishedBufferIndex, subpartitionId);
+        addFinishedBuffer(toAddBuffer);
+    }
+
+    void addSegmentId(int segmentId) {
+        NettyPayload segmentNettyPayload = NettyPayload.newSegment(segmentId);
+        addFinishedBuffer(segmentNettyPayload);
+    }
+
+    void release() {
+        if (nettyConnectionWriter != null) {
+            nettyConnectionWriter.close(null);
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private void addFinishedBuffer(NettyPayload nettyPayload) {
+        finishedBufferIndex++;
+        checkNotNull(nettyConnectionWriter).writeBuffer(nettyPayload);
+        if (nettyConnectionWriter.numQueuedBuffers() <= 1
+                && nettyService.getClass() == TieredStorageNettyServiceImpl.class) {
+            ((TieredStorageNettyServiceImpl) nettyService)

Review Comment:
   Make sense, maybe this refactor only can be done in the future as netty shuffle plugin relies a bit on the concept of `subpartitionView` now. 😞 



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1227900712


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgentTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.TestingTieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageIdMappingUtils;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingNettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TestingTieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MemoryTierProducerAgent}. */
+class MemoryTierProducerAgentTest {
+
+    private static final int NUM_SUBPARTITIONS = 10;
+
+    private static final int BUFFER_SIZE = 1024;
+
+    private static final int SEGMENT_SIZE_BYTES = 1024;
+
+    private static final TieredStoragePartitionId PARTITION_ID =
+            TieredStorageIdMappingUtils.convertId(new ResultPartitionID());
+    private static final TieredStorageSubpartitionId SUBPARTITION_ID =
+            new TieredStorageSubpartitionId(0);
+
+    @Test
+    void testTryStartNewSegment() {
+        MemoryTierProducerAgent memoryTierProducerAgent = createMemoryTierProducerAgent(false);

Review Comment:
   Fixed.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] TanYuxin-tyx commented on a diff in pull request #22728: [FLINK-31641][network] Implement the memory tier producer for the tiered storage

Posted by "TanYuxin-tyx (via GitHub)" <gi...@apache.org>.
TanYuxin-tyx commented on code in PR #22728:
URL: https://github.com/apache/flink/pull/22728#discussion_r1226171959


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/memory/MemoryTierProducerAgent.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.tier.memory;
+
+import org.apache.flink.runtime.io.network.api.EndOfSegmentEvent;
+import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionId;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyConnectionWriter;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.NettyServiceProducer;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.netty.TieredStorageNettyService;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageResourceRegistry;
+import org.apache.flink.runtime.io.network.partition.hybrid.tiered.tier.TierProducerAgent;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** The memory tier implementation of {@link TierProducerAgent}. */
+public class MemoryTierProducerAgent implements TierProducerAgent, NettyServiceProducer {
+
+    private final int segmentSizeBytes;
+
+    private final int segmentSizeBuffers;
+
+    private final TieredStorageMemoryManager memoryManager;
+
+    private final boolean isBroadcastOnly;
+
+    // Record the byte number currently written to each subpartition.
+    private final int[] numSubpartitionEmitBytes;
+
+    /**
+     * Whether a subpartition has been registered to the netty service. The array index is
+     * corresponding to the subpartition id.
+     */
+    private final boolean[] nettyServiceRegistered;
+
+    private final MemoryTierSubpartitionProducerAgent[] subpartitionProducerAgents;
+
+    public MemoryTierProducerAgent(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            int bufferSize,
+            int segmentSizeBytes,
+            boolean isBroadcastOnly,
+            TieredStorageMemoryManager memoryManager,
+            TieredStorageNettyService nettyService,
+            TieredStorageResourceRegistry resourceRegistry) {
+        checkArgument(segmentSizeBytes >= bufferSize, "One segment contains at least one buffer.");
+
+        this.segmentSizeBytes = segmentSizeBytes;
+        this.segmentSizeBuffers = segmentSizeBytes / bufferSize;
+        this.isBroadcastOnly = isBroadcastOnly;
+        this.memoryManager = memoryManager;
+        this.numSubpartitionEmitBytes = new int[numSubpartitions];
+        this.nettyServiceRegistered = new boolean[numSubpartitions];
+        this.subpartitionProducerAgents = new MemoryTierSubpartitionProducerAgent[numSubpartitions];
+
+        Arrays.fill(numSubpartitionEmitBytes, 0);
+        nettyService.registerProducer(partitionId, this);
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; ++subpartitionId) {
+            subpartitionProducerAgents[subpartitionId] =
+                    new MemoryTierSubpartitionProducerAgent(subpartitionId, nettyService);
+        }
+        resourceRegistry.registerResource(partitionId, this::releaseResources);
+    }
+
+    @Override
+    public boolean tryStartNewSegment(TieredStorageSubpartitionId subpartitionId, int segmentId) {
+        if (isBroadcastOnly) {
+            return false;
+        }
+        boolean canStartNewSegment =
+                isSubpartitionRegistered(subpartitionId)
+                        && (memoryManager.getMaxNonReclaimableBuffers(this)
+                                        - memoryManager.numOwnerRequestedBuffer(this))
+                                > segmentSizeBuffers;
+        if (canStartNewSegment) {
+            getSubpartitionProducerAgent(subpartitionId.getSubpartitionId())
+                    .addSegmentId(segmentId);
+        }
+        return canStartNewSegment;
+    }
+
+    @Override
+    public boolean tryWrite(TieredStorageSubpartitionId subpartitionId, Buffer finishedBuffer) {
+        int subpartitionIndex = subpartitionId.getSubpartitionId();
+        if (numSubpartitionEmitBytes[subpartitionIndex] != 0
+                && numSubpartitionEmitBytes[subpartitionIndex] + finishedBuffer.readableBytes()
+                        > segmentSizeBytes) {
+            appendEndOfSegmentEvent(subpartitionIndex);
+            numSubpartitionEmitBytes[subpartitionIndex] = 0;
+            return false;
+        }
+        numSubpartitionEmitBytes[subpartitionIndex] += finishedBuffer.readableBytes();
+        append(finishedBuffer, subpartitionIndex);
+        return true;
+    }
+
+    @Override
+    public void connectionEstablished(
+            TieredStorageSubpartitionId subpartitionId,
+            NettyConnectionWriter nettyConnectionWriter) {
+        if (isBroadcastOnly) {
+            throw new RuntimeException(
+                    "Broadcast-only result partition is not allowed to use the memory tier.");
+        }
+        this.subpartitionProducerAgents[subpartitionId.getSubpartitionId()].registerNettyService(
+                nettyConnectionWriter);
+        nettyServiceRegistered[subpartitionId.getSubpartitionId()] = true;
+    }
+
+    @Override
+    public void connectionBroken(NettyConnectionId connectionId) {
+        // noop
+    }
+
+    @Override
+    public void close() {
+        // noop
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private void releaseResources() {
+        Arrays.stream(subpartitionProducerAgents)
+                .forEach(MemoryTierSubpartitionProducerAgent::release);
+    }
+
+    private void appendEndOfSegmentEvent(int targetChannel) {
+        try {
+            getSubpartitionProducerAgent(targetChannel)
+                    .addEndOfSegmentEvent(
+                            EventSerializer.toSerializedEvent(EndOfSegmentEvent.INSTANCE));
+        } catch (IOException e) {
+            ExceptionUtils.rethrow(e, "Failed to append end of segment event,");
+        }
+    }
+
+    private void append(Buffer finishedBuffer, int targetChannel) {
+        getSubpartitionProducerAgent(targetChannel).addFinishedBuffer(finishedBuffer);
+    }
+
+    private boolean isSubpartitionRegistered(TieredStorageSubpartitionId subpartitionId) {
+        return nettyServiceRegistered[subpartitionId.getSubpartitionId()];
+    }
+
+    private MemoryTierSubpartitionProducerAgent getSubpartitionProducerAgent(int targetChannel) {
+        return subpartitionProducerAgents[targetChannel];
+    }

Review Comment:
   Removed it.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org