luchunliang commented on a change in pull request #3240:
URL: https://github.com/apache/incubator-inlong/pull/3240#discussion_r830450018
##########
File path:
inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/source/sortsdk/SortSdkSource.java
##########
@@ -132,30 +136,78 @@ private void initReloadExecutor() {
* <p> Create new clients with new sort task id, and remove the finished
or scheduled ones. </p>
*
* <p> Current version of SortSdk <b>DO NOT</b> support to get the
corresponding sort id of {@link SortClient}.
- * Hence, the maintenance of mapping of {@literal sortId, SortClient}
should be done by Source itself. Which
- * is not elegant, the <b>REMOVE</b> of expire clients will <b>NOT</b> be
supported right now. </p>
+ * Hence, the maintenance of mapping of {@literal sortId, SortClient}
should be done by Source itself.
*/
- private void reload() {
+ private void reloadAll() {
final List<SortTaskConfig> configs =
SortClusterConfigHolder.getClusterConfig().getSortTasks();
LOG.info("start to reload SortSdkSource");
+ this.startNewClients(configs);
+ this.stopExpiryClients(configs);
+ this.updateAllClientConfig();
+ }
- // Start new clients
- for (SortTaskConfig taskConfig : configs) {
-
- // If exits, skip.
- final String sortId = taskConfig.getName();
- SortClient client = this.clients.get(sortId);
- if (client != null) {
- continue;
- }
-
- // Otherwise, new one client.
- client = this.newClient(sortId);
- if (client != null) {
- this.clients.put(sortId, client);
- }
- }
+ /**
+ * Start a new client from SortTaskConfig.
+ * <p>
+ * If the sortId is in configs, but not in active clients, start it.
+ * </p>
+ *
+ * @param configs Updated SortTaskConfig
+ */
+ private void startNewClients(final List<SortTaskConfig> configs) {
+ configs.stream()
+ .map(SortTaskConfig::getName)
+ .filter(sortId -> !clients.containsKey(sortId))
+ .forEach(sortId -> {
+ final SortClient client = this.newClient(sortId);
+ Optional.ofNullable(client)
+ .ifPresent(c -> clients.put(sortId, c));
+ });
+ }
+
+ /**
+ * Stop an expiry client from SortTaskConfig.
+ * <p>
+ * If the sortId is not in active clients, but not in configs, stop it.
+ * </p>
+ *
+ * @param configs Updated SortTaskConfig
+ */
+ private void stopExpiryClients(final List<SortTaskConfig> configs) {
+ Set<String> updatedSortIds = configs.stream()
+ .map(SortTaskConfig::getName)
+ .collect(Collectors.toSet());
+
+ clients.keySet().stream()
+ .filter(updatedSortIds::contains)
+ .forEach(sortId -> {
+ final SortClient client = clients.get(sortId);
+ try {
+ client.close();
+ } catch (Throwable th) {
+ LOG.error("Got a throwable when close client {}, {}",
sortId, th.getMessage());
+ }
+ clients.remove(sortId);
+ });
+ }
+
+ /**
+ * Update all client config.
+ */
+ private void updateAllClientConfig() {
+ clients.values().stream()
+ .map(SortClient::getConfig)
+ .forEach(this::updateClientConfig);
+ }
+
+ /**
+ * Update one client config.
+ *
+ * @param config The config to be updated.
+ */
+ private void updateClientConfig(SortClientConfig config) {
+
config.setManagerApiUrl(CommonPropertiesHolder.getSourceConfigManagerUrl());
Review comment:
"CommonPropertiesHolder.getSourceConfigManagerUrl()" do not support to
periodiclly update.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]