Skip to content

Commit

Permalink
optimize: add saga compatible subcomponent-level api (#6329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptyin authored Feb 5, 2024
1 parent 736f2b9 commit 843d2a4
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 14 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6301](https://github.com/apache/incubator-seata/pull/6301)] upgrade console frontend dependencies and supported nodejs versions
- [[#6301](https://github.com/apache/incubator-seata/pull/6312)] add saga related io.seata compatible api
- [[#6313](https://github.com/apache/incubator-seata/pull/6313)] console display the version number
- [[#6329](https://github.com/apache/incubator-seata/pull/6312)] add saga subcomponent-level io.seata compatible api
- [[#6254](https://github.com/apache/incubator-seata/pull/6254)] optimize Hessian Serialize

### security:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- [[#6301](https://github.com/apache/incubator-seata/pull/6301)] 升级console前端依赖及支持的nodejs版本
- [[#6301](https://github.com/apache/incubator-seata/pull/6312)] 添加saga相关的io.seata兼容性API
- [[#6313](https://github.com/apache/incubator-seata/pull/6313)] console展示版本号
- [[#6329](https://github.com/apache/incubator-seata/pull/6312)] 添加saga子组件的io.seata兼容性API
- [[#6254](https://github.com/apache/incubator-seata/pull/6254)] 优化Hessian 序列化


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package io.seata.saga.engine;

import org.apache.seata.saga.engine.expression.ExpressionFactoryManager;
import io.seata.saga.engine.expression.ExpressionFactoryManager;
import io.seata.saga.engine.repo.StateLogRepository;
import io.seata.saga.engine.repo.StateMachineRepository;
import org.apache.seata.saga.engine.expression.ExpressionResolver;
import org.apache.seata.saga.engine.invoker.ServiceInvokerManager;
import org.apache.seata.saga.engine.repo.StateLogRepository;
import org.apache.seata.saga.engine.repo.StateMachineRepository;
import org.apache.seata.saga.engine.sequence.SeqGenerator;
import org.apache.seata.saga.engine.store.StateLangStore;
import org.apache.seata.saga.engine.store.StateLogStore;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 io.seata.saga.engine.expression;

/**
* Expression
*
*/
public interface Expression extends org.apache.seata.saga.engine.expression.Expression {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 io.seata.saga.engine.expression;

/**
* Expression Factory
*
*/
public interface ExpressionFactory extends org.apache.seata.saga.engine.expression.ExpressionFactory {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 io.seata.saga.engine.expression;

import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Expression factory manager
*/
public class ExpressionFactoryManager {

private final org.apache.seata.saga.engine.expression.ExpressionFactoryManager actual;

public static final String DEFAULT_EXPRESSION_TYPE = "Default";

private ExpressionFactoryManager(org.apache.seata.saga.engine.expression.ExpressionFactoryManager actual) {
this.actual = actual;
}

public ExpressionFactoryManager() {
actual = new org.apache.seata.saga.engine.expression.ExpressionFactoryManager();
}


public ExpressionFactory getExpressionFactory(String expressionType) {
org.apache.seata.saga.engine.expression.ExpressionFactory expressionFactory = actual.getExpressionFactory(expressionType);
return expressionFactory::createExpression;
}

public void setExpressionFactoryMap(Map<String, ExpressionFactory> expressionFactoryMap) {
Map<String, org.apache.seata.saga.engine.expression.ExpressionFactory> actualExpressionFactoryMap = expressionFactoryMap.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<String, org.apache.seata.saga.engine.expression.ExpressionFactory>(e.getKey(), e.getValue()))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
actual.setExpressionFactoryMap(actualExpressionFactoryMap);
}

public void putExpressionFactory(String type, ExpressionFactory factory) {
actual.putExpressionFactory(type, factory);
}

public org.apache.seata.saga.engine.expression.ExpressionFactoryManager unwrap() {
return actual;
}

public static ExpressionFactoryManager wrap(org.apache.seata.saga.engine.expression.ExpressionFactoryManager actual) {
return new ExpressionFactoryManager(actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,31 @@
package io.seata.saga.engine.impl;

import io.seata.saga.engine.StateMachineConfig;
import org.apache.seata.saga.engine.expression.ExpressionFactoryManager;
import io.seata.saga.engine.expression.ExpressionFactoryManager;
import io.seata.saga.engine.repo.StateLogRepository;
import io.seata.saga.engine.repo.StateMachineRepository;
import io.seata.saga.statelang.domain.StateInstance;
import io.seata.saga.statelang.domain.StateMachineInstance;
import io.seata.saga.statelang.domain.impl.StateInstanceImpl;
import io.seata.saga.statelang.domain.impl.StateMachineInstanceImpl;
import org.apache.seata.saga.engine.expression.ExpressionResolver;
import org.apache.seata.saga.engine.invoker.ServiceInvokerManager;
import org.apache.seata.saga.engine.repo.StateLogRepository;
import org.apache.seata.saga.engine.repo.StateMachineRepository;
import org.apache.seata.saga.engine.sequence.SeqGenerator;
import org.apache.seata.saga.engine.store.StateLangStore;
import org.apache.seata.saga.engine.store.StateLogStore;
import org.apache.seata.saga.engine.strategy.StatusDecisionStrategy;
import org.apache.seata.saga.proctrl.eventing.impl.ProcessCtrlEventPublisher;
import org.apache.seata.saga.statelang.domain.StateMachine;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

import javax.script.ScriptEngineManager;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
* Default state machine configuration
Expand All @@ -42,7 +51,15 @@ public class DefaultStateMachineConfig implements StateMachineConfig, Applicatio

private final org.apache.seata.saga.engine.impl.DefaultStateMachineConfig actual;

public DefaultStateMachineConfig(org.apache.seata.saga.engine.impl.DefaultStateMachineConfig actual) {
private ExpressionFactoryManager expressionFactoryManager;

private ExpressionResolver expressionResolver;

public DefaultStateMachineConfig() {
this.actual = new org.apache.seata.saga.engine.impl.DefaultStateMachineConfig();
}

private DefaultStateMachineConfig(org.apache.seata.saga.engine.impl.DefaultStateMachineConfig actual) {
this.actual = actual;
}

Expand Down Expand Up @@ -71,11 +88,15 @@ public void setStateLangStore(StateLangStore stateLangStore) {

@Override
public ExpressionFactoryManager getExpressionFactoryManager() {
return actual.getExpressionFactoryManager();
if (expressionFactoryManager == null) {
expressionFactoryManager = ExpressionFactoryManager.wrap(actual.getExpressionFactoryManager());
}
return expressionFactoryManager;
}

public void setExpressionFactoryManager(ExpressionFactoryManager expressionFactoryManager) {
actual.setExpressionFactoryManager(expressionFactoryManager);
this.expressionFactoryManager = expressionFactoryManager;
this.expressionResolver.setExpressionFactoryManager(expressionFactoryManager.unwrap());
}

@Override
Expand All @@ -98,7 +119,37 @@ public void setCharset(String charset) {

@Override
public StateMachineRepository getStateMachineRepository() {
return actual.getStateMachineRepository();
org.apache.seata.saga.engine.repo.StateMachineRepository repository = actual.getStateMachineRepository();
if (repository instanceof StateMachineRepository) {
return (StateMachineRepository) repository;
}

return new StateMachineRepository() {
@Override
public StateMachine getStateMachineById(String stateMachineId) {
return repository.getStateMachineById(stateMachineId);
}

@Override
public StateMachine getStateMachine(String stateMachineName, String tenantId) {
return repository.getStateMachine(stateMachineName, tenantId);
}

@Override
public StateMachine getStateMachine(String stateMachineName, String tenantId, String version) {
return repository.getStateMachine(stateMachineName, tenantId, version);
}

@Override
public StateMachine registryStateMachine(StateMachine stateMachine) {
return repository.registryStateMachine(stateMachine);
}

@Override
public void registryByResources(Resource[] resources, String tenantId) throws IOException {
repository.registryByResources(resources, tenantId);
}
};
}

public void setStateMachineRepository(StateMachineRepository stateMachineRepository) {
Expand Down Expand Up @@ -168,10 +219,41 @@ public void setEnableAsync(boolean enableAsync) {

@Override
public StateLogRepository getStateLogRepository() {
return actual.getStateLogRepository();
}

public void setStateLogRepository(StateLogRepository stateLogRepository) {
org.apache.seata.saga.engine.repo.StateLogRepository repository = actual.getStateLogRepository();
if (repository instanceof StateLogRepository) {
return (StateLogRepository) repository;
}
return new StateLogRepository() {
@Override
public StateMachineInstance getStateMachineInstance(String stateMachineInstanceId) {
return StateMachineInstanceImpl.wrap(repository.getStateMachineInstance(stateMachineInstanceId));
}

@Override
public StateMachineInstance getStateMachineInstanceByBusinessKey(String businessKey, String tenantId) {
return StateMachineInstanceImpl.wrap(repository.getStateMachineInstanceByBusinessKey(businessKey, tenantId));
}

@Override
public List<StateMachineInstance> queryStateMachineInstanceByParentId(String parentId) {
return repository.queryStateMachineInstanceByParentId(parentId).stream()
.map(StateMachineInstanceImpl::wrap).collect(Collectors.toList());
}

@Override
public StateInstance getStateInstance(String stateInstanceId, String machineInstId) {
return StateInstanceImpl.wrap(repository.getStateInstance(stateInstanceId, machineInstId));
}

@Override
public List<StateInstance> queryStateInstanceListByMachineInstanceId(String stateMachineInstanceId) {
return repository.queryStateInstanceListByMachineInstanceId(stateMachineInstanceId).stream()
.map(StateInstanceImpl::wrap).collect(Collectors.toList());
}
};
}

public void setStateLogRepository(org.apache.seata.saga.engine.repo.StateLogRepository stateLogRepository) {
actual.setStateLogRepository(stateLogRepository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public StateMachineConfig getStateMachineConfig() {
actual.getStateMachineConfig());
}

public void setStateMachineConfig(StateMachineConfig stateMachineConfig) {
actual.setStateMachineConfig(((DefaultStateMachineConfig) stateMachineConfig).unwrap());
}

@Override
public StateMachineInstance reloadStateMachineInstance(String instId) {
return StateMachineInstanceImpl.wrap(actual.reloadStateMachineInstance(instId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 io.seata.saga.engine.repo;

import io.seata.saga.statelang.domain.StateInstance;
import io.seata.saga.statelang.domain.StateMachineInstance;

import java.util.List;

/**
* State Log Repository
*
*/
public interface StateLogRepository {

/**
* Get state machine instance
*
* @param stateMachineInstanceId the state machine instance id
* @return the state machine instance
*/
StateMachineInstance getStateMachineInstance(String stateMachineInstanceId);

/**
* Get state machine instance by businessKey
*
* @param businessKey the business key
* @param tenantId the tenant id
* @return the state machine instance
*/
StateMachineInstance getStateMachineInstanceByBusinessKey(String businessKey, String tenantId);

/**
* Query the list of state machine instances by parent id
*
* @param parentId the state parent id
* @return state machine instance list
*/
List<StateMachineInstance> queryStateMachineInstanceByParentId(String parentId);

/**
* Get state instance
*
* @param stateInstanceId the state instance id
* @param machineInstId the state machine instance id
* @return the state instance
*/
StateInstance getStateInstance(String stateInstanceId, String machineInstId);

/**
* Get a list of state instances by state machine instance id
*
* @param stateMachineInstanceId the state machine instance id
* @return the state machine instance list
*/
List<StateInstance> queryStateInstanceListByMachineInstanceId(String stateMachineInstanceId);
}
Loading

0 comments on commit 843d2a4

Please sign in to comment.