Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #133 from Lunarscave/master
Browse files Browse the repository at this point in the history
add ut
  • Loading branch information
lvjing2 authored Sep 28, 2023
2 parents c201977 + d146fb2 commit 44f6c2c
Show file tree
Hide file tree
Showing 28 changed files with 1,230 additions and 205 deletions.
6 changes: 6 additions & 0 deletions sofa-serverless-runtime/arklet-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
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 @@ -41,19 +41,16 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
* @author Lunarscave
*/
@Singleton
public class HealthServiceImpl implements HealthService {

private static final ArkletLogger LOGGER = ArkletLoggerFactory
.getDefaultLogger();
private final HealthBuilder healthBuilder = new HealthBuilder();
private static final ArkletLogger LOGGER = ArkletLoggerFactory.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 +65,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 +102,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 +118,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 +155,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 All @@ -179,20 +176,19 @@ public Health queryModuleInfo(PluginModel pluginModel) {

@Override
public Health queryMasterBiz() {
BizHealthMeta bizHealthMeta = BizHealthMeta.createBizMeta(ArkClient.getMasterBiz());
return healthBuilder
.init()
.putHealthData(Constants.MASTER_BIZ_INFO,
JSON.parseObject(toJSONString(bizHealthMeta), JSONObject.class)).build();
BizHealthMeta.createBizMeta(ArkClient.getMasterBiz())).build();
}

@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
Expand Up @@ -16,11 +16,18 @@
*/
package com.alipay.sofa.serverless.arklet.core;

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.serverless.arklet.core.command.CommandService;
import com.alipay.sofa.serverless.arklet.core.health.HealthService;
import com.alipay.sofa.serverless.arklet.core.health.model.Health;
import com.alipay.sofa.serverless.arklet.core.ops.UnifiedOperationService;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

/**
* @author mingmen
Expand All @@ -33,7 +40,7 @@ public class BaseTest {
public static HealthService healthService;

@Before
public void setup() {
public void setup() throws Throwable {
if (componentRegistry == null) {
ArkletComponentRegistry registry = new ArkletComponentRegistry();
registry.initComponents();
Expand All @@ -43,4 +50,5 @@ public void setup() {
healthService = ArkletComponentRegistry.getHealthServiceInstance();
}
}

}
Loading

0 comments on commit 44f6c2c

Please sign in to comment.