Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdClear committed Sep 14, 2023
1 parent 77b7075 commit bf47eef
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @author Lunarscave
*/
public class PluginModel {
public class PluginInfo {

private String pluginName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
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;
import com.alipay.sofa.serverless.arklet.core.health.model.Constants;
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;
Expand All @@ -53,7 +53,7 @@ public class HealthServiceImpl implements HealthService {
.getDefaultLogger();
private final HealthBuilder healthBuilder = new HealthBuilder();

private final Map<String, ArkletBaseIndicator> indicators = new ConcurrentHashMap<>(3);
private final Map<String, Indicator> indicators = new ConcurrentHashMap<>(3);

@Override
public void init() {
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +29,7 @@
/**
* @author Lunarscave
*/
public class CpuIndicator extends ArkletBaseIndicator {
public class CpuIndicator extends Indicator {

private final CpuIndicatorHandler cpuIndicatorHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* @author Lunarscave
*/
public class JvmIndicator extends ArkletBaseIndicator {
public class JvmIndicator extends Indicator {

private final JvmIndicatorHandler jvmIndicatorHandler;

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -82,4 +81,5 @@ public void testIndicators() {
Assert.assertNotNull(healthService.getIndicator(Constants.CPU));
Assert.assertNotNull(healthService.getIndicator(Constants.JVM));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@
/**
* @author Lunarscave
*/
public class MasterBizHealthIndicator extends ArkletBaseIndicator {
public class MasterBizHealthIndicator extends Indicator {

private ApplicationAvailability applicationAvailability;

Expand Down

0 comments on commit bf47eef

Please sign in to comment.