{"id":"dd04380d03a0988b","repo":"apache/kafka","slug":"cannot-create-a-new-partition-reassignment-without","errorCode":null,"errorMessage":"Cannot create a new partition reassignment without any replicas","messagePattern":"Cannot create a new partition reassignment without any replicas","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/admin/NewPartitionReassignment.java","lineNumber":37,"sourceCode":"\nimport org.apache.kafka.common.annotation.InterfaceAudience;\n\nimport java.util.List;\nimport java.util.Map;\n\n/**\n * A new partition reassignment, which can be applied via {@link AdminClient#alterPartitionReassignments(Map, AlterPartitionReassignmentsOptions)}.\n */\n@InterfaceAudience.Public\npublic class NewPartitionReassignment {\n    private final List<Integer> targetReplicas;\n\n    /**\n     * @throws IllegalArgumentException if no replicas are supplied\n     */\n    public NewPartitionReassignment(List<Integer> targetReplicas) {\n        if (targetReplicas == null || targetReplicas.isEmpty())\n            throw new IllegalArgumentException(\"Cannot create a new partition reassignment without any replicas\");\n        this.targetReplicas = List.copyOf(targetReplicas);\n    }\n\n    public List<Integer> targetReplicas() {\n        return targetReplicas;\n    }\n}\n","sourceCodeStart":19,"sourceCodeEnd":45,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/admin/NewPartitionReassignment.java#L19-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nadmin.alterPartitionReassignments(Map.of(\n    tp, new NewPartitionReassignment(List.of()))); // throws\n\n// after - assign replicas\nadmin.alterPartitionReassignments(Map.of(\n    tp, new NewPartitionReassignment(List.of(1, 2, 3))));\n// after - cancel an existing reassignment\nadmin.alterPartitionReassignments(Map.of(tp, null));","handlingStrategy":"validation","validationCode":"List<Integer> targetReplicas = ...;\nif (targetReplicas == null || targetReplicas.isEmpty()) {\n    throw new IllegalArgumentException(\n        \"targetReplicas must be a non-null, non-empty list of broker ids\");\n}\n// Optional: also reject unknown/negative broker ids before constructing.\nif (targetReplicas.stream().anyMatch(id -> id == null || id < 0)) {\n    throw new IllegalArgumentException(\"targetReplicas contains null or negative broker id\");\n}\nNewPartitionReassignment reassignment = new NewPartitionReassignment(targetReplicas);","typeGuard":"static boolean isValidReassignment(List<Integer> replicas) {\n    return replicas != null && !replicas.isEmpty()\n        && replicas.stream().noneMatch(id -> id == null || id < 0);\n}","tryCatchPattern":"try {\n    NewPartitionReassignment reassignment = new NewPartitionReassignment(targetReplicas);\n} catch (IllegalArgumentException e) {\n    // No replicas supplied; this is a config/input error.\n    // Surface it to the operator or skip the reassignment for this partition.\n    log.error(\"Cannot build reassignment: {}\", e.getMessage());\n}","preventionTips":["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."],"tags":["kafka","admin-client","partition-reassignment","invalid-argument"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}