TopicsService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package com.provectus.kafka.ui.service;
  2. import static java.util.stream.Collectors.toList;
  3. import static java.util.stream.Collectors.toMap;
  4. import com.provectus.kafka.ui.exception.TopicMetadataException;
  5. import com.provectus.kafka.ui.exception.TopicNotFoundException;
  6. import com.provectus.kafka.ui.exception.TopicRecreationException;
  7. import com.provectus.kafka.ui.exception.ValidationException;
  8. import com.provectus.kafka.ui.model.Feature;
  9. import com.provectus.kafka.ui.model.InternalLogDirStats;
  10. import com.provectus.kafka.ui.model.InternalPartition;
  11. import com.provectus.kafka.ui.model.InternalPartitionsOffsets;
  12. import com.provectus.kafka.ui.model.InternalReplica;
  13. import com.provectus.kafka.ui.model.InternalTopic;
  14. import com.provectus.kafka.ui.model.InternalTopicConfig;
  15. import com.provectus.kafka.ui.model.KafkaCluster;
  16. import com.provectus.kafka.ui.model.Metrics;
  17. import com.provectus.kafka.ui.model.PartitionsIncreaseDTO;
  18. import com.provectus.kafka.ui.model.PartitionsIncreaseResponseDTO;
  19. import com.provectus.kafka.ui.model.ReplicationFactorChangeDTO;
  20. import com.provectus.kafka.ui.model.ReplicationFactorChangeResponseDTO;
  21. import com.provectus.kafka.ui.model.Statistics;
  22. import com.provectus.kafka.ui.model.TopicCreationDTO;
  23. import com.provectus.kafka.ui.model.TopicUpdateDTO;
  24. import java.time.Duration;
  25. import java.util.Collection;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Optional;
  31. import java.util.function.Function;
  32. import java.util.stream.Collectors;
  33. import lombok.RequiredArgsConstructor;
  34. import org.apache.kafka.clients.admin.ConfigEntry;
  35. import org.apache.kafka.clients.admin.NewPartitionReassignment;
  36. import org.apache.kafka.clients.admin.NewPartitions;
  37. import org.apache.kafka.clients.admin.OffsetSpec;
  38. import org.apache.kafka.clients.admin.TopicDescription;
  39. import org.apache.kafka.common.Node;
  40. import org.apache.kafka.common.TopicPartition;
  41. import org.apache.kafka.common.errors.TopicExistsException;
  42. import org.springframework.beans.factory.annotation.Value;
  43. import org.springframework.stereotype.Service;
  44. import reactor.core.publisher.Mono;
  45. import reactor.util.retry.Retry;
  46. @Service
  47. @RequiredArgsConstructor
  48. public class TopicsService {
  49. private final AdminClientService adminClientService;
  50. private final DeserializationService deserializationService;
  51. private final StatisticsCache statisticsCache;
  52. @Value("${topic.recreate.maxRetries:15}")
  53. private int recreateMaxRetries;
  54. @Value("${topic.recreate.delay.seconds:1}")
  55. private int recreateDelayInSeconds;
  56. @Value("${topic.load.after.create.maxRetries:10}")
  57. private int loadTopicAfterCreateRetries;
  58. @Value("${topic.load.after.create.delay.ms:500}")
  59. private int loadTopicAfterCreateDelayInMs;
  60. public Mono<List<InternalTopic>> loadTopics(KafkaCluster c, List<String> topics) {
  61. if (topics.isEmpty()) {
  62. return Mono.just(List.of());
  63. }
  64. return adminClientService.get(c)
  65. .flatMap(ac ->
  66. ac.describeTopics(topics).zipWith(ac.getTopicsConfig(topics, false),
  67. (descriptions, configs) -> {
  68. statisticsCache.update(c, descriptions, configs);
  69. return getPartitionOffsets(descriptions, ac).map(offsets -> {
  70. var metrics = statisticsCache.get(c);
  71. return createList(
  72. topics,
  73. descriptions,
  74. configs,
  75. offsets,
  76. metrics.getMetrics(),
  77. metrics.getLogDirInfo()
  78. );
  79. });
  80. })).flatMap(Function.identity());
  81. }
  82. private Mono<InternalTopic> loadTopic(KafkaCluster c, String topicName) {
  83. return loadTopics(c, List.of(topicName))
  84. .flatMap(lst -> lst.stream().findFirst()
  85. .map(Mono::just)
  86. .orElse(Mono.error(TopicNotFoundException::new)));
  87. }
  88. /**
  89. * After creation topic can be invisible via API for some time.
  90. * To workaround this, we retyring topic loading until it becomes visible.
  91. */
  92. private Mono<InternalTopic> loadTopicAfterCreation(KafkaCluster c, String topicName) {
  93. return loadTopic(c, topicName)
  94. .retryWhen(
  95. Retry
  96. .fixedDelay(
  97. loadTopicAfterCreateRetries,
  98. Duration.ofMillis(loadTopicAfterCreateDelayInMs)
  99. )
  100. .filter(TopicNotFoundException.class::isInstance)
  101. .onRetryExhaustedThrow((spec, sig) ->
  102. new TopicMetadataException(
  103. String.format(
  104. "Error while loading created topic '%s' - topic is not visible via API "
  105. + "after waiting for %d ms.",
  106. topicName,
  107. loadTopicAfterCreateDelayInMs * loadTopicAfterCreateRetries)))
  108. );
  109. }
  110. private List<InternalTopic> createList(List<String> orderedNames,
  111. Map<String, TopicDescription> descriptions,
  112. Map<String, List<ConfigEntry>> configs,
  113. InternalPartitionsOffsets partitionsOffsets,
  114. Metrics metrics,
  115. InternalLogDirStats logDirInfo) {
  116. return orderedNames.stream()
  117. .filter(descriptions::containsKey)
  118. .map(t -> InternalTopic.from(
  119. descriptions.get(t),
  120. configs.getOrDefault(t, List.of()),
  121. partitionsOffsets,
  122. metrics,
  123. logDirInfo
  124. ))
  125. .collect(toList());
  126. }
  127. private Mono<InternalPartitionsOffsets> getPartitionOffsets(Map<String, TopicDescription>
  128. descriptions,
  129. ReactiveAdminClient ac) {
  130. var topicPartitions = descriptions.values().stream()
  131. .flatMap(desc ->
  132. desc.partitions().stream()
  133. // list offsets should only be applied to partitions with existing leader
  134. // (see ReactiveAdminClient.listOffsetsUnsafe(..) docs)
  135. .filter(tp -> tp.leader() != null)
  136. .map(p -> new TopicPartition(desc.name(), p.partition())))
  137. .collect(toList());
  138. return ac.listOffsetsUnsafe(topicPartitions, OffsetSpec.earliest())
  139. .zipWith(ac.listOffsetsUnsafe(topicPartitions, OffsetSpec.latest()),
  140. (earliest, latest) ->
  141. topicPartitions.stream()
  142. .filter(tp -> earliest.containsKey(tp) && latest.containsKey(tp))
  143. .map(tp ->
  144. Map.entry(tp,
  145. new InternalPartitionsOffsets.Offsets(
  146. earliest.get(tp), latest.get(tp))))
  147. .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)))
  148. .map(InternalPartitionsOffsets::new);
  149. }
  150. public Mono<InternalTopic> getTopicDetails(KafkaCluster cluster, String topicName) {
  151. return loadTopic(cluster, topicName);
  152. }
  153. public Mono<List<ConfigEntry>> getTopicConfigs(KafkaCluster cluster, String topicName) {
  154. return adminClientService.get(cluster)
  155. .flatMap(ac -> ac.getTopicsConfig(List.of(topicName), true))
  156. .map(m -> m.values().stream().findFirst().orElseThrow(TopicNotFoundException::new));
  157. }
  158. private Mono<InternalTopic> createTopic(KafkaCluster c, ReactiveAdminClient adminClient,
  159. Mono<TopicCreationDTO> topicCreation) {
  160. return topicCreation.flatMap(topicData ->
  161. adminClient.createTopic(
  162. topicData.getName(),
  163. topicData.getPartitions(),
  164. topicData.getReplicationFactor(),
  165. topicData.getConfigs()
  166. ).thenReturn(topicData)
  167. )
  168. .onErrorMap(t -> new TopicMetadataException(t.getMessage(), t))
  169. .flatMap(topicData -> loadTopicAfterCreation(c, topicData.getName()));
  170. }
  171. public Mono<InternalTopic> createTopic(KafkaCluster cluster, Mono<TopicCreationDTO> topicCreation) {
  172. return adminClientService.get(cluster)
  173. .flatMap(ac -> createTopic(cluster, ac, topicCreation));
  174. }
  175. public Mono<InternalTopic> recreateTopic(KafkaCluster cluster, String topicName) {
  176. return loadTopic(cluster, topicName)
  177. .flatMap(t -> deleteTopic(cluster, topicName)
  178. .thenReturn(t)
  179. .delayElement(Duration.ofSeconds(recreateDelayInSeconds))
  180. .flatMap(topic ->
  181. adminClientService.get(cluster)
  182. .flatMap(ac ->
  183. ac.createTopic(
  184. topic.getName(),
  185. topic.getPartitionCount(),
  186. topic.getReplicationFactor(),
  187. topic.getTopicConfigs()
  188. .stream()
  189. .collect(Collectors.toMap(InternalTopicConfig::getName,
  190. InternalTopicConfig::getValue))
  191. )
  192. .thenReturn(topicName)
  193. )
  194. .retryWhen(
  195. Retry.fixedDelay(recreateMaxRetries, Duration.ofSeconds(recreateDelayInSeconds))
  196. .filter(TopicExistsException.class::isInstance)
  197. .onRetryExhaustedThrow((a, b) ->
  198. new TopicRecreationException(topicName,
  199. recreateMaxRetries * recreateDelayInSeconds))
  200. )
  201. .flatMap(a -> loadTopicAfterCreation(cluster, topicName))
  202. )
  203. );
  204. }
  205. private Mono<InternalTopic> updateTopic(KafkaCluster cluster,
  206. String topicName,
  207. TopicUpdateDTO topicUpdate) {
  208. return adminClientService.get(cluster)
  209. .flatMap(ac ->
  210. ac.updateTopicConfig(topicName, topicUpdate.getConfigs())
  211. .then(loadTopic(cluster, topicName)));
  212. }
  213. public Mono<InternalTopic> updateTopic(KafkaCluster cl, String topicName,
  214. Mono<TopicUpdateDTO> topicUpdate) {
  215. return topicUpdate
  216. .flatMap(t -> updateTopic(cl, topicName, t));
  217. }
  218. private Mono<InternalTopic> changeReplicationFactor(
  219. KafkaCluster cluster,
  220. ReactiveAdminClient adminClient,
  221. String topicName,
  222. Map<TopicPartition, Optional<NewPartitionReassignment>> reassignments
  223. ) {
  224. return adminClient.alterPartitionReassignments(reassignments)
  225. .then(loadTopic(cluster, topicName));
  226. }
  227. /**
  228. * Change topic replication factor, works on brokers versions 5.4.x and higher
  229. */
  230. public Mono<ReplicationFactorChangeResponseDTO> changeReplicationFactor(
  231. KafkaCluster cluster,
  232. String topicName,
  233. ReplicationFactorChangeDTO replicationFactorChange) {
  234. return loadTopic(cluster, topicName).flatMap(topic -> adminClientService.get(cluster)
  235. .flatMap(ac -> {
  236. Integer actual = topic.getReplicationFactor();
  237. Integer requested = replicationFactorChange.getTotalReplicationFactor();
  238. Integer brokersCount = statisticsCache.get(cluster).getClusterDescription()
  239. .getNodes().size();
  240. if (requested.equals(actual)) {
  241. return Mono.error(
  242. new ValidationException(
  243. String.format("Topic already has replicationFactor %s.", actual)));
  244. }
  245. if (requested <= 0) {
  246. return Mono.error(
  247. new ValidationException(
  248. String.format("Requested replication factor (%s) should be greater or equal to 1.", requested)));
  249. }
  250. if (requested > brokersCount) {
  251. return Mono.error(
  252. new ValidationException(
  253. String.format("Requested replication factor %s more than brokers count %s.",
  254. requested, brokersCount)));
  255. }
  256. return changeReplicationFactor(cluster, ac, topicName,
  257. getPartitionsReassignments(cluster, topic,
  258. replicationFactorChange));
  259. })
  260. .map(t -> new ReplicationFactorChangeResponseDTO()
  261. .topicName(t.getName())
  262. .totalReplicationFactor(t.getReplicationFactor())));
  263. }
  264. private Map<TopicPartition, Optional<NewPartitionReassignment>> getPartitionsReassignments(
  265. KafkaCluster cluster,
  266. InternalTopic topic,
  267. ReplicationFactorChangeDTO replicationFactorChange) {
  268. // Current assignment map (Partition number -> List of brokers)
  269. Map<Integer, List<Integer>> currentAssignment = getCurrentAssignment(topic);
  270. // Brokers map (Broker id -> count)
  271. Map<Integer, Integer> brokersUsage = getBrokersMap(cluster, currentAssignment);
  272. int currentReplicationFactor = topic.getReplicationFactor();
  273. // If we should to increase Replication factor
  274. if (replicationFactorChange.getTotalReplicationFactor() > currentReplicationFactor) {
  275. // For each partition
  276. for (var assignmentList : currentAssignment.values()) {
  277. // Get brokers list sorted by usage
  278. var brokers = brokersUsage.entrySet().stream()
  279. .sorted(Map.Entry.comparingByValue())
  280. .map(Map.Entry::getKey)
  281. .collect(toList());
  282. // Iterate brokers and try to add them in assignment
  283. // while partition replicas count != requested replication factor
  284. for (Integer broker : brokers) {
  285. if (!assignmentList.contains(broker)) {
  286. assignmentList.add(broker);
  287. brokersUsage.merge(broker, 1, Integer::sum);
  288. }
  289. if (assignmentList.size() == replicationFactorChange.getTotalReplicationFactor()) {
  290. break;
  291. }
  292. }
  293. if (assignmentList.size() != replicationFactorChange.getTotalReplicationFactor()) {
  294. throw new ValidationException("Something went wrong during adding replicas");
  295. }
  296. }
  297. // If we should to decrease Replication factor
  298. } else if (replicationFactorChange.getTotalReplicationFactor() < currentReplicationFactor) {
  299. for (Map.Entry<Integer, List<Integer>> assignmentEntry : currentAssignment.entrySet()) {
  300. var partition = assignmentEntry.getKey();
  301. var brokers = assignmentEntry.getValue();
  302. // Get brokers list sorted by usage in reverse order
  303. var brokersUsageList = brokersUsage.entrySet().stream()
  304. .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  305. .map(Map.Entry::getKey)
  306. .collect(toList());
  307. // Iterate brokers and try to remove them from assignment
  308. // while partition replicas count != requested replication factor
  309. for (Integer broker : brokersUsageList) {
  310. // Check is the broker the leader of partition
  311. if (!topic.getPartitions().get(partition).getLeader()
  312. .equals(broker)) {
  313. brokers.remove(broker);
  314. brokersUsage.merge(broker, -1, Integer::sum);
  315. }
  316. if (brokers.size() == replicationFactorChange.getTotalReplicationFactor()) {
  317. break;
  318. }
  319. }
  320. if (brokers.size() != replicationFactorChange.getTotalReplicationFactor()) {
  321. throw new ValidationException("Something went wrong during removing replicas");
  322. }
  323. }
  324. } else {
  325. throw new ValidationException("Replication factor already equals requested");
  326. }
  327. // Return result map
  328. return currentAssignment.entrySet().stream().collect(toMap(
  329. e -> new TopicPartition(topic.getName(), e.getKey()),
  330. e -> Optional.of(new NewPartitionReassignment(e.getValue()))
  331. ));
  332. }
  333. private Map<Integer, List<Integer>> getCurrentAssignment(InternalTopic topic) {
  334. return topic.getPartitions().values().stream()
  335. .collect(toMap(
  336. InternalPartition::getPartition,
  337. p -> p.getReplicas().stream()
  338. .map(InternalReplica::getBroker)
  339. .collect(toList())
  340. ));
  341. }
  342. private Map<Integer, Integer> getBrokersMap(KafkaCluster cluster,
  343. Map<Integer, List<Integer>> currentAssignment) {
  344. Map<Integer, Integer> result = statisticsCache.get(cluster).getClusterDescription().getNodes()
  345. .stream()
  346. .map(Node::id)
  347. .collect(toMap(
  348. c -> c,
  349. c -> 0
  350. ));
  351. currentAssignment.values().forEach(brokers -> brokers
  352. .forEach(broker -> result.put(broker, result.get(broker) + 1)));
  353. return result;
  354. }
  355. public Mono<PartitionsIncreaseResponseDTO> increaseTopicPartitions(
  356. KafkaCluster cluster,
  357. String topicName,
  358. PartitionsIncreaseDTO partitionsIncrease) {
  359. return loadTopic(cluster, topicName).flatMap(topic ->
  360. adminClientService.get(cluster).flatMap(ac -> {
  361. Integer actualCount = topic.getPartitionCount();
  362. Integer requestedCount = partitionsIncrease.getTotalPartitionsCount();
  363. if (requestedCount < actualCount) {
  364. return Mono.error(
  365. new ValidationException(String.format(
  366. "Topic currently has %s partitions, which is higher than the requested %s.",
  367. actualCount, requestedCount)));
  368. }
  369. if (requestedCount.equals(actualCount)) {
  370. return Mono.error(
  371. new ValidationException(
  372. String.format("Topic already has %s partitions.", actualCount)));
  373. }
  374. Map<String, NewPartitions> newPartitionsMap = Collections.singletonMap(
  375. topicName,
  376. NewPartitions.increaseTo(partitionsIncrease.getTotalPartitionsCount())
  377. );
  378. return ac.createPartitions(newPartitionsMap)
  379. .then(loadTopic(cluster, topicName));
  380. }).map(t -> new PartitionsIncreaseResponseDTO()
  381. .topicName(t.getName())
  382. .totalPartitionsCount(t.getPartitionCount())
  383. )
  384. );
  385. }
  386. public Mono<Void> deleteTopic(KafkaCluster cluster, String topicName) {
  387. if (statisticsCache.get(cluster).getFeatures().contains(Feature.TOPIC_DELETION)) {
  388. return adminClientService.get(cluster).flatMap(c -> c.deleteTopic(topicName))
  389. .doOnSuccess(t -> statisticsCache.onTopicDelete(cluster, topicName));
  390. } else {
  391. return Mono.error(new ValidationException("Topic deletion restricted"));
  392. }
  393. }
  394. public Mono<InternalTopic> cloneTopic(
  395. KafkaCluster cluster, String topicName, String newTopicName) {
  396. return loadTopic(cluster, topicName).flatMap(topic ->
  397. adminClientService.get(cluster)
  398. .flatMap(ac ->
  399. ac.createTopic(
  400. newTopicName,
  401. topic.getPartitionCount(),
  402. topic.getReplicationFactor(),
  403. topic.getTopicConfigs()
  404. .stream()
  405. .collect(Collectors
  406. .toMap(InternalTopicConfig::getName, InternalTopicConfig::getValue))
  407. )
  408. ).thenReturn(newTopicName)
  409. .flatMap(a -> loadTopicAfterCreation(cluster, newTopicName))
  410. );
  411. }
  412. public Mono<List<InternalTopic>> getTopicsForPagination(KafkaCluster cluster) {
  413. Statistics stats = statisticsCache.get(cluster);
  414. return filterExisting(cluster, stats.getTopicDescriptions().keySet())
  415. .map(lst -> lst.stream()
  416. .map(topicName ->
  417. InternalTopic.from(
  418. stats.getTopicDescriptions().get(topicName),
  419. stats.getTopicConfigs().getOrDefault(topicName, List.of()),
  420. InternalPartitionsOffsets.empty(),
  421. stats.getMetrics(),
  422. stats.getLogDirInfo()))
  423. .collect(toList())
  424. );
  425. }
  426. private Mono<List<String>> filterExisting(KafkaCluster cluster, Collection<String> topics) {
  427. return adminClientService.get(cluster).flatMap(ac -> ac.listTopics(true))
  428. .map(existing -> existing.stream().filter(topics::contains).collect(toList()));
  429. }
  430. }