TopicsService.java 20 KB

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