You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/06/04 14:27:46 UTC

[GitHub] [flink] pnowojski commented on a change in pull request #8455: [FLINK-12284, FLINK-12637][Network, Metrics]Fix the incorrect inputBufferUsage metric in credit-based network mode

pnowojski 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_r290322189
 
 

 ##########
 File path: flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/consumer/InputBuffersMetricsTest.java
 ##########
 @@ -0,0 +1,207 @@
+/*
+ * 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.memory.MemorySegment;
+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.NetworkEnvironment;
+import org.apache.flink.runtime.io.network.NetworkEnvironmentBuilder;
+import org.apache.flink.runtime.io.network.PartitionRequestClient;
+import org.apache.flink.runtime.io.network.TaskEventDispatcher;
+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 java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test for input buffer usage related metrics.
+ */
+public class InputBuffersMetricsTest {
+
+	@Test
+	public void testBufferUsageMetrics() throws IOException, InterruptedException {
+
+		final NetworkEnvironment network = new NetworkEnvironmentBuilder().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();
+
+			SingleInputGate[] inputGates = new SingleInputGate[]{inputGate1, inputGate2};
+			FloatingBuffersUsageGauge floatingBuffersUsageGauge = new FloatingBuffersUsageGauge(inputGates);
+			ExclusiveBuffersUsageGauge exclusiveBuffersUsageGauge = new ExclusiveBuffersUsageGauge(inputGates);
+			CreditBasedInputBufferPoolUsageGauge inputBufferPoolUsageGauge = new CreditBasedInputBufferPoolUsageGauge(floatingBuffersUsageGauge, exclusiveBuffersUsageGauge, inputGates);
+
+			inputMetric.gauge("floatingBuffersUsage", floatingBuffersUsageGauge);
+			inputMetric.gauge("exclusiveBuffersUsage", exclusiveBuffersUsageGauge);
+			inputMetric.gauge("inputBufferUsage", inputBufferPoolUsageGauge);
+
+			assertEquals(0.0, floatingBuffersUsageGauge.getValue(), 0.0);
+			assertEquals(0.0, exclusiveBuffersUsageGauge.getValue(), 0.0);
+			assertEquals(0.0, inputBufferPoolUsageGauge.getValue(), 0.0);
+
+			assertEquals(buffersPerChannel * 2, exclusiveBuffersUsageGauge.calculateBufferPoolSize(inputGate1));
+			assertEquals(extraNetworkBuffersPerGate, floatingBuffersUsageGauge.calculateBufferPoolSize(inputGate1));
+
+			assertEquals(buffersPerChannel, exclusiveBuffersUsageGauge.calculateBufferPoolSize(inputGate2));
+			assertEquals(extraNetworkBuffersPerGate, floatingBuffersUsageGauge.calculateBufferPoolSize(inputGate2));
+
+			List<Buffer> exclusiveBuffers = new ArrayList<>();
+
+			// test for exclusive buffer used
+			exclusiveBuffers.add(remoteInputChannel1.requestBuffer());
+			assertEquals(1, exclusiveBuffersUsageGauge.calculateUsedBuffers(inputGate1));
+
+			for (int i = 0; i < buffersPerChannel; i++){
+				Buffer buffer = remoteInputChannel1.requestBuffer();
+				if (buffer != null) {
+					exclusiveBuffers.add(buffer);
+				} else {
+					break;
+				}
+			}
+			assertEquals(buffersPerChannel, exclusiveBuffers.size());
+			assertEquals(buffersPerChannel, exclusiveBuffersUsageGauge.calculateUsedBuffers(inputGate1));
+			assertEquals(1 / 3.0, exclusiveBuffersUsageGauge.getValue(), 0.0001);
+			assertEquals(buffersPerChannel / (extraNetworkBuffersPerGate * 2 + buffersPerChannel * 3.0), inputBufferPoolUsageGauge.getValue(), 0.0001);
+
+			for (int i = 0; i < buffersPerChannel; i++){
+				Buffer buffer = remoteInputChannel2.requestBuffer();
+				if (buffer != null) {
+					exclusiveBuffers.add(buffer);
+				} else {
+					break;
+				}
+			}
+			assertEquals(buffersPerChannel * 2, exclusiveBuffers.size());
+			assertEquals(buffersPerChannel * 2, exclusiveBuffersUsageGauge.calculateUsedBuffers(inputGate1));
+			assertEquals(2 / 3.0, exclusiveBuffersUsageGauge.getValue(), 0.0001);
+			assertEquals(buffersPerChannel * 2 / (extraNetworkBuffersPerGate * 2 + buffersPerChannel * 3.0), inputBufferPoolUsageGauge.getValue(), 0.0001);
+
+			// test for floating buffer used
+			List<Buffer> floatingBuffers = new ArrayList<>();
+
+			remoteInputChannel1.requestSubpartition(0);
+			remoteInputChannel1.onSenderBacklog(3);
+			assertEquals(buffersPerChannel + 3, remoteInputChannel1.unsynchronizedGetFloatingBuffersAvailable());
+
+			for (int i = 0; i < buffersPerChannel + 3; i++) {
+				Buffer buffer = remoteInputChannel1.requestBuffer();
+				if (buffer != null) {
+					floatingBuffers.add(buffer);
+				} else {
+					break;
+				}
+			}
+			assertEquals(0, remoteInputChannel1.unsynchronizedGetFloatingBuffersAvailable());
+			assertEquals(buffersPerChannel * 2, exclusiveBuffersUsageGauge.calculateUsedBuffers(inputGate1));
+			assertEquals(2 / 3.0, exclusiveBuffersUsageGauge.getValue(), 0.0001);
+			assertEquals((buffersPerChannel * 2 + buffersPerChannel + 3) / (extraNetworkBuffersPerGate * 2 + buffersPerChannel * 3.0), inputBufferPoolUsageGauge.getValue(), 0.0001);
+
+			List<MemorySegment> segments = new ArrayList<>();
+			for (Buffer buffer : exclusiveBuffers) {
+				segments.add(buffer.getMemorySegment());
+			}
+			network.getNetworkBufferPool().recycleMemorySegments(segments);
 
 Review comment:
   Couple of questions:
   1. Shouldn't this be a part of `finally`?
   2. Couldn't you call `buffer.recycle()`?
   3. Maybe it would be better to run whole test with a `ClosableRegistry` that you would be populating as you create more objects? For example input gates there once you create them, or register `() -> { buffer.recycle() }` every time you just requested a buffer? It would make closing/releasing resource more reliable and more fine grained. 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services