LvJiancheng commented on a change in pull request #2778:
URL: https://github.com/apache/incubator-inlong/pull/2778#discussion_r817595248



##########
File path: 
inlong-agent/agent-core/src/main/java/org/apache/inlong/agent/core/HeartbeatManager.java
##########
@@ -0,0 +1,163 @@
+/**
+ * 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.agent.core;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.inlong.agent.common.AbstractDaemon;
+import org.apache.inlong.agent.conf.AgentConfiguration;
+import org.apache.inlong.agent.core.job.JobManager;
+import org.apache.inlong.agent.core.job.JobWrapper;
+import org.apache.inlong.agent.utils.ExcuteLinux;
+import org.apache.inlong.agent.utils.HttpManager;
+import org.apache.inlong.common.pojo.agent.TaskSnapshotMessage;
+import org.apache.inlong.common.pojo.agent.TaskSnapshotRequest;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.inlong.agent.constant.AgentConstants.AGENT_LOCAL_IP;
+import static org.apache.inlong.agent.constant.AgentConstants.AGENT_LOCAL_UUID;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_REPORTSNAPSHOT_HTTP_PATH;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_VIP_HTTP_HOST;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_VIP_HTTP_PORT;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_VIP_HTTP_PREFIX_PATH;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_AGENT_MANAGER_REPORTSNAPSHOT_HTTP_PATH;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_AGENT_MANAGER_VIP_HTTP_PREFIX_PATH;
+import static 
org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_LOCAL_IP;
+
+public class HeartbeatManager  extends AbstractDaemon {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(HeartbeatManager.class);
+
+    private final AgentManager agentManager;
+    private final JobManager jobmanager;
+    private final AgentConfiguration conf;
+    private final HttpManager httpManager;
+    private final String baseManagerUrl;
+    private final String reportSnapshotUrl;
+
+    /**
+     * Init heartbeat manager.
+     */
+    public HeartbeatManager(AgentManager agentManager) {
+        this.agentManager = agentManager;
+        jobmanager = agentManager.getJobManager();
+        conf = AgentConfiguration.getAgentConf();
+        httpManager = new HttpManager(conf);
+        baseManagerUrl = buildBaseUrl();
+        reportSnapshotUrl = builReportSnapShotUrl(baseManagerUrl);
+    }
+
+    /**
+     * fetch heartbeat of job
+     * @return
+     */
+    private TaskSnapshotRequest getHeartBeat() {
+        AgentManager agentManager = new AgentManager();
+        HeartbeatManager heartbeatManager = new HeartbeatManager(agentManager);
+        JobManager jobManager = agentManager.getJobManager();
+        Map<String, JobWrapper> jobWrapperMap = jobManager.getJobs();
+
+        List<TaskSnapshotMessage> taskSnapshotMessageList = new ArrayList<>();
+        TaskSnapshotRequest taskSnapshotRequest = new TaskSnapshotRequest();
+        TaskSnapshotMessage snapshotMessage = new TaskSnapshotMessage();
+
+        Date date = new Date(System.currentTimeMillis());
+
+        for (Map.Entry<String, JobWrapper> entry:jobWrapperMap.entrySet()) {
+            if (StringUtils.isBlank(entry.getKey()) || entry.getValue() == 
null) {
+                LOGGER.info(" key : {} , value : {} exits 
null",entry.getKey(),entry.getValue());
+                continue;
+            }
+            String offset = entry.getValue().getSnapshot();
+            String jobId = entry.getKey();
+            snapshotMessage.setSnapshot(offset);
+            snapshotMessage.setJobId(Integer.valueOf(jobId));
+            taskSnapshotMessageList.add(snapshotMessage);
+
+        }
+        taskSnapshotRequest.setSnapshotList(taskSnapshotMessageList);
+        taskSnapshotRequest.setReportTime(date);
+        taskSnapshotRequest.setAgentIp(fetchLocalIp());
+        taskSnapshotRequest.setUuid(fetchLocalUuid());
+        return taskSnapshotRequest;
+    }
+
+    /**
+     * check agent ip from manager
+     */
+    private String fetchLocalIp() {
+        String localIp = AgentConfiguration.getAgentConf().get(AGENT_LOCAL_IP, 
DEFAULT_LOCAL_IP);
+        return localIp;
+    }
+
+    /**
+     * check agent uuid from manager
+     */
+    private String  fetchLocalUuid() {

Review comment:
       done




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