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 2021/08/18 09:28:53 UTC

[GitHub] [flink] fapaul opened a new pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

fapaul opened a new pull request #16875:
URL: https://github.com/apache/flink/pull/16875


   <!--
   *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
   
   This PR adds the FLIP-33 metrics numBytesOut and currentSendTime to the FLIP-143 KafkaWriter.
   
   
   ## Brief change log
   
   - 01de3e6 adds a test util to properly assert on operator io metrics
   
   ## Verifying this change
   
   Added two tests verifying that both metrics are correctly emitted.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (yes / **no**)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / **no**)
     - The serializers: (yes / **no** / don't know)
     - The runtime per-record code paths (performance sensitive): (yes / **no** / don't know)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (yes / **no** / don't know)
     - The S3 file system connector: (yes / **no** / don't know)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes / **no**)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / **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] flinkbot edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547",
       "triggerID" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "triggerType" : "PUSH"
     }, {
       "hash" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22558",
       "triggerID" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 90e1347327b6610de7379a987a7c36554acb1d80 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22558) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] flinkbot edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547",
       "triggerID" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "triggerType" : "PUSH"
     }, {
       "hash" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   * 23b1d535a1ebe8962f5129cf4c125bd94dc3de36 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542) 
   * 6ae373c946982c8bf129722453a6105cc710a38e Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547) 
   * 90e1347327b6610de7379a987a7c36554acb1d80 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] flinkbot edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   * 23b1d535a1ebe8962f5129cf4c125bd94dc3de36 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] fapaul commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
fapaul commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692765396



##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -32,40 +40,44 @@
 
 import org.apache.kafka.clients.producer.ProducerRecord;
 import org.apache.kafka.common.serialization.ByteArraySerializer;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import org.hamcrest.MatcherAssert;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testcontainers.containers.KafkaContainer;
 import org.testcontainers.containers.Network;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.utility.DockerImageName;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.time.Duration;
-import java.util.List;
+import java.util.Map;
 import java.util.Properties;
+import java.util.stream.IntStream;
 
+import static org.hamcrest.Matchers.greaterThan;
 import static org.junit.Assert.assertThrows;
 
 /** Tests for the standalone KafkaWriter. */
-@RunWith(Parameterized.class)
 public class KafkaWriterITCase {
 
     private static final Logger LOG = LoggerFactory.getLogger(KafkaWriterITCase.class);
     private static final Slf4jLogConsumer LOG_CONSUMER = new Slf4jLogConsumer(LOG);
     private static final String INTER_CONTAINER_KAFKA_ALIAS = "kafka";
     private static final Network NETWORK = Network.newNetwork();
     private static final String KAFKA_METRIC_WITH_GROUP_NAME = "KafkaProducer.incoming-byte-total";
-
-    private final DeliveryGuarantee guarantee;
+    private static final SinkWriter.Context SINK_WRITER_CONTEXT = new DummySinkWriterContext();
 
     private MetricListener metricListener;
 
-    @ClassRule
-    public static final KafkaContainer KAFKA_CONTAINER =
+    private static final KafkaContainer KAFKA_CONTAINER =

Review comment:
       I think all the tests need the KafkaContainer or what are you referring to?




-- 
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] fapaul commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
fapaul commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692798234



##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -82,49 +94,107 @@
                     .withLogConsumer(LOG_CONSUMER)
                     .withNetworkAliases(INTER_CONTAINER_KAFKA_ALIAS);
 
