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

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyuan2021 committed Sep 5, 2023
1 parent 702cb2b commit a5136a4
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 82 deletions.
2 changes: 1 addition & 1 deletion arklet/arklet-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>${oshi.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -69,6 +68,7 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.fastjson.JSONObject;

import com.alipay.sofa.ark.common.util.BizIdentityUtils;
import com.alipay.sofa.common.utils.StringUtil;
import com.alipay.sofa.serverless.arklet.core.api.model.ResponseCode;
Expand All @@ -35,7 +34,6 @@
import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.UninstallBizHandler;
import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.*;
import com.alipay.sofa.serverless.arklet.core.command.coordinate.BizOpsCommandCoordinator;
import com.alipay.sofa.serverless.arklet.core.command.coordinate.CommandMutexException;
import com.alipay.sofa.serverless.arklet.core.command.executor.ExecutorServiceManager;
import com.alipay.sofa.serverless.arklet.core.command.meta.AbstractCommandHandler;
import com.alipay.sofa.serverless.arklet.core.command.meta.bizops.ArkBizMeta;
Expand Down Expand Up @@ -111,26 +109,7 @@ public Output<?> process(String cmd, Map content) throws CommandValidationExcept
if (isBizOpsHandler(handler)) {
ArkBizMeta arkBizMeta = (ArkBizMeta) input;
AssertUtils.assertNotNull(arkBizMeta,
"when execute bizOpsHandler, arkBizMeta should not be null");
boolean canProcess = BizOpsCommandCoordinator.checkAndLock(arkBizMeta.getBizName(),
arkBizMeta.getBizVersion(), handler.command());
if (!canProcess) {
return Output
.ofFailed(ResponseCode.FAILED.name()
+ ":"
+ String.format(
"%s %s conflict, exist unfinished command(%s) for this biz",
BizIdentityUtils.generateBizIdentity(arkBizMeta.getBizName(),
arkBizMeta.getBizVersion()),
handler.command().getId(),
BizOpsCommandCoordinator.getCurrentProcessingCommand(
arkBizMeta.getBizName(), arkBizMeta.getBizVersion()).getId()));
}
try {
handler.handle(input);
} finally {
BizOpsCommandCoordinator
.unlock(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());
"when execute bizOpsHandler, arkBizMeta should not be null");
if (arkBizMeta.isAync()) {
String requestId = arkBizMeta.getRequestId();
if (ProcessRecordHolder.getProcessRecord(requestId) != null) {
Expand All @@ -141,31 +120,50 @@ public Output<?> process(String cmd, Map content) throws CommandValidationExcept
ThreadPoolExecutor executor = ExecutorServiceManager.getArkBizOpsExecutor();
executor.submit(() -> {
try {
BizOpsCommandCoordinator.putBizExecution(arkBizMeta.getBizName(), arkBizMeta.getBizVersion(), handler.command());
processRecord.start();
Output output = handler.handle(input);
if (output.success()) {
processRecord.success();
boolean canProcess = BizOpsCommandCoordinator.checkAndLock(arkBizMeta.getBizName(),
arkBizMeta.getBizVersion(), handler.command());
if (!canProcess) {
processRecord.fail("command conflict, exist unfinished command for this biz");
} else {
processRecord.fail(output.getMessage());
processRecord.start();
Output output = handler.handle(input);
if (output.success()) {
processRecord.success();
} else {
processRecord.fail(output.getMessage());
}
}
} catch (Throwable throwable) {
processRecord.fail(throwable.getMessage(), throwable);
LOGGER.error("Error happened when handling command, requestId=" + requestId, throwable);
} finally {
processRecord.markFinishTime();
BizOpsCommandCoordinator.popBizExecution(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());
BizOpsCommandCoordinator
.unlock(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());
}
});
return Output.ofSuccess(processRecord);
} else {
boolean canProcess = BizOpsCommandCoordinator.checkAndLock(arkBizMeta.getBizName(),
arkBizMeta.getBizVersion(), handler.command());
if (!canProcess) {
return Output
.ofFailed(ResponseCode.FAILED.name()
+ ":"
+ String.format(
"%s %s conflict, exist unfinished command(%s) for this biz",
BizIdentityUtils.generateBizIdentity(arkBizMeta.getBizName(),
arkBizMeta.getBizVersion()),
handler.command().getId(),
BizOpsCommandCoordinator.getCurrentProcessingCommand(
arkBizMeta.getBizName(), arkBizMeta.getBizVersion()).getId()));
}
try {
BizOpsCommandCoordinator.putBizExecution(arkBizMeta.getBizName(), arkBizMeta.getBizVersion(), handler.command());
return handler.handle(input);
} catch (Throwable e) {
throw e;
handler.handle(input);
} finally {
BizOpsCommandCoordinator.popBizExecution(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());
BizOpsCommandCoordinator
.unlock(arkBizMeta.getBizName(), arkBizMeta.getBizVersion());

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.alipay.sofa.ark.api.ClientResponse;
import com.alipay.sofa.ark.api.ResponseCode;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.spi.model.BizInfo;
import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand;
import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler.Input;
import com.alipay.sofa.serverless.arklet.core.command.meta.AbstractCommandHandler;
Expand Down Expand Up @@ -62,8 +61,8 @@ public Command command() {
public void validate(Input input) throws CommandValidationException {
notBlank(input.getBizName(), "bizName should not be blank");
notBlank(input.getBizVersion(), "bizVersion should not be blank");
notBlank(input.getArkBizFilePath(), "arkBizFilePath should not be blank");
isTrue(!input.isAync() || !StringUtils.isEmpty(input.getRequestId()), "requestId should not be blank when aync is true");
isTrue(!input.isAync() || !StringUtils.isEmpty(input.getRequestId()),
"requestId should not be blank when aync is true");
notBlank(input.getBizUrl(), "bizUrl should not be blank");
}

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.command.builtin.handler;

import com.alipay.sofa.serverless.arklet.core.command.meta.AbstractCommandHandler;
Expand All @@ -16,7 +32,8 @@
* @author: yuanyuan
* @date: 2023/9/4 9:50 下午
*/
public class QueryBizOpsHandler extends AbstractCommandHandler<QueryBizOpsHandler.Input, ProcessRecord> {
public class QueryBizOpsHandler extends
AbstractCommandHandler<QueryBizOpsHandler.Input, ProcessRecord> {

@Override
public void validate(Input input) throws CommandValidationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public Command command() {
public void validate(Input input) throws CommandValidationException {
notBlank(input.getBizName(), "bizName should not be blank");
notBlank(input.getBizVersion(), "bizVersion should not be blank");
isTrue(!input.isAync() || !StringUtils.isEmpty(input.getRequestId()), "requestId should not be blank when aync is true");
isTrue(!input.isAync() || !StringUtils.isEmpty(input.getRequestId()),
"requestId should not be blank when aync is true");
}

public static class Input extends ArkBizMeta {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public Command command() {
public void validate(Input input) throws CommandValidationException {
notBlank(input.getBizName(), "bizName should not be blank");
notBlank(input.getBizVersion(), "bizVersion should not be blank");
isTrue(!input.isAync() || !StringUtils.isEmpty(input.getRequestId()), "requestId should not be blank when aync is true");
isTrue(!input.isAync() || !StringUtils.isEmpty(input.getRequestId()),
"requestId should not be blank when aync is true");
}

public static class Input extends ArkBizMeta {
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.command.executor;

import java.util.concurrent.ArrayBlockingQueue;
Expand All @@ -10,16 +26,14 @@
*/
public class ExecutorServiceManager {

private static ThreadPoolExecutor ARK_BIZ_OPS_EXECUTOR = new ThreadPoolExecutor(
20,
50,
30,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(
100),
new NamedThreadFactory(
"ark-biz-ops"),
new ThreadPoolExecutor.CallerRunsPolicy());
private static ThreadPoolExecutor ARK_BIZ_OPS_EXECUTOR = new ThreadPoolExecutor(
20,
50,
30,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(100),
new NamedThreadFactory("ark-biz-ops"),
new ThreadPoolExecutor.CallerRunsPolicy());

public static ThreadPoolExecutor getArkBizOpsExecutor() {
return ARK_BIZ_OPS_EXECUTOR;
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.command.executor;

import java.util.concurrent.ThreadFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
* @date 2023/8/21
*/
public class ArkBizMeta extends InputMeta {
private String bizName;
private String bizVersion;
private String requestId;
private String bizName;
private String bizVersion;
private String requestId;
private boolean aync;

public String getBizName() {
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.command.record;

import com.alipay.sofa.serverless.arklet.core.common.log.ArkletLogger;
Expand All @@ -20,24 +36,23 @@ public class ProcessRecord {

private static final ArkletLogger LOGGER = ArkletLoggerFactory.getDefaultLogger();

private String requestId;

private String threadName;
private String requestId;

private Status status;
private String threadName;

private Throwable throwable;
private Status status;

private String errorCode;
private Throwable throwable;

private String message;
private String errorCode;

private long startTimestamp;
private String message;

private long endTimestamp;
private long startTimestamp;

private long elapsedTime;
private long endTimestamp;

private long elapsedTime;

public enum Status {

Expand All @@ -49,7 +64,7 @@ public enum Status {

FAILED("FAILED");

private String name;
private String name;

Status(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
/*
* 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 org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
Expand Down
Loading

0 comments on commit a5136a4

Please sign in to comment.