From cbeabd20c9e8ce6b5478f7795ab5321e12fccb13 Mon Sep 17 00:00:00 2001 From: tjq Date: Mon, 19 Aug 2024 01:03:34 +0800 Subject: [PATCH] test: add openclient debug log to find problem ##970 --- .../client/service/PowerRequestBody.java | 2 + .../ClusterRequestServiceOkHttp3Impl.java | 19 +++++++++ .../client/test/ClientInitializer.java | 2 + .../powerjob/client/test/TestAnything.java | 35 ++++++++++++++++ .../tech/powerjob/client/test/TestClient.java | 26 ++---------- .../powerjob/client/test/utils/TestUtils.java | 42 +++++++++++++++++++ .../login/impl/PowerJobLoginServiceImpl.java | 7 +++- .../server/openapi/OpenAPIController.java | 8 +++- 8 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 powerjob-client/src/test/java/tech/powerjob/client/test/TestAnything.java create mode 100644 powerjob-client/src/test/java/tech/powerjob/client/test/utils/TestUtils.java diff --git a/powerjob-client/src/main/java/tech/powerjob/client/service/PowerRequestBody.java b/powerjob-client/src/main/java/tech/powerjob/client/service/PowerRequestBody.java index 7b0ef102..57aa0ec4 100644 --- a/powerjob-client/src/main/java/tech/powerjob/client/service/PowerRequestBody.java +++ b/powerjob-client/src/main/java/tech/powerjob/client/service/PowerRequestBody.java @@ -2,6 +2,7 @@ package tech.powerjob.client.service; import com.google.common.collect.Maps; import lombok.Getter; +import lombok.ToString; import tech.powerjob.common.enums.MIME; import java.util.Map; @@ -13,6 +14,7 @@ import java.util.Map; * @since 2024/8/10 */ @Getter +@ToString public class PowerRequestBody { private MIME mime; diff --git a/powerjob-client/src/main/java/tech/powerjob/client/service/impl/ClusterRequestServiceOkHttp3Impl.java b/powerjob-client/src/main/java/tech/powerjob/client/service/impl/ClusterRequestServiceOkHttp3Impl.java index ff24f8d9..81b13b46 100644 --- a/powerjob-client/src/main/java/tech/powerjob/client/service/impl/ClusterRequestServiceOkHttp3Impl.java +++ b/powerjob-client/src/main/java/tech/powerjob/client/service/impl/ClusterRequestServiceOkHttp3Impl.java @@ -4,12 +4,14 @@ import com.google.common.collect.Maps; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import okhttp3.*; +import org.apache.commons.lang3.exception.ExceptionUtils; import tech.powerjob.client.ClientConfig; import tech.powerjob.client.common.Protocol; import tech.powerjob.client.service.HttpResponse; import tech.powerjob.client.service.PowerRequestBody; import tech.powerjob.common.OmsConstant; import tech.powerjob.common.serialize.JsonUtils; +import tech.powerjob.common.utils.DebugUtils; import javax.net.ssl.*; import java.io.IOException; @@ -45,6 +47,11 @@ public class ClusterRequestServiceOkHttp3Impl extends AppAuthClusterRequestServi @Override protected HttpResponse sendHttpRequest(String url, PowerRequestBody powerRequestBody) throws IOException { + boolean debugMode = DebugUtils.inDebugMode(); + if (debugMode) { + log.info("[ClusterRequestService] try to sendHttpRequest, url: {}, powerRequestBody: {}", url, powerRequestBody); + } + // 添加公共 header powerRequestBody.addHeaders(config.getDefaultHeaders()); @@ -91,7 +98,19 @@ public class ClusterRequestServiceOkHttp3Impl extends AppAuthClusterRequestServi httpResponse.setHeaders(respHeaderMap); + if (debugMode) { + log.info("[ClusterRequestService] originHttpResponse: {}, convertedResponse: {}", response, httpResponse); + } + return httpResponse; + } catch (Exception e) { + + if (debugMode) { + log.warn("[ClusterRequestService] request[{}] failed!", url, e); + } + + ExceptionUtils.rethrow(e); + throw new RuntimeException(e); } } diff --git a/powerjob-client/src/test/java/tech/powerjob/client/test/ClientInitializer.java b/powerjob-client/src/test/java/tech/powerjob/client/test/ClientInitializer.java index 4cad73ba..e0b28b93 100644 --- a/powerjob-client/src/test/java/tech/powerjob/client/test/ClientInitializer.java +++ b/powerjob-client/src/test/java/tech/powerjob/client/test/ClientInitializer.java @@ -4,6 +4,7 @@ import com.google.common.collect.Lists; import org.junit.jupiter.api.BeforeAll; import tech.powerjob.client.IPowerJobClient; import tech.powerjob.client.PowerJobClient; +import tech.powerjob.common.PowerJobDKey; /** * Initialize OhMyClient @@ -17,6 +18,7 @@ public class ClientInitializer { @BeforeAll public static void initClient() throws Exception { + System.setProperty(PowerJobDKey.DEBUG_LEVEL, "INFO"); powerJobClient = new PowerJobClient(Lists.newArrayList("127.0.0.1:7700", "127.0.0.1:7701"), "powerjob-worker-samples", "powerjob123"); } } diff --git a/powerjob-client/src/test/java/tech/powerjob/client/test/TestAnything.java b/powerjob-client/src/test/java/tech/powerjob/client/test/TestAnything.java new file mode 100644 index 00000000..5d728346 --- /dev/null +++ b/powerjob-client/src/test/java/tech/powerjob/client/test/TestAnything.java @@ -0,0 +1,35 @@ +package tech.powerjob.client.test; + +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import tech.powerjob.client.test.utils.TestUtils; +import tech.powerjob.common.request.http.SaveJobInfoRequest; +import tech.powerjob.common.response.ResultDTO; +import tech.powerjob.common.utils.CommonUtils; + +/** + * TestAnything + * + * @author tjq + * @since 2024/8/19 + */ +@Slf4j +public class TestAnything extends ClientInitializer { + + @Test + void testMultiClientRequest() { + for (int i = 0; i < 5; i++) { + log.info("START ================== {} ================== START", i); + + SaveJobInfoRequest saveJobInfoRequest = TestUtils.newSaveJobRequest(null, "TEST-JOB-" + i + "-" + System.currentTimeMillis()); + ResultDTO saveJobResult = powerJobClient.saveJob(saveJobInfoRequest); + log.info("[testMultiClientRequest] saveJobResult: {}", JSONObject.toJSONString(saveJobResult)); + + log.info("END ================== {} ================== END", i); + + CommonUtils.easySleep(100); + } + } + +} diff --git a/powerjob-client/src/test/java/tech/powerjob/client/test/TestClient.java b/powerjob-client/src/test/java/tech/powerjob/client/test/TestClient.java index ac5658d6..d8491477 100644 --- a/powerjob-client/src/test/java/tech/powerjob/client/test/TestClient.java +++ b/powerjob-client/src/test/java/tech/powerjob/client/test/TestClient.java @@ -5,9 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import tech.powerjob.client.PowerJobClient; -import tech.powerjob.common.enums.ExecuteType; -import tech.powerjob.common.enums.ProcessorType; -import tech.powerjob.common.enums.TimeExpressionType; +import tech.powerjob.client.test.utils.TestUtils; import tech.powerjob.common.request.http.SaveJobInfoRequest; import tech.powerjob.common.response.InstanceInfoDTO; import tech.powerjob.common.response.JobInfoDTO; @@ -27,26 +25,8 @@ class TestClient extends ClientInitializer { @Test void testSaveJob() { - - SaveJobInfoRequest newJobInfo = new SaveJobInfoRequest(); - newJobInfo.setId(JOB_ID); - newJobInfo.setJobName("omsOpenAPIJobccccc" + System.currentTimeMillis()); - newJobInfo.setJobDescription("test OpenAPI" + System.currentTimeMillis()); - newJobInfo.setJobParams("{'aa':'bb'}"); - newJobInfo.setTimeExpressionType(TimeExpressionType.CRON); - newJobInfo.setTimeExpression("0 0 * * * ? "); - newJobInfo.setExecuteType(ExecuteType.STANDALONE); - newJobInfo.setProcessorType(ProcessorType.BUILT_IN); - newJobInfo.setProcessorInfo("tech.powerjob.samples.processors.StandaloneProcessorDemo"); - newJobInfo.setDesignatedWorkers(""); - - newJobInfo.setMinCpuCores(1.1); - newJobInfo.setMinMemorySpace(1.2); - newJobInfo.setMinDiskSpace(1.3); - - log.info("[TestClient] [testSaveJob] SaveJobInfoRequest: {}", JSONObject.toJSONString(newJobInfo)); - - ResultDTO resultDTO = powerJobClient.saveJob(newJobInfo); + SaveJobInfoRequest saveJobInfoRequest = TestUtils.newSaveJobRequest(JOB_ID, null); + ResultDTO resultDTO = powerJobClient.saveJob(saveJobInfoRequest); log.info("[TestClient] [testSaveJob] result: {}", JSONObject.toJSONString(resultDTO)); Assertions.assertNotNull(resultDTO); } diff --git a/powerjob-client/src/test/java/tech/powerjob/client/test/utils/TestUtils.java b/powerjob-client/src/test/java/tech/powerjob/client/test/utils/TestUtils.java new file mode 100644 index 00000000..85b32fac --- /dev/null +++ b/powerjob-client/src/test/java/tech/powerjob/client/test/utils/TestUtils.java @@ -0,0 +1,42 @@ +package tech.powerjob.client.test.utils; + +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import tech.powerjob.common.enums.ExecuteType; +import tech.powerjob.common.enums.ProcessorType; +import tech.powerjob.common.enums.TimeExpressionType; +import tech.powerjob.common.request.http.SaveJobInfoRequest; + +/** + * TestUtils + * + * @author tjq + * @since 2024/8/19 + */ +@Slf4j +public class TestUtils { + + public static SaveJobInfoRequest newSaveJobRequest(Long jobId, String jobName) { + SaveJobInfoRequest newJobInfo = new SaveJobInfoRequest(); + newJobInfo.setId(jobId); + String jobNameF = StringUtils.isEmpty(jobName) ? "OpenApiJob-" + System.currentTimeMillis() : jobName; + newJobInfo.setJobName(jobNameF); + newJobInfo.setJobDescription("test OpenAPI" + System.currentTimeMillis()); + newJobInfo.setJobParams("{'aa':'bb'}"); + newJobInfo.setTimeExpressionType(TimeExpressionType.CRON); + newJobInfo.setTimeExpression("0 0 * * * ? "); + newJobInfo.setExecuteType(ExecuteType.STANDALONE); + newJobInfo.setProcessorType(ProcessorType.BUILT_IN); + newJobInfo.setProcessorInfo("tech.powerjob.samples.processors.StandaloneProcessorDemo"); + newJobInfo.setDesignatedWorkers(""); + + newJobInfo.setMinCpuCores(1.1); + newJobInfo.setMinMemorySpace(1.2); + newJobInfo.setMinDiskSpace(1.3); + + log.info("[TestClient] [testSaveJob] SaveJobInfoRequest: {}", JSONObject.toJSONString(newJobInfo)); + + return newJobInfo; + } +} diff --git a/powerjob-server/powerjob-server-auth/src/main/java/tech/powerjob/server/auth/service/login/impl/PowerJobLoginServiceImpl.java b/powerjob-server/powerjob-server-auth/src/main/java/tech/powerjob/server/auth/service/login/impl/PowerJobLoginServiceImpl.java index 80aa018e..959eb346 100644 --- a/powerjob-server/powerjob-server-auth/src/main/java/tech/powerjob/server/auth/service/login/impl/PowerJobLoginServiceImpl.java +++ b/powerjob-server/powerjob-server-auth/src/main/java/tech/powerjob/server/auth/service/login/impl/PowerJobLoginServiceImpl.java @@ -18,6 +18,7 @@ import tech.powerjob.common.enums.ErrorCodes; import tech.powerjob.server.auth.common.PowerJobAuthException; import tech.powerjob.server.auth.common.utils.HttpServletUtils; import tech.powerjob.server.auth.jwt.JwtService; +import tech.powerjob.server.auth.jwt.ParseResult; import tech.powerjob.server.auth.login.*; import tech.powerjob.server.auth.service.login.LoginRequest; import tech.powerjob.server.auth.service.login.PowerJobLoginService; @@ -236,7 +237,11 @@ public class PowerJobLoginServiceImpl implements PowerJobLoginService { if (StringUtils.isEmpty(jwtStr)) { return Optional.empty(); } - final Map jwtBodyMap = jwtService.parse(jwtStr, null).getResult(); + ParseResult parseResult = jwtService.parse(jwtStr, null); + if (ParseResult.Status.EXPIRED.equals(parseResult.getStatus())) { + throw new PowerJobAuthException(ErrorCodes.USER_NOT_LOGIN, "LoginExpired"); + } + final Map jwtBodyMap = parseResult.getResult(); if (MapUtils.isEmpty(jwtBodyMap)) { return Optional.empty(); diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/openapi/OpenAPIController.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/openapi/OpenAPIController.java index 40a9abf3..267c4355 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/openapi/OpenAPIController.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/openapi/OpenAPIController.java @@ -27,6 +27,7 @@ import tech.powerjob.server.persistence.remote.model.WorkflowInfoDO; import tech.powerjob.server.persistence.remote.model.WorkflowNodeInfoDO; import tech.powerjob.server.web.response.WorkflowInfoVO; +import javax.servlet.http.HttpServletResponse; import java.util.List; /** @@ -67,9 +68,12 @@ public class OpenAPIController { * @return 鉴权响应 */ @PostMapping(OpenAPIConstant.AUTH_APP) - public PowerResultDTO auth(@RequestBody AppAuthRequest appAuthRequest) { + public PowerResultDTO auth(@RequestBody AppAuthRequest appAuthRequest, HttpServletResponse response) { try { - return PowerResultDTO.s(openApiSecurityService.authAppByParam(appAuthRequest)); + AppAuthResult appAuthResult = openApiSecurityService.authAppByParam(appAuthRequest); + // 能顺利返回代表通过了鉴权 + response.addHeader(OpenAPIConstant.RESPONSE_HEADER_AUTH_STATUS, Boolean.TRUE.toString()); + return PowerResultDTO.s(appAuthResult); } catch (PowerJobException pje) { PowerResultDTO f = PowerResultDTO.f(pje.getMessage()); f.setCode(pje.getCode());