diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginModel.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginInfo.java similarity index 98% rename from arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginModel.java rename to arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginInfo.java index e22540171..72fc52fc1 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginModel.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginInfo.java @@ -19,7 +19,7 @@ /** * @author Lunarscave */ -public class PluginModel { +public class PluginInfo { private String pluginName; diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthService.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthService.java index 5716525e7..328a38d09 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthService.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthService.java @@ -17,33 +17,81 @@ package com.alipay.sofa.serverless.arklet.core.health; import com.alipay.sofa.serverless.arklet.core.ArkletComponent; -import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator; +import com.alipay.sofa.serverless.arklet.core.health.indicator.Indicator; import com.alipay.sofa.serverless.arklet.core.health.model.Health; import com.alipay.sofa.serverless.arklet.core.command.builtin.model.BizInfo; -import com.alipay.sofa.serverless.arklet.core.command.builtin.model.PluginModel; +import com.alipay.sofa.serverless.arklet.core.command.builtin.model.PluginInfo; /** * @author Lunarscave */ public interface HealthService extends ArkletComponent { + /** + * get system health with all indicators + * @return health with all details of indicators + */ Health getHealth(); + /** + * get system health with indicator id + * @param indicatorId indicator ids + * @return health with indicator detail + */ Health getHealth(String indicatorId); + /** + * get system health with indicator ids + * @param indicatorIds indicator ids + * @return health with indicator detail(s) + */ Health getHealth(String[] indicatorIds); + /** + * query all module info + * @return health with module infos + */ Health queryModuleInfo(); + /** + * query module info with type, name and version + * @param type module type, must in ("biz", "plugin") + * @param name module name + * @param version module version + * @return health with module info(s) + */ Health queryModuleInfo(String type, String name, String version); + /** + * query biz info + * @param bizInfo input plugin info + * @return health with biz info(list) + */ Health queryModuleInfo(BizInfo bizInfo); - Health queryModuleInfo(PluginModel pluginModel); + /** + * query plugin info + * @param pluginInfo input plugin info + * @return health with plugin info(list) + */ + Health queryModuleInfo(PluginInfo pluginInfo); + /** + * query master biz info + * @return health with master biz + */ Health queryMasterBiz(); - ArkletBaseIndicator getIndicator(String indicatorId); + /** + * get indicator by indicator id + * @param indicatorId indicator id + * @return indicator or null + */ + Indicator getIndicator(String indicatorId); - void registerIndicator(ArkletBaseIndicator arkletBaseIndicator); + /** + * register indicator + * @param indicator input indicator + */ + void registerIndicator(Indicator indicator); } diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java index ceee9a51c..dc4fb1b7a 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java @@ -24,7 +24,7 @@ import com.alipay.sofa.ark.spi.model.Biz; import com.alipay.sofa.common.utils.ArrayUtil; import com.alipay.sofa.serverless.arklet.core.command.builtin.model.BizInfo; -import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator; +import com.alipay.sofa.serverless.arklet.core.health.indicator.Indicator; import com.alipay.sofa.serverless.arklet.core.health.indicator.CpuIndicator; import com.alipay.sofa.serverless.arklet.core.health.indicator.JvmIndicator; import com.alipay.sofa.serverless.arklet.core.health.model.BizHealthMeta; @@ -32,7 +32,7 @@ import com.alipay.sofa.serverless.arklet.core.health.model.Health; import com.alipay.sofa.serverless.arklet.core.health.model.Health.HealthBuilder; import com.alipay.sofa.serverless.arklet.core.health.model.PluginHealthMeta; -import com.alipay.sofa.serverless.arklet.core.command.builtin.model.PluginModel; +import com.alipay.sofa.serverless.arklet.core.command.builtin.model.PluginInfo; import com.alipay.sofa.serverless.arklet.core.common.log.ArkletLogger; import com.alipay.sofa.serverless.arklet.core.common.log.ArkletLoggerFactory; import com.google.inject.Singleton; @@ -53,7 +53,7 @@ public class HealthServiceImpl implements HealthService { .getDefaultLogger(); private final HealthBuilder healthBuilder = new HealthBuilder(); - private final Map indicators = new ConcurrentHashMap<>(3); + private final Map indicators = new ConcurrentHashMap<>(3); @Override public void init() { @@ -68,7 +68,7 @@ public void destroy() { @Override public Health getHealth() { HealthBuilder builder = new HealthBuilder(); - for (ArkletBaseIndicator indicator : this.indicators.values()) { + for (Indicator indicator : this.indicators.values()) { builder.putAllHealthData(indicator.getHealthModel(healthBuilder)); } return builder.build(); @@ -105,7 +105,7 @@ public Health queryModuleInfo() { HealthBuilder builder = new HealthBuilder(); return builder.init().putAllHealthData(queryMasterBiz()) .putAllHealthData(queryModuleInfo(new BizInfo())) - .putAllHealthData(queryModuleInfo(new PluginModel())).build(); + .putAllHealthData(queryModuleInfo(new PluginInfo())).build(); } @Override @@ -121,10 +121,10 @@ public Health queryModuleInfo(String type, String name, String version) { builder.putAllHealthData(queryModuleInfo(bizInfo)); } if (StringUtils.isEmpty(type) || Constants.PLUGIN.equals(type)) { - PluginModel pluginModel = new PluginModel(); - pluginModel.setPluginName(name); - pluginModel.setPluginVersion(version); - builder.putAllHealthData(queryModuleInfo(pluginModel)); + PluginInfo pluginInfo = new PluginInfo(); + pluginInfo.setPluginName(name); + pluginInfo.setPluginVersion(version); + builder.putAllHealthData(queryModuleInfo(pluginInfo)); } } catch (Throwable e) { builder.putErrorData(Constants.HEALTH_ERROR, e.getMessage()); @@ -158,8 +158,8 @@ public Health queryModuleInfo(BizInfo bizInfo) { } @Override - public Health queryModuleInfo(PluginModel pluginModel) { - String pluginName = pluginModel.getPluginName(); + public Health queryModuleInfo(PluginInfo pluginInfo) { + String pluginName = pluginInfo.getPluginName(); healthBuilder.init(); try { if (StringUtils.isEmpty(pluginName)) { @@ -187,12 +187,12 @@ public Health queryMasterBiz() { } @Override - public ArkletBaseIndicator getIndicator(String indicatorId) { + public Indicator getIndicator(String indicatorId) { return indicators.get(indicatorId); } @Override - public void registerIndicator(ArkletBaseIndicator indicator) { + public void registerIndicator(Indicator indicator) { this.indicators.put(indicator.getIndicatorId(), indicator); LOGGER.info("register indicator " + indicator.getIndicatorId()); } diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/CpuIndicator.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/CpuIndicator.java index 42949c489..87e02afe1 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/CpuIndicator.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/CpuIndicator.java @@ -19,8 +19,6 @@ import com.alipay.sofa.serverless.arklet.core.health.model.Constants; import oshi.SystemInfo; import oshi.hardware.CentralProcessor; -import oshi.util.GlobalConfig; -import oshi.util.Util; import java.util.HashMap; import java.util.HashSet; @@ -31,7 +29,7 @@ /** * @author Lunarscave */ -public class CpuIndicator extends ArkletBaseIndicator { +public class CpuIndicator extends Indicator { private final CpuIndicatorHandler cpuIndicatorHandler; diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/ArkletBaseIndicator.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/Indicator.java similarity index 81% rename from arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/ArkletBaseIndicator.java rename to arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/Indicator.java index c0deaad59..cd2a44256 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/ArkletBaseIndicator.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/Indicator.java @@ -24,20 +24,33 @@ /** * @author Lunarscave */ -public abstract class ArkletBaseIndicator { +public abstract class Indicator { private final String indicatorId; - public ArkletBaseIndicator(String indicatorId) { + public Indicator(String indicatorId) { this.indicatorId = indicatorId; } + /** + * get health details + * @return a map of health details + */ protected abstract Map getHealthDetails(); + /** + * get indicator id + * @return indicator id + */ public String getIndicatorId() { return indicatorId; } + /** + * get health model + * @param builder input health builder + * @return health model + */ public Health getHealthModel(HealthBuilder builder) { return builder.init().putHealthData(getIndicatorId(), getHealthDetails()).build(); } diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/JvmIndicator.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/JvmIndicator.java index 3255219eb..9972572b4 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/JvmIndicator.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/JvmIndicator.java @@ -31,7 +31,7 @@ /** * @author Lunarscave */ -public class JvmIndicator extends ArkletBaseIndicator { +public class JvmIndicator extends Indicator { private final JvmIndicatorHandler jvmIndicatorHandler; @@ -129,7 +129,7 @@ public long getFreeMemory() { } public double getDuration() { - return ConvertUtils.millisecond2Second(new Date(runtimeMxBean.getStartTime())); + return ConvertUtils.getDurationSecond(new Date(runtimeMxBean.getStartTime())); } public long getInitHeapMemory() { diff --git a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/util/ConvertUtils.java b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/util/ConvertUtils.java index 7129449c9..b85bd92c4 100644 --- a/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/util/ConvertUtils.java +++ b/arklet/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/util/ConvertUtils.java @@ -24,16 +24,21 @@ */ public class ConvertUtils { + /** + * convert bytes(B) to megabyte(MB) + * @param bytes input byte(B) value + * @return megabyte(MB) + */ public static double bytes2Megabyte(Long bytes) { return ((double) bytes) / 1024 / 1024; } - public static String endDate2Duration(Date date) { - long duration = System.currentTimeMillis() - date.getTime(); - return new SimpleDateFormat("HH-mm-ss").format(duration); - } - - public static double millisecond2Second(Date date) { + /** + * get duration from param date till now and change to second(s) + * @param date input date + * @return output duration(s) + */ + public static double getDurationSecond(Date date) { return ((double) System.currentTimeMillis() - date.getTime()) / 1000; } diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/HealthHandlerTest.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/HealthHandlerTest.java new file mode 100644 index 000000000..8c2ec0dc8 --- /dev/null +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/command/HealthHandlerTest.java @@ -0,0 +1,27 @@ +package com.alipay.sofa.serverless.arklet.core.command; + +import com.alipay.sofa.serverless.arklet.core.BaseTest; +import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand; +import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.HealthHandler; +import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.HealthHandler.Input; +import com.alipay.sofa.serverless.arklet.core.common.exception.CommandValidationException; +import org.junit.Test; + +/** + * @author lunarscave + */ +public class HealthHandlerTest extends BaseTest { + + private void testValidate(Input input) throws CommandValidationException{ + HealthHandler handler = (HealthHandler) commandService.getHandler(BuiltinCommand.HEALTH); + handler.validate(input); + } + + @Test(expected = CommandValidationException.class) + public void testValidate_InvalidType() throws CommandValidationException { + Input input = new Input(); + input.setType("non type"); + testValidate(input); + } + +} diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/HealthTests.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/HealthTests.java index 40be30153..bc2de39dd 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/HealthTests.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/HealthTests.java @@ -50,7 +50,6 @@ private void validateHealth(Health health, String errorCode, String errorMessage @Before public void initHealthService() throws IOException { this.healthService = ArkletComponentRegistry.getHealthServiceInstance(); - // ClassLoader cl = Thread.currentThread().getContextClassLoader(); // URL testBiz = cl.getResource("test-biz.jar"); // BizOperation bizOperation = new BizOperation(); @@ -82,4 +81,5 @@ public void testIndicators() { Assert.assertNotNull(healthService.getIndicator(Constants.CPU)); Assert.assertNotNull(healthService.getIndicator(Constants.JVM)); } + } diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/custom/CustomIndicator.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/custom/CustomIndicator.java index fdfef9642..80f2400c4 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/custom/CustomIndicator.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/custom/CustomIndicator.java @@ -16,12 +16,12 @@ */ package com.alipay.sofa.serverless.arklet.core.health.custom; -import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator; +import com.alipay.sofa.serverless.arklet.core.health.indicator.Indicator; import java.util.HashMap; import java.util.Map; -public class CustomIndicator extends ArkletBaseIndicator { +public class CustomIndicator extends Indicator { public CustomIndicator() { super("custom"); } diff --git a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/indicator/IndicatorTests.java b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/indicator/IndicatorTests.java index 3d01e1107..34c4e1a1b 100644 --- a/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/indicator/IndicatorTests.java +++ b/arklet/arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/indicator/IndicatorTests.java @@ -26,7 +26,7 @@ public class IndicatorTests { @Test public void testCpuIndicator() { - ArkletBaseIndicator indicator = new CpuIndicator(); + Indicator indicator = new CpuIndicator(); final String[] indicatorMetrics = new String[] { "count", "type", "total used (%)", "user used (%)", "system used (%)", "free (%)" }; final String indicatorId = "cpu"; @@ -41,7 +41,7 @@ public void testCpuIndicator() { @Test public void testJvmIndicator() { - ArkletBaseIndicator indicator = new JvmIndicator(); + Indicator indicator = new JvmIndicator(); final String[] indicatorMetrics = new String[] { "java version", "java home", "total memory(M)", "max memory(M)", "free memory(M)", "run time(s)", "init heap memory(M)", "used heap memory(M)", "committed heap memory(M)", diff --git a/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/HealthAutoConfiguration.java b/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/HealthAutoConfiguration.java index 1fc02e4ef..962b99875 100644 --- a/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/HealthAutoConfiguration.java +++ b/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/HealthAutoConfiguration.java @@ -66,7 +66,7 @@ public ArkHealthCodeEndpoint arkHealthCodeEndpoint() { } @Bean - @DependsOn("componentRegistry") + @DependsOn("arkletComponentRegistry") public MasterBizHealthIndicator masterBizHealthIndicator() { MasterBizHealthIndicator masterBizHealthIndicator = new MasterBizHealthIndicator(); masterBizHealthIndicator.setApplicationAvailability(context diff --git a/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/extension/indicator/MasterBizHealthIndicator.java b/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/extension/indicator/MasterBizHealthIndicator.java index 819b3fe48..011d1e1b5 100644 --- a/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/extension/indicator/MasterBizHealthIndicator.java +++ b/arklet/arklet-springboot-starter/src/main/java/com/alipay/sofa/serverless/arklet/springboot/starter/health/extension/indicator/MasterBizHealthIndicator.java @@ -16,7 +16,7 @@ */ package com.alipay.sofa.serverless.arklet.springboot.starter.health.extension.indicator; -import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator; +import com.alipay.sofa.serverless.arklet.core.health.indicator.Indicator; import com.alipay.sofa.serverless.arklet.core.health.model.Constants; import com.alipay.sofa.serverless.arklet.core.util.AssertUtils; import org.springframework.boot.availability.ApplicationAvailability; @@ -27,7 +27,7 @@ /** * @author Lunarscave */ -public class MasterBizHealthIndicator extends ArkletBaseIndicator { +public class MasterBizHealthIndicator extends Indicator { private ApplicationAvailability applicationAvailability;