From 25a34957387c3fadfd759a2b241593d79f57c788 Mon Sep 17 00:00:00 2001 From: sususama <1023605039@qq.com> Date: Thu, 5 Oct 2023 21:44:15 +0800 Subject: [PATCH 1/2] record meta space --- .../builtin/handler/InstallBizHandler.java | 36 +++++++++++++++---- .../core/command/handler/BaseHandlerTest.java | 8 ++--- .../handler/InstallBizHandlerTests.java | 9 +++-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java b/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java index 1fb20cacd..600ce284b 100644 --- a/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java +++ b/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java @@ -30,23 +30,38 @@ import lombok.Getter; import lombok.Setter; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryPoolMXBean; +import java.util.List; + /** * @author mingmen * @date 2023/6/8 */ -public class InstallBizHandler extends - AbstractCommandHandler - implements - ArkBizOps { +public class InstallBizHandler + extends + AbstractCommandHandler + implements + ArkBizOps { @Override - public Output handle(Input input) { + public Output handle(Input input) { + MemoryPoolMXBean metaSpaceMXBean = null; + List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) + if (memoryPoolMXBean.getName().equals("Metaspace")) + metaSpaceMXBean = memoryPoolMXBean; + long startSpace = metaSpaceMXBean.getUsage().getUsed(); + InstallBizHandler.ClientResponseMetaSpace clientResponseMetaSpace = new InstallBizHandler.ClientResponseMetaSpace(); try { ClientResponse res = getOperationService().install(input.getBizUrl()); + clientResponseMetaSpace.setElapsedSpace(metaSpaceMXBean.getUsage().getUsed() + - startSpace); + clientResponseMetaSpace.setClientResponse(res); if (ResponseCode.SUCCESS.equals(res.getCode())) { - return Output.ofSuccess(res); + return Output.ofSuccess(clientResponseMetaSpace); } else { - return Output.ofFailed(res, "install biz not success!"); + return Output.ofFailed(clientResponseMetaSpace, "install biz not success!"); } } catch (Throwable e) { throw new ArkletRuntimeException(e); @@ -73,4 +88,11 @@ public static class Input extends ArkBizMeta { private String bizUrl; } + @Getter + @Setter + public static class ClientResponseMetaSpace extends ClientResponse { + private long elapsedSpace; + private ClientResponse clientResponse; + } + } diff --git a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java index 9e95ae8cc..42fcc9173 100644 --- a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java +++ b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java @@ -17,10 +17,10 @@ package com.alipay.sofa.serverless.arklet.core.command.handler; import com.alipay.sofa.ark.api.ArkClient; -import com.alipay.sofa.ark.api.ClientResponse; import com.alipay.sofa.ark.api.ResponseCode; import com.alipay.sofa.ark.spi.model.BizOperation; import com.alipay.sofa.serverless.arklet.core.BaseTest; +import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler; import com.alipay.sofa.serverless.arklet.core.health.custom.CustomBizManagerService; import com.alipay.sofa.serverless.arklet.core.health.custom.CustomPluginManagerService; import org.junit.After; @@ -35,9 +35,9 @@ */ public class BaseHandlerTest extends BaseTest { - public final ClientResponse success = new ClientResponse(); - public final ClientResponse failed = new ClientResponse(); - public MockedStatic arkClient; + public final InstallBizHandler.ClientResponseMetaSpace success = new InstallBizHandler.ClientResponseMetaSpace(); + public final InstallBizHandler.ClientResponseMetaSpace failed = new InstallBizHandler.ClientResponseMetaSpace(); + public MockedStatic arkClient; @Before public void setupHandler() { diff --git a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java index 5711923f8..3b41d7fe3 100644 --- a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java +++ b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java @@ -16,7 +16,6 @@ */ package com.alipay.sofa.serverless.arklet.core.command.handler; -import com.alipay.sofa.ark.api.ClientResponse; import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand; import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler; import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler.Input; @@ -48,9 +47,9 @@ public void testHandle_Success() throws Throwable { when(handler.getOperationService().install(input.getBizUrl())).thenReturn(success); - Output result = handler.handle(input); + Output result = handler.handle(input); - Assert.assertEquals(success, result.getData()); + Assert.assertEquals(success, result.getData().getClientResponse()); Assert.assertTrue(result.success()); } @@ -61,9 +60,9 @@ public void testHandle_Failure() throws Throwable { when(handler.getOperationService().install(input.getBizUrl())).thenReturn(failed); - Output result = handler.handle(input); + Output result = handler.handle(input); - Assert.assertSame(failed, result.getData()); + Assert.assertSame(failed, result.getData().getClientResponse()); Assert.assertTrue(result.failed()); } From 6e71307e1369ef85722a4cbe1991beb152706287 Mon Sep 17 00:00:00 2001 From: sususama <1023605039@qq.com> Date: Sun, 8 Oct 2023 11:28:51 +0800 Subject: [PATCH 2/2] fix --- .../builtin/handler/InstallBizHandler.java | 40 ++++++++++--------- .../core/command/handler/BaseHandlerTest.java | 6 +-- .../handler/InstallBizHandlerTests.java | 8 ++-- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java b/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java index 600ce284b..fd66aec2e 100644 --- a/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java +++ b/sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/handler/InstallBizHandler.java @@ -40,34 +40,39 @@ */ public class InstallBizHandler extends - AbstractCommandHandler - implements - ArkBizOps { + AbstractCommandHandler + implements + ArkBizOps { @Override - public Output handle(Input input) { - MemoryPoolMXBean metaSpaceMXBean = null; - List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) - if (memoryPoolMXBean.getName().equals("Metaspace")) - metaSpaceMXBean = memoryPoolMXBean; + public Output handle(Input input) { + MemoryPoolMXBean metaSpaceMXBean = getMetaSpaceMXBean(); long startSpace = metaSpaceMXBean.getUsage().getUsed(); - InstallBizHandler.ClientResponseMetaSpace clientResponseMetaSpace = new InstallBizHandler.ClientResponseMetaSpace(); + InstallBizClientResponse installBizClientResponse; try { ClientResponse res = getOperationService().install(input.getBizUrl()); - clientResponseMetaSpace.setElapsedSpace(metaSpaceMXBean.getUsage().getUsed() - - startSpace); - clientResponseMetaSpace.setClientResponse(res); + installBizClientResponse = (InstallBizClientResponse) res; + installBizClientResponse.setElapsedSpace(metaSpaceMXBean.getUsage().getUsed() + - startSpace); if (ResponseCode.SUCCESS.equals(res.getCode())) { - return Output.ofSuccess(clientResponseMetaSpace); + return Output.ofSuccess(installBizClientResponse); } else { - return Output.ofFailed(clientResponseMetaSpace, "install biz not success!"); + return Output.ofFailed(installBizClientResponse, "install biz not success!"); } } catch (Throwable e) { throw new ArkletRuntimeException(e); } } + private MemoryPoolMXBean getMetaSpaceMXBean() { + MemoryPoolMXBean metaSpaceMXBean = null; + List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) + if (memoryPoolMXBean.getName().equals("Metaspace")) + metaSpaceMXBean = memoryPoolMXBean; + return metaSpaceMXBean; + } + @Override public Command command() { return BuiltinCommand.INSTALL_BIZ; @@ -90,9 +95,8 @@ public static class Input extends ArkBizMeta { @Getter @Setter - public static class ClientResponseMetaSpace extends ClientResponse { - private long elapsedSpace; - private ClientResponse clientResponse; + public static class InstallBizClientResponse extends ClientResponse { + private long elapsedSpace; } } diff --git a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java index 42fcc9173..039255ce5 100644 --- a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java +++ b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/BaseHandlerTest.java @@ -35,9 +35,9 @@ */ public class BaseHandlerTest extends BaseTest { - public final InstallBizHandler.ClientResponseMetaSpace success = new InstallBizHandler.ClientResponseMetaSpace(); - public final InstallBizHandler.ClientResponseMetaSpace failed = new InstallBizHandler.ClientResponseMetaSpace(); - public MockedStatic arkClient; + public final InstallBizHandler.InstallBizClientResponse success = new InstallBizHandler.InstallBizClientResponse(); + public final InstallBizHandler.InstallBizClientResponse failed = new InstallBizHandler.InstallBizClientResponse(); + public MockedStatic arkClient; @Before public void setupHandler() { diff --git a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java index 3b41d7fe3..9d4a2da11 100644 --- a/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java +++ b/sofa-serverless-runtime/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/handler/InstallBizHandlerTests.java @@ -47,9 +47,9 @@ public void testHandle_Success() throws Throwable { when(handler.getOperationService().install(input.getBizUrl())).thenReturn(success); - Output result = handler.handle(input); + Output result = handler.handle(input); - Assert.assertEquals(success, result.getData().getClientResponse()); + Assert.assertEquals(success, result.getData()); Assert.assertTrue(result.success()); } @@ -60,9 +60,9 @@ public void testHandle_Failure() throws Throwable { when(handler.getOperationService().install(input.getBizUrl())).thenReturn(failed); - Output result = handler.handle(input); + Output result = handler.handle(input); - Assert.assertSame(failed, result.getData().getClientResponse()); + Assert.assertSame(failed, result.getData()); Assert.assertTrue(result.failed()); }