apache/kafka · error · IllegalArgumentException
Cannot create a new partition reassignment without any repli
Error message
Cannot create a new partition reassignment without any replicas
What it means
Thrown by the NewPartitionReassignment constructor when targetReplicas is null or empty. A reassignment must specify at least one replica (broker id), so an empty list is meaningless and rejected immediately rather than sent to the controller.
Source
Thrown at clients/src/main/java/org/apache/kafka/clients/admin/NewPartitionReassignment.java:37
import org.apache.kafka.common.annotation.InterfaceAudience;
import java.util.List;
import java.util.Map;
/**
* A new partition reassignment, which can be applied via {@link AdminClient#alterPartitionReassignments(Map, AlterPartitionReassignmentsOptions)}.
*/
@InterfaceAudience.Public
public class NewPartitionReassignment {
private final List<Integer> targetReplicas;
/**
* @throws IllegalArgumentException if no replicas are supplied
*/
public NewPartitionReassignment(List<Integer> targetReplicas) {
if (targetReplicas == null || targetReplicas.isEmpty())
throw new IllegalArgumentException("Cannot create a new partition reassignment without any replicas");
this.targetReplicas = List.copyOf(targetReplicas);
}
public List<Integer> targetReplicas() {
return targetReplicas;
}
}
View on GitHub (pinned to c31c9215e1)
Solutions
- To cancel an ongoing reassignment, pass null as the value for that partition in the alterPartitionReassignments map instead of an empty NewPartitionReassignment
- Ensure targetReplicas contains at least one valid broker id before constructing the object
- Validate the computed replica list is non-empty and log the inputs when it is unexpectedly empty
Example fix
// before
admin.alterPartitionReassignments(Map.of(
tp, new NewPartitionReassignment(List.of()))); // throws
// after - assign replicas
admin.alterPartitionReassignments(Map.of(
tp, new NewPartitionReassignment(List.of(1, 2, 3))));
// after - cancel an existing reassignment
admin.alterPartitionReassignments(Map.of(tp, null)); Defensive patterns
Strategy: validation
Validate before calling
List<Integer> targetReplicas = ...;
if (targetReplicas == null || targetReplicas.isEmpty()) {
throw new IllegalArgumentException(
"targetReplicas must be a non-null, non-empty list of broker ids");
}
// Optional: also reject unknown/negative broker ids before constructing.
if (targetReplicas.stream().anyMatch(id -> id == null || id < 0)) {
throw new IllegalArgumentException("targetReplicas contains null or negative broker id");
}
NewPartitionReassignment reassignment = new NewPartitionReassignment(targetReplicas); Type guard
static boolean isValidReassignment(List<Integer> replicas) {
return replicas != null && !replicas.isEmpty()
&& replicas.stream().noneMatch(id -> id == null || id < 0);
} Try / catch
try {
NewPartitionReassignment reassignment = new NewPartitionReassignment(targetReplicas);
} catch (IllegalArgumentException e) {
// No replicas supplied; this is a config/input error.
// Surface it to the operator or skip the reassignment for this partition.
log.error("Cannot build reassignment: {}", e.getMessage());
} Prevention
- Source targetReplicas from a validated broker inventory (e.g. describeCluster) so the list is never empty.
- Defensively filter null/negative broker ids before constructing the reassignment object.
- Validate the replica count against the topic's replication factor requirements before submission.
When it happens
Trigger: Constructing new NewPartitionReassignment(Collections.emptyList()) or passing a null list, then using it in Admin.alterPartitionReassignments. Also triggered when a list built from filtering or partitioning happens to contain zero broker ids at runtime.
Common situations: Building the replica list dynamically from a config or discovery call that returned nothing; passing an empty list to signal cancellation of a reassignment (the API uses null in the value of the alterPartitionReassignments map for cancellation, not an empty NewPartitionReassignment); off-by-one or empty ranges when computing target brokers.
Related errors
- Invalid empty members has been provided
- Offsets for consumer group '{groupId}' were not requested.
- List Offsets for partition "{partition}" was not attempted
- Group ID not found: {groupId}
- The method: memberResult is not applicable in 'removeAll' mo
AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03).
Data as JSON: /data/errors/dd04380d03a0988b.json.
Report an issue: GitHub.