PartitionDistributionStatsTest.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.provectus.kafka.ui.model;
  2. import static org.assertj.core.api.Assertions.assertThat;
  3. import com.provectus.kafka.ui.service.ReactiveAdminClient;
  4. import java.math.BigDecimal;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import org.apache.kafka.clients.admin.TopicDescription;
  9. import org.apache.kafka.common.Node;
  10. import org.apache.kafka.common.TopicPartitionInfo;
  11. import org.assertj.core.data.Percentage;
  12. import org.junit.jupiter.api.Test;
  13. class PartitionDistributionStatsTest {
  14. @Test
  15. void skewCalculatedBasedOnPartitionsCounts() {
  16. Node n1 = new Node(1, "n1", 9092);
  17. Node n2 = new Node(2, "n2", 9092);
  18. Node n3 = new Node(3, "n3", 9092);
  19. var stats = PartitionDistributionStats.create(
  20. Statistics.builder()
  21. .clusterDescription(
  22. new ReactiveAdminClient.ClusterDescription(null, "test", Set.of(n1, n2, n3), null))
  23. .topicDescriptions(
  24. Map.of(
  25. "t1", new TopicDescription(
  26. "t1", false,
  27. List.of(
  28. new TopicPartitionInfo(0, n1, List.of(n1, n2), List.of(n1, n2)),
  29. new TopicPartitionInfo(1, n2, List.of(n2, n3), List.of(n2, n3))
  30. )
  31. ),
  32. "t2", new TopicDescription(
  33. "t2", false,
  34. List.of(
  35. new TopicPartitionInfo(0, n1, List.of(n1, n2), List.of(n1, n2)),
  36. new TopicPartitionInfo(1, null, List.of(n2, n1), List.of(n1))
  37. )
  38. )
  39. )
  40. )
  41. .build(), 4
  42. );
  43. assertThat(stats.getPartitionLeaders())
  44. .containsExactlyInAnyOrderEntriesOf(Map.of(n1, 2, n2, 1));
  45. assertThat(stats.getPartitionsCount())
  46. .containsExactlyInAnyOrderEntriesOf(Map.of(n1, 3, n2, 4, n3, 1));
  47. assertThat(stats.getInSyncPartitions())
  48. .containsExactlyInAnyOrderEntriesOf(Map.of(n1, 3, n2, 3, n3, 1));
  49. // 4 partitions, 3 brokers = avg partition cnt per broker is 1.333.
  50. assertThat(stats.getAvgPartitionsPerBroker())
  51. .isCloseTo(1.333, Percentage.withPercentage(1));
  52. // Node(partitions): n1(3), n2(4), n3(1)
  53. assertThat(stats.partitionsSkew(n1))
  54. .isCloseTo(BigDecimal.valueOf(125), Percentage.withPercentage(1));
  55. assertThat(stats.partitionsSkew(n2))
  56. .isCloseTo(BigDecimal.valueOf(200), Percentage.withPercentage(1));
  57. assertThat(stats.partitionsSkew(n3))
  58. .isCloseTo(BigDecimal.valueOf(-25), Percentage.withPercentage(1));
  59. // Node(leaders): n1(2), n2(1), n3(0)
  60. assertThat(stats.leadersSkew(n1))
  61. .isCloseTo(BigDecimal.valueOf(50), Percentage.withPercentage(1));
  62. assertThat(stats.leadersSkew(n2))
  63. .isCloseTo(BigDecimal.valueOf(-25), Percentage.withPercentage(1));
  64. assertThat(stats.leadersSkew(n3))
  65. .isCloseTo(BigDecimal.valueOf(-100), Percentage.withPercentage(1));
  66. }
  67. }