This is an automated email from the ASF dual-hosted git repository.
mchades pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new a25d7e7555 [#14576] fix(kafka): map invalid partition counts to
argument errors (#14577)
a25d7e7555 is described below
commit a25d7e7555b23dfda81f911923e5dc41a811457c
Author: mchades <[email protected]>
AuthorDate: Wed Nov 4 14:37:02 2026 +0800
[#14576] fix(kafka): map invalid partition counts to argument errors
(#14577)
### What changes were proposed in this pull request?
Map `InvalidPartitionsException` to `IllegalArgumentException` in Kafka
topic creation, preserving the Kafka error message and cause.
### Why are the changes needed?
Kafka rejects invalid partition counts such as `0` and `-2`, but the
generic exception wrapper currently makes REST return HTTP 500 instead
of 400.
Fixes #14576
### Does this PR introduce _any_ user-facing change?
Yes. Invalid partition counts return HTTP 400 instead of 500. The valid
`-1` broker-default behavior is preserved.
### How was this patch tested?
- An embedded Kafka regression test for invalid partition counts fails
before the fix.
- New tests cover `0` and `-2`, preservation of Kafka's error message
and cause, and `-1` resolving to the same partition count as an omitted
value.
- `./gradlew :catalogs:catalog-kafka:spotlessApply
:catalogs:catalog-kafka:check -PskipITs` passes: all 17 unit tests.
- `git diff --check` passes.
- Docker integration tests were not run.
---
.../catalog/kafka/KafkaCatalogOperations.java | 4 ++
.../catalog/kafka/TestKafkaCatalogOperations.java | 60 ++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git
a/catalogs/catalog-kafka/src/main/java/org/apache/gravitino/catalog/kafka/KafkaCatalogOperations.java
b/catalogs/catalog-kafka/src/main/java/org/apache/gravitino/catalog/kafka/KafkaCatalogOperations.java
index 85a5feaa29..f234d44fad 100644
---
a/catalogs/catalog-kafka/src/main/java/org/apache/gravitino/catalog/kafka/KafkaCatalogOperations.java
+++
b/catalogs/catalog-kafka/src/main/java/org/apache/gravitino/catalog/kafka/KafkaCatalogOperations.java
@@ -86,6 +86,7 @@ import org.apache.kafka.common.Uuid;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.config.ConfigResource;
import org.apache.kafka.common.errors.InvalidConfigurationException;
+import org.apache.kafka.common.errors.InvalidPartitionsException;
import org.apache.kafka.common.errors.InvalidReplicationFactorException;
import org.apache.kafka.common.errors.TopicExistsException;
import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
@@ -292,6 +293,9 @@ public class KafkaCatalogOperations implements
CatalogOperations, SupportsSchema
if (e.getCause() instanceof TopicExistsException) {
throw new TopicAlreadyExistsException(e, "Topic %s already exists",
ident);
+ } else if (e.getCause() instanceof InvalidPartitionsException) {
+ throw ExceptionMessages.illegalArgument("Invalid partition count for
topic " + ident, e);
+
} else if (e.getCause() instanceof InvalidReplicationFactorException) {
throw ExceptionMessages.illegalArgument("Invalid replication factor
for topic " + ident, e);
diff --git
a/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
b/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
index c2c0a5d60e..c85f3d4f04 100644
---
a/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
+++
b/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
@@ -89,6 +89,7 @@ import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.ListTopicsResult;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidPartitionsException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@@ -443,6 +444,65 @@ public class TestKafkaCatalogOperations extends
KafkaClusterEmbedded {
exception.getMessage());
}
+ @Test
+ void testCreateTopicWithInvalidPartitionCount() {
+ for (String partitionCount : new String[] {"0", "-2"}) {
+ NameIdentifier ident =
+ NameIdentifier.of(
+ METALAKE_NAME,
+ CATALOG_NAME,
+ DEFAULT_SCHEMA_NAME,
+ "test_create_topic_invalid_partitions_" + partitionCount);
+ Map<String, String> properties =
+ ImmutableMap.of(PARTITION_COUNT, partitionCount, REPLICATION_FACTOR,
"1");
+
+ IllegalArgumentException exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> kafkaCatalogOperations.createTopic(ident, null, null,
properties));
+ Assertions.assertTrue(
+ exception.getMessage().contains("Invalid partition count for topic "
+ ident),
+ exception.getMessage());
+ Throwable cause = exception.getCause().getCause();
+ Assertions.assertInstanceOf(InvalidPartitionsException.class, cause);
+
Assertions.assertTrue(exception.getMessage().contains(cause.getMessage()));
+ }
+ }
+
+ @Test
+ void testCreateTopicWithDefaultPartitionCount() {
+ NameIdentifier defaultIdent =
+ NameIdentifier.of(
+ METALAKE_NAME,
+ CATALOG_NAME,
+ DEFAULT_SCHEMA_NAME,
+ "test_create_topic_default_partitions");
+ Topic defaultTopic =
+ kafkaCatalogOperations.createTopic(
+ defaultIdent, null, null, ImmutableMap.of(REPLICATION_FACTOR,
"1"));
+
+ NameIdentifier negativeOneIdent =
+ NameIdentifier.of(
+ METALAKE_NAME,
+ CATALOG_NAME,
+ DEFAULT_SCHEMA_NAME,
+ "test_create_topic_negative_one_partitions");
+ Topic negativeOneTopic =
+ kafkaCatalogOperations.createTopic(
+ negativeOneIdent,
+ null,
+ null,
+ ImmutableMap.of(PARTITION_COUNT, "-1", REPLICATION_FACTOR, "1"));
+
+ String defaultPartitionCount =
defaultTopic.properties().get(PARTITION_COUNT);
+ Assertions.assertTrue(Integer.parseInt(defaultPartitionCount) > 0);
+ Assertions.assertEquals(
+ defaultPartitionCount,
negativeOneTopic.properties().get(PARTITION_COUNT));
+ Assertions.assertEquals(
+ defaultPartitionCount,
+
kafkaCatalogOperations.loadTopic(negativeOneIdent).properties().get(PARTITION_COUNT));
+ }
+
@Test
public void testLoadTopic() {
Topic topic =