zhijiangW commented on a change in pull request #8455: 
[FLINK-12284,FLINK-12637][Network,Metrics]Fix the incorrect inputBufferUsage 
metric in credit-based network mode
URL: https://github.com/apache/flink/pull/8455#discussion_r295115863
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/consumer/InputBuffersMetricsTest.java
 ##########
 @@ -0,0 +1,180 @@
+/*
+ * 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.consumer;
+
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.runtime.io.network.ConnectionID;
+import org.apache.flink.runtime.io.network.ConnectionManager;
+import org.apache.flink.runtime.io.network.NettyShuffleEnvironment;
+import org.apache.flink.runtime.io.network.NettyShuffleEnvironmentBuilder;
+import org.apache.flink.runtime.io.network.TaskEventDispatcher;
+import org.apache.flink.runtime.io.network.TestingConnectionManager;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import 
org.apache.flink.runtime.io.network.metrics.CreditBasedInputBufferPoolUsageGauge;
+import org.apache.flink.runtime.io.network.metrics.ExclusiveBuffersUsageGauge;
+import org.apache.flink.runtime.io.network.metrics.FloatingBuffersUsageGauge;
+import org.apache.flink.runtime.io.network.partition.InputChannelTestUtils;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
+import org.apache.flink.runtime.io.network.partition.ResultPartitionType;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test for input buffer usage related metrics.
+ */
+public class InputBuffersMetricsTest {
+
+       @Test
+       public void testBufferUsageMetrics() throws IOException, 
InterruptedException {
+
+               CloseableRegistry closeableRegistry = new CloseableRegistry();
+
+               final NettyShuffleEnvironment network = new 
NettyShuffleEnvironmentBuilder().setIsCreditBased(true).build();
+
+               final SingleInputGate inputGate1 = new SingleInputGateBuilder()
+                       .setNumberOfChannels(2)
+                       .setIsCreditBased(true)
+                       
.setResultPartitionType(ResultPartitionType.PIPELINED_BOUNDED)
+                       .setupBufferPoolFactory(network)
+                       .build();
+
+               final SingleInputGate inputGate2 =  new SingleInputGateBuilder()
+                       .setNumberOfChannels(2)
+                       .setIsCreditBased(true)
+                       
.setResultPartitionType(ResultPartitionType.PIPELINED_BOUNDED)
+                       .setupBufferPoolFactory(network)
+                       .build();
+
+               int buffersPerChannel = 2;
+               int extraNetworkBuffersPerGate = 8;
+
+               try {
+                       final ResultPartitionID resultPartitionId1 = new 
ResultPartitionID();
+                       final ResultPartitionID resultPartitionId2 = new 
ResultPartitionID();
+
+                       final ConnectionID connectionId1 = new ConnectionID(new 
InetSocketAddress("localhost", 5000), 0);
+                       final ConnectionID connectionId2 = new ConnectionID(new 
InetSocketAddress("localhost", 5000), 0);
+
+                       RemoteInputChannel remoteInputChannel1 = 
createUnknownInputChannel(network, inputGate1, resultPartitionId1, 
0).toRemoteInputChannel(connectionId1);
+                       
inputGate1.setInputChannel(resultPartitionId1.getPartitionId(), 
remoteInputChannel1);
+
+                       RemoteInputChannel remoteInputChannel2 = 
createUnknownInputChannel(network, inputGate1, resultPartitionId2, 
1).toRemoteInputChannel(connectionId2);
+                       
inputGate1.setInputChannel(resultPartitionId2.getPartitionId(), 
remoteInputChannel2);
+
+                       RemoteInputChannel remoteInputChannel3 = 
createUnknownInputChannel(network, inputGate2, resultPartitionId1, 
0).toRemoteInputChannel(connectionId1);
+                       
inputGate2.setInputChannel(resultPartitionId1.getPartitionId(), 
remoteInputChannel3);
+
+                       LocalInputChannel localInputChannel1 = 
createUnknownInputChannel(network, inputGate2, resultPartitionId2, 
1).toLocalInputChannel();
+                       
inputGate2.setInputChannel(resultPartitionId2.getPartitionId(), 
localInputChannel1);
+
+                       MetricGroup inputMetric = 
UnregisteredMetricGroups.createUnregisteredTaskMetricGroup().getIOMetricGroup().addGroup("buffers");
+
+                       inputGate1.setup();
+                       inputGate2.setup();
 
 Review comment:
   If possible we could extract a local method for extracting the relevant 
logics for creating `SingleInputGate`, including the InputGate/InputChannel 
creations and setup. Then it would not do the same things for two gates. The 
`InputChannel` used in following parts could also be got from created gate.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to