OffsetsResetServiceTest.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.provectus.kafka.ui.service;
  2. import static org.assertj.core.api.Assertions.assertThat;
  3. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  4. import com.provectus.kafka.ui.AbstractBaseTest;
  5. import com.provectus.kafka.ui.exception.NotFoundException;
  6. import com.provectus.kafka.ui.exception.ValidationException;
  7. import com.provectus.kafka.ui.model.KafkaCluster;
  8. import java.time.Duration;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Properties;
  12. import java.util.UUID;
  13. import java.util.regex.Pattern;
  14. import java.util.stream.Collectors;
  15. import java.util.stream.IntStream;
  16. import java.util.stream.Stream;
  17. import org.apache.kafka.clients.admin.NewTopic;
  18. import org.apache.kafka.clients.consumer.Consumer;
  19. import org.apache.kafka.clients.consumer.ConsumerConfig;
  20. import org.apache.kafka.clients.consumer.OffsetAndMetadata;
  21. import org.apache.kafka.clients.producer.KafkaProducer;
  22. import org.apache.kafka.clients.producer.ProducerConfig;
  23. import org.apache.kafka.clients.producer.ProducerRecord;
  24. import org.apache.kafka.common.TopicPartition;
  25. import org.apache.kafka.common.serialization.BytesSerializer;
  26. import org.apache.kafka.common.utils.Bytes;
  27. import org.junit.jupiter.api.AfterEach;
  28. import org.junit.jupiter.api.BeforeEach;
  29. import org.junit.jupiter.api.Test;
  30. import org.springframework.test.context.ContextConfiguration;
  31. @ContextConfiguration(initializers = {AbstractBaseTest.Initializer.class})
  32. public class OffsetsResetServiceTest extends AbstractBaseTest {
  33. private static final int PARTITIONS = 5;
  34. private static final KafkaCluster CLUSTER =
  35. KafkaCluster.builder()
  36. .name(LOCAL)
  37. .bootstrapServers(kafka.getBootstrapServers())
  38. .properties(new Properties())
  39. .build();
  40. private final String groupId = "OffsetsResetServiceTestGroup-" + UUID.randomUUID();
  41. private final String topic = "OffsetsResetServiceTestTopic-" + UUID.randomUUID();
  42. private KafkaService kafkaService;
  43. private OffsetsResetService offsetsResetService;
  44. @BeforeEach
  45. void init() {
  46. kafkaService = new KafkaService(null, null, null, null);
  47. kafkaService.setClientTimeout(5_000);
  48. offsetsResetService = new OffsetsResetService(kafkaService);
  49. createTopic(new NewTopic(topic, PARTITIONS, (short) 1));
  50. createConsumerGroup();
  51. }
  52. @AfterEach
  53. void cleanUp() {
  54. deleteTopic(topic);
  55. }
  56. private void createConsumerGroup() {
  57. try (var consumer = groupConsumer()) {
  58. consumer.subscribe(Pattern.compile("no-such-topic-pattern"));
  59. consumer.poll(Duration.ofMillis(200));
  60. consumer.commitSync();
  61. }
  62. }
  63. @Test
  64. void failsIfGroupDoesNotExists() {
  65. assertThatThrownBy(
  66. () -> offsetsResetService.resetToEarliest(CLUSTER, "non-existing-group", topic, null))
  67. .isInstanceOf(NotFoundException.class);
  68. assertThatThrownBy(
  69. () -> offsetsResetService.resetToLatest(CLUSTER, "non-existing-group", topic, null))
  70. .isInstanceOf(NotFoundException.class);
  71. assertThatThrownBy(() -> offsetsResetService
  72. .resetToTimestamp(CLUSTER, "non-existing-group", topic, null, System.currentTimeMillis()))
  73. .isInstanceOf(NotFoundException.class);
  74. assertThatThrownBy(
  75. () -> offsetsResetService.resetToOffsets(CLUSTER, "non-existing-group", topic, Map.of()))
  76. .isInstanceOf(NotFoundException.class);
  77. }
  78. @Test
  79. void failsIfGroupIsActive() {
  80. // starting consumer to activate group
  81. try (var consumer = groupConsumer()) {
  82. consumer.subscribe(Pattern.compile("no-such-topic-pattern"));
  83. consumer.poll(Duration.ofMillis(100));
  84. assertThatThrownBy(() -> offsetsResetService.resetToEarliest(CLUSTER, groupId, topic, null))
  85. .isInstanceOf(ValidationException.class);
  86. assertThatThrownBy(() -> offsetsResetService.resetToLatest(CLUSTER, groupId, topic, null))
  87. .isInstanceOf(ValidationException.class);
  88. assertThatThrownBy(() -> offsetsResetService
  89. .resetToTimestamp(CLUSTER, groupId, topic, null, System.currentTimeMillis()))
  90. .isInstanceOf(ValidationException.class);
  91. assertThatThrownBy(
  92. () -> offsetsResetService.resetToOffsets(CLUSTER, groupId, topic, Map.of()))
  93. .isInstanceOf(ValidationException.class);
  94. }
  95. }
  96. @Test
  97. void resetToOffsets() {
  98. sendMsgsToPartition(Map.of(0, 10, 1, 10, 2, 10));
  99. var expectedOffsets = Map.of(0, 5L, 1, 5L, 2, 5L);
  100. offsetsResetService.resetToOffsets(CLUSTER, groupId, topic, expectedOffsets);
  101. assertOffsets(expectedOffsets);
  102. }
  103. @Test
  104. void resetToOffsetsCommitsEarliestOrLatestOffsetsIfOffsetsBoundsNotValid() {
  105. sendMsgsToPartition(Map.of(0, 10, 1, 10, 2, 10));
  106. var offsetsWithInValidBounds = Map.of(0, -2L, 1, 5L, 2, 500L);
  107. var expectedOffsets = Map.of(0, 0L, 1, 5L, 2, 10L);
  108. offsetsResetService.resetToOffsets(CLUSTER, groupId, topic, offsetsWithInValidBounds);
  109. assertOffsets(expectedOffsets);
  110. }
  111. @Test
  112. void resetToEarliest() {
  113. sendMsgsToPartition(Map.of(0, 10, 1, 10, 2, 10));
  114. commit(Map.of(0, 5L, 1, 5L, 2, 5L));
  115. offsetsResetService.resetToEarliest(CLUSTER, groupId, topic, List.of(0, 1));
  116. assertOffsets(Map.of(0, 0L, 1, 0L, 2, 5L));
  117. commit(Map.of(0, 5L, 1, 5L, 2, 5L));
  118. offsetsResetService.resetToEarliest(CLUSTER, groupId, topic, null);
  119. assertOffsets(Map.of(0, 0L, 1, 0L, 2, 0L, 3, 0L, 4, 0L));
  120. }
  121. @Test
  122. void resetToLatest() {
  123. sendMsgsToPartition(Map.of(0, 10, 1, 10, 2, 10, 3, 10, 4, 10));
  124. commit(Map.of(0, 5L, 1, 5L, 2, 5L));
  125. offsetsResetService.resetToLatest(CLUSTER, groupId, topic, List.of(0, 1));
  126. assertOffsets(Map.of(0, 10L, 1, 10L, 2, 5L));
  127. commit(Map.of(0, 5L, 1, 5L, 2, 5L));
  128. offsetsResetService.resetToLatest(CLUSTER, groupId, topic, null);
  129. assertOffsets(Map.of(0, 10L, 1, 10L, 2, 10L, 3, 10L, 4, 10L));
  130. }
  131. @Test
  132. void resetToTimestamp() {
  133. send(
  134. Stream.of(
  135. new ProducerRecord<Bytes, Bytes>(topic, 0, 1000L, null, null),
  136. new ProducerRecord<Bytes, Bytes>(topic, 0, 1500L, null, null),
  137. new ProducerRecord<Bytes, Bytes>(topic, 0, 2000L, null, null),
  138. new ProducerRecord<Bytes, Bytes>(topic, 1, 1000L, null, null),
  139. new ProducerRecord<Bytes, Bytes>(topic, 1, 2000L, null, null),
  140. new ProducerRecord<Bytes, Bytes>(topic, 2, 1000L, null, null),
  141. new ProducerRecord<Bytes, Bytes>(topic, 2, 1100L, null, null),
  142. new ProducerRecord<Bytes, Bytes>(topic, 2, 1200L, null, null)));
  143. offsetsResetService.resetToTimestamp(CLUSTER, groupId, topic, List.of(0, 1, 2, 3), 1600L);
  144. assertOffsets(Map.of(0, 2L, 1, 1L, 2, 3L, 3, 0L));
  145. }
  146. private void commit(Map<Integer, Long> offsetsToCommit) {
  147. try (var consumer = groupConsumer()) {
  148. consumer.commitSync(
  149. offsetsToCommit.entrySet().stream()
  150. .collect(Collectors.toMap(
  151. e -> new TopicPartition(topic, e.getKey()),
  152. e -> new OffsetAndMetadata(e.getValue())))
  153. );
  154. }
  155. }
  156. private void sendMsgsToPartition(Map<Integer, Integer> msgsCountForPartitions) {
  157. Bytes bytes = new Bytes("noMatter".getBytes());
  158. send(
  159. msgsCountForPartitions.entrySet().stream()
  160. .flatMap(e ->
  161. IntStream.range(0, e.getValue())
  162. .mapToObj(i -> new ProducerRecord<>(topic, e.getKey(), bytes, bytes))));
  163. }
  164. private void send(Stream<ProducerRecord<Bytes, Bytes>> toSend) {
  165. var properties = new Properties();
  166. properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
  167. var serializer = new BytesSerializer();
  168. try (var producer = new KafkaProducer<>(properties, serializer, serializer)) {
  169. toSend.forEach(producer::send);
  170. producer.flush();
  171. }
  172. }
  173. private void assertOffsets(Map<Integer, Long> expectedOffsets) {
  174. try (var consumer = groupConsumer()) {
  175. var tps = expectedOffsets.keySet().stream()
  176. .map(idx -> new TopicPartition(topic, idx))
  177. .collect(Collectors.toSet());
  178. var actualOffsets = consumer.committed(tps).entrySet().stream()
  179. .collect(Collectors.toMap(e -> e.getKey().partition(), e -> e.getValue().offset()));
  180. assertThat(actualOffsets).isEqualTo(expectedOffsets);
  181. }
  182. }
  183. private Consumer<?, ?> groupConsumer() {
  184. return kafkaService.createConsumer(CLUSTER, Map.of(ConsumerConfig.GROUP_ID_CONFIG, groupId));
  185. }
  186. }