|
71 | 71 | import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.getBrokerMetadata;
|
72 | 72 | import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.getReplicaAssignmentForPartitions;
|
73 | 73 | import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.getReplicaAssignmentForTopics;
|
| 74 | +import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.getReplicaToLogDir; |
74 | 75 | import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.modifyInterBrokerThrottle;
|
75 | 76 | import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.modifyLogDirThrottle;
|
76 | 77 | import static org.apache.kafka.tools.reassign.ReassignPartitionsCommand.modifyTopicThrottles;
|
@@ -436,29 +437,50 @@ public void testGenerateAssignmentWithFewerBrokers() throws Exception {
|
436 | 437 |
|
437 | 438 | @Test
|
438 | 439 | public void testCurrentPartitionReplicaAssignmentToString() throws Exception {
|
439 |
| - Map<TopicPartition, List<Integer>> proposedParts = new HashMap<>(); |
440 |
| - |
441 |
| - proposedParts.put(new TopicPartition("foo", 1), List.of(1, 2, 3)); |
442 |
| - proposedParts.put(new TopicPartition("bar", 0), List.of(7, 8, 9)); |
443 |
| - |
444 |
| - Map<TopicPartition, List<Integer>> currentParts = new HashMap<>(); |
445 |
| - |
446 |
| - currentParts.put(new TopicPartition("foo", 0), List.of(1, 2, 3)); |
447 |
| - currentParts.put(new TopicPartition("foo", 1), List.of(4, 5, 6)); |
448 |
| - currentParts.put(new TopicPartition("bar", 0), List.of(7, 8)); |
449 |
| - currentParts.put(new TopicPartition("baz", 0), List.of(10, 11, 12)); |
| 440 | + try (MockAdminClient adminClient = new MockAdminClient.Builder() |
| 441 | + .numBrokers(6) |
| 442 | + .brokerLogDirs(List.of( |
| 443 | + List.of("/tmp/broker0/logs"), |
| 444 | + List.of("/tmp/broker1/logs"), |
| 445 | + List.of("/tmp/broker2/logs"), |
| 446 | + List.of("/tmp/broker3/logs"), |
| 447 | + List.of("/tmp/broker4/logs"), |
| 448 | + List.of("/tmp/broker5/logs") |
| 449 | + )) |
| 450 | + .build() |
| 451 | + ) { |
| 452 | + |
| 453 | + List<Node> brokers = adminClient.brokers(); |
| 454 | + adminClient.addTopic(false, "foo", List.of( |
| 455 | + new TopicPartitionInfo(1, brokers.get(1), |
| 456 | + List.of(brokers.get(1), brokers.get(2), brokers.get(3)), |
| 457 | + List.of(brokers.get(1), brokers.get(2), brokers.get(3))) |
| 458 | + ), Map.of()); |
| 459 | + |
| 460 | + adminClient.addTopic(false, "bar", List.of( |
| 461 | + new TopicPartitionInfo(0, brokers.get(4), |
| 462 | + List.of(brokers.get(4), brokers.get(5)), |
| 463 | + List.of(brokers.get(4), brokers.get(5))) |
| 464 | + ), Map.of()); |
| 465 | + |
| 466 | + Map<TopicPartition, List<Integer>> proposedParts = new HashMap<>(); |
| 467 | + proposedParts.put(new TopicPartition("foo", 1), List.of(0, 1, 2)); |
| 468 | + proposedParts.put(new TopicPartition("bar", 0), List.of(3, 4, 5)); |
| 469 | + |
| 470 | + Map<TopicPartition, List<Integer>> currentParts = new HashMap<>(); |
| 471 | + currentParts.put(new TopicPartition("foo", 1), List.of(1, 2, 3)); |
| 472 | + currentParts.put(new TopicPartition("bar", 0), List.of(4, 5)); |
450 | 473 |
|
451 |
| - assertEquals(String.join(System.lineSeparator(), |
452 |
| - "Current partition replica assignment", |
453 |
| - "", |
454 |
| - "{\"version\":1,\"partitions\":" + |
455 |
| - "[{\"topic\":\"bar\",\"partition\":0,\"replicas\":[7,8],\"log_dirs\":[\"any\",\"any\"]}," + |
456 |
| - "{\"topic\":\"foo\",\"partition\":1,\"replicas\":[4,5,6],\"log_dirs\":[\"any\",\"any\",\"any\"]}]" + |
457 |
| - "}", |
458 |
| - "", |
459 |
| - "Save this to use as the --reassignment-json-file option during rollback"), |
460 |
| - currentPartitionReplicaAssignmentToString(proposedParts, currentParts) |
461 |
| - ); |
| 474 | + assertEquals(String.join(System.lineSeparator(), |
| 475 | + "Current partition replica assignment", |
| 476 | + "", |
| 477 | + "{\"version\":1,\"partitions\":[{\"topic\":\"bar\",\"partition\":0,\"replicas\":[4,5],\"log_dirs\":[\"/tmp/broker4/logs\",\"/tmp/broker4/logs\"]}," + |
| 478 | + "{\"topic\":\"foo\",\"partition\":1,\"replicas\":[1,2,3],\"log_dirs\":[\"any\",\"any\",\"any\"]}]}", |
| 479 | + "", |
| 480 | + "Save this to use as the --reassignment-json-file option during rollback"), |
| 481 | + currentPartitionReplicaAssignmentToString(adminClient, proposedParts, currentParts) |
| 482 | + ); |
| 483 | + } |
462 | 484 | }
|
463 | 485 |
|
464 | 486 | @Test
|
@@ -765,4 +787,38 @@ public void testPropagateInvalidJsonError() {
|
765 | 787 | assertThrows(AdminOperationException.class, () -> executeAssignment(adminClient, false, "{invalid_json", -1L, -1L, 10000L, Time.SYSTEM, false)).getMessage());
|
766 | 788 | }
|
767 | 789 | }
|
| 790 | + |
| 791 | + @Test |
| 792 | + public void testGetReplicaToLogDir() throws Exception { |
| 793 | + try (MockAdminClient adminClient = new MockAdminClient.Builder() |
| 794 | + .numBrokers(4) |
| 795 | + .brokerLogDirs(List.of( |
| 796 | + List.of("/tmp/broker0/logs0"), |
| 797 | + List.of("/tmp/broker1/logs0"), |
| 798 | + List.of("/tmp/broker2/logs0"), |
| 799 | + List.of("/tmp/broker3/logs0") |
| 800 | + )).build() |
| 801 | + ) { |
| 802 | + addTopics(adminClient); |
| 803 | + |
| 804 | + Map<TopicPartition, List<Integer>> topicPartitionToReplicas = Map.of( |
| 805 | + new TopicPartition("foo", 0), List.of(0, 1, 2), |
| 806 | + new TopicPartition("foo", 1), List.of(1, 2, 3), |
| 807 | + new TopicPartition("bar", 0), List.of(2, 3, 0) |
| 808 | + ); |
| 809 | + |
| 810 | + Map<TopicPartitionReplica, String> result = getReplicaToLogDir(adminClient, topicPartitionToReplicas); |
| 811 | + |
| 812 | + assertFalse(result.isEmpty()); |
| 813 | + assertEquals("/tmp/broker0/logs0", result.get(new TopicPartitionReplica("foo", 0, 0))); |
| 814 | + assertEquals("/tmp/broker0/logs0", result.get(new TopicPartitionReplica("foo", 0, 1))); |
| 815 | + assertEquals("/tmp/broker0/logs0", result.get(new TopicPartitionReplica("foo", 0, 2))); |
| 816 | + assertEquals("/tmp/broker1/logs0", result.get(new TopicPartitionReplica("foo", 1, 1))); |
| 817 | + assertEquals("/tmp/broker1/logs0", result.get(new TopicPartitionReplica("foo", 1, 2))); |
| 818 | + assertEquals("/tmp/broker1/logs0", result.get(new TopicPartitionReplica("foo", 1, 3))); |
| 819 | + assertEquals("/tmp/broker2/logs0", result.get(new TopicPartitionReplica("bar", 0, 0))); |
| 820 | + assertEquals("/tmp/broker2/logs0", result.get(new TopicPartitionReplica("bar", 0, 2))); |
| 821 | + assertEquals("/tmp/broker2/logs0", result.get(new TopicPartitionReplica("bar", 0, 3))); |
| 822 | + } |
| 823 | + } |
768 | 824 | }
|
0 commit comments