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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
