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

Increase the usage of MetaSpace in computing #85

Merged
merged 9 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.alipay.sofa.serverless.arklet.core.command.meta.Command;
import com.alipay.sofa.serverless.arklet.core.command.meta.InputMeta;
import com.alipay.sofa.serverless.arklet.core.command.meta.Output;
import com.alipay.sofa.serverless.arklet.core.command.record.MetaSpaceRecord;
import com.alipay.sofa.serverless.arklet.core.command.record.ProcessRecord;
import com.alipay.sofa.serverless.arklet.core.command.record.ProcessRecordHolder;
import com.alipay.sofa.serverless.arklet.core.common.exception.ArkletInitException;
Expand Down Expand Up @@ -110,6 +111,7 @@ public Output<?> process(String cmd, Map content) throws CommandValidationExcept
ArkBizMeta arkBizMeta = (ArkBizMeta) input;
AssertUtils.assertNotNull(arkBizMeta,
"when execute bizOpsHandler, arkBizMeta should not be null");
final MetaSpaceRecord metaSpaceRecord = new MetaSpaceRecord();
if (arkBizMeta.isAsync()) {
String requestId = arkBizMeta.getRequestId();
if (ProcessRecordHolder.getProcessRecord(requestId) != null) {
Expand All @@ -127,6 +129,7 @@ public Output<?> process(String cmd, Map content) throws CommandValidationExcept
processRecord.fail("command conflict, exist unfinished command for this biz");
} else {
processRecord.start();
processRecord.setStartSpace(metaSpaceRecord.getMetaSpaceMXBean().getUsage().getUsed());
Output output = handler.handle(input);
if (output.success()) {
processRecord.success();
Expand All @@ -138,6 +141,7 @@ public Output<?> process(String cmd, Map content) throws CommandValidationExcept
processRecord.fail(throwable.getMessage(), throwable);
LOGGER.error("Error happened when handling command, requestId=" + requestId, throwable);
} finally {
processRecord.setEndSpace(metaSpaceRecord.getMetaSpaceMXBean().getUsage().getUsed());
processRecord.markFinishTime();
BizOpsCommandCoordinator
.unlock(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());
Expand All @@ -160,7 +164,10 @@ public Output<?> process(String cmd, Map content) throws CommandValidationExcept
arkBizMeta.getBizName(), arkBizMeta.getBizVersion()).getId()));
}
try {
return handler.handle(input);
final long startSpace = metaSpaceRecord.getMetaSpaceMXBean().getUsage().getUsed();
Output<?> output = handler.handle(input);
output.setElapsedSpace(metaSpaceRecord.getMetaSpaceMXBean().getUsage().getUsed() - startSpace);
return output;
} finally {
BizOpsCommandCoordinator
.unlock(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Output<T> {

private ResponseCode code;
private String message;
private long elapsedSpace;
private T data;

private Output() {
Expand Down Expand Up @@ -84,4 +85,12 @@ public T getData() {
public void setData(T data) {
this.data = data;
}

public void setElapsedSpace(long elapsedSpace) {
this.elapsedSpace = elapsedSpace;
}

public long getElapsedSpace() {
return this.elapsedSpace;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.command.record;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.util.List;

/**
* @author sususama
* @since 2023-09-21
*/

public class MetaSpaceRecord {
private static MemoryPoolMXBean metaSpaceMXBean;
static {
List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans)
if (memoryPoolMXBean.getName().equals("Metaspace"))
metaSpaceMXBean = memoryPoolMXBean;
}

public MemoryPoolMXBean getMetaSpaceMXBean() {
return metaSpaceMXBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@ public class ProcessRecord {

private Date startTime;

private long startSpace;

private long startTimestamp;

private Date endTime;

private long endSpace;

private long endTimestamp;

private long elapsedTime;

private long elapsedSpace;

public enum Status {

INITIALIZED("INITIALIZED"),
Expand All @@ -89,6 +95,7 @@ public void setName(String name) {
}

public void markFinishTime() {
setElapsedSpace(getEndSpace() - getStartSpace());
Date date = new Date();
setEndTime(date);
setEndTimestamp(date.getTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alipay.sofa.serverless.arklet.core.command.record;

import com.alipay.sofa.serverless.arklet.core.command.meta.InputMeta;
import com.alipay.sofa.serverless.arklet.core.command.meta.bizops.ArkBizMeta;
import org.apache.commons.lang3.StringUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
@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, Indicator> indicators = new ConcurrentHashMap<>(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
*/
package com.alipay.sofa.serverless.arklet.core.command;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

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.InstallBizHandler;
Expand All @@ -32,6 +28,9 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
* @author mingmen
* @date 2023/6/26
Expand Down Expand Up @@ -67,12 +66,16 @@ public void testInstallHandler() throws InterruptedException {
Assert.assertNotNull(output);
ProcessRecord processRecord = (ProcessRecord) output.getData();
Assert.assertNotNull(processRecord);
Assert.assertNotNull(processRecord.getEndSpace());

QueryBizOpsHandler.Input input1 = new QueryBizOpsHandler.Input();
input1.setRequestId(rid);
Map map1 = JSONObject.parseObject(JSONObject.toJSONString(input1), Map.class);
Output<?> output1 = commandService.process(BuiltinCommand.QUERY_BIZ_OPS.getId(), map1);
Assert.assertNotNull(output1);
ProcessRecord processRecord1 = (ProcessRecord) output1.getData();
Assert.assertNotNull(processRecord1);
Assert.assertNotNull(processRecord1.getEndSpace());
}

}
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.command;

import com.alipay.sofa.serverless.arklet.core.BaseTest;
Expand All @@ -12,7 +28,7 @@
*/
public class HealthHandlerTest extends BaseTest {

private void testValidate(Input input) throws CommandValidationException{
private void testValidate(Input input) throws CommandValidationException {
HealthHandler handler = (HealthHandler) commandService.getHandler(BuiltinCommand.HEALTH);
handler.validate(input);
}
Expand Down