You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/09/07 21:47:08 UTC

[GitHub] [kafka] rgroothuijsen opened a new pull request, #12602: KAFKA-13985: Skip committing MirrorSourceTask records without metadata

rgroothuijsen opened a new pull request, #12602:
URL: https://github.com/apache/kafka/pull/12602

   This adds an additional check if there is any metadata before MirrorSourceTask commits a record, to avoid a potential null pointer exception. This can happen, for example, if the record was filtered out by some transformation. The NPE would immediately be caught even without this check, but catching it beforehand allows the error to be handled more gracefully.
   
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] rgroothuijsen commented on pull request #12602: KAFKA-13985: Skip committing MirrorSourceTask records without metadata

Posted by GitBox <gi...@apache.org>.
rgroothuijsen commented on PR #12602:
URL: https://github.com/apache/kafka/pull/12602#issuecomment-1239941258

   There are a couple of things I'm not certain about yet, the first being how much logging should be produced in this case, since the possibility of having null metadata appears to be a legitimate use case for dropped records, so it could be handled entirely silently but someone might still have some tracing for records that are being ignored.
   
   The second is trying to find a way to write a unit test for this one, since any NPE is caught within the same method and thus the test never produces an error. I considered checking committed offsets, but those appear to be mocked in the unit tests anyway.


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] rgroothuijsen commented on pull request #12602: KAFKA-13985: Skip committing MirrorSourceTask records without metadata

Posted by GitBox <gi...@apache.org>.
rgroothuijsen commented on PR #12602:
URL: https://github.com/apache/kafka/pull/12602#issuecomment-1242776194

   I think removing the duplicate try-catch does make sense. Any unexpected exceptions that appear here could be considered severe enough for `error` level logging, and it shouldn't affect anything else since `commitRecord` is only being called there.


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] C0urante merged pull request #12602: KAFKA-13985: Skip committing MirrorSourceTask records without metadata

Posted by GitBox <gi...@apache.org>.
C0urante merged PR #12602:
URL: https://github.com/apache/kafka/pull/12602


-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] C0urante commented on a diff in pull request #12602: KAFKA-13985: Skip committing MirrorSourceTask records without metadata

Posted by GitBox <gi...@apache.org>.
C0urante commented on code in PR #12602:
URL: https://github.com/apache/kafka/pull/12602#discussion_r969617914


##########
connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorSourceTaskTest.java:
##########
@@ -160,6 +160,33 @@ public void testPoll() {
         }
     }
 
