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

add ut #133

Merged
merged 30 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
34e9cce
Increase the usage of MetaSpace in computing
sususama Sep 9, 2023
d5aaeea
Merge branch 'sofastack:feature.arklet_v1' into feature.arklet_v1
sususama Sep 11, 2023
bf47eef
optimize code
Sep 14, 2023
0d3944c
Merge pull request #99 from Lunarscave/feature.arklet_v1
TomorJM Sep 15, 2023
6463ab5
optimize code
Sep 15, 2023
13d8e09
Merge remote-tracking branch 'origin/feature.arklet_v1' into feature.…
Sep 15, 2023
100c7ce
optimize code
Sep 15, 2023
a832a20
fix test bug
Sep 15, 2023
cbae98d
Merge branch 'sofastack:feature.arklet_v1' into feature.arklet_v1
sususama Sep 19, 2023
9dfd8a9
fix import
sususama Sep 19, 2023
d0a68a8
add test
sususama Sep 20, 2023
ccccf62
fix
sususama Sep 20, 2023
31262fe
fix
sususama Sep 20, 2023
a3dccdb
fix
sususama Sep 21, 2023
5039b8b
fix
sususama Sep 21, 2023
1505ad4
fix actuator pom, make springboot-actuator dependencies strong
Sep 25, 2023
a514afd
Merge remote-tracking branch 'origin/master' into feature.arklet_v1
Sep 25, 2023
da37aff
fix actuator pom, make springboot-actuator dependencies strong
Sep 25, 2023
53a123c
remove useless code
Sep 25, 2023
48ee18c
fix actuator pom, and add unit test
Sep 25, 2023
1d8972b
fix actuator pom, and add unit test
Sep 25, 2023
3c8ec4b
fix actuator pom, and add unit test
Sep 25, 2023
4386f73
Merge pull request #85 from sususama/feature.arklet_v1
TomorJM Sep 27, 2023
1597718
Revert "Increase the usage of MetaSpace in computing"
TomorJM Sep 27, 2023
ddd2c70
Merge pull request #126 from sofastack/revert-85-feature.arklet_v1
TomorJM Sep 27, 2023
c87cc57
fix actuator pom, and add unit test
Sep 28, 2023
417e7d7
Merge branch 'sofastack:feature.arklet_v1' into feature.arklet_v1
Lunarscave Sep 28, 2023
152a5b5
git commit
Sep 28, 2023
5c8e6eb
remove useless code
Sep 28, 2023
d146fb2
remove useless code
Sep 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

Check warning on line 22 in sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginInfo.java

View check run for this annotation

Codecov / codecov/patch

sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/command/builtin/model/PluginInfo.java#L22

Added line #L22 was not covered by tests

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 @@
@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 @@
HealthBuilder builder = new HealthBuilder();
return builder.init().putAllHealthData(queryMasterBiz())
.putAllHealthData(queryModuleInfo(new BizInfo()))
.putAllHealthData(queryModuleInfo(new PluginModel())).build();
.putAllHealthData(queryModuleInfo(new PluginInfo())).build();

Check warning on line 105 in sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java#L105

Added line #L105 was not covered by tests
}

@Override
Expand All @@ -121,10 +118,10 @@
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));

Check warning on line 124 in sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java#L121-L124

Added lines #L121 - L124 were not covered by tests
}
} catch (Throwable e) {
builder.putErrorData(Constants.HEALTH_ERROR, e.getMessage());
Expand Down Expand Up @@ -158,8 +155,8 @@
}

@Override
public Health queryModuleInfo(PluginModel pluginModel) {
String pluginName = pluginModel.getPluginName();
public Health queryModuleInfo(PluginInfo pluginInfo) {
String pluginName = pluginInfo.getPluginName();

Check warning on line 159 in sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java#L159

Added line #L159 was not covered by tests
healthBuilder.init();
try {
if (StringUtils.isEmpty(pluginName)) {
Expand All @@ -179,20 +176,19 @@

@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();

Check warning on line 182 in sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/HealthServiceImpl.java#L182

Added line #L182 was not covered by tests
}

@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 double getDuration() {
return ConvertUtils.millisecond2Second(new Date(runtimeMxBean.getStartTime()));
return ConvertUtils.getDurationSecond(new Date(runtimeMxBean.getStartTime()));

Check warning on line 132 in sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/JvmIndicator.java

View check run for this annotation

Codecov / codecov/patch

sofa-serverless-runtime/arklet-core/src/main/java/com/alipay/sofa/serverless/arklet/core/health/indicator/JvmIndicator.java#L132

Added line #L132 was not covered by tests
}

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
Loading