|
| 1 | +package io.kafbat.ui.service; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.google.common.collect.ImmutableTable; |
| 6 | +import io.kafbat.ui.config.ClustersProperties; |
| 7 | +import io.kafbat.ui.model.InternalTopicConsumerGroup; |
| 8 | +import io.kafbat.ui.model.KafkaCluster; |
| 9 | +import io.kafbat.ui.model.Metrics; |
| 10 | +import io.kafbat.ui.model.ServerStatusDTO; |
| 11 | +import io.kafbat.ui.model.Statistics; |
| 12 | +import io.kafbat.ui.service.metrics.scrape.ScrapedClusterState; |
| 13 | +import io.kafbat.ui.service.rbac.AccessControlService; |
| 14 | +import java.time.Instant; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.stream.Collectors; |
| 21 | +import java.util.stream.Stream; |
| 22 | +import org.apache.kafka.clients.admin.ConsumerGroupDescription; |
| 23 | +import org.apache.kafka.clients.admin.ConsumerGroupListing; |
| 24 | +import org.apache.kafka.clients.admin.MemberAssignment; |
| 25 | +import org.apache.kafka.clients.admin.MemberDescription; |
| 26 | +import org.apache.kafka.common.ConsumerGroupState; |
| 27 | +import org.apache.kafka.common.TopicPartition; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import reactor.core.publisher.Mono; |
| 31 | + |
| 32 | +class ConsumerGroupServiceTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void getConsumerGroupsForTopicConsumerGroups() { |
| 36 | + // given |
| 37 | + ClustersProperties.Cluster clusterProperties = new ClustersProperties.Cluster(); |
| 38 | + clusterProperties.setName("test"); |
| 39 | + |
| 40 | + ClustersProperties clustersProperties = new ClustersProperties(); |
| 41 | + clustersProperties.getClusters().add(clusterProperties); |
| 42 | + |
| 43 | + |
| 44 | + KafkaCluster cluster = KafkaCluster.builder() |
| 45 | + .name("test") |
| 46 | + .originalProperties(clusterProperties) |
| 47 | + .build(); |
| 48 | + |
| 49 | + ReactiveAdminClient client = Mockito.mock(ReactiveAdminClient.class); |
| 50 | + AdminClientService admin = Mockito.mock(AdminClientService.class); |
| 51 | + Mockito.when(admin.get(cluster)).thenReturn(Mono.just(client)); |
| 52 | + |
| 53 | + final String topic = UUID.randomUUID().toString(); |
| 54 | + final String anotherTopic = UUID.randomUUID().toString(); |
| 55 | + |
| 56 | + Map<String, ScrapedClusterState.ConsumerGroupState> consumersWithTopic = |
| 57 | + Stream.generate(() -> generate( |
| 58 | + List.of(new TopicPartition(topic, 0)), |
| 59 | + Map.of(new TopicPartition(topic, 0), 100L), |
| 60 | + ConsumerGroupState.DEAD |
| 61 | + )).limit(10).collect(Collectors.toMap( |
| 62 | + ScrapedClusterState.ConsumerGroupState::group, |
| 63 | + s -> s |
| 64 | + )); |
| 65 | + |
| 66 | + Map<String, ScrapedClusterState.ConsumerGroupState> consumersWithoutTopic = |
| 67 | + Stream.generate(() -> generate( |
| 68 | + List.of(new TopicPartition(anotherTopic, 0)), |
| 69 | + Map.of(new TopicPartition(anotherTopic, 0), 100L), |
| 70 | + ConsumerGroupState.DEAD |
| 71 | + )).limit(10).collect(Collectors.toMap( |
| 72 | + ScrapedClusterState.ConsumerGroupState::group, |
| 73 | + s -> s |
| 74 | + )); |
| 75 | + |
| 76 | + Map<String, ScrapedClusterState.ConsumerGroupState> stableConsumersWithTopic = |
| 77 | + Stream.generate(() -> generate( |
| 78 | + List.of(new TopicPartition(topic, 0)), |
| 79 | + Map.of(new TopicPartition(topic, 0), 100L), |
| 80 | + ConsumerGroupState.STABLE |
| 81 | + )).limit(10).collect(Collectors.toMap( |
| 82 | + ScrapedClusterState.ConsumerGroupState::group, |
| 83 | + s -> s |
| 84 | + )); |
| 85 | + |
| 86 | + Map<String, ScrapedClusterState.ConsumerGroupState> stableConsumersWithoutTopic = |
| 87 | + Stream.generate(() -> generate( |
| 88 | + List.of(new TopicPartition(anotherTopic, 0)), |
| 89 | + Map.of(new TopicPartition(anotherTopic, 0), 100L), |
| 90 | + ConsumerGroupState.STABLE |
| 91 | + )).limit(10).collect(Collectors.toMap( |
| 92 | + ScrapedClusterState.ConsumerGroupState::group, |
| 93 | + s -> s |
| 94 | + )); |
| 95 | + |
| 96 | + Map<String, ScrapedClusterState.ConsumerGroupState> consumerGroupStates = new HashMap<>(); |
| 97 | + consumerGroupStates.putAll(consumersWithTopic); |
| 98 | + consumerGroupStates.putAll(consumersWithoutTopic); |
| 99 | + consumerGroupStates.putAll(stableConsumersWithTopic); |
| 100 | + consumerGroupStates.putAll(stableConsumersWithoutTopic); |
| 101 | + |
| 102 | + Mockito.when(client.listConsumerGroups()).thenReturn(Mono.just( |
| 103 | + consumerGroupStates.keySet() |
| 104 | + .stream() |
| 105 | + .map(s -> new ConsumerGroupListing(s, false)) |
| 106 | + .toList() |
| 107 | + )); |
| 108 | + |
| 109 | + Mockito.when(client.listConsumerGroupNames()).thenReturn(Mono.just( |
| 110 | + List.copyOf(consumerGroupStates.keySet()) |
| 111 | + )); |
| 112 | + |
| 113 | + Mockito.when(client.listTopicOffsets(Mockito.eq(topic), Mockito.any(), Mockito.eq(false))) |
| 114 | + .thenReturn(Mono.just(Map.of(new TopicPartition(topic, 0), 100L))); |
| 115 | + |
| 116 | + Mockito.when(client.describeConsumerGroups( |
| 117 | + Mockito.any()) |
| 118 | + ).thenReturn( |
| 119 | + Mono.just( |
| 120 | + consumerGroupStates.values().stream() |
| 121 | + .collect(Collectors.toMap( |
| 122 | + ScrapedClusterState.ConsumerGroupState::group, |
| 123 | + ScrapedClusterState.ConsumerGroupState::description |
| 124 | + )) |
| 125 | + ) |
| 126 | + ); |
| 127 | + |
| 128 | + Mockito.when(client.listConsumerGroupOffsets(Mockito.any(), Mockito.any())).thenAnswer( |
| 129 | + a -> { |
| 130 | + List<String> groupIds = (List<String>) a.getArgument(0); |
| 131 | + var table = ImmutableTable.<String, TopicPartition, Long>builder(); |
| 132 | + for (String groupId : groupIds) { |
| 133 | + ScrapedClusterState.ConsumerGroupState state = consumerGroupStates.get(groupId); |
| 134 | + for (Map.Entry<TopicPartition, Long> entry : state.committedOffsets().entrySet()) { |
| 135 | + table.put(groupId, entry.getKey(), entry.getValue()); |
| 136 | + } |
| 137 | + } |
| 138 | + return Mono.just(table.build()); |
| 139 | + } |
| 140 | + ); |
| 141 | + |
| 142 | + ScrapedClusterState state = ScrapedClusterState.builder() |
| 143 | + .scrapeFinishedAt(Instant.now()) |
| 144 | + .nodesStates(Map.of()) |
| 145 | + .topicStates(Map.of()) |
| 146 | + .consumerGroupsStates(consumerGroupStates) |
| 147 | + .build(); |
| 148 | + |
| 149 | + Statistics statistics = Statistics.builder() |
| 150 | + .status(ServerStatusDTO.ONLINE) |
| 151 | + .version("Unknown") |
| 152 | + .features(List.of()) |
| 153 | + .clusterDescription(ReactiveAdminClient.ClusterDescription.empty()) |
| 154 | + .metrics(Metrics.empty()) |
| 155 | + .clusterState(state) |
| 156 | + .build(); |
| 157 | + |
| 158 | + StatisticsCache cache = Mockito.mock(StatisticsCache.class); |
| 159 | + Mockito.when(cache.get(cluster)).thenReturn(statistics); |
| 160 | + |
| 161 | + AccessControlService acl = Mockito.mock(AccessControlService.class); |
| 162 | + ConsumerGroupService consumerGroupService = |
| 163 | + new ConsumerGroupService(admin, acl, clustersProperties, cache); |
| 164 | + |
| 165 | + // should |
| 166 | + List<InternalTopicConsumerGroup> groups = |
| 167 | + consumerGroupService.getConsumerGroupsForTopic(cluster, topic).block(); |
| 168 | + |
| 169 | + assertThat(groups).size().isEqualTo( |
| 170 | + consumersWithTopic.size() + stableConsumersWithTopic.size() |
| 171 | + ); |
| 172 | + |
| 173 | + List<String> resultedGroupIds = groups.stream().map(InternalTopicConsumerGroup::getGroupId).toList(); |
| 174 | + assertThat(resultedGroupIds).containsAll(consumersWithTopic.keySet()); |
| 175 | + |
| 176 | + assertThat(resultedGroupIds).containsAll(stableConsumersWithTopic.keySet()); |
| 177 | + } |
| 178 | + |
| 179 | + private ScrapedClusterState.ConsumerGroupState generate( |
| 180 | + List<TopicPartition> topicPartitions, |
| 181 | + Map<TopicPartition, Long> lastOffsets, |
| 182 | + ConsumerGroupState state |
| 183 | + ) { |
| 184 | + final String name = UUID.randomUUID().toString(); |
| 185 | + Map<TopicPartition, Long> commited = topicPartitions.stream() |
| 186 | + .map(tp -> Map.entry(tp, lastOffsets.get(tp) - 1)) |
| 187 | + .collect(Collectors.toMap( |
| 188 | + Map.Entry::getKey, |
| 189 | + Map.Entry::getValue |
| 190 | + )); |
| 191 | + |
| 192 | + List<MemberDescription> members = state.equals(ConsumerGroupState.STABLE) ? List.of( |
| 193 | + new MemberDescription( |
| 194 | + UUID.randomUUID().toString(), |
| 195 | + UUID.randomUUID().toString(), |
| 196 | + "localhost", |
| 197 | + new MemberAssignment(new HashSet<>(topicPartitions)) |
| 198 | + ) |
| 199 | + ) : List.of(); |
| 200 | + |
| 201 | + return new ScrapedClusterState.ConsumerGroupState( |
| 202 | + name, |
| 203 | + new ConsumerGroupDescription( |
| 204 | + name, |
| 205 | + false, |
| 206 | + members, "", |
| 207 | + state, |
| 208 | + null |
| 209 | + ), commited |
| 210 | + ); |
| 211 | + } |
| 212 | +} |
0 commit comments