Skip to content

Commit

Permalink
bugfix and append doc
Browse files Browse the repository at this point in the history
  • Loading branch information
TomorJM committed Sep 11, 2023
1 parent 40945aa commit 77b7075
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
package com.alipay.sofa.serverless.arklet.core;

/**
* * Arklet component interface, managed by registry
* * @see ArkletComponentRegistry
* @author mingmen
* @date 2023/6/8
*/
public interface ArkletComponent {

/**
* ArkletComponent init method, called when arklet try to start
* the extended custom component should use this method to do some initialization
*/
void init();

/**
* ArkletComponent destroy method, called when arklet try to stop
* the extended custom component should use this method to destroy itself
*/
void destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.slf4j.Marker;

/**
* Logger Implementation for SOFAArk
/**
* @author mingmen
* @date 2023/6/14
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import com.alipay.sofa.common.log.LoggerSpaceManager;

/**
* LoggerFactory for SOFAArk
/**
* @author mingmen
* @date 2023/6/14
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alipay.sofa.ark.common.util.StringUtils;
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.CpuIndicator;
import com.alipay.sofa.serverless.arklet.core.health.indicator.JvmIndicator;
Expand All @@ -31,7 +32,6 @@
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.BizModel;
import com.alipay.sofa.serverless.arklet.core.command.builtin.model.PluginModel;
import com.alipay.sofa.serverless.arklet.core.common.log.ArkletLogger;
import com.alipay.sofa.serverless.arklet.core.common.log.ArkletLoggerFactory;
Expand Down Expand Up @@ -104,7 +104,7 @@ public Health getHealth(String[] indicatorIds) {
public Health queryModuleInfo() {
HealthBuilder builder = new HealthBuilder();
return builder.init().putAllHealthData(queryMasterBiz())
.putAllHealthData(queryModuleInfo(new BizModel()))
.putAllHealthData(queryModuleInfo(new BizInfo()))
.putAllHealthData(queryModuleInfo(new PluginModel())).build();
}

Expand All @@ -115,10 +115,10 @@ public Health queryModuleInfo(String type, String name, String version) {
AssertUtils.isTrue(StringUtils.isEmpty(type) || Constants.typeOfInfo(type),
"illegal type: %s", type);
if (StringUtils.isEmpty(type) || Constants.BIZ.equals(type)) {
BizModel bizModel = new BizModel();
bizModel.setBizName(name);
bizModel.setBizVersion(version);
builder.putAllHealthData(queryModuleInfo(bizModel));
BizInfo bizInfo = new BizInfo();
bizInfo.setBizName(name);
bizInfo.setBizVersion(version);
builder.putAllHealthData(queryModuleInfo(bizInfo));
}
if (StringUtils.isEmpty(type) || Constants.PLUGIN.equals(type)) {
PluginModel pluginModel = new PluginModel();
Expand All @@ -133,8 +133,8 @@ public Health queryModuleInfo(String type, String name, String version) {
}

@Override
public Health queryModuleInfo(BizModel bizModel) {
String bizName = bizModel.getBizName(), bizVersion = bizModel.getBizVersion();
public Health queryModuleInfo(BizInfo bizInfo) {
String bizName = bizInfo.getBizName(), bizVersion = bizInfo.getBizVersion();
healthBuilder.init();
try {
if (StringUtils.isEmpty(bizName) && StringUtils.isEmpty(bizVersion)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.alipay.sofa.serverless.arklet.core.ArkletComponent;

/**
* 统一操作服务接口
* Unified operation service interface, mainly interacts with the sofa-ark container
* @author mingmen
* @date 2023/6/14
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ public class InstallBizHandlerTests extends BaseTest {
// assertTrue(result.success());
//}

// @Test(expected = ArkRuntimeException.class)
// public void testHandle_Failure() throws Throwable {
//
// InstallBizHandler handler = (InstallBizHandler) commandService
// .getHandler(BuiltinCommand.INSTALL_BIZ);
//
// // 准备测试数据
// Input input = new Input();
// input.setBizUrl("testUrl");
//
// ClientResponse response = new ClientResponse();
// response.setCode(ResponseCode.FAILED);
//
// // 设置Mock行为
// when(operationService.install(input.getBizUrl())).thenReturn(response);
//
// // 执行测试
// Output<ClientResponse> result = handler.handle(input);
//
// // 验证结果
// Assert.assertSame(response, result.getData());
// Assert.assertTrue(result.failed());
// }
// @Test(expected = ArkRuntimeException.class)
// public void testHandle_Failure() throws Throwable {
//
// InstallBizHandler handler = (InstallBizHandler) commandService
// .getHandler(BuiltinCommand.INSTALL_BIZ);
//
// // 准备测试数据
// Input input = new Input();
// input.setBizUrl("testUrl");
//
// ClientResponse response = new ClientResponse();
// response.setCode(ResponseCode.FAILED);
//
// // 设置Mock行为
// when(operationService.install(input.getBizUrl())).thenReturn(response);
//
// // 执行测试
// Output<ClientResponse> result = handler.handle(input);
//
// // 验证结果
// Assert.assertSame(response, result.getData());
// Assert.assertTrue(result.failed());
// }

@Test(expected = CommandValidationException.class)
public void testValidate_BlankBizName() throws CommandValidationException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.arklet.core.health;

import com.alipay.sofa.serverless.arklet.core.ArkletComponentRegistry;
Expand All @@ -19,8 +35,9 @@ public class HealthTests extends BaseTest {
private void validateHealth(Health health, final String[] expectedMetrics) {
Assert.assertTrue(health != null && !health.getHealthData().isEmpty());
Map<String, Object> healthData = health.getHealthData();
for (String metric: expectedMetrics) {
Assert.assertTrue(healthData.containsKey(metric) && !((Map<?, ?>) healthData.get(metric)).isEmpty());
for (String metric : expectedMetrics) {
Assert.assertTrue(healthData.containsKey(metric)
&& !((Map<?, ?>) healthData.get(metric)).isEmpty());
}
}

Expand All @@ -34,11 +51,11 @@ private void validateHealth(Health health, String errorCode, String errorMessage
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();
// bizOperation.setBizVersion("test version");
// ArkClient.getBizFactoryService().createBiz(bizOperation, new File(testBiz.getFile()));
// ClassLoader cl = Thread.currentThread().getContextClassLoader();
// URL testBiz = cl.getResource("test-biz.jar");
// BizOperation bizOperation = new BizOperation();
// bizOperation.setBizVersion("test version");
// ArkClient.getBizFactoryService().createBiz(bizOperation, new File(testBiz.getFile()));
}

@Test
Expand All @@ -50,13 +67,14 @@ public void registerCustomCIndicator() {

@Test
public void testGetHealth() {
final String[] allMetrics = new String[]{Constants.CPU, Constants.JVM};
final String[] testMetrics = new String[]{Constants.CPU};
final String[] errorMetrics = new String[]{"nonMetrics"};
final String[] allMetrics = new String[] { Constants.CPU, Constants.JVM };
final String[] testMetrics = new String[] { Constants.CPU };
final String[] errorMetrics = new String[] { "nonMetrics" };
validateHealth(healthService.getHealth(), allMetrics);
validateHealth(healthService.getHealth(new String[0]), allMetrics);
validateHealth(healthService.getHealth(testMetrics), testMetrics);
validateHealth(healthService.getHealth(errorMetrics), Constants.HEALTH_ERROR, "indicator not registered");
validateHealth(healthService.getHealth(errorMetrics), Constants.HEALTH_ERROR,
"indicator not registered");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.arklet.core.health.custom;

import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.arklet.core.health.indicator;

import com.alipay.sofa.serverless.arklet.core.health.model.Health.HealthBuilder;
Expand All @@ -11,30 +27,33 @@ public class IndicatorTests {
@Test
public void testCpuIndicator() {
ArkletBaseIndicator indicator = new CpuIndicator();
final String[] indicatorMetrics = new String[]{"count", "type", "total used (%)", "user used (%)", "system used (%)", "free (%)"};
final String[] indicatorMetrics = new String[] { "count", "type", "total used (%)",
"user used (%)", "system used (%)", "free (%)" };
final String indicatorId = "cpu";
Map<String, Object> indicatorData = indicator.getHealthDetails();
Assert.assertEquals(indicator.getIndicatorId(), indicatorId);
Assert.assertNotNull(indicatorData);
Assert.assertNotNull(indicator.getHealthModel(new HealthBuilder()));
for (String indicatorMetric: indicatorMetrics) {
for (String indicatorMetric : indicatorMetrics) {
Assert.assertNotNull(indicatorData.get(indicatorMetric));
}
}

@Test
public void testJvmIndicator() {
ArkletBaseIndicator 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)",
"max heap memory(M)", "init non heap memory(M)", "used non heap memory(M)", "committed non heap memory(M)",
"max non heap memory(M)", "loaded class count", "unload class count", "total class count"};
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)",
"max heap memory(M)", "init non heap memory(M)", "used non heap memory(M)",
"committed non heap memory(M)", "max non heap memory(M)", "loaded class count",
"unload class count", "total class count" };
final String indicatorId = "jvm";
Map<String, Object> indicatorData = indicator.getHealthDetails();
Assert.assertEquals(indicator.getIndicatorId(), indicatorId);
Assert.assertNotNull(indicatorData);
Assert.assertNotNull(indicator.getHealthModel(new HealthBuilder()));
for (String indicatorMetric: indicatorMetrics) {
for (String indicatorMetric : indicatorMetrics) {
Assert.assertNotNull(indicatorData.get(indicatorMetric));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public class ArkletAutoConfiguration {

@Bean
@ConditionalOnMasterBiz
public ArkletComponentRegistry componentRegistry() {
public ArkletComponentRegistry arkletComponentRegistry() {
ArkletComponentRegistry registry = new ArkletComponentRegistry();
registry.initComponents();
return registry;
}

@Bean
@ConditionalOnMasterBiz
@DependsOn("componentRegistry")
@DependsOn("arkletComponentRegistry")
public MasterBizCmdHandlerCollector masterBizCmdHandlerCollector() {
return new MasterBizCmdHandlerCollector();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/**
* @author mingmen
* @date 2023/6/14
* custom directive extension for master base application
*/
@SuppressWarnings("rawtypes")
public class MasterBizCmdHandlerCollector implements ApplicationContextAware {
Expand All @@ -35,6 +36,7 @@ public class MasterBizCmdHandlerCollector implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, AbstractCommandHandler> map = applicationContext.getBeansOfType(AbstractCommandHandler.class);
map.forEach((k, v) -> {
// find custom directive beans from master base's spring context
ArkletComponentRegistry.getCommandServiceInstance().registerCommandHandler(v);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void onApplicationEvent(ApplicationContextEvent event) {
LOGGER.info("total supported commands:{}", commands);
}
if (event instanceof ContextClosedEvent) {
// destroy arklet components
ArkletComponentRegistry componentRegistry = event.getApplicationContext().getBean(ArkletComponentRegistry.class);
componentRegistry.destroyComponents();
}
Expand Down

0 comments on commit 77b7075

Please sign in to comment.