+    @Test
+    public void testCommitRecordWithNullMetadata() {
+        // Create a consumer mock
+        byte[] key1 = "abc".getBytes();
+        byte[] value1 = "fgh".getBytes();
+        String topicName = "test";
+        String headerKey = "key";
+        RecordHeaders headers = new RecordHeaders(new Header[] {
+            new RecordHeader(headerKey, "value".getBytes()),
+        });
+
+        @SuppressWarnings("unchecked")
+        KafkaConsumer<byte[], byte[]> consumer = mock(KafkaConsumer.class);
+        MirrorMetrics metrics = mock(MirrorMetrics.class);
+
+        String sourceClusterName = "cluster1";
+        ReplicationPolicy replicationPolicy = new DefaultReplicationPolicy();
+        MirrorSourceTask mirrorSourceTask = new MirrorSourceTask(consumer, metrics, sourceClusterName,
+                replicationPolicy, 50);
+
+        SourceRecord sourceRecord = mirrorSourceTask.convertRecord(new ConsumerRecord<>(topicName, 0, 0, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, key1.length, value1.length, key1, value1, headers, Optional.empty()));
+
+        // Expect that commitRecord will not throw an exception
+        mirrorSourceTask.commitRecord(sourceRecord, null);

Review Comment:
   It's good that we're ensuring this code path doesn't lead to an exception, but it'd be nice if we could also ensure that we don't try to produce offsets in this case as well.
   
   Could we add an extra parameter to the testing-only constructor defined [here](https://github.com/apache/kafka/blob/3d2ac7cdbe89cdabfd95db9971de31878afa5498/connect/mirror/src/main/java/org/apache/kafka/connect/mirror/MirrorSourceTask.java#L70-L79) for the `offsetProducer` field, and then, using a mocked producer and [Mockito::verifyNoInteractions](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#verifyNoInteractions(java.lang.Object...)), check to make sure that the source task doesn't use that producer to try to send any offset sync records?



##########
connect/mirror/src/main/java/org/apache/kafka/connect/mirror/MirrorSourceTask.java:
##########
@@ -170,25 +170,25 @@ public List<SourceRecord> poll() {
  
     @Override
     public void commitRecord(SourceRecord record, RecordMetadata metadata) {
-        try {
-            if (stopping) {
-                return;
-            }
-            if (!metadata.hasOffset()) {
-                log.error("RecordMetadata has no offset -- can't sync offsets for {}.", record.topic());
-                return;
-            }
-            TopicPartition topicPartition = new TopicPartition(record.topic(), record.kafkaPartition());
-            long latency = System.currentTimeMillis() - record.timestamp();
-            metrics.countRecord(topicPartition);
-            metrics.replicationLatency(topicPartition, latency);
-            TopicPartition sourceTopicPartition = MirrorUtils.unwrapPartition(record.sourcePartition());
-            long upstreamOffset = MirrorUtils.unwrapOffset(record.sourceOffset());
-            long downstreamOffset = metadata.offset();
-            maybeSyncOffsets(sourceTopicPartition, upstreamOffset, downstreamOffset);
-        } catch (Throwable e) {
-            log.warn("Failure committing record.", e);
+        if (stopping) {
+            return;
+        }
+        if (metadata == null) {
+            log.debug("No RecordMetadata -- can't sync offsets for {}.", record.topic());

Review Comment:
   Can we note here the likely cause of this scenario? Thinking something like this:
   
   ```suggestion
               log.debug("No RecordMetadata (source record was probably filtered out during transformation) -- can't sync offsets for {}.", record.topic());
   ```



-- 
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: jira-unsubscribe@kafka.apache.org

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


[GitHub] [kafka] C0urante commented on pull request #12602: KAFKA-13985: Skip committing MirrorSourceTask records without metadata

Posted by GitBox <gi...@apache.org>.
C0urante commented on PR #12602:
URL: https://github.com/apache/kafka/pull/12602#issuecomment-1240821236

   Thanks @rgroothuijsen! I agree with the `DEBUG` level for logging; it's tempting to make this silent, but I'd prefer to err on the side of giving users more information since we can't add logging to releases once they've gone out.
   
   RE testing, there are a few approaches I can think of:
   
   0. Simply remove the try/catch block in `MirrorSourceTask::commitRecord`, since it essentially duplicates [logic in the Connect framework](https://github.com/apache/kafka/blob/0c97be53fa7e1e0720f2086b5d9d80ffcc1db470/connect/runtime/src/main/java/org/apache/kafka/connect/runtime/AbstractWorkerSourceTask.java#L544-L548) with the small change of using the `WARN` level for logging instead of `ERROR`
   0. If we'd prefer to keep the logging as-is, we can isolate all of the logic for the method except exception handling into a package-private `doCommitRecord` method, which is then wrapped by the public `commitRecord` method. I.e.:
   ```java
   @Override
   public void commitRecord(SourceRecord record, RecordMetadata metadata) {
       try {
           doCommitRecord(record, metadata);
       } catch (Throwable e) {
           log.warn("Failure committing record.", e);
       }
   }
   
   // Visible for testing
   void doCommitRecord(SourceRecord record, RecordMetadata metadata) {
       if (stopping) {
           return;
       }
       if (!metadata.hasOffset()) {
           log.error("RecordMetadata has no offset -- can't sync offsets for {}.", record.topic());
           return;
       }
       TopicPartition topicPartition = new TopicPartition(record.topic(), record.kafkaPartition());
       long latency = System.currentTimeMillis() - record.timestamp();
       metrics.countRecord(topicPartition);
       metrics.replicationLatency(topicPartition, latency);
       TopicPartition sourceTopicPartition = MirrorUtils.unwrapPartition(record.sourcePartition());
       long upstreamOffset = MirrorUtils.unwrapOffset(record.sourceOffset());
       long downstreamOffset = metadata.offset();
       maybeSyncOffsets(sourceTopicPartition, upstreamOffset, downstreamOffset);
   }
   ```


-- 
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: jira-unsubscribe@kafka.apache.org

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