Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge remote-tracking branch 'apache-kafka/3.8' #1512

Open
wants to merge 5 commits into
base: 3.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ class KRaftClusterTest {
() => admin.createTopics(newTopics).all().get())
assertNotNull(executionException.getCause)
assertEquals(classOf[PolicyViolationException], executionException.getCause.getClass)
assertEquals("Unable to perform excessively large batch operation.",
assertEquals("Excessively large number of partitions per request.",
executionException.getCause.getMessage)
} finally {
admin.close()
Expand Down
2 changes: 1 addition & 1 deletion docs/js/templateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ limitations under the License.
var context={
"version": "38",
"dotVersion": "3.8",
"fullDotVersion": "3.8.1-SNAPSHOT",
"fullDotVersion": "3.8.2-SNAPSHOT",
"scalaVersion": "2.13"
};
2 changes: 1 addition & 1 deletion kafka-merge-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

DEV_BRANCH_NAME = "trunk"

DEFAULT_FIX_VERSION = os.environ.get("DEFAULT_FIX_VERSION", "3.8.1-SNAPSHOT")
DEFAULT_FIX_VERSION = os.environ.get("DEFAULT_FIX_VERSION", "3.8.2-SNAPSHOT")

ORIGINAL_HEAD = ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
*/
public class ReplicationControlManager {
static final int MAX_ELECTIONS_PER_IMBALANCE = 1_000;
static final int MAX_PARTITIONS_PER_BATCH = 10_000;

static class Builder {
private SnapshotRegistry snapshotRegistry = null;
Expand Down Expand Up @@ -605,6 +606,8 @@ ControllerResult<CreateTopicsResponseData> createTopics(
Map<String, ApiError> topicErrors = new HashMap<>();
List<ApiMessageAndVersion> records = BoundedList.newArrayBacked(MAX_RECORDS_PER_USER_OP);

validateTotalNumberOfPartitions(request, defaultNumPartitions);

// Check the topic names.
validateNewTopicNames(topicErrors, request.topics(), topicsWithCollisionChars);

Expand Down Expand Up @@ -1150,6 +1153,34 @@ ControllerResult<AlterPartitionResponseData> alterPartition(
return ControllerResult.of(records, response);
}

/**
* Validates that a batch of topics will create less than {@value MAX_PARTITIONS_PER_BATCH}. Exceeding this number of topics per batch
* has led to out-of-memory exceptions. We use this validation to fail earlier to avoid allocating the memory.
* Validates an upper bound number of partitions. The actual number may be smaller if some topics are misconfigured.
*
* @param request a batch of topics to create.
* @param defaultNumPartitions default number of partitions to assign if unspecified.
* @throws PolicyViolationException if total number of partitions exceeds {@value MAX_PARTITIONS_PER_BATCH}.
*/
static void validateTotalNumberOfPartitions(CreateTopicsRequestData request, int defaultNumPartitions) {
int totalPartitions = 0;
for (CreatableTopic topic: request.topics()) {
if (topic.assignments().isEmpty()) {
if (topic.numPartitions() == -1) {
totalPartitions += defaultNumPartitions;
} else if (topic.numPartitions() > 0) {
totalPartitions += topic.numPartitions();
}
} else {
totalPartitions += topic.assignments().size();
}

}
if (totalPartitions > MAX_PARTITIONS_PER_BATCH) {
throw new PolicyViolationException("Excessively large number of partitions per request.");
}
}

/**
* Validate the partition information included in the alter partition request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -556,6 +557,44 @@ public void configure(Map<String, ?> configs) {
}
}

@Test
public void testExcessiveNumberOfTopicsCannotBeCreated() {
// number of partitions is explicitly set without assignments
ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder().build();
ReplicationControlManager replicationControl = ctx.replicationControl;
CreateTopicsRequestData request = new CreateTopicsRequestData();
request.topics().add(new CreatableTopic().setName("foo").
setNumPartitions(5000).setReplicationFactor((short) 1));
request.topics().add(new CreatableTopic().setName("bar").
setNumPartitions(5000).setReplicationFactor((short) 1));
request.topics().add(new CreatableTopic().setName("baz").
setNumPartitions(1).setReplicationFactor((short) 1));
ControllerRequestContext requestContext = anonymousContextFor(ApiKeys.CREATE_TOPICS);
PolicyViolationException error = assertThrows(
PolicyViolationException.class,
() -> replicationControl.createTopics(requestContext, request, Stream.of("foo", "bar", "baz").collect(Collectors.toSet())));
assertEquals(error.getMessage(), "Excessively large number of partitions per request.");
}

@Test
public void testExcessiveNumberOfTopicsCannotBeCreatedWithAssignments() {
CreateTopicsRequestData request = new CreateTopicsRequestData();
request.topics().add(new CreatableTopic().setName("foo").
setNumPartitions(-1).setReplicationFactor((short) 1));
CreateTopicsRequestData.CreatableReplicaAssignmentCollection assignments =
new CreateTopicsRequestData.CreatableReplicaAssignmentCollection();
assignments.add(new CreatableReplicaAssignment().setPartitionIndex(1));
assignments.add(new CreatableReplicaAssignment().setPartitionIndex(2));
request.topics().add(new CreatableTopic()
.setName("baz")
.setAssignments(assignments));
PolicyViolationException error = assertThrows(
PolicyViolationException.class,
() -> ReplicationControlManager.validateTotalNumberOfPartitions(request, 9999)
);
assertEquals(error.getMessage(), "Excessively large number of partitions per request.");
}

@Test
public void testCreateTopics() {
ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder().build();
Expand Down