From 496aeeba61992c32c388101c20d2b5664155230a Mon Sep 17 00:00:00 2001 From: janghs Date: Wed, 24 Apr 2024 13:21:42 +0900 Subject: [PATCH 1/2] Code simplification and flow restructuring and trans. --- .../collect/icmp/IcmpCollectImpl.java | 29 +++++++++---------- .../collector/collect/jmx/JmxCollectImpl.java | 17 +++++------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java index 624e184ce2f..e8042679eec 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java @@ -41,14 +41,14 @@ public IcmpCollectImpl(){} @Override public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) { long startTime = System.currentTimeMillis(); - // 简单校验必有参数 + // Simple validation requires mandatory parameters if (metrics == null || metrics.getIcmp() == null) { builder.setCode(CollectRep.Code.FAIL); builder.setMsg("ICMP collect must has icmp params"); return; } IcmpProtocol icmp = metrics.getIcmp(); - // 超时时间默认6000毫秒 + // The default timeout is 6000 milliseconds int timeout = 6000; try { timeout = Integer.parseInt(icmp.getTimeout()); @@ -56,26 +56,25 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri log.warn(e.getMessage()); } try { - // todo need root java jcm to use ICMP, else it telnet the peer server 7 port available - // todo 需要配置java虚拟机root权限从而使用ICMP,否则是判断telnet对端7号端口是否开通 + // todo requires Java JVM with root permissions to use ICMP, otherwise check if telnet is available on peer server's port 7 + // todo requires configuring Java JVM with root permissions to use ICMP, otherwise check if telnet is available on the peer's port 7 // todo https://stackoverflow.com/questions/11506321/how-to-ping-an-ip-address boolean status = InetAddress.getByName(icmp.getHost()).isReachable(timeout); long responseTime = System.currentTimeMillis() - startTime; - if (status) { - CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); - for (String alias : metrics.getAliasFields()) { - if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { - valueRowBuilder.addColumns(Long.toString(responseTime)); - } else { - valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); - } - } - builder.addValues(valueRowBuilder.build()); - } else { + if (!status) { builder.setCode(CollectRep.Code.UN_REACHABLE); builder.setMsg("Un Reachable, Timeout " + timeout + "ms"); return; } + CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); + for (String alias : metrics.getAliasFields()) { + if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { + valueRowBuilder.addColumns(Long.toString(responseTime)); + } else { + valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); + } + } + builder.addValues(valueRowBuilder.build()); } catch (UnknownHostException unknownHostException) { String errorMsg = CommonUtil.getMessageFromThrowable(unknownHostException); builder.setCode(CollectRep.Code.UN_REACHABLE); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java index e3ca9e3aef6..73c380030a8 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java @@ -53,6 +53,7 @@ import org.apache.hertzbeat.common.entity.job.protocol.JmxProtocol; import org.apache.hertzbeat.common.entity.message.CollectRep; import org.apache.hertzbeat.common.util.CommonUtil; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -76,8 +77,8 @@ public JmxCollectImpl() { public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) { try { - JmxProtocol jmxProtocol = metrics.getJmx(); validateParams(metrics); + JmxProtocol jmxProtocol = metrics.getJmx(); // Create a jndi remote connection JMXConnector jmxConnector = getConnectSession(jmxProtocol); @@ -161,14 +162,12 @@ private Map extractAttributeValue(AttributeList attributeList) { return attributeValueMap; } - private void validateParams(Metrics metrics) throws IllegalArgumentException { - if (metrics == null || metrics.getJmx() == null) { - throw new IllegalArgumentException("JMX collect must has jmx params"); - } - if (StringUtils.hasText(metrics.getJmx().getUrl())) { - if (metrics.getJmx().getUrl().contains(IGNORED_STUB)) { - throw new IllegalArgumentException("JMX url prohibit contains stub, please check"); - } + private void validateParams(Metrics metrics) { + Assert.isTrue(metrics != null && metrics.getJmx() != null, "JMX collect must have JMX params"); + + String url = metrics.getJmx().getUrl(); + if (StringUtils.hasText(url)) { + Assert.doesNotContain(url, IGNORED_STUB, "JMX url prohibit contains stub, please check"); } } From b671e3676dc742e198ca96d218bf38075efa869d Mon Sep 17 00:00:00 2001 From: janghs Date: Thu, 25 Apr 2024 13:04:16 +0900 Subject: [PATCH 2/2] Trans & Use Assert --- .../memcached/MemcachedCollectImpl.java | 8 +++--- .../mongodb/MongodbSingleCollectImpl.java | 4 +-- .../collect/mq/RocketmqSingleCollectImpl.java | 7 +++-- .../nebulagraph/NebulaGraphCollectImpl.java | 16 ++++++------ .../collect/nginx/NginxCollectImpl.java | 26 +++++++++---------- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java index a09d1007360..4c7d1df2e48 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java @@ -74,7 +74,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri long responseTime = System.currentTimeMillis() - startTime; PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); - // 发送统计命令 + // Send a command to collect statistics Map resultMap = new HashMap<>(128); parseCmdResponse(resultMap, in, out, STATS); parseCmdResponse(resultMap, in, out, STATS_SETTINGS); @@ -82,7 +82,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri resultMap.put(CollectorConstants.RESPONSE_TIME, Long.toString(responseTime)); - // 关闭输出流和Socket连接 + // Close the output stream and socket connection in.close(); out.close(); socket.close(); @@ -130,7 +130,7 @@ private static void parseCmdResponse(Map statsMap, out.println(cmd); String line; while ((line = in.readLine()) != null && !line.equals(STATS_END_RSP)) { - // 解析每一行,将键值对存入HashMap + // Parse each line and store the key-value pairs in a HashMap String[] parts = line.split(" "); if (parts.length == 3) { statsMap.put(parts[1], parts[2]); @@ -145,7 +145,7 @@ private static void parseSizesOutput(Map statsMap, String line; while ((line = in.readLine()) != null && !line.equals(STATS_END_RSP)) { String[] parts = line.split("\\s+"); - // 提取 slab size 和 slab count,并放入HashMap + // Extract slab size and slab count, then add them to the HashMap if (parts.length >= 3 && "STAT".equals(parts[0])) { statsMap.put("item_size", parts[1]); statsMap.put("item_count", parts[2]); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java index 78c95e81032..000f4937c4f 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java @@ -169,9 +169,7 @@ private void fillBuilder(Metrics metrics, CollectRep.ValueRow.Builder valueRowBu * Check that the mongodb connection information in metrics is complete */ private void preCheck(Metrics metrics) { - if (metrics == null || metrics.getMongodb() == null) { - throw new IllegalArgumentException("Mongodb collect must has mongodb params"); - } + Assert.isTrue(metrics != null && metrics.getMongodb() != null, "Mongodb collect must has mongodb params"); MongodbProtocol mongodbProtocol = metrics.getMongodb(); Assert.hasText(mongodbProtocol.getCommand(), "Mongodb Protocol command is required."); Assert.hasText(mongodbProtocol.getHost(), "Mongodb Protocol host is required."); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectImpl.java index f39c24bebad..7547012d2ef 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectImpl.java @@ -146,9 +146,7 @@ public String supportProtocol() { * @param metrics metrics config */ private void preCheck(Metrics metrics) { - if (metrics == null || metrics.getRocketmq() == null) { - throw new IllegalArgumentException("Mongodb collect must has rocketmq params"); - } + Assert.isTrue(metrics != null && metrics.getRocketmq() != null, "Mongodb collect must has rocketmq params"); RocketmqProtocol rocketmq = metrics.getRocketmq(); Assert.hasText(rocketmq.getNamesrvHost(), "Rocketmq Protocol namesrvHost is required."); Assert.hasText(rocketmq.getNamesrvPort(), "Rocketmq Protocol namesrvPort is required."); @@ -339,7 +337,8 @@ private void collectTopicData(DefaultMQAdminExt mqAdminExt, RocketmqCollectData Map> topicQueueInfoTable = new HashMap<>(32); List topicQueueInfoList = new ArrayList<>(); - // todo 查询topic的queue信息需要for循环调用 mqAdminExt.examineTopicStats(), topic数量很大的情况, 调用次数也会很多 + // When querying queue information for a topic, you need to use a for-loop to call mqAdminExt.examineTopicStats(). + // If the number of topics is large, the number of calls will also be high topicQueueInfoTable.put(topic, topicQueueInfoList); topicInfoList.add(topicQueueInfoTable); rocketmqCollectData.setTopicInfoList(topicInfoList); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java index 135bb42ad6b..1ffb6b67417 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java @@ -99,7 +99,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri HttpUriRequest request = createHttpRequest(nebulaGraph.getHost(), nebulaGraph.getPort(), nebulaGraph.getUrl(), nebulaGraph.getTimeout()); try { - // 发起http请求,获取响应数据 + // Send an HTTP request to obtain response data response = CommonHttpClient.getHttpClient().execute(request, httpContext); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != SUCCESS_CODE) { @@ -110,7 +110,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); responseTime = System.currentTimeMillis() - startTime; resultMap.put(CollectorConstants.RESPONSE_TIME, Long.toString(responseTime)); - // 根据API进行不同解析 + // Parse the response differently depending on the API if (GRAPH_API.equals(nebulaGraph.getUrl())) { parseStatsResponse(resp, nebulaGraph.getTimePeriod(), resultMap); } else if (STORAGE_API.equals(nebulaGraph.getUrl())) { @@ -177,13 +177,13 @@ private HttpUriRequest createHttpRequest(String host, String port, String url, S } /** - * 解析Stats响应通过时间间隔进行筛选 + * Parse Stats response and filter by time period * - * @param responseBody 响应体 - * @param timePeriod 时间间隔 + * @param responseBody response body + * @param timePeriod time period */ private void parseStatsResponse(String responseBody, String timePeriod, HashMap resultMap) { - // 设置正则匹配 + // Set up regular expression matching String timeRegex = String.format(REGEX, timePeriod); Pattern pattern = Pattern.compile(timeRegex); String[] strArray = responseBody.split(STR_SPLIT); @@ -198,9 +198,9 @@ private void parseStatsResponse(String responseBody, String timePeriod, HashMap< /** - * 解析Storage响应通过时间间隔进行筛选 + * Parse the Storage response and filter by time period * - * @param responseBody 响应体 + * @param responseBody response body */ private void parseStorageResponse(String responseBody, HashMap resultMap) { String[] strArray = responseBody.split(STR_SPLIT); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java index 5a3935a3b92..d9804182e5d 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java @@ -218,7 +218,7 @@ private void parseNginxStatusResponse(CollectRep.MetricsData.Builder builder, St } /** - * 解析ngx_http_reqstat_module模块暴露信息 + * Analyze the information exposed by the ngx_http_reqstat_module module * * @param builder builder * @param resp resp @@ -228,17 +228,17 @@ private void parseNginxStatusResponse(CollectRep.MetricsData.Builder builder, St private void parseReqStatusResponse(CollectRep.MetricsData.Builder builder, String resp, Metrics metrics, Long responseTime) { //example - //zone_name key max_active max_bw traffic requests active bandwidth - //imgstore_appid 43 27 6M 63G 374063 0 0 - //imgstore_appid 53 329 87M 2058G 7870529 50 25M - //server_addr 10.128.1.17 2 8968 24M 1849 0 0 - //server_addr 127.0.0.1 1 6M 5G 912 1 0 - //server_addr 180.96.x.1 3358 934M 27550G 141277391 891 356M - //server_addr 180.96.x.2 78 45M 220G 400704 0 0 - //server_addr 180.96.x.3 242 58M 646G 2990547 42 7M - //server_name d.123.sogou.com 478 115M 2850G 30218726 115 39M - //server_name dl.pinyin.sogou.com 913 312M 8930G 35345453 225 97M - //server_name download.ie.sogou.com 964 275M 7462G 7979817 297 135M + //zone_name key max_active max_bw traffic requests active bandwidth + //imgstore_appid 43 27 6M 63G 374063 0 0 + //imgstore_appid 53 329 87M 2058G 7870529 50 25M + //server_addr 10.128.1.17 2 8968 24M 1849 0 0 + //server_addr 127.0.0.1 1 6M 5G 912 1 0 + //server_addr 180.96.x.1 3358 934M 27550G 141277391 891 356M + //server_addr 180.96.x.2 78 45M 220G 400704 0 0 + //server_addr 180.96.x.3 242 58M 646G 2990547 42 7M + //server_name d.123.sogou.com 478 115M 2850G 30218726 115 39M + //server_name dl.pinyin.sogou.com 913 312M 8930G 35345453 225 97M + //server_name download.ie.sogou.com 964 275M 7462G 7979817 297 135M List reqStatusResponses = regexReqStatusMatch(resp); List aliasFields = metrics.getAliasFields(); @@ -273,7 +273,7 @@ private Object reflect(ReqStatusResponse reqStatusResponse, String methodName) t private Map regexNginxStatusMatch(String resp, Integer aliasFieldsSize) { Map metricsMap = new HashMap<>(aliasFieldsSize); - // 正则提取监控信息 + // Extract monitoring information using regular expressions Pattern pattern = Pattern.compile(REGEX_SERVER); Matcher matcher = pattern.matcher(resp); while (matcher.find()) {