You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "brandboat (via GitHub)" <gi...@apache.org> on 2025/02/20 17:06:24 UTC

[PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

brandboat opened a new pull request, #18983:
URL: https://github.com/apache/kafka/pull/18983

   KAFKA-14121
   
   Add new option `allowReplicationFactorChange` to alterPartitionReassignments. Set this option to false prevents partition replication factor being changed during partition reassignment.
   See https://cwiki.apache.org/confluence/display/KAFKA/KIP-860%3A+Add+client-provided+option+to+guard+against+replication+factor+change+during+partition+reassignments for more detail
   
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on PR #18983:
URL: https://github.com/apache/kafka/pull/18983#issuecomment-2694686895

   Heya, thanks for the updated code! I will have a look at it tomorrow!


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1977728460


##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {
+        MetadataVersion metadataVersion = MetadataVersion.latestTesting();
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder()
+                .setMetadataVersion(metadataVersion)
+                .build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3);
+        ctx.unfenceBrokers(0, 1, 2, 3);
+        ctx.createTestTopic("foo", new int[][] {new int[] {0, 1, 2}, new int[] {0, 1, 2}, new int[] {0, 1, 2}});
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                new ReassignableTopic().setName("foo").setPartitions(asList(
+                                        new ReassignablePartition().setPartitionIndex(0).
+                                                setReplicas(asList(1, 2, 3)),

Review Comment:
   Make sense 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1976657588


##########
clients/src/main/java/org/apache/kafka/clients/admin/AlterPartitionReassignmentsOptions.java:
##########
@@ -28,4 +28,25 @@
  */
 @InterfaceStability.Evolving
 public class AlterPartitionReassignmentsOptions extends AbstractOptions<AlterPartitionReassignmentsOptions> {
+
+    private Boolean allowReplicationFactorChange = true;
+
+    /**
+     * Set the option indicating if the alter partition reassignments call should be
+     * allowed to alter the replication factor of a partition.
+     * In cases where it is not allowed, any replication factor change will result in an exception thrown by the API.
+     */

Review Comment:
   It seems that this follows a pattern used in other xxxOptions, such as `CreateTopicsOptions.java`, `DeleteTopicsOptions.java`, and `DescribeClusterOptions.java`, etc. While modifying it could help reduce repetition in the description, I prefer to keep it as is to maintain consistency with other xxxOptions. Thanks!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1977696277


##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {
+        MetadataVersion metadataVersion = MetadataVersion.latestTesting();
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder()
+                .setMetadataVersion(metadataVersion)
+                .build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3);
+        ctx.unfenceBrokers(0, 1, 2, 3);
+        ctx.createTestTopic("foo", new int[][] {new int[] {0, 1, 2}, new int[] {0, 1, 2}, new int[] {0, 1, 2}});
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                new ReassignableTopic().setName("foo").setPartitions(asList(
+                                        new ReassignablePartition().setPartitionIndex(0).
+                                                setReplicas(asList(1, 2, 3)),

Review Comment:
   nit: `List.of`



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on PR #18983:
URL: https://github.com/apache/kafka/pull/18983#issuecomment-2694696867

   > Heya, thanks for the updated code! I will have a look at it tomorrow!
   
   Thank you!


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on PR #18983:
URL: https://github.com/apache/kafka/pull/18983#issuecomment-2697870661

   > Could you just reduce the length of the PR title because a linter is complaining? I will also leave this open for another day in case any of the other reviewers have blocking comments
   
   I'm surprised that we have this linter. If I understand correctly, the PR title should match the JIRA title, right? Does that mean I need to shorten the JIRA title to maintain consistency?


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1969023824


##########
clients/src/main/java/org/apache/kafka/clients/admin/AlterPartitionReassignmentsOptions.java:
##########
@@ -28,4 +28,25 @@
  */
 @InterfaceStability.Evolving
 public class AlterPartitionReassignmentsOptions extends AbstractOptions<AlterPartitionReassignmentsOptions> {
+
+    private Boolean allowReplicationFactorChange = true;
+
+    /**
+     * Set the option indicating if the alter partition reassignments call should be
+     * allowed to alter the replication factor of a partition.
+     * In cases where it is not allowed, any replication factor change will result in an exception thrown by the API.
+     */
+    public boolean allowReplicationFactorChange(boolean allow) {
+        this.allowReplicationFactorChange = allow;
+        return allowReplicationFactorChange;

Review Comment:
   +1 to fluent pattern



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1976667995


##########
core/src/test/scala/integration/kafka/api/PlaintextAdminIntegrationTest.scala:
##########
@@ -3452,11 +3452,26 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
     val tp3 = new TopicPartition(topic, 2)
     createTopic(topic, numPartitions = 4)
 
-
     val validAssignment = Optional.of(new NewPartitionReassignment(
       (0 until brokerCount).map(_.asInstanceOf[Integer]).asJava
     ))
 
+    val alterOptions = new AlterPartitionReassignmentsOptions
+    alterOptions.allowReplicationFactorChange(false)
+    val alterReplicaNumberTo1 = Optional.of(new NewPartitionReassignment(List(1.asInstanceOf[Integer]).asJava))
+    val alterReplicaNumberTo2 = Optional.of(new NewPartitionReassignment((0 until brokerCount - 1).map(_.asInstanceOf[Integer]).asJava))
+    val alterReplicaNumberTo3 = Optional.of(new NewPartitionReassignment((0 until brokerCount).map(_.asInstanceOf[Integer]).asJava))
+    val alterReplicaResults = client.alterPartitionReassignments(Map(
+      tp1 -> alterReplicaNumberTo1,
+      tp2 -> alterReplicaNumberTo2,
+      tp3 -> alterReplicaNumberTo3,
+    ).asJava, alterOptions).values()
+    assertDoesNotThrow(() => alterReplicaResults.get(tp1).get())
+    assertEquals("The replication factor is changed from 1 to 2",
+      assertFutureThrows(classOf[InvalidReplicationFactorException], alterReplicaResults.get(tp2)).getMessage)
+    assertEquals("The replication factor is changed from 1 to 3",

Review Comment:
   Sure! I modify the replication factor from 1 to 2 in this test case. 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on PR #18983:
URL: https://github.com/apache/kafka/pull/18983#issuecomment-2700635940

   I will also let you resolve the JIRA appropriately and move the KIP to the correct section + update the Kafka version in which it will be going out!


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on PR #18983:
URL: https://github.com/apache/kafka/pull/18983#issuecomment-2701492450

   > I will also let you resolve the JIRA appropriately and move the KIP to the correct section + update the Kafka version in which it will be going out!
   
   Thank you for merging this PR @clolov! I’ve updated the KIP and marked the JIRA as resolved, with the target version set to 4.1.0.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1965316204


##########
clients/src/main/java/org/apache/kafka/clients/admin/AlterPartitionReassignmentsOptions.java:
##########
@@ -28,4 +28,25 @@
  */
 @InterfaceStability.Evolving
 public class AlterPartitionReassignmentsOptions extends AbstractOptions<AlterPartitionReassignmentsOptions> {
+
+    private Boolean allowReplicationFactorChange = true;
+
+    /**
+     * Set the option indicating if the alter partition reassignments call should be
+     * allowed to alter the replication factor of a partition.
+     * In cases where it is not allowed, any replication factor change will result in an exception thrown by the API.
+     */
+    public boolean allowReplicationFactorChange(boolean allow) {
+        this.allowReplicationFactorChange = allow;
+        return allowReplicationFactorChange;

Review Comment:
   On a quick cross-check with other *Options it appears that for such methods we return the *Options class itself. In other words, something along these lines
   ```
   public AlterPartitionReassignmentsOptions allowReplicationFactorChange(boolean allow) {
       this.allowReplicationFactorChange = allow;
       return this;
   }
   ```



##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {
+        MetadataVersion metadataVersion = MetadataVersion.latestTesting();
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder()
+                .setMetadataVersion(metadataVersion)
+                .build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3);
+        ctx.unfenceBrokers(0, 1, 2, 3);
+        ctx.createTestTopic("foo", new int[][] {new int[] {1, 2, 3}, new int[] {1, 2, 3}, new int[] {1, 2, 3}});
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                new ReassignableTopic().setName("foo").setPartitions(asList(
+                                        new ReassignablePartition().setPartitionIndex(0).
+                                                setReplicas(asList(0, 1, 2)),
+                                        new ReassignablePartition().setPartitionIndex(1).
+                                                setReplicas(asList(0, 1)),
+                                        new ReassignablePartition().setPartitionIndex(2).
+                                                setReplicas(asList(0, 1, 2, 3)))))).
+                                setAllowReplicationFactorChange(false));
+        assertEquals(new AlterPartitionReassignmentsResponseData().
+                        setErrorMessage(null).setAllowReplicationFactorChange(false).setResponses(singletonList(
+                                new ReassignableTopicResponse().setName("foo").setPartitions(asList(
+                                        new ReassignablePartitionResponse().setPartitionIndex(0).
+                                                setErrorMessage(null),
+                                        new ReassignablePartitionResponse().setPartitionIndex(1).
+                                                setErrorCode(INVALID_REPLICATION_FACTOR.code()).
+                                                setErrorMessage("The replication factor is changed from 3 to 2"),
+                                        new ReassignablePartitionResponse().setPartitionIndex(2).
+                                                setErrorCode(INVALID_REPLICATION_FACTOR.code()).
+                                                setErrorMessage("The replication factor is changed from 3 to 4"))))),
+                alterResult.response());
+        ctx.replay(alterResult.records());
+        ListPartitionReassignmentsResponseData currentReassigning =
+                new ListPartitionReassignmentsResponseData().setErrorMessage(null).
+                        setTopics(singletonList(new OngoingTopicReassignment().
+                                setName("foo").setPartitions(singletonList(
+                                        new OngoingPartitionReassignment().setPartitionIndex(0).
+                                                setRemovingReplicas(singletonList(3)).

Review Comment:
   Nit: Partition 0 started with replicas (0, 1, 2). For the sake of continuity, could you remove 0 and add 3 rather than the other way around?



##########
metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java:
##########
@@ -2406,6 +2414,31 @@ private void updatePartitionInfo(
             newPartInfo.elr);
     }
 
+    private void validatePartitionReplicationFactorUnchanged(
+            PartitionRegistration part,

Review Comment:
   Nit: Could you align the first argument with the opening bracket as other methods in this file?



##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {

Review Comment:
   Maybe there already are such tests, in which case please point them out to me, but if not, could you add the same tests but with `setAllowReplicationFactorChange(true)`?



##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {
+        MetadataVersion metadataVersion = MetadataVersion.latestTesting();
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder()
+                .setMetadataVersion(metadataVersion)
+                .build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3);
+        ctx.unfenceBrokers(0, 1, 2, 3);
+        ctx.createTestTopic("foo", new int[][] {new int[] {1, 2, 3}, new int[] {1, 2, 3}, new int[] {1, 2, 3}});
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                new ReassignableTopic().setName("foo").setPartitions(asList(
+                                        new ReassignablePartition().setPartitionIndex(0).
+                                                setReplicas(asList(0, 1, 2)),
+                                        new ReassignablePartition().setPartitionIndex(1).
+                                                setReplicas(asList(0, 1)),
+                                        new ReassignablePartition().setPartitionIndex(2).
+                                                setReplicas(asList(0, 1, 2, 3)))))).
+                                setAllowReplicationFactorChange(false));
+        assertEquals(new AlterPartitionReassignmentsResponseData().
+                        setErrorMessage(null).setAllowReplicationFactorChange(false).setResponses(singletonList(
+                                new ReassignableTopicResponse().setName("foo").setPartitions(asList(
+                                        new ReassignablePartitionResponse().setPartitionIndex(0).
+                                                setErrorMessage(null),
+                                        new ReassignablePartitionResponse().setPartitionIndex(1).
+                                                setErrorCode(INVALID_REPLICATION_FACTOR.code()).
+                                                setErrorMessage("The replication factor is changed from 3 to 2"),
+                                        new ReassignablePartitionResponse().setPartitionIndex(2).
+                                                setErrorCode(INVALID_REPLICATION_FACTOR.code()).
+                                                setErrorMessage("The replication factor is changed from 3 to 4"))))),
+                alterResult.response());
+        ctx.replay(alterResult.records());
+        ListPartitionReassignmentsResponseData currentReassigning =
+                new ListPartitionReassignmentsResponseData().setErrorMessage(null).
+                        setTopics(singletonList(new OngoingTopicReassignment().
+                                setName("foo").setPartitions(singletonList(
+                                        new OngoingPartitionReassignment().setPartitionIndex(0).
+                                                setRemovingReplicas(singletonList(3)).
+                                                setAddingReplicas(singletonList(0)).
+                                                setReplicas(asList(0, 1, 2, 3))))));
+        assertEquals(currentReassigning, replication.listPartitionReassignments(singletonList(
+                new ListPartitionReassignmentsTopics().setName("foo").
+                        setPartitionIndexes(asList(0, 1, 2))), Long.MAX_VALUE));
+
+        // test alter replica factor not allow to change when partition reassignment is ongoing
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterReassigningResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                new ReassignableTopic().setName("foo").setPartitions(singletonList(
+                                        new ReassignablePartition().setPartitionIndex(0).setReplicas(asList(0, 1)))))).
+                                setAllowReplicationFactorChange(false));
+        assertEquals(new AlterPartitionReassignmentsResponseData().
+                        setErrorMessage(null).setAllowReplicationFactorChange(false).setResponses(singletonList(
+                                new ReassignableTopicResponse().setName("foo").setPartitions(singletonList(
+                                        new ReassignablePartitionResponse().setPartitionIndex(0).
+                                                setErrorCode(INVALID_REPLICATION_FACTOR.code()).
+                                                setErrorMessage("The replication factor is changed from 3 to 2"))))),
+                alterReassigningResult.response());
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterReassigningResult2 =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                        new ReassignableTopic().setName("foo").setPartitions(singletonList(
+                                                new ReassignablePartition().setPartitionIndex(0).setReplicas(asList(0, 2, 3)))))).
+                                setAllowReplicationFactorChange(false));
+        assertEquals(new AlterPartitionReassignmentsResponseData().
+                        setErrorMessage(null).setAllowReplicationFactorChange(false).setResponses(singletonList(
+                                new ReassignableTopicResponse().setName("foo").setPartitions(singletonList(
+                                        new ReassignablePartitionResponse().setPartitionIndex(0).
+                                                setErrorMessage(null))))),
+                alterReassigningResult2.response());
+    }
+
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testDisallowReplicationFactorChangeNoEffectWhenCancelAlterPartition(short version) {
+        MetadataVersion metadataVersion = MetadataVersion.latestTesting();
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder()
+                .setMetadataVersion(metadataVersion)
+                .build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3);
+        ctx.unfenceBrokers(0, 1, 2, 3);
+        ctx.createTestTopic("foo", new int[][] {new int[] {1, 2, 3}}).topicId();
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                        new ReassignableTopic().setName("foo").setPartitions(singletonList(
+                                                new ReassignablePartition().setPartitionIndex(0).
+                                                        setReplicas(asList(0, 1, 2)))))));
+        assertEquals(new AlterPartitionReassignmentsResponseData().
+                        setErrorMessage(null).setResponses(singletonList(
+                                new ReassignableTopicResponse().setName("foo").setPartitions(singletonList(
+                                        new ReassignablePartitionResponse().setPartitionIndex(0).setErrorMessage(null))))),
+                alterResult.response());
+        ctx.replay(alterResult.records());
+        ListPartitionReassignmentsResponseData currentReassigning =
+                new ListPartitionReassignmentsResponseData().setErrorMessage(null).
+                        setTopics(singletonList(new OngoingTopicReassignment().
+                                setName("foo").setPartitions(singletonList(
+                                        new OngoingPartitionReassignment().setPartitionIndex(0).
+                                                setRemovingReplicas(singletonList(3)).

Review Comment:
   Nit: Same comment as in the previous test



##########
tools/src/test/java/org/apache/kafka/tools/reassign/ReassignPartitionsCommandTest.java:
##########
@@ -432,6 +433,21 @@ topicPartition, new PartitionReassignmentState(asList(0, 1, 2), asList(0, 1, 2),
         }
     }
 
+    @ClusterTest
+    public void testDisallowReplicationFactorChange() {

Review Comment:
   Could you also add a test (or build on this one) which showcases that **increasing** the replication factor also fails?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1969041052


##########
core/src/test/scala/integration/kafka/api/PlaintextAdminIntegrationTest.scala:
##########
@@ -3452,11 +3452,26 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
     val tp3 = new TopicPartition(topic, 2)
     createTopic(topic, numPartitions = 4)
 
-
     val validAssignment = Optional.of(new NewPartitionReassignment(
       (0 until brokerCount).map(_.asInstanceOf[Integer]).asJava
     ))
 
+    val alterOptions = new AlterPartitionReassignmentsOptions
+    alterOptions.allowReplicationFactorChange(false)
+    val alterReplicaNumberTo1 = Optional.of(new NewPartitionReassignment(List(1.asInstanceOf[Integer]).asJava))
+    val alterReplicaNumberTo2 = Optional.of(new NewPartitionReassignment((0 until brokerCount - 1).map(_.asInstanceOf[Integer]).asJava))
+    val alterReplicaNumberTo3 = Optional.of(new NewPartitionReassignment((0 until brokerCount).map(_.asInstanceOf[Integer]).asJava))
+    val alterReplicaResults = client.alterPartitionReassignments(Map(
+      tp1 -> alterReplicaNumberTo1,
+      tp2 -> alterReplicaNumberTo2,
+      tp3 -> alterReplicaNumberTo3,
+    ).asJava, alterOptions).values()
+    assertDoesNotThrow(() => alterReplicaResults.get(tp1).get())
+    assertEquals("The replication factor is changed from 1 to 2",
+      assertFutureThrows(classOf[InvalidReplicationFactorException], alterReplicaResults.get(tp2)).getMessage)
+    assertEquals("The replication factor is changed from 1 to 3",

Review Comment:
   Could you please add a test of decreasing the RF?



##########
clients/src/main/java/org/apache/kafka/clients/admin/AlterPartitionReassignmentsOptions.java:
##########
@@ -28,4 +28,25 @@
  */
 @InterfaceStability.Evolving
 public class AlterPartitionReassignmentsOptions extends AbstractOptions<AlterPartitionReassignmentsOptions> {
+
+    private Boolean allowReplicationFactorChange = true;

Review Comment:
   `boolean`



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "frankvicky (via GitHub)" <gi...@apache.org>.
frankvicky commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1969948672


##########
clients/src/main/java/org/apache/kafka/clients/admin/AlterPartitionReassignmentsOptions.java:
##########
@@ -28,4 +28,25 @@
  */
 @InterfaceStability.Evolving
 public class AlterPartitionReassignmentsOptions extends AbstractOptions<AlterPartitionReassignmentsOptions> {
+
+    private Boolean allowReplicationFactorChange = true;
+
+    /**
+     * Set the option indicating if the alter partition reassignments call should be
+     * allowed to alter the replication factor of a partition.
+     * In cases where it is not allowed, any replication factor change will result in an exception thrown by the API.
+     */

Review Comment:
   The javadoc is almost the same as one of the getter.
   We could move it to the field and leverage `@see` for people who want to know about details.
   
   For example:
   ```java
       /**
        * A boolean indicating if the alter partition reassignments should be
        * allowed to alter the replication factor of a partition.
        * In cases where it is not allowed, any replication factor change will result in an exception thrown by the API.
        */
       private boolean allowReplicationFactorChange = true;
   
       /**
        * @see AlterPartitionReassignmentsOptions#allowReplicationFactorChange
        */
       public boolean allowReplicationFactorChange(boolean allow) {
           this.allowReplicationFactorChange = allow;
           return allowReplicationFactorChange;
       }
   
       /**
        * @see AlterPartitionReassignmentsOptions#allowReplicationFactorChange
        */
       public boolean allowReplicationFactorChange() {
           return this.allowReplicationFactorChange;
       }
   ```
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1976655774


##########
clients/src/main/java/org/apache/kafka/clients/admin/AlterPartitionReassignmentsOptions.java:
##########
@@ -28,4 +28,25 @@
  */
 @InterfaceStability.Evolving
 public class AlterPartitionReassignmentsOptions extends AbstractOptions<AlterPartitionReassignmentsOptions> {
+
+    private Boolean allowReplicationFactorChange = true;

Review Comment:
   This is defined in the KIP, but I'm happy to modify it.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on PR #18983:
URL: https://github.com/apache/kafka/pull/18983#issuecomment-2700629939

   That's a good question, I will merge as-is and reachout to people to hear their opinions.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov merged PR #18983:
URL: https://github.com/apache/kafka/pull/18983


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1976664045


##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {

Review Comment:
   Yes, we have `testReassignPartitions` and `testCancelReassignPartitions`. I believe these 2 tests already cover the scenario where altering the replication factor is possible when `allowReplicationFactorChange` is true (default value is true).



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1976647014


##########
metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java:
##########
@@ -2406,6 +2414,31 @@ private void updatePartitionInfo(
             newPartInfo.elr);
     }
 
+    private void validatePartitionReplicationFactorUnchanged(
+            PartitionRegistration part,

Review Comment:
   The code style in this class is somewhat inconsistent, and the formatting isn't fully aligned. That said, I can address this comment. Perhaps in the future, we could consider adding a checkstyle rule to standardize the argument alignment.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1976666782


##########
tools/src/test/java/org/apache/kafka/tools/reassign/ReassignPartitionsCommandTest.java:
##########
@@ -432,6 +433,21 @@ topicPartition, new PartitionReassignmentState(asList(0, 1, 2), asList(0, 1, 2),
         }
     }
 
+    @ClusterTest
+    public void testDisallowReplicationFactorChange() {

Review Comment:
   Sure!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-14121: AlterPartitionReassignments API should allow callers to specify the option of preserving the replication factor [kafka]

Posted by "brandboat (via GitHub)" <gi...@apache.org>.
brandboat commented on code in PR #18983:
URL: https://github.com/apache/kafka/pull/18983#discussion_r1977713877


##########
metadata/src/test/java/org/apache/kafka/controller/ReplicationControlManagerTest.java:
##########
@@ -1922,6 +1922,135 @@ public void testReassignPartitions(short version) {
         assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null, Long.MAX_VALUE));
     }
 
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ALTER_PARTITION)
+    public void testAlterPartitionDisallowReplicationFactorChange(short version) {
+        MetadataVersion metadataVersion = MetadataVersion.latestTesting();
+        ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder()
+                .setMetadataVersion(metadataVersion)
+                .build();
+        ReplicationControlManager replication = ctx.replicationControl;
+        ctx.registerBrokers(0, 1, 2, 3);
+        ctx.unfenceBrokers(0, 1, 2, 3);
+        ctx.createTestTopic("foo", new int[][] {new int[] {0, 1, 2}, new int[] {0, 1, 2}, new int[] {0, 1, 2}});
+
+        ControllerResult<AlterPartitionReassignmentsResponseData> alterResult =
+                replication.alterPartitionReassignments(
+                        new AlterPartitionReassignmentsRequestData().setTopics(singletonList(
+                                new ReassignableTopic().setName("foo").setPartitions(asList(
+                                        new ReassignablePartition().setPartitionIndex(0).
+                                                setReplicas(asList(1, 2, 3)),

Review Comment:
   I've given this some thought, and I agree with you that List.of is a better choice. However, since this test class consistently uses Arrays.asList, perhaps we can open a separate minor PR to address this improvement. As you suggested, we can replace asList with List.of in this test class.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org