jsancio commented on code in PR #15007:
URL: https://github.com/apache/kafka/pull/15007#discussion_r1428347367


##########
metadata/src/main/java/org/apache/kafka/metadata/migration/BufferingBatchConsumer.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.kafka.metadata.migration;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
+/**
+ * A record batch consumer that merges incoming batches into batches of a 
minimum a given size. It does so
+ * by buffering the records into an array that is later flushed to a 
downstream consumer. Batches consumed
+ * by this class will not be broken apart, only combined with other batches to 
reach the minimum batch size.
+ * </p>
+ * Note that {@link #flush()} must be called after the last batch has been 
accepted in order to flush any
+ * buffered records.
+ */
+public class BufferingBatchConsumer<T> implements Consumer<List<T>> {
+
+    private final Consumer<List<T>> delegateConsumer;
+    private final List<T> bufferedBatch;
+    private final int minBatchSize;
+
+    BufferingBatchConsumer(Consumer<List<T>> delegateConsumer, int 
minBatchSize) {
+        this.delegateConsumer = delegateConsumer;
+        this.bufferedBatch = new ArrayList<>(minBatchSize);
+        this.minBatchSize = minBatchSize;
+    }
+
+    @Override
+    public void accept(List<T> batch) {
+        bufferedBatch.addAll(batch);
+        if (bufferedBatch.size() >= minBatchSize) {
+            delegateConsumer.accept(new ArrayList<>(bufferedBatch));
+            bufferedBatch.clear();

Review Comment:
   How bout this implication?
   ```java
               delegateConsumer.accept(bufferedBatch);
               bufferedBatch = new ArrayList<>(minBatchSize);
   ```
   
   Similar in `flush`. There seems to be some code duplication between these 
two methods.



##########
metadata/src/main/java/org/apache/kafka/metadata/migration/KRaftMigrationDriver.java:
##########
@@ -645,6 +648,29 @@ public void run() throws Exception {
         }
     }
 
+    private BufferingBatchConsumer<ApiMessageAndVersion> 
buildMigrationBatchConsumer(
+        MigrationManifest.Builder manifestBuilder
+    ) {
+        return new BufferingBatchConsumer<>(batch -> {
+            try {
+                if (log.isTraceEnabled()) {
+                    batch.forEach(apiMessageAndVersion ->
+                        
log.trace(recordRedactor.toLoggableString(apiMessageAndVersion.message())));
+                }
+                CompletableFuture<?> future = 
zkRecordConsumer.acceptBatch(batch);
+                long batchStart = time.nanoseconds();
+                FutureUtils.waitWithLogging(KRaftMigrationDriver.this.log, "",

Review Comment:
   That's fair. I keep forgetting that we still need to support Java 8. Looking 
forward to 4.x.



##########
metadata/src/main/java/org/apache/kafka/metadata/migration/MigrationManifest.java:
##########
@@ -41,15 +41,17 @@ public static class Builder {
         private final Map<MetadataRecordType, Integer> counts = new 
HashMap<>();
         private int batches = 0;
         private int total = 0;
+        private long batchDurationsNs = 0;
         private long endTimeNanos = 0;
 
         Builder(Time time) {
             this.time = time;
             this.startTimeNanos = time.nanoseconds();
         }
 
-        public void acceptBatch(List<ApiMessageAndVersion> recordBatch) {
+        public void acceptBatch(List<ApiMessageAndVersion> recordBatch, long 
durationNs) {
             batches++;
+            batchDurationsNs += durationNs;

Review Comment:
   Okay. This is measure how much time the ZK migration spent in the 
controller, writing and committing the batches to KRaft, right?



##########
metadata/src/main/java/org/apache/kafka/metadata/migration/KRaftMigrationDriver.java:
##########
@@ -645,6 +648,29 @@ public void run() throws Exception {
         }
     }
 
+    private BufferingBatchConsumer<ApiMessageAndVersion> 
buildMigrationBatchConsumer(
+        MigrationManifest.Builder manifestBuilder
+    ) {
+        return new BufferingBatchConsumer<>(batch -> {
+            try {
+                if (log.isTraceEnabled()) {
+                    batch.forEach(apiMessageAndVersion ->
+                        
log.trace(recordRedactor.toLoggableString(apiMessageAndVersion.message())));
+                }
+                CompletableFuture<?> future = 
zkRecordConsumer.acceptBatch(batch);
+                long batchStart = time.nanoseconds();
+                FutureUtils.waitWithLogging(KRaftMigrationDriver.this.log, "",

Review Comment:
   This is an existing issue but `Time.waitForFuture` doesn't look correct. It 
is comparing nano times. In the JVM you can't compare nano times because they 
can overflow. It is recommended to instead compare elapse time: 
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#nanoTime()
   
   This will cause this code to block forever when it overflows.



-- 
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-unsubscr...@kafka.apache.org

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

Reply via email to