-    @Before
-    public void setUp() {
-        metricListener = new MetricListener();
+    @BeforeAll
+    public static void beforeAll() {
+        KAFKA_CONTAINER.start();
     }
 
-    @Parameterized.Parameters
-    public static List<DeliveryGuarantee> guarantees() {
-        return ImmutableList.of(
-                DeliveryGuarantee.NONE,
-                DeliveryGuarantee.AT_LEAST_ONCE,
-                DeliveryGuarantee.EXACTLY_ONCE);
+    @AfterAll
+    public static void afterAll() {
+        KAFKA_CONTAINER.stop();
     }
 
-    public KafkaWriterITCase(DeliveryGuarantee guarantee) {
-        this.guarantee = guarantee;
+    @BeforeEach
+    public void setUp() {
+        metricListener = new MetricListener();
     }
 
-    @Test
-    public void testRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)
+    public void testRegisterMetrics(DeliveryGuarantee guarantee) throws Exception {
         try (final KafkaWriter<Integer> ignored =
-                createWriterWithConfiguration(getKafkaClientConfiguration())) {
+                createWriterWithConfiguration(getKafkaClientConfiguration(), guarantee)) {
             metricListener.getGauge(KAFKA_METRIC_WITH_GROUP_NAME);
         }
     }
 
-    @Test
-    public void testNotRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)
+    public void testNotRegisterMetrics(DeliveryGuarantee guarantee) throws Exception {
         final Properties config = getKafkaClientConfiguration();
         config.put("flink.disable-metrics", "true");
-        try (final KafkaWriter<Integer> ignored = createWriterWithConfiguration(config)) {
+        try (final KafkaWriter<Integer> ignored =
+                createWriterWithConfiguration(config, guarantee)) {
             assertThrows(
                     IllegalArgumentException.class,
                     () -> metricListener.getGauge(KAFKA_METRIC_WITH_GROUP_NAME));
         }
     }
 
-    private KafkaWriter<Integer> createWriterWithConfiguration(Properties config) {
+    @Test
+    public void testIncreasingByteOutCounter() throws Exception {
+        final OperatorIOMetricGroup operatorIOMetricGroup = new NumBytesOutOperatorIOMetricGroup();
+        final InternalSinkWriterMetricGroup metricGroup =
+                InternalSinkWriterMetricGroup.mock(
+                        metricListener.getMetricGroup(), operatorIOMetricGroup);
+        try (final KafkaWriter<Integer> writer =
+                createWriterWithConfiguration(
+                        getKafkaClientConfiguration(), DeliveryGuarantee.NONE, metricGroup)) {
+            final Counter numBytesOut = operatorIOMetricGroup.getNumBytesOutCounter();
+            Assertions.assertEquals(numBytesOut.getCount(), 0L);
+            writer.write(1, SINK_WRITER_CONTEXT);
+            metricListener.getGauge("currentSendTime");
+            MatcherAssert.assertThat(numBytesOut.getCount(), greaterThan(0L));
+        }
+    }
+
+    @Test
+    public void testCurrentSendTimeMetric() throws Exception {
+        final OperatorIOMetricGroup operatorIOMetricGroup = new NumBytesOutOperatorIOMetricGroup();
+        final InternalSinkWriterMetricGroup metricGroup =
+                InternalSinkWriterMetricGroup.mock(
+                        metricListener.getMetricGroup(), operatorIOMetricGroup);
+        try (final KafkaWriter<Integer> writer =
+                createWriterWithConfiguration(
+                        getKafkaClientConfiguration(),
+                        DeliveryGuarantee.AT_LEAST_ONCE,
+                        metricGroup)) {
+            final Gauge<Long> currentSendTime = metricListener.getGauge("currentSendTime");
+            Assertions.assertEquals(currentSendTime.getValue(), 0L);
+            IntStream.range(0, 100)
+                    .forEach(
+                            (run) -> {
+                                try {
+                                    writer.write(1, SINK_WRITER_CONTEXT);
+                                    // Manually flush the records to generate a sendTime
+                                    if (run % 10 == 0) {
+                                        writer.prepareCommit(false);

Review comment:
       I am very hesitant to use `@VisibleForTesting` because it always feels to me like proof of bad design if something cannot be tested without it. In this case, I'd refrain from adding it. WDYT?




-- 
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] PatrickRen commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
PatrickRen commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692235797



##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/MetricUtil.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.connector.kafka;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Counter;
+
+import org.apache.kafka.common.Metric;
+import org.apache.kafka.common.MetricName;
+
+import java.util.Map;
+
+/** Collection of methods to interact with Kafka's client metric system. */
+@Internal
+public class MetricUtil {
+
+    /**
+     * Tries to find the the Kafka {@link Metric} in the provided metrics.
+     *
+     * @return {@link Metric} which exposes continuous updates
+     * @throws IllegalStateException if the metric is not part of the provided metrics
+     */
+    public static Metric getKafkaMetric(
+            Map<MetricName, ? extends Metric> metrics, String metricGroup, String metricName) {

Review comment:
       What about having another ```Predicate<Metric>``` as an argument? There might be multiple metrics with the same group and metric name but different in tags, such as "records-lag". The predicate can let user to filter the specific metric they want. 




-- 
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 edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] flinkbot edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   * 23b1d535a1ebe8962f5129cf4c125bd94dc3de36 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] AHeise commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
AHeise commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692777039



##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -32,40 +40,44 @@
 
 import org.apache.kafka.clients.producer.ProducerRecord;
 import org.apache.kafka.common.serialization.ByteArraySerializer;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import org.hamcrest.MatcherAssert;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testcontainers.containers.KafkaContainer;
 import org.testcontainers.containers.Network;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.utility.DockerImageName;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.time.Duration;
-import java.util.List;
+import java.util.Map;
 import java.util.Properties;
+import java.util.stream.IntStream;
 
+import static org.hamcrest.Matchers.greaterThan;
 import static org.junit.Assert.assertThrows;
 
 /** Tests for the standalone KafkaWriter. */
-@RunWith(Parameterized.class)
 public class KafkaWriterITCase {
 
     private static final Logger LOG = LoggerFactory.getLogger(KafkaWriterITCase.class);
     private static final Slf4jLogConsumer LOG_CONSUMER = new Slf4jLogConsumer(LOG);
     private static final String INTER_CONTAINER_KAFKA_ALIAS = "kafka";
     private static final Network NETWORK = Network.newNetwork();
     private static final String KAFKA_METRIC_WITH_GROUP_NAME = "KafkaProducer.incoming-byte-total";
-
-    private final DeliveryGuarantee guarantee;
+    private static final SinkWriter.Context SINK_WRITER_CONTEXT = new DummySinkWriterContext();
 
     private MetricListener metricListener;
 
-    @ClassRule
-    public static final KafkaContainer KAFKA_CONTAINER =
+    private static final KafkaContainer KAFKA_CONTAINER =

Review comment:
       Okay sorry didn't see that. Then of course, leave as is.




-- 
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] fapaul commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
fapaul commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692797008



##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/MetricUtil.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.connector.kafka;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Counter;
+
+import org.apache.kafka.common.Metric;
+import org.apache.kafka.common.MetricName;
+
+import java.util.Map;
+
+/** Collection of methods to interact with Kafka's client metric system. */
+@Internal
+public class MetricUtil {
+
+    /**
+     * Tries to find the the Kafka {@link Metric} in the provided metrics.
+     *
+     * @return {@link Metric} which exposes continuous updates
+     * @throws IllegalStateException if the metric is not part of the provided metrics
+     */
+    public static Metric getKafkaMetric(
+            Map<MetricName, ? extends Metric> metrics, String metricGroup, String metricName) {

Review comment:
       That sounds like a good idea 👍  I've added another util to filter with a given Predicate.




-- 
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 edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] AHeise merged pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
AHeise merged pull request #16875:
URL: https://github.com/apache/flink/pull/16875


   


-- 
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] AHeise commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
AHeise commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692825902



##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/sink/KafkaWriter.java
##########
@@ -389,4 +409,28 @@ private void initMetrics(FlinkKafkaInternalProducer<byte[], byte[]> producer) {
             return Collections.emptyList();
         }
     }
+
+    private static long computeSendTime(Producer<?, ?> producer) {
+        final Metric sendTime =
+                MetricUtil.getKafkaMetric(
+                        producer.metrics(), "producer-metrics", "request-latency-avg");
+        final Metric queueTime =
+                MetricUtil.getKafkaMetric(
+                        producer.metrics(), "producer-metrics", "record-queue-time-avg");
+        return ((Number) sendTime.metricValue()).longValue()
+                + ((Number) queueTime.metricValue()).longValue();
+    }
+
+    private void registerMetricSync() {
+        if (closed) {
+            return;
+        }
+        timeService.registerProcessingTimer(
+                lastSync + METRIC_UPDATE_INTERVAL_MILLIS,
+                (time) -> {
+                    MetricUtil.sync(byteOutMetric, numBytesOutCounter);
+                    lastSync = time;
+                    registerMetricSync();
+                });

Review comment:
       To avoid reading the Kafka metric after producer is closed (not sure what the semantics is here)
   
   ```suggestion
         
           timeService.registerProcessingTimer(
                   lastSync + METRIC_UPDATE_INTERVAL_MILLIS,
                   (time) -> {
                     if (!closed) {
                       MetricUtil.sync(byteOutMetric, numBytesOutCounter);
                       lastSync = time;
                       registerMetricSync();
                      }
                   });
   ```
   
   It might be easier to just use `System.currentTimeMillis()` instead of `lastSync` to not trigger stuff to often in case of overload? But not sure.




-- 
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] fapaul commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
fapaul commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692797381



##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/sink/KafkaWriter.java
##########
@@ -139,6 +147,7 @@ public void write(IN element, Context context) throws IOException {
                 recordSerializer.serialize(element, kafkaSinkContext, context.timestamp());
         pendingRecords.incrementAndGet();
         currentProducer.send(record, deliveryCallback);
+        MetricUtil.sync(byteOutMetric, numBytesOutCounter);

Review comment:
       I registered a timer to update the metrics every 500ms, let me know if this is a reasonable time.




-- 
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 #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-900966486


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit 5181be4fa5157535ebc1277fda6a97cc36d60c53 (Wed Aug 18 09:32:20 UTC 2021)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </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] fapaul commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
fapaul commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692878739



##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/sink/KafkaWriter.java
##########
@@ -389,4 +409,28 @@ private void initMetrics(FlinkKafkaInternalProducer<byte[], byte[]> producer) {
             return Collections.emptyList();
         }
     }
+
+    private static long computeSendTime(Producer<?, ?> producer) {
+        final Metric sendTime =
+                MetricUtil.getKafkaMetric(
+                        producer.metrics(), "producer-metrics", "request-latency-avg");
+        final Metric queueTime =
+                MetricUtil.getKafkaMetric(
+                        producer.metrics(), "producer-metrics", "record-queue-time-avg");
+        return ((Number) sendTime.metricValue()).longValue()
+                + ((Number) queueTime.metricValue()).longValue();
+    }
+
+    private void registerMetricSync() {
+        if (closed) {
+            return;
+        }
+        timeService.registerProcessingTimer(
+                lastSync + METRIC_UPDATE_INTERVAL_MILLIS,
+                (time) -> {
+                    MetricUtil.sync(byteOutMetric, numBytesOutCounter);
+                    lastSync = time;
+                    registerMetricSync();
+                });

Review comment:
       > It might be easier to just use `System.currentTimeMillis()` instead of `lastSync` to not trigger stuff to often in case of overload? But not sure.
   
   I am not sure what the best way here is. With your suggestion we need to call `System.currentTimeMillis()` for every registration. Currently is only needed once.
   
   




-- 
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 edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   * 23b1d535a1ebe8962f5129cf4c125bd94dc3de36 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542) 
   * 6ae373c946982c8bf129722453a6105cc710a38e UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] flinkbot edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "status" : "CANCELED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547",
       "triggerID" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "triggerType" : "PUSH"
     }, {
       "hash" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22558",
       "triggerID" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 6ae373c946982c8bf129722453a6105cc710a38e Azure: [CANCELED](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547) 
   * 90e1347327b6610de7379a987a7c36554acb1d80 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22558) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] flinkbot edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303






-- 
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 #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] AHeise commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
AHeise commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692810598



##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -82,49 +94,107 @@
                     .withLogConsumer(LOG_CONSUMER)
                     .withNetworkAliases(INTER_CONTAINER_KAFKA_ALIAS);
 
-    @Before
-    public void setUp() {
-        metricListener = new MetricListener();
+    @BeforeAll
+    public static void beforeAll() {
+        KAFKA_CONTAINER.start();
     }
 
-    @Parameterized.Parameters
-    public static List<DeliveryGuarantee> guarantees() {
-        return ImmutableList.of(
-                DeliveryGuarantee.NONE,
-                DeliveryGuarantee.AT_LEAST_ONCE,
-                DeliveryGuarantee.EXACTLY_ONCE);
+    @AfterAll
+    public static void afterAll() {
+        KAFKA_CONTAINER.stop();
     }
 
-    public KafkaWriterITCase(DeliveryGuarantee guarantee) {
-        this.guarantee = guarantee;
+    @BeforeEach
+    public void setUp() {
+        metricListener = new MetricListener();
     }
 
-    @Test
-    public void testRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)
+    public void testRegisterMetrics(DeliveryGuarantee guarantee) throws Exception {
         try (final KafkaWriter<Integer> ignored =
-                createWriterWithConfiguration(getKafkaClientConfiguration())) {
+                createWriterWithConfiguration(getKafkaClientConfiguration(), guarantee)) {
             metricListener.getGauge(KAFKA_METRIC_WITH_GROUP_NAME);
         }
     }
 
-    @Test
-    public void testNotRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)
+    public void testNotRegisterMetrics(DeliveryGuarantee guarantee) throws Exception {
         final Properties config = getKafkaClientConfiguration();
         config.put("flink.disable-metrics", "true");
-        try (final KafkaWriter<Integer> ignored = createWriterWithConfiguration(config)) {
+        try (final KafkaWriter<Integer> ignored =
+                createWriterWithConfiguration(config, guarantee)) {
             assertThrows(
                     IllegalArgumentException.class,
                     () -> metricListener.getGauge(KAFKA_METRIC_WITH_GROUP_NAME));
         }
     }
 
-    private KafkaWriter<Integer> createWriterWithConfiguration(Properties config) {
+    @Test
+    public void testIncreasingByteOutCounter() throws Exception {
+        final OperatorIOMetricGroup operatorIOMetricGroup = new NumBytesOutOperatorIOMetricGroup();
+        final InternalSinkWriterMetricGroup metricGroup =
+                InternalSinkWriterMetricGroup.mock(
+                        metricListener.getMetricGroup(), operatorIOMetricGroup);
+        try (final KafkaWriter<Integer> writer =
+                createWriterWithConfiguration(
+                        getKafkaClientConfiguration(), DeliveryGuarantee.NONE, metricGroup)) {
+            final Counter numBytesOut = operatorIOMetricGroup.getNumBytesOutCounter();
+            Assertions.assertEquals(numBytesOut.getCount(), 0L);
+            writer.write(1, SINK_WRITER_CONTEXT);
+            metricListener.getGauge("currentSendTime");
+            MatcherAssert.assertThat(numBytesOut.getCount(), greaterThan(0L));
+        }
+    }
+
+    @Test
+    public void testCurrentSendTimeMetric() throws Exception {
+        final OperatorIOMetricGroup operatorIOMetricGroup = new NumBytesOutOperatorIOMetricGroup();
+        final InternalSinkWriterMetricGroup metricGroup =
+                InternalSinkWriterMetricGroup.mock(
+                        metricListener.getMetricGroup(), operatorIOMetricGroup);
+        try (final KafkaWriter<Integer> writer =
+                createWriterWithConfiguration(
+                        getKafkaClientConfiguration(),
+                        DeliveryGuarantee.AT_LEAST_ONCE,
+                        metricGroup)) {
+            final Gauge<Long> currentSendTime = metricListener.getGauge("currentSendTime");
+            Assertions.assertEquals(currentSendTime.getValue(), 0L);
+            IntStream.range(0, 100)
+                    .forEach(
+                            (run) -> {
+                                try {
+                                    writer.write(1, SINK_WRITER_CONTEXT);
+                                    // Manually flush the records to generate a sendTime
+                                    if (run % 10 == 0) {
+                                        writer.prepareCommit(false);

Review comment:
       I'm fine with leaving as is.




-- 
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 #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-900966486






-- 
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 edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547",
       "triggerID" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 5181be4fa5157535ebc1277fda6a97cc36d60c53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439) 
   * 23b1d535a1ebe8962f5129cf4c125bd94dc3de36 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542) 
   * 6ae373c946982c8bf129722453a6105cc710a38e Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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] AHeise commented on a change in pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
AHeise commented on a change in pull request #16875:
URL: https://github.com/apache/flink/pull/16875#discussion_r692752256



##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -82,49 +94,107 @@
                     .withLogConsumer(LOG_CONSUMER)
                     .withNetworkAliases(INTER_CONTAINER_KAFKA_ALIAS);
 
-    @Before
-    public void setUp() {
-        metricListener = new MetricListener();
+    @BeforeAll
+    public static void beforeAll() {
+        KAFKA_CONTAINER.start();
     }
 
-    @Parameterized.Parameters
-    public static List<DeliveryGuarantee> guarantees() {
-        return ImmutableList.of(
-                DeliveryGuarantee.NONE,
-                DeliveryGuarantee.AT_LEAST_ONCE,
-                DeliveryGuarantee.EXACTLY_ONCE);
+    @AfterAll
+    public static void afterAll() {
+        KAFKA_CONTAINER.stop();
     }
 
-    public KafkaWriterITCase(DeliveryGuarantee guarantee) {
-        this.guarantee = guarantee;
+    @BeforeEach
+    public void setUp() {
+        metricListener = new MetricListener();
     }
 
-    @Test
-    public void testRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)

Review comment:
       👍 

##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -200,4 +270,96 @@ public UserCodeClassLoader getUserCodeClassLoader() {
             throw new UnsupportedOperationException("Not implemented.");
         }
     }
+
+    private static class DummySinkWriterContext implements SinkWriter.Context {
+        @Override
+        public long currentWatermark() {
+            return 0;
+        }
+
+        @Override
+        public Long timestamp() {
+            return null;
+        }
+    }
+
+    private static class NumBytesOutOperatorIOMetricGroup implements OperatorIOMetricGroup {

Review comment:
       Could you please check if you can use `UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup().getIOMetricGroup()` instead? For another idea, see below.

##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -32,40 +40,44 @@
 
 import org.apache.kafka.clients.producer.ProducerRecord;
 import org.apache.kafka.common.serialization.ByteArraySerializer;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import org.hamcrest.MatcherAssert;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testcontainers.containers.KafkaContainer;
 import org.testcontainers.containers.Network;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.utility.DockerImageName;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.time.Duration;
-import java.util.List;
+import java.util.Map;
 import java.util.Properties;
+import java.util.stream.IntStream;
 
+import static org.hamcrest.Matchers.greaterThan;
 import static org.junit.Assert.assertThrows;
 
 /** Tests for the standalone KafkaWriter. */
-@RunWith(Parameterized.class)
 public class KafkaWriterITCase {
 
     private static final Logger LOG = LoggerFactory.getLogger(KafkaWriterITCase.class);
     private static final Slf4jLogConsumer LOG_CONSUMER = new Slf4jLogConsumer(LOG);
     private static final String INTER_CONTAINER_KAFKA_ALIAS = "kafka";
     private static final Network NETWORK = Network.newNetwork();
     private static final String KAFKA_METRIC_WITH_GROUP_NAME = "KafkaProducer.incoming-byte-total";
-
-    private final DeliveryGuarantee guarantee;
+    private static final SinkWriter.Context SINK_WRITER_CONTEXT = new DummySinkWriterContext();
 
     private MetricListener metricListener;
 
-    @ClassRule
-    public static final KafkaContainer KAFKA_CONTAINER =
+    private static final KafkaContainer KAFKA_CONTAINER =

Review comment:
       Add `@Nested` test? Then you don't need to spawn container when running a test in IDE that doesn't need it.

##########
File path: flink-connectors/flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/sink/KafkaWriterITCase.java
##########
@@ -82,49 +94,107 @@
                     .withLogConsumer(LOG_CONSUMER)
                     .withNetworkAliases(INTER_CONTAINER_KAFKA_ALIAS);
 
-    @Before
-    public void setUp() {
-        metricListener = new MetricListener();
+    @BeforeAll
+    public static void beforeAll() {
+        KAFKA_CONTAINER.start();
     }
 
-    @Parameterized.Parameters
-    public static List<DeliveryGuarantee> guarantees() {
-        return ImmutableList.of(
-                DeliveryGuarantee.NONE,
-                DeliveryGuarantee.AT_LEAST_ONCE,
-                DeliveryGuarantee.EXACTLY_ONCE);
+    @AfterAll
+    public static void afterAll() {
+        KAFKA_CONTAINER.stop();
     }
 
-    public KafkaWriterITCase(DeliveryGuarantee guarantee) {
-        this.guarantee = guarantee;
+    @BeforeEach
+    public void setUp() {
+        metricListener = new MetricListener();
     }
 
-    @Test
-    public void testRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)
+    public void testRegisterMetrics(DeliveryGuarantee guarantee) throws Exception {
         try (final KafkaWriter<Integer> ignored =
-                createWriterWithConfiguration(getKafkaClientConfiguration())) {
+                createWriterWithConfiguration(getKafkaClientConfiguration(), guarantee)) {
             metricListener.getGauge(KAFKA_METRIC_WITH_GROUP_NAME);
         }
     }
 
-    @Test
-    public void testNotRegisterMetrics() throws Exception {
+    @ParameterizedTest
+    @EnumSource(DeliveryGuarantee.class)
+    public void testNotRegisterMetrics(DeliveryGuarantee guarantee) throws Exception {
         final Properties config = getKafkaClientConfiguration();
         config.put("flink.disable-metrics", "true");
-        try (final KafkaWriter<Integer> ignored = createWriterWithConfiguration(config)) {
+        try (final KafkaWriter<Integer> ignored =
+                createWriterWithConfiguration(config, guarantee)) {
             assertThrows(
                     IllegalArgumentException.class,
                     () -> metricListener.getGauge(KAFKA_METRIC_WITH_GROUP_NAME));
         }
     }
 
-    private KafkaWriter<Integer> createWriterWithConfiguration(Properties config) {
+    @Test
+    public void testIncreasingByteOutCounter() throws Exception {
+        final OperatorIOMetricGroup operatorIOMetricGroup = new NumBytesOutOperatorIOMetricGroup();
+        final InternalSinkWriterMetricGroup metricGroup =
+                InternalSinkWriterMetricGroup.mock(
+                        metricListener.getMetricGroup(), operatorIOMetricGroup);
+        try (final KafkaWriter<Integer> writer =
+                createWriterWithConfiguration(
+                        getKafkaClientConfiguration(), DeliveryGuarantee.NONE, metricGroup)) {
+            final Counter numBytesOut = operatorIOMetricGroup.getNumBytesOutCounter();
+            Assertions.assertEquals(numBytesOut.getCount(), 0L);
+            writer.write(1, SINK_WRITER_CONTEXT);
+            metricListener.getGauge("currentSendTime");
+            MatcherAssert.assertThat(numBytesOut.getCount(), greaterThan(0L));
+        }
+    }
+
+    @Test
+    public void testCurrentSendTimeMetric() throws Exception {
+        final OperatorIOMetricGroup operatorIOMetricGroup = new NumBytesOutOperatorIOMetricGroup();
+        final InternalSinkWriterMetricGroup metricGroup =
+                InternalSinkWriterMetricGroup.mock(
+                        metricListener.getMetricGroup(), operatorIOMetricGroup);
+        try (final KafkaWriter<Integer> writer =
+                createWriterWithConfiguration(
+                        getKafkaClientConfiguration(),
+                        DeliveryGuarantee.AT_LEAST_ONCE,
+                        metricGroup)) {
+            final Gauge<Long> currentSendTime = metricListener.getGauge("currentSendTime");
+            Assertions.assertEquals(currentSendTime.getValue(), 0L);
+            IntStream.range(0, 100)
+                    .forEach(
+                            (run) -> {
+                                try {
+                                    writer.write(1, SINK_WRITER_CONTEXT);
+                                    // Manually flush the records to generate a sendTime
+                                    if (run % 10 == 0) {
+                                        writer.prepareCommit(false);

Review comment:
       How about calling a `VisibleForTesting` only flush method here?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/metrics/groups/InternalSinkWriterMetricGroup.java
##########
@@ -55,6 +55,12 @@ public static InternalSinkWriterMetricGroup mock(MetricGroup metricGroup) {
                 metricGroup, UnregisteredMetricsGroup.createOperatorIOMetricGroup());

Review comment:
       If we use `UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup().getIOMetricGroup()` here, maybe we can get rid of your new `mock`? Then we could just use `metricGroup#getIOMetricGroup` for assertions.

##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/sink/KafkaWriter.java
##########
@@ -139,6 +147,7 @@ public void write(IN element, Context context) throws IOException {
                 recordSerializer.serialize(element, kafkaSinkContext, context.timestamp());
         pendingRecords.incrementAndGet();
         currentProducer.send(record, deliveryCallback);
+        MetricUtil.sync(byteOutMetric, numBytesOutCounter);

Review comment:
       I just saw that `metricValue` is under lock, so this is probably too expensive for every record. How about adding a timerservice callback every second that syncs?
   Or we could use `Partitioner#newBatch` to sync.




-- 
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 edited a comment on pull request #16875: [FLINK-23838][connectors/kafka] Implement FLIP-33 metrics for KafkaSink

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #16875:
URL: https://github.com/apache/flink/pull/16875#issuecomment-901000303


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22439",
       "triggerID" : "5181be4fa5157535ebc1277fda6a97cc36d60c53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "status" : "CANCELED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542",
       "triggerID" : "23b1d535a1ebe8962f5129cf4c125bd94dc3de36",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547",
       "triggerID" : "6ae373c946982c8bf129722453a6105cc710a38e",
       "triggerType" : "PUSH"
     }, {
       "hash" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22558",
       "triggerID" : "90e1347327b6610de7379a987a7c36554acb1d80",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 23b1d535a1ebe8962f5129cf4c125bd94dc3de36 Azure: [CANCELED](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22542) 
   * 6ae373c946982c8bf129722453a6105cc710a38e Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22547) 
   * 90e1347327b6610de7379a987a7c36554acb1d80 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=22558) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@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