RetryingKafkaConnectClient.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.provectus.kafka.ui.client;
  2. import static com.provectus.kafka.ui.config.ClustersProperties.ConnectCluster;
  3. import com.provectus.kafka.ui.config.ClustersProperties;
  4. import com.provectus.kafka.ui.connect.ApiClient;
  5. import com.provectus.kafka.ui.connect.api.KafkaConnectClientApi;
  6. import com.provectus.kafka.ui.connect.model.Connector;
  7. import com.provectus.kafka.ui.connect.model.ConnectorPlugin;
  8. import com.provectus.kafka.ui.connect.model.ConnectorPluginConfigValidationResponse;
  9. import com.provectus.kafka.ui.connect.model.ConnectorStatus;
  10. import com.provectus.kafka.ui.connect.model.ConnectorTask;
  11. import com.provectus.kafka.ui.connect.model.ConnectorTopics;
  12. import com.provectus.kafka.ui.connect.model.NewConnector;
  13. import com.provectus.kafka.ui.connect.model.TaskStatus;
  14. import com.provectus.kafka.ui.exception.KafkaConnectConflictReponseException;
  15. import com.provectus.kafka.ui.exception.ValidationException;
  16. import com.provectus.kafka.ui.util.WebClientConfigurator;
  17. import java.time.Duration;
  18. import java.util.List;
  19. import java.util.Map;
  20. import javax.annotation.Nullable;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.http.ResponseEntity;
  23. import org.springframework.util.unit.DataSize;
  24. import org.springframework.web.client.RestClientException;
  25. import org.springframework.web.reactive.function.client.WebClient;
  26. import org.springframework.web.reactive.function.client.WebClientResponseException;
  27. import reactor.core.publisher.Flux;
  28. import reactor.core.publisher.Mono;
  29. import reactor.util.retry.Retry;
  30. @Slf4j
  31. public class RetryingKafkaConnectClient extends KafkaConnectClientApi {
  32. private static final int MAX_RETRIES = 5;
  33. private static final Duration RETRIES_DELAY = Duration.ofMillis(200);
  34. public RetryingKafkaConnectClient(ConnectCluster config,
  35. @Nullable ClustersProperties.TruststoreConfig truststoreConfig,
  36. DataSize maxBuffSize) {
  37. super(new RetryingApiClient(config, truststoreConfig, maxBuffSize));
  38. }
  39. private static Retry conflictCodeRetry() {
  40. return Retry
  41. .fixedDelay(MAX_RETRIES, RETRIES_DELAY)
  42. .filter(e -> e instanceof WebClientResponseException.Conflict)
  43. .onRetryExhaustedThrow((spec, signal) ->
  44. new KafkaConnectConflictReponseException(
  45. (WebClientResponseException.Conflict) signal.failure()));
  46. }
  47. private static <T> Mono<T> withRetryOnConflict(Mono<T> publisher) {
  48. return publisher.retryWhen(conflictCodeRetry());
  49. }
  50. private static <T> Flux<T> withRetryOnConflict(Flux<T> publisher) {
  51. return publisher.retryWhen(conflictCodeRetry());
  52. }
  53. private static <T> Mono<T> withBadRequestErrorHandling(Mono<T> publisher) {
  54. return publisher
  55. .onErrorResume(WebClientResponseException.BadRequest.class, e ->
  56. Mono.error(new ValidationException("Invalid configuration")))
  57. .onErrorResume(WebClientResponseException.InternalServerError.class, e ->
  58. Mono.error(new ValidationException("Invalid configuration")));
  59. }
  60. @Override
  61. public Mono<Connector> createConnector(NewConnector newConnector) throws RestClientException {
  62. return withBadRequestErrorHandling(
  63. super.createConnector(newConnector)
  64. );
  65. }
  66. @Override
  67. public Mono<Connector> setConnectorConfig(String connectorName, Map<String, Object> requestBody)
  68. throws RestClientException {
  69. return withBadRequestErrorHandling(
  70. super.setConnectorConfig(connectorName, requestBody)
  71. );
  72. }
  73. @Override
  74. public Mono<ResponseEntity<Connector>> createConnectorWithHttpInfo(NewConnector newConnector)
  75. throws WebClientResponseException {
  76. return withRetryOnConflict(super.createConnectorWithHttpInfo(newConnector));
  77. }
  78. @Override
  79. public Mono<Void> deleteConnector(String connectorName) throws WebClientResponseException {
  80. return withRetryOnConflict(super.deleteConnector(connectorName));
  81. }
  82. @Override
  83. public Mono<ResponseEntity<Void>> deleteConnectorWithHttpInfo(String connectorName)
  84. throws WebClientResponseException {
  85. return withRetryOnConflict(super.deleteConnectorWithHttpInfo(connectorName));
  86. }
  87. @Override
  88. public Mono<Connector> getConnector(String connectorName) throws WebClientResponseException {
  89. return withRetryOnConflict(super.getConnector(connectorName));
  90. }
  91. @Override
  92. public Mono<ResponseEntity<Connector>> getConnectorWithHttpInfo(String connectorName)
  93. throws WebClientResponseException {
  94. return withRetryOnConflict(super.getConnectorWithHttpInfo(connectorName));
  95. }
  96. @Override
  97. public Mono<Map<String, Object>> getConnectorConfig(String connectorName) throws WebClientResponseException {
  98. return withRetryOnConflict(super.getConnectorConfig(connectorName));
  99. }
  100. @Override
  101. public Mono<ResponseEntity<Map<String, Object>>> getConnectorConfigWithHttpInfo(String connectorName)
  102. throws WebClientResponseException {
  103. return withRetryOnConflict(super.getConnectorConfigWithHttpInfo(connectorName));
  104. }
  105. @Override
  106. public Flux<ConnectorPlugin> getConnectorPlugins() throws WebClientResponseException {
  107. return withRetryOnConflict(super.getConnectorPlugins());
  108. }
  109. @Override
  110. public Mono<ResponseEntity<List<ConnectorPlugin>>> getConnectorPluginsWithHttpInfo()
  111. throws WebClientResponseException {
  112. return withRetryOnConflict(super.getConnectorPluginsWithHttpInfo());
  113. }
  114. @Override
  115. public Mono<ConnectorStatus> getConnectorStatus(String connectorName) throws WebClientResponseException {
  116. return withRetryOnConflict(super.getConnectorStatus(connectorName));
  117. }
  118. @Override
  119. public Mono<ResponseEntity<ConnectorStatus>> getConnectorStatusWithHttpInfo(String connectorName)
  120. throws WebClientResponseException {
  121. return withRetryOnConflict(super.getConnectorStatusWithHttpInfo(connectorName));
  122. }
  123. @Override
  124. public Mono<TaskStatus> getConnectorTaskStatus(String connectorName, Integer taskId)
  125. throws WebClientResponseException {
  126. return withRetryOnConflict(super.getConnectorTaskStatus(connectorName, taskId));
  127. }
  128. @Override
  129. public Mono<ResponseEntity<TaskStatus>> getConnectorTaskStatusWithHttpInfo(String connectorName, Integer taskId)
  130. throws WebClientResponseException {
  131. return withRetryOnConflict(super.getConnectorTaskStatusWithHttpInfo(connectorName, taskId));
  132. }
  133. @Override
  134. public Flux<ConnectorTask> getConnectorTasks(String connectorName) throws WebClientResponseException {
  135. return withRetryOnConflict(super.getConnectorTasks(connectorName));
  136. }
  137. @Override
  138. public Mono<ResponseEntity<List<ConnectorTask>>> getConnectorTasksWithHttpInfo(String connectorName)
  139. throws WebClientResponseException {
  140. return withRetryOnConflict(super.getConnectorTasksWithHttpInfo(connectorName));
  141. }
  142. @Override
  143. public Mono<Map<String, ConnectorTopics>> getConnectorTopics(String connectorName) throws WebClientResponseException {
  144. return withRetryOnConflict(super.getConnectorTopics(connectorName));
  145. }
  146. @Override
  147. public Mono<ResponseEntity<Map<String, ConnectorTopics>>> getConnectorTopicsWithHttpInfo(String connectorName)
  148. throws WebClientResponseException {
  149. return withRetryOnConflict(super.getConnectorTopicsWithHttpInfo(connectorName));
  150. }
  151. @Override
  152. public Flux<String> getConnectors(String search) throws WebClientResponseException {
  153. return withRetryOnConflict(super.getConnectors(search));
  154. }
  155. @Override
  156. public Mono<ResponseEntity<List<String>>> getConnectorsWithHttpInfo(String search) throws WebClientResponseException {
  157. return withRetryOnConflict(super.getConnectorsWithHttpInfo(search));
  158. }
  159. @Override
  160. public Mono<Void> pauseConnector(String connectorName) throws WebClientResponseException {
  161. return withRetryOnConflict(super.pauseConnector(connectorName));
  162. }
  163. @Override
  164. public Mono<ResponseEntity<Void>> pauseConnectorWithHttpInfo(String connectorName) throws WebClientResponseException {
  165. return withRetryOnConflict(super.pauseConnectorWithHttpInfo(connectorName));
  166. }
  167. @Override
  168. public Mono<Void> restartConnector(String connectorName, Boolean includeTasks, Boolean onlyFailed)
  169. throws WebClientResponseException {
  170. return withRetryOnConflict(super.restartConnector(connectorName, includeTasks, onlyFailed));
  171. }
  172. @Override
  173. public Mono<ResponseEntity<Void>> restartConnectorWithHttpInfo(String connectorName, Boolean includeTasks,
  174. Boolean onlyFailed) throws WebClientResponseException {
  175. return withRetryOnConflict(super.restartConnectorWithHttpInfo(connectorName, includeTasks, onlyFailed));
  176. }
  177. @Override
  178. public Mono<Void> restartConnectorTask(String connectorName, Integer taskId) throws WebClientResponseException {
  179. return withRetryOnConflict(super.restartConnectorTask(connectorName, taskId));
  180. }
  181. @Override
  182. public Mono<ResponseEntity<Void>> restartConnectorTaskWithHttpInfo(String connectorName, Integer taskId)
  183. throws WebClientResponseException {
  184. return withRetryOnConflict(super.restartConnectorTaskWithHttpInfo(connectorName, taskId));
  185. }
  186. @Override
  187. public Mono<Void> resumeConnector(String connectorName) throws WebClientResponseException {
  188. return super.resumeConnector(connectorName);
  189. }
  190. @Override
  191. public Mono<ResponseEntity<Void>> resumeConnectorWithHttpInfo(String connectorName)
  192. throws WebClientResponseException {
  193. return withRetryOnConflict(super.resumeConnectorWithHttpInfo(connectorName));
  194. }
  195. @Override
  196. public Mono<ResponseEntity<Connector>> setConnectorConfigWithHttpInfo(String connectorName,
  197. Map<String, Object> requestBody)
  198. throws WebClientResponseException {
  199. return withRetryOnConflict(super.setConnectorConfigWithHttpInfo(connectorName, requestBody));
  200. }
  201. @Override
  202. public Mono<ConnectorPluginConfigValidationResponse> validateConnectorPluginConfig(String pluginName,
  203. Map<String, Object> requestBody)
  204. throws WebClientResponseException {
  205. return withRetryOnConflict(super.validateConnectorPluginConfig(pluginName, requestBody));
  206. }
  207. @Override
  208. public Mono<ResponseEntity<ConnectorPluginConfigValidationResponse>> validateConnectorPluginConfigWithHttpInfo(
  209. String pluginName, Map<String, Object> requestBody) throws WebClientResponseException {
  210. return withRetryOnConflict(super.validateConnectorPluginConfigWithHttpInfo(pluginName, requestBody));
  211. }
  212. private static class RetryingApiClient extends ApiClient {
  213. public RetryingApiClient(ConnectCluster config,
  214. ClustersProperties.TruststoreConfig truststoreConfig,
  215. DataSize maxBuffSize) {
  216. super(buildWebClient(maxBuffSize, config, truststoreConfig), null, null);
  217. setBasePath(config.getAddress());
  218. setUsername(config.getUsername());
  219. setPassword(config.getPassword());
  220. }
  221. public static WebClient buildWebClient(DataSize maxBuffSize,
  222. ConnectCluster config,
  223. ClustersProperties.TruststoreConfig truststoreConfig) {
  224. return new WebClientConfigurator()
  225. .configureSsl(
  226. truststoreConfig,
  227. new ClustersProperties.KeystoreConfig(
  228. config.getKeystoreLocation(),
  229. config.getKeystorePassword()
  230. )
  231. )
  232. .configureBasicAuth(
  233. config.getUsername(),
  234. config.getPassword()
  235. )
  236. .configureBufferSize(maxBuffSize)
  237. .build();
  238. }
  239. }
  240. }