chia7712 commented on code in PR #24065:
URL: https://github.com/apache/kafka/pull/24065#discussion_r4438286921


##########
clients/src/main/java/org/apache/kafka/clients/admin/Admin.java:
##########
@@ -704,6 +704,10 @@ default DeleteRecordsResult 
deleteRecords(Map<TopicPartition, RecordsToDelete> r
      * if the records of the partition can not be deleted due to the 
configured {@code cleanup.policy},
      * for example because the topic's {@code cleanup.policy} is {@code 
compact} and does not include
      * {@code delete}.</li>
+     * <li>{@link org.apache.kafka.common.errors.OffsetOutOfRangeException}
+     * if the given offset is negative and not {@code -1}, or is larger than 
the high watermark
+     * of the partition. An offset of {@code -1} is replaced with the high 
watermark. When this

Review Comment:
   RecordsToDelete already describes the valid values. Maybe Admin can 
reference it?
   
   ```java
   * <li>{@link org.apache.kafka.common.errors.OffsetOutOfRangeException}
   * if the given offset is out of range, for example larger than the high 
watermark
   * of the partition. See {@link RecordsToDelete#beforeOffset(long)} for valid 
values.</li>
   * ```



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/admin/DeleteRecordsTest.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.KafkaFuture;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.OffsetOutOfRangeException;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+import static org.apache.kafka.clients.ClientsTestUtils.sendRecords;
+import static org.apache.kafka.test.TestUtils.assertFutureThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+@ClusterTestDefaults(types = {Type.KRAFT})
+public class DeleteRecordsTest {
+    private static final String TOPIC = "topic";
+    private static final TopicPartition TOPIC_PARTITION = new 
TopicPartition(TOPIC, 0);
+    private static final int NUM_RECORDS = 10;
+
+    @ClusterTest
+    public void 
testDeleteRecordsWithOffsetLargerThanHighWatermark(ClusterInstance cluster) 
throws Exception {
+        cluster.createTopic(TOPIC, 1, (short) 1);
+        sendRecords(cluster, TOPIC_PARTITION, NUM_RECORDS);
+        try (Admin admin = cluster.admin()) {
+            assertFutureThrows(OffsetOutOfRangeException.class,
+                deleteRecords(admin, NUM_RECORDS + 1));
+            assertEquals(0L, earliestOffset(admin));
+        }
+    }
+
+    @ClusterTest
+    public void testDeleteRecordsWithOffsetSmallerThanMinusOne(ClusterInstance 
cluster) throws Exception {
+        cluster.createTopic(TOPIC, 1, (short) 1);
+        sendRecords(cluster, TOPIC_PARTITION, NUM_RECORDS);
+        try (Admin admin = cluster.admin()) {
+            assertFutureThrows(OffsetOutOfRangeException.class,
+                deleteRecords(admin, -2L));
+            assertEquals(0L, earliestOffset(admin));
+        }
+    }
+
+    @ClusterTest
+    public void testDeleteRecordsToHighWatermark(ClusterInstance cluster) 
throws Exception {
+        cluster.createTopic(TOPIC, 1, (short) 1);
+        sendRecords(cluster, TOPIC_PARTITION, NUM_RECORDS);
+        try (Admin admin = cluster.admin()) {
+            assertEquals(NUM_RECORDS, deleteRecords(admin, 
-1L).get().lowWatermark());

Review Comment:
   Should we also assert `assertEquals(NUM_RECORDS, earliestOffset(admin))` to 
make sure the records are actually deleted?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to