Merge branch 'master' into new-wall-2

This commit is contained in:
Jast
2025-01-26 13:09:06 +08:00
committed by GitHub
7 changed files with 160 additions and 36 deletions
@@ -25,13 +25,12 @@ import java.util.Arrays;
public enum AiTypeEnum {
/**
* 智普
* ZhiPu
*/
zhiPu,
/**
* sparkDesk
* 科大讯飞
*/
sparkDesk,
@@ -52,7 +52,7 @@ public class RocketmqProtocol implements CommonRequestProtocol, Protocol {
private String secretKey;
/**
* jsonpath解析脚本
* jsonpath parsing script
*/
private String parseScript;
@@ -0,0 +1,123 @@
/*
* 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.hertzbeat.collector.collect.basic.http;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.collector.collect.AbstractCollectE2eTest;
import org.apache.hertzbeat.collector.collect.http.HttpCollectImpl;
import org.apache.hertzbeat.collector.util.CollectUtil;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.job.Job;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.HttpProtocol;
import org.apache.hertzbeat.common.entity.job.protocol.Protocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* E2E test for HTTP monitor
*/
@Slf4j
@ExtendWith(MockitoExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpMonitorE2eTest extends AbstractCollectE2eTest {
private static final int MOCK_SERVER_PORT = 52376;
private static final String LOCALHOST = "127.0.0.1";
private static final String RELATIVE_PATH = "/";
private static HttpServer mockServer;
@AfterAll
public static void tearDown() {
if (mockServer != null) {
mockServer.stop(0);
}
}
@BeforeEach
public void setUp() throws Exception {
super.setUp();
// Setup collect instance
collect = new HttpCollectImpl();
// Setup mock server and endpoints
mockServer = HttpServer.create(new InetSocketAddress(MOCK_SERVER_PORT), 0);
mockServer.setExecutor(null);
mockServer.start();
mockServer.createContext(RELATIVE_PATH, exchange -> sendJsonResponse(exchange, ""));
}
private void sendJsonResponse(HttpExchange exchange, String response) throws IOException {
exchange.getResponseHeaders().set("Content-Type", "application/json");
final byte[] array = response.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, array.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(array);
}
}
@Test
public void testHttpMonitor() {
Job dockerJob = appService.getAppDefine("api");
List<Map<String, Configmap>> configmapFromPreCollectData = new LinkedList<>();
for (Metrics metricsDef : dockerJob.getMetrics()) {
metricsDef = CollectUtil.replaceCryPlaceholderToMetrics(metricsDef, configmapFromPreCollectData.size() > 0 ? configmapFromPreCollectData.get(0) : new HashMap<>());
CollectRep.MetricsData metricsData = validateMetricsCollection(metricsDef, metricsDef.getName());
configmapFromPreCollectData = CollectUtil.getConfigmapFromPreCollectData(metricsData);
}
}
@Override
protected Protocol buildProtocol(Metrics metricsDef) {
// Setup HTTP protocol
HttpProtocol protocol = new HttpProtocol();
protocol.setHost(LOCALHOST);
protocol.setUrl(RELATIVE_PATH);
protocol.setMethod("GET");
protocol.setPort(String.valueOf(MOCK_SERVER_PORT));
protocol.setParseType(metricsDef.getHttp().getParseType());
protocol.setParseScript(metricsDef.getHttp().getParseScript());
return protocol;
}
@Override
protected CollectRep.MetricsData.Builder collectMetrics(Metrics metricsDef) {
HttpProtocol protocol = (HttpProtocol) buildProtocol(metricsDef);
metrics.setHttp(protocol);
return collectMetricsData(metrics, metricsDef);
}
}
@@ -17,11 +17,12 @@
package org.apache.hertzbeat.manager.scheduler;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* List of assigned collection tasks
*/
@@ -33,17 +34,17 @@ public class AssignJobs {
* current assign jobIds
*/
private Set<Long> jobs;
/**
* jobs to be adding
*/
private Set<Long> addingJobs;
/**
* jobs to be removed
*/
private Set<Long> removingJobs;
/**
* jobs has pinned in this collector
*/
@@ -59,15 +60,15 @@ public class AssignJobs {
public void addAssignJob(Long jobId) {
jobs.add(jobId);
}
public void addAddingJob(Long jobId) {
addingJobs.add(jobId);
}
public void addRemovingJob(Long jobId) {
removingJobs.add(jobId);
}
public void addPinnedJob(Long jobId) {
pinnedJobs.add(jobId);
}
@@ -77,19 +78,19 @@ public class AssignJobs {
jobs.addAll(jobSet);
}
}
public void addAddingJobs(Set<Long> jobSet) {
if (jobSet != null && !jobSet.isEmpty()) {
addingJobs.addAll(jobSet);
}
}
public void addRemovingJobs(Set<Long> jobSet) {
if (jobSet != null && !jobSet.isEmpty()) {
removingJobs.addAll(jobSet);
}
}
public void addPinnedJobs(Set<Long> jobSet) {
if (jobSet != null && !jobSet.isEmpty()) {
pinnedJobs.addAll(jobSet);
@@ -102,14 +103,14 @@ public class AssignJobs {
}
jobs.removeAll(jobIds);
}
public void removeAddingJobs(Set<Long> jobIds) {
if (addingJobs == null || jobIds == null || jobIds.isEmpty()) {
return;
}
addingJobs.removeAll(jobIds);
}
public void clearRemovingJobs() {
if (removingJobs == null) {
return;
@@ -118,9 +119,10 @@ public class AssignJobs {
}
/**
* 判断是否存在对应的jobId
* Check if the corresponding jobId exists
*
* @param jobId jobId
* @return 若存在返回true,并把jobId从assignJobs remove掉
* @return true if exists and removes the jobId from assignJobs
*/
public boolean containAndRemoveJob(Long jobId) {
if (jobs.isEmpty()) {
@@ -128,21 +130,21 @@ public class AssignJobs {
}
return jobs.remove(jobId);
}
public void removeAddingJob(Long jobId) {
if (addingJobs == null || jobId == null) {
return;
}
addingJobs.remove(jobId);
}
public void removeRemovingJob(Long jobId) {
if (removingJobs == null || jobId == null) {
return;
}
removingJobs.remove(jobId);
}
public void removePinnedJob(Long jobId) {
if (pinnedJobs == null || jobId == null) {
return;
@@ -151,7 +153,7 @@ public class AssignJobs {
}
/**
* 清理数据
* Clean data
*/
public void clear() {
if (!jobs.isEmpty()) {
@@ -115,7 +115,7 @@ public class AccountController {
@PostMapping("/register")
@Transactional
public ResponseEntity<Message<String>> accountRegister(@RequestBody @Validated SignUpDto account) {
//此处先让前端传递明文密码,后续改为加密密码
// TODO Let the front-end pass the plaintext password here first, and then change it to an encrypted password later
if (accountService.registerAccount(account)) {
Long authUser = roleService.getRoleIdByCode("role_user");
@@ -70,38 +70,38 @@ public class SurenessFilterExample implements Filter {
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, authorization,x-requested-with, *");
try {
//获取过滤器链中的filters属性
// Retrieve the filters attribute from the filter chain
Field filtersField = filterChain.getClass().getDeclaredField("filters");
//反射设置属性可达
// Reflection setting attribute reachable
filtersField.setAccessible(true);
//获取filters属性值
// Get the value of the filters attribute
FilterConfig[] filters = (FilterConfig[]) filtersField.get(filterChain);
//跳过过滤器个数记录
// Skip the record of the number of filters
int k = 0;
//遍历所有过滤器
// Traverse all filters
for (int i = 0; i < filters.length; i++) {
if (filters[i] != null) {
//获取过滤器filterDef属性
// Get the filterDef attribute of the filter
Field filterDefField = filters[i].getClass().getDeclaredField("filterDef");
filterDefField.setAccessible(true);
//获取filter的class
// Get the class of the filter
Field filterClassField = filterDefField.get(filters[i]).getClass().getDeclaredField("filterClass");
filterClassField.setAccessible(true);
String filterClass = (String) filterClassField.get(filterDefField.get(filters[i]));
String FILTER_REFERENCE1 = "com.usthe.sureness.configuration.SurenessJakartaServletFilter";
String FILTER_REFERENCE2 = "org.apache.tomcat.websocket.server.WsFilter";
//跳过指定过滤器处理
// Skip specified filter processing
if (FILTER_REFERENCE1.equals(filterClass)||FILTER_REFERENCE2.equals(filterClass)) {
filters[i] = null;
k++;
break;
}
//属性可达关闭
// Attribute can be disabled
filterClassField.setAccessible(false);
filterDefField.setAccessible(false);
}
}
//过滤器数组重新赋值,调整移除指定过滤器后过滤器数组
// Re assign the filter array and adjust it after removing the specified filter
int index = 0;
for (int i = 0; i < filters.length; i++) {
if (index == 0 && filters[i] == null) {
@@ -113,7 +113,7 @@ public class SurenessFilterExample implements Filter {
index = 0;
}
}
//n值重新赋值
// Reassignment of n value
filtersField.setAccessible(false);
Field n = filterChain.getClass().getDeclaredField("n");
n.setAccessible(true);
@@ -39,7 +39,7 @@ public class DatabasePathTreeProvider implements PathTreeProvider {
@Override
public Set<String> providePathData() {
// 从数据库中读取出path信息,取出所有状态为1,即正常的path信息
// Read path information from the database and retrieve all path information with a status of 1, which is normal
Set<String> pathSet = SurenessCommonUtil.attachContextPath(getContextPath(), resourceService.getAllEnableResourcePath());
return pathSet;
@@ -47,7 +47,7 @@ public class DatabasePathTreeProvider implements PathTreeProvider {
@Override
public Set<String> provideExcludedResource() {
// 从数据库中读取出path信息,取出所有状态为9,即禁用的path信息
// Read path information from the database and retrieve all path information with a status of 9, which is disabled
Set<String> exlResourceSet = SurenessCommonUtil.attachContextPath(getContextPath(), resourceService.getAllDisableResourcePath());
return exlResourceSet;
}