leosanqing commented on code in PR #5332:
URL: https://github.com/apache/inlong/pull/5332#discussion_r936798078


##########
inlong-dataproxy/dataproxy-source/src/main/java/org/apache/inlong/dataproxy/heartbeat/HeartbeatManager.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.dataproxy.heartbeat;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.inlong.common.enums.ComponentTypeEnum;
+import org.apache.inlong.common.heartbeat.AbstractHeartbeatManager;
+import org.apache.inlong.common.heartbeat.GroupHeartbeat;
+import org.apache.inlong.common.heartbeat.HeartbeatMsg;
+import org.apache.inlong.common.heartbeat.StreamHeartbeat;
+import org.apache.inlong.dataproxy.config.ConfigManager;
+import org.apache.inlong.dataproxy.consts.ConfigConstants;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.TimeUnit;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+@Slf4j
+public class HeartbeatManager implements AbstractHeartbeatManager {
+
+    private CloseableHttpClient httpClient;
+    private Gson gson;
+
+    public HeartbeatManager() {
+        httpClient = constructHttpClient();
+        gson = new GsonBuilder().create();
+    }
+
+    public void start() {
+        Thread reportHeartbeatThread = new Thread(() -> {
+            while (true) {
+                HeartbeatMsg heartbeatMsg = buildHeartbeat();
+                reportHeartbeat(heartbeatMsg);
+                try {
+                    SECONDS.sleep(heartbeatInterval());
+                } catch (InterruptedException ex) {
+                    log.error("", ex);
+                }
+            }
+        });
+        reportHeartbeatThread.setDaemon(true);
+        reportHeartbeatThread.start();
+    }
+
+    @Override
+    public void reportHeartbeat(HeartbeatMsg heartbeat) {
+        ConfigManager configManager = ConfigManager.getInstance();
+        final String managerHost = 
configManager.getCommonProperties().get(ConfigConstants.MANAGER_HOST);
+        final String url =
+                "http://"; + managerHost + ConfigConstants.MANAGER_PATH + 
ConfigConstants.MANAGER_HEARTBEAT_REPORT;
+        try {
+            HttpPost post = new HttpPost(url);
+            String body = gson.toJson(heartbeat);
+            StringEntity stringEntity = new StringEntity(body);
+            stringEntity.setContentType("application/json");
+            post.setEntity(stringEntity);
+            CloseableHttpResponse response = httpClient.execute(post);
+            String isSuccess = EntityUtils.toString(response.getEntity());
+            if (StringUtils.isNotEmpty(isSuccess)
+                    && response.getStatusLine().getStatusCode() == 200) {
+                if (log.isDebugEnabled()) {
+                    log.debug("reportHeartbeat url {}, heartbeat: {}, return 
str {}", url, body, isSuccess);
+                }
+            }
+        } catch (Exception ex) {
+            log.error("reportHeartbeat failed for url {}", url, ex);
+        }
+    }
+
+    private synchronized CloseableHttpClient constructHttpClient() {
+        long timeoutInMs = TimeUnit.MILLISECONDS.toMillis(50000);
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout((int) timeoutInMs)
+                .setSocketTimeout((int) timeoutInMs).build();
+        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
+        httpClientBuilder.setDefaultRequestConfig(requestConfig);
+        return httpClientBuilder.build();
+    }
+
+    private HeartbeatMsg buildHeartbeat() {
+        ConfigManager configManager = ConfigManager.getInstance();
+        HeartbeatMsg heartbeatMsg = new HeartbeatMsg();
+        Map<String, String> commonProperties = 
configManager.getCommonProperties();
+        String localIp = commonProperties.get(ConfigConstants.PROXY_LOCAL_IP);
+        heartbeatMsg.setIp(localIp);
+        heartbeatMsg.setComponentType(ComponentTypeEnum.DataProxy.getName());
+        heartbeatMsg.setReportTime(System.currentTimeMillis());
+        Map<String, String> groupIdMappings = 
configManager.getGroupIdMappingProperties();
+        Map<String, Map<String, String>> streamIdMappings = 
configManager.getStreamIdMappingProperties();
+        Map<String, String> groupIdEnableMappings = 
configManager.getGroupIdEnableMappingProperties();
+        List<GroupHeartbeat> groupHeartbeats = new ArrayList<>();

Review Comment:
   Add blank line



-- 
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]

Reply via email to