TopicsService.java 20 KB

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