You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/08/03 12:07:28 UTC

[GitHub] [inlong] healchow commented on a diff in pull request #5332: [INLONG-5222][Manager][Agent][DataProxy] Add heartbeat mechanism for Inlong component cluster

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


##########
inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/cluster/agent/AgentClusterRequest.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.manager.pojo.cluster.agent;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.ToString;
+import org.apache.inlong.manager.common.enums.ClusterType;
+import org.apache.inlong.manager.pojo.cluster.ClusterRequest;
+import org.apache.inlong.manager.common.util.JsonTypeDefine;
+
+/**
+ * Inlong cluster request for agent
+ */
+@Data
+@ToString(callSuper = true)
+@EqualsAndHashCode(callSuper = true)
+@JsonTypeDefine(value = ClusterType.DATAPROXY)
+@ApiModel("Inlong cluster request for DataProxy")

Review Comment:
   This is `Agent` or `DataProxy`?



##########
pom.xml:
##########
@@ -1553,6 +1553,11 @@
                 <artifactId>dlc-data-catalog-metastore-client</artifactId>
                 <version>${dlc.client.version}</version>
             </dependency>
+            <dependency>

Review Comment:
   Does this newly added dependency have a special license and notice? If so, please add it to the `{project_path}/licenses` directory.



##########
inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/core/impl/HeartbeatServiceTest.java:
##########
@@ -17,130 +17,62 @@
 
 package org.apache.inlong.manager.service.core.impl;
 
-import com.github.pagehelper.Page;
-import com.github.pagehelper.PageInfo;
-import org.apache.inlong.manager.pojo.heartbeat.ComponentHeartbeat;
-import org.apache.inlong.manager.pojo.heartbeat.ComponentHeartbeatResponse;
-import org.apache.inlong.manager.pojo.heartbeat.GroupHeartbeat;
-import org.apache.inlong.manager.pojo.heartbeat.GroupHeartbeatResponse;
-import org.apache.inlong.manager.pojo.heartbeat.HeartbeatPageRequest;
-import org.apache.inlong.manager.pojo.heartbeat.HeartbeatQueryRequest;
-import org.apache.inlong.manager.pojo.heartbeat.HeartbeatReportRequest;
-import org.apache.inlong.manager.pojo.heartbeat.StreamHeartbeat;
-import org.apache.inlong.manager.pojo.heartbeat.StreamHeartbeatResponse;
-import org.apache.inlong.manager.dao.entity.ComponentHeartbeatEntity;
-import org.apache.inlong.manager.dao.entity.GroupHeartbeatEntity;
-import org.apache.inlong.manager.dao.entity.StreamHeartbeatEntity;
+import com.google.common.collect.Maps;
+import org.apache.inlong.common.heartbeat.GroupHeartbeat;
+import org.apache.inlong.common.heartbeat.StreamHeartbeat;
+import org.apache.inlong.manager.common.util.JsonUtils;
 import org.apache.inlong.manager.dao.mapper.ComponentHeartbeatEntityMapper;
 import org.apache.inlong.manager.dao.mapper.GroupHeartbeatEntityMapper;
 import org.apache.inlong.manager.dao.mapper.StreamHeartbeatEntityMapper;
+import org.apache.inlong.manager.pojo.heartbeat.HeartbeatQueryRequest;
+import org.apache.inlong.manager.pojo.heartbeat.HeartbeatReportRequest;
+import org.apache.inlong.manager.pojo.heartbeat.StreamHeartbeatResponse;
+import org.apache.inlong.manager.service.ServiceBaseTest;
 import org.apache.inlong.manager.service.core.HeartbeatService;
 import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.List;
-
-import static org.mockito.BDDMockito.given;
+import java.util.Map;
 
 /**
  * Heartbeat service test.
  */
-public class HeartbeatServiceTest {
+@EnableAutoConfiguration
+public class HeartbeatServiceTest extends ServiceBaseTest {
 
-    @InjectMocks
-    private HeartbeatService heartbeatService = new HeartbeatServiceImpl();
-    @Mock
+    @Autowired
+    private HeartbeatService heartbeatService;
+    @Autowired
     private ComponentHeartbeatEntityMapper componentHeartbeatMapper;
-    @Mock
+    @Autowired
     private GroupHeartbeatEntityMapper groupHeartbeatMapper;
-    @Mock
+    @Autowired
     private StreamHeartbeatEntityMapper streamHeartbeatMapper;
 
-    /**
-     * setUp
-     */
-    @BeforeEach
-    public void setUp() {
-        MockitoAnnotations.openMocks(this);
-        ComponentHeartbeatEntity componentHeartbeat = new ComponentHeartbeatEntity();
-        componentHeartbeat.setComponent("Sort");
-        componentHeartbeat.setInstance("127.0.0.1");
-        componentHeartbeat.setStatusHeartbeat("[{\"status\":\"running\"}]");
-        componentHeartbeat.setMetricHeartbeat("[{\"mem\":\"16gb\",\"cpu\":\"60%\"}]");
-        componentHeartbeat.setReportTime(System.currentTimeMillis());
-        Page<ComponentHeartbeatEntity> componentPage = new Page<>();
-        componentPage.add(componentHeartbeat);
-        componentPage.setTotal(1);
-        given(componentHeartbeatMapper.insert(new ComponentHeartbeatEntity())).willReturn(1);
-        given(componentHeartbeatMapper.selectByKey(Mockito.anyString(),
-                Mockito.anyString())).willReturn(componentHeartbeat);
-        given(componentHeartbeatMapper.selectByCondition(Mockito.any())).willReturn(componentPage);
-
-        GroupHeartbeatEntity groupHeartbeat = new GroupHeartbeatEntity();
-        groupHeartbeat.setComponent("Sort");
-        groupHeartbeat.setInstance("127.0.0.1");
-        groupHeartbeat.setStatusHeartbeat("[{\"summaryMetric\":{\"totalRecordNumOfRead\""
-                + ": \"10\"},\"streamMetrics\":[{\"streamId\":\"stream1\"}]}]");
-        groupHeartbeat.setReportTime(System.currentTimeMillis());
-        groupHeartbeat.setMetricHeartbeat("[{\"summaryMetric\":{\"totalRecordNumOfRead\""
-                + ": \"10\"},"
-                + "\"streamMetrics\":[{\"streamId\":\"stream1\"}]}]");
-        Page<GroupHeartbeatEntity> groupPage = new Page<>();
-        groupPage.add(groupHeartbeat);
-        groupPage.setTotal(1);
-        given(groupHeartbeatMapper.selectByKey(Mockito.anyString(),
-                Mockito.anyString(), Mockito.anyString())).willReturn(groupHeartbeat);
-        given(groupHeartbeatMapper.selectByCondition(Mockito.any())).willReturn(groupPage);
-
-        StreamHeartbeatEntity streamHeartbeat = new StreamHeartbeatEntity();
-        streamHeartbeat.setComponent("Sort");
-        streamHeartbeat.setInstance("127.0.0.1");
-        streamHeartbeat.setInlongGroupId("group1");
-        streamHeartbeat.setInlongStreamId("test_test");
-        streamHeartbeat.setStatusHeartbeat("[{\"statue\":\"running\"}]");
-        streamHeartbeat.setMetricHeartbeat("[{\"outMsg\":\"1\",\"inMsg\":2}]");
-        streamHeartbeat.setReportTime(System.currentTimeMillis());
-
-        Page<StreamHeartbeatEntity> streamPage = new Page<>();
-        streamPage.add(streamHeartbeat);
-        streamPage.setTotal(1);
-
-        given(streamHeartbeatMapper.selectByKey(Mockito.anyString(),
-                Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
-                .willReturn(streamHeartbeat);
-        given(streamHeartbeatMapper.selectByCondition(Mockito.any())).willReturn(streamPage);
-    }
-
     @Test
     public void testReportHeartbeat() {
         HeartbeatReportRequest request = new HeartbeatReportRequest();
-        request.setComponent("Sort");
-        request.setInstance("127.0.0.1");
+        request.setComponentType("AGENT");

Review Comment:
   Please replace it with the constant of `ClusterType`.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/core/heartbeat/HeartbeatManager.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.manager.service.core.heartbeat;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import com.github.benmanes.caffeine.cache.RemovalCause;
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.inlong.common.heartbeat.AbstractHeartbeatManager;
+import org.apache.inlong.common.heartbeat.ComponentHeartbeat;
+import org.apache.inlong.common.heartbeat.HeartbeatMsg;
+import org.apache.inlong.manager.common.consts.InlongConstants;
+import org.apache.inlong.manager.common.enums.ClusterStatus;
+import org.apache.inlong.manager.common.enums.NodeStatus;
+import org.apache.inlong.manager.dao.entity.InlongClusterEntity;
+import org.apache.inlong.manager.dao.entity.InlongClusterNodeEntity;
+import org.apache.inlong.manager.dao.mapper.InlongClusterEntityMapper;
+import org.apache.inlong.manager.dao.mapper.InlongClusterNodeEntityMapper;
+import org.apache.inlong.manager.pojo.cluster.ClusterInfo;
+import org.apache.inlong.manager.pojo.cluster.ClusterNodeRequest;
+import org.apache.inlong.manager.pojo.user.UserRoleCode;
+import org.apache.inlong.manager.service.cluster.InlongClusterOperator;
+import org.apache.inlong.manager.service.cluster.InlongClusterOperatorFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Slf4j
+public class HeartbeatManager implements AbstractHeartbeatManager {
+
+    @Getter
+    private Cache<ComponentHeartbeat, HeartbeatMsg> heartbeats;

Review Comment:
   It is recommended to change it to `heartbeatCache`, which is easier to understand.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/core/heartbeat/HeartbeatManager.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.manager.service.core.heartbeat;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import com.github.benmanes.caffeine.cache.RemovalCause;
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.inlong.common.heartbeat.AbstractHeartbeatManager;
+import org.apache.inlong.common.heartbeat.ComponentHeartbeat;
+import org.apache.inlong.common.heartbeat.HeartbeatMsg;
+import org.apache.inlong.manager.common.consts.InlongConstants;
+import org.apache.inlong.manager.common.enums.ClusterStatus;
+import org.apache.inlong.manager.common.enums.NodeStatus;
+import org.apache.inlong.manager.dao.entity.InlongClusterEntity;
+import org.apache.inlong.manager.dao.entity.InlongClusterNodeEntity;
+import org.apache.inlong.manager.dao.mapper.InlongClusterEntityMapper;
+import org.apache.inlong.manager.dao.mapper.InlongClusterNodeEntityMapper;
+import org.apache.inlong.manager.pojo.cluster.ClusterInfo;
+import org.apache.inlong.manager.pojo.cluster.ClusterNodeRequest;
+import org.apache.inlong.manager.pojo.user.UserRoleCode;
+import org.apache.inlong.manager.service.cluster.InlongClusterOperator;
+import org.apache.inlong.manager.service.cluster.InlongClusterOperatorFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Slf4j
+public class HeartbeatManager implements AbstractHeartbeatManager {
+
+    @Getter
+    private Cache<ComponentHeartbeat, HeartbeatMsg> heartbeats;
+
+    @Getter
+    private LoadingCache<ComponentHeartbeat, ClusterInfo> clusterInfos;

Review Comment:
   It is recommended to change it to `clusterInfoCache`, which is easier to understand.



##########
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 = new HeartbeatMsg();
+                reportHeartbeat(heartbeatMsg);
+                try {
+                    SECONDS.sleep(heartbeatInterval());
+                } catch (InterruptedException ex) {
+                    log.error("", ex);

Review Comment:
   It is recommended to add specific information. Because no matter what type of exception, simple and understandable logging is a good coding style.



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org