chia7712 commented on a change in pull request #9401:
URL: https://github.com/apache/kafka/pull/9401#discussion_r521931887



##########
File path: 
clients/src/main/java/org/apache/kafka/common/requests/ProduceRequest.java
##########
@@ -323,27 +222,30 @@ public String toString(boolean verbose) {
     @Override
     public ProduceResponse getErrorResponse(int throttleTimeMs, Throwable e) {
         /* In case the producer doesn't actually want any response */
-        if (acks == 0)
-            return null;
-
+        if (acks == 0) return null;
         Errors error = Errors.forException(e);
-        Map<TopicPartition, ProduceResponse.PartitionResponse> responseMap = 
new HashMap<>();
-        ProduceResponse.PartitionResponse partitionResponse = new 
ProduceResponse.PartitionResponse(error);
-
-        for (TopicPartition tp : partitions())
-            responseMap.put(tp, partitionResponse);
-
-        return new ProduceResponse(responseMap, throttleTimeMs);
+        return new ProduceResponse(new ProduceResponseData()
+            
.setResponses(partitionSizes().keySet().stream().collect(Collectors.groupingBy(TopicPartition::topic)).entrySet()
+                .stream()
+                .map(entry -> new ProduceResponseData.TopicProduceResponse()
+                    .setPartitionResponses(entry.getValue().stream().map(p -> 
new ProduceResponseData.PartitionProduceResponse()
+                        .setIndex(p.partition())
+                        .setRecordErrors(Collections.emptyList())
+                        .setBaseOffset(INVALID_OFFSET)
+                        .setLogAppendTimeMs(RecordBatch.NO_TIMESTAMP)
+                        .setLogStartOffset(INVALID_OFFSET)
+                        .setErrorMessage(e.getMessage())
+                        .setErrorCode(error.code()))
+                        .collect(Collectors.toList()))
+                    .setName(entry.getKey()))
+                .collect(Collectors.toList()))
+            .setThrottleTimeMs(throttleTimeMs));

Review comment:
       I used  ```data``` to generate ProduceResponseData. However, the 
```data``` may be null when create ProduceResponseData. That is to say, it 
require if-else to handle null ```data``` in ```getErrorResponse```. It seems 
to me that is a bit ugly so not sure whether it is worth doing that. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

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


Reply via email to