showuon commented on code in PR #13760: URL: https://github.com/apache/kafka/pull/13760#discussion_r1229107591
########## clients/src/test/java/org/apache/kafka/clients/admin/KafkaAdminClientTest.java: ########## @@ -2359,31 +2334,22 @@ public void testDeleteRecords() throws Exception { assertTrue(e0.getCause() instanceof OffsetOutOfRangeException); } - // "leader not available" failure on metadata request for partition 2 + // not authorized to delete records for partition 2 KafkaFuture<DeletedRecords> myTopicPartition2Result = values.get(myTopicPartition2); try { myTopicPartition2Result.get(); fail("get() should throw ExecutionException"); } catch (ExecutionException e1) { - assertTrue(e1.getCause() instanceof LeaderNotAvailableException); + assertTrue(e1.getCause() instanceof TopicAuthorizationException); } Review Comment: This can be replaced with `assertThrows(TopicAuthorizationException.class, () ->myTopicPartition2Result.get(), "get() should throw ExecutionException" )` Also, Should we change the comment into `TopicAuthorizationException`? It doesn't throw `ExecutionException` at all. Same comment applies to other places. ########## clients/src/test/java/org/apache/kafka/clients/admin/internals/DeleteRecordsHandlerTest.java: ########## @@ -0,0 +1,209 @@ +/* + * 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.clients.admin.internals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.kafka.clients.admin.DeletedRecords; +import org.apache.kafka.clients.admin.RecordsToDelete; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.message.DeleteRecordsRequestData; +import org.apache.kafka.common.message.DeleteRecordsResponseData; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.DeleteRecordsRequest; +import org.apache.kafka.common.requests.DeleteRecordsResponse; +import org.apache.kafka.common.utils.LogContext; +import org.junit.jupiter.api.Test; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; +import static java.util.Collections.singleton; + +import static org.apache.kafka.common.utils.Utils.mkSet; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DeleteRecordsHandlerTest { + private final LogContext logContext = new LogContext(); + private final TopicPartition t0p0 = new TopicPartition("t0", 0); + private final TopicPartition t0p1 = new TopicPartition("t0", 1); + private final Node node = new Node(1, "host", 1234); + private final Map<TopicPartition, RecordsToDelete> recordsToDelete = new HashMap<TopicPartition, RecordsToDelete>() { + { + put(t0p0, RecordsToDelete.beforeOffset(10L)); + put(t0p1, RecordsToDelete.beforeOffset(10L)); + } + }; + + @Test + public void testBuildRequestSimple() { + DeleteRecordsHandler handler = new DeleteRecordsHandler(recordsToDelete, logContext); + DeleteRecordsRequest request = handler.buildBatchedRequest(node.id(), mkSet(t0p0, t0p1)).build(); + List<DeleteRecordsRequestData.DeleteRecordsTopic> topicPartitions = request.data().topics(); + assertEquals(1, topicPartitions.size()); + DeleteRecordsRequestData.DeleteRecordsTopic topic = topicPartitions.get(0); + assertEquals(2, topic.partitions().size()); + } + + @Test + public void testHandleSuccessfulResponse() { + AdminApiHandler.ApiResult<TopicPartition, DeletedRecords> result = + handleResponse(createResponse(emptyMap(), recordsToDelete.keySet())); + assertResult(result, recordsToDelete.keySet(), emptyMap(), emptyList(), emptySet()); + } + + @Test + public void testHandleRetriablePartitionTimeoutResponse() { + TopicPartition errorPartition = t0p0; + Map<TopicPartition, Short> errorsByPartition = new HashMap<>(); + errorsByPartition.put(errorPartition, Errors.REQUEST_TIMED_OUT.code()); + + AdminApiHandler.ApiResult<TopicPartition, DeletedRecords> result = + handleResponse(createResponse(errorsByPartition)); + + // Timeouts should be retried within the fulfillment stage as they are a common type of + // retriable error. + Set<TopicPartition> retriable = singleton(errorPartition); + Set<TopicPartition> completed = new HashSet<>(recordsToDelete.keySet()); + completed.removeAll(retriable); + assertResult(result, completed, emptyMap(), emptyList(), retriable); + } + + @Test + public void testHandleLookupRetriablePartitionInvalidMetadataResponse() { + TopicPartition errorPartition = t0p0; + Errors error = Errors.NOT_LEADER_OR_FOLLOWER; + Map<TopicPartition, Short> errorsByPartition = new HashMap<>(); + errorsByPartition.put(errorPartition, error.code()); + + AdminApiHandler.ApiResult<TopicPartition, DeletedRecords> result = + handleResponse(createResponse(errorsByPartition)); + + // Some invalid metadata errors should be retried from the lookup stage as the partition-to-leader + // mappings should be recalculated. + List<TopicPartition> unmapped = new ArrayList<>(); + unmapped.add(errorPartition); + Set<TopicPartition> completed = new HashSet<>(recordsToDelete.keySet()); + completed.removeAll(unmapped); + assertResult(result, completed, emptyMap(), unmapped, emptySet()); + } + + @Test + public void testHandlePartitionErrorResponse() { + TopicPartition errorPartition = t0p0; + Errors error = Errors.TOPIC_AUTHORIZATION_FAILED; + Map<TopicPartition, Short> errorsByPartition = new HashMap<>(); + errorsByPartition.put(errorPartition, error.code()); + + AdminApiHandler.ApiResult<TopicPartition, DeletedRecords> result = + handleResponse(createResponse(errorsByPartition)); + + Map<TopicPartition, Throwable> failed = new HashMap<>(); + failed.put(errorPartition, error.exception()); + Set<TopicPartition> completed = new HashSet<>(recordsToDelete.keySet()); + completed.removeAll(failed.keySet()); + assertResult(result, completed, failed, emptyList(), emptySet()); Review Comment: Could we add a test to verify a mixed case? I.e. a test case contains completed, failed, unmapped, and retriable. -- 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