Skip to content

Commit

Permalink
add tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
wt-better committed Nov 16, 2024
1 parent 7eaf5c7 commit d4fb80d
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-all</artifactId>
<version>2.1.0</version>
<version>2.3.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,59 @@
package org.apache.seata.consumer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.seata.consumer.service.SagaAnnotationTransactionService;
import org.apache.seata.saga.engine.AsyncCallback;
import org.apache.seata.saga.engine.StateMachineEngine;
import org.apache.seata.saga.proctrl.ProcessContext;
import org.apache.seata.saga.statelang.domain.ExecutionStatus;
import org.apache.seata.saga.statelang.domain.StateMachineInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static org.apache.seata.e2e.E2EUtil.isInE2ETest;
import static org.apache.seata.e2e.E2EUtil.writeE2EResFile;

@EnableDubbo(scanBasePackages = {"org.apache.seata.consumer"})
@ComponentScan(basePackages = {"org.apache.seata.consumer"})
public class SagaTransactionStarter {

private static final Logger LOGGER = LoggerFactory.getLogger(SagaTransactionStarter.class);

public static void main(String[] args) throws InterruptedException {
if (isInE2ETest()) {
// wait provider
Thread.sleep(5000);
}

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SagaTransactionStarter.class);

StateMachineEngine stateMachineEngine = (StateMachineEngine) applicationContext.getBean("stateMachineEngine");
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/seata-dubbo-reference.xml");
applicationContext.start();

transactionCommittedDemo(stateMachineEngine);
SagaAnnotationTransactionService sagaAnnotationTransactionService = (SagaAnnotationTransactionService) applicationContext.getBean("sagaAnnotationTransactionService");
sagaAnnotationTransactionService.doTransactionCommit();

transactionCompensatedDemo(stateMachineEngine);
if (isInE2ETest()) {
String res = "{\"res\": \"commit\"}";
writeE2EResFile(res, "commit.yaml");
res = "{\"res\": \"rollback\"}";
writeE2EResFile(res, "rollback.yaml");
}
//keep run
sagaAnnotationTransactionService.doTransactionRollback();
// if (isInE2ETest()) {
// // wait provider
// Thread.sleep(5000);
// }
//
// AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SagaTransactionStarter.class);
//
// StateMachineEngine stateMachineEngine = (StateMachineEngine) applicationContext.getBean("stateMachineEngine");
//
// transactionCommittedDemo(stateMachineEngine);
//
// transactionCompensatedDemo(stateMachineEngine);
// if (isInE2ETest()) {
// String res = "{\"res\": \"commit\"}";
// writeE2EResFile(res, "commit.yaml");
// res = "{\"res\": \"rollback\"}";
// writeE2EResFile(res, "rollback.yaml");
// }
// //keep run
Thread.currentThread().join();
}


private static void transactionCommittedDemo(StateMachineEngine stateMachineEngine) {

Map<String, Object> startParams = new HashMap<>(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.seata.service;
package org.apache.seata.consumer.service;

import org.apache.seata.action.ResultHolder;
import org.apache.seata.action.TccActionOne;
import org.apache.seata.action.TccActionTwo;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.provider.action.SagaActionOne;
import org.apache.seata.provider.action.SagaActionTwo;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -31,77 +28,62 @@
*
* @author zhangsen
*/
public class TccTransactionService {
public class SagaAnnotationTransactionService {

private TccActionOne tccActionOne;
private SagaActionOne sagaActionOne;

private TccActionTwo tccActionTwo;
private SagaActionTwo sagaActionTwo;

/**
* 发起分布式事务
*
* @return string string
*/
@GlobalTransactional
public String doTransactionCommit() {
public void doTransactionCommit() {
//第一个TCC 事务参与者
boolean result = tccActionOne.prepare(null, 1);
boolean result = sagaActionOne.prepare(null, 1);
if (!result) {
throw new RuntimeException("TccActionOne failed.");
}
List<String> list = new ArrayList<>();
list.add("c1");
list.add("c2");
result = tccActionTwo.prepare(null, "two", list);
result = sagaActionTwo.prepare(null, "two", list);
if (!result) {
throw new RuntimeException("TccActionTwo failed.");
}
return RootContext.getXID();
}

public void checkBranchTransaction(String xid, boolean commit) {
String actionOneResult = ResultHolder.getActionOneResult(xid);
String actionTwoResult = ResultHolder.getActionTwoResult(xid);
Assert.isTrue(commit ? "T".equals(actionOneResult) : "F".equals(actionOneResult), "分支事务" + (commit ? "提交" : "回滚") + "失败");
Assert.isTrue(commit ? "T".equals(actionTwoResult) : "F".equals(actionTwoResult), "分支事务" + (commit ? "提交" : "回滚") + "失败");
}



/**
* Do transaction rollback string.
*/
@GlobalTransactional
public void doTransactionRollback() {
//第一个TCC 事务参与者
boolean result = tccActionOne.prepare(null, 1);
boolean result = sagaActionOne.prepare(null, 1);
if (!result) {
throw new RuntimeException("TccActionOne failed.");
}
List<String> list = new ArrayList<>();
list.add("c1");
list.add("c2");
result = tccActionTwo.prepare(null, "two", list);
result = sagaActionTwo.prepare(null, "two", list);
if (!result) {
throw new RuntimeException("TccActionTwo failed.");
}

throw new RuntimeException("transaction rollback");
}

/**
* Sets tcc action one.
*
* @param tccActionOne the tcc action one
*/
public void setTccActionOne(TccActionOne tccActionOne) {
this.tccActionOne = tccActionOne;
public void setSagaActionOne(SagaActionOne sagaActionOne) {
this.sagaActionOne = sagaActionOne;
}

/**
* Sets tcc action two.
*
* @param tccActionTwo the tcc action two
*/
public void setTccActionTwo(TccActionTwo tccActionTwo) {
this.tccActionTwo = tccActionTwo;
public void setSagaActionTwo(SagaActionTwo sagaActionTwo) {
this.sagaActionTwo = sagaActionTwo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.apache.seata.provider.action;

import org.apache.seata.rm.tcc.api.BusinessActionContext;

/**
* @author wangte
* Create At 2024/11/16
*/
public interface SagaActionOne {

boolean prepare(BusinessActionContext actionContext, int a);

boolean compensation(BusinessActionContext actionContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.apache.seata.provider.action;

import org.apache.seata.rm.tcc.api.BusinessActionContext;

import java.util.List;

/**
* @author wangte
* Create At 2024/11/16
*/
public interface SagaActionTwo {

/**
* Prepare boolean.
*
* @param actionContext the action context
* @param b the b
* @param list the list
* @return the boolean
*/
boolean prepare(BusinessActionContext actionContext, String b, List list);

/**
* Rollback boolean.
*
* @param actionContext the action context
* @return the boolean
*/
public boolean rollback(BusinessActionContext actionContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@


<bean class="org.apache.seata.spring.annotation.GlobalTransactionScanner">
<constructor-arg value="tcc-sample-reference"/>
<constructor-arg value="saga-sample-reference"/>
<constructor-arg value="my_test_tx_group"/>
</bean>

<dubbo:application name="tcc-sample-reference">
<dubbo:application name="saga-sample-reference">
<dubbo:parameter key="qos.enable" value="false"/>
</dubbo:application>

Expand All @@ -43,12 +43,12 @@
<dubbo:provider timeout="10000" threads="10" threadpool="fixed" loadbalance="roundrobin"/>

<!-- 第一个TCC参与者 服务订阅 -->
<dubbo:reference id="tccActionOne" interface="org.apache.seata.action.TccActionOne" check="false"
<dubbo:reference id="sagaActionOne" interface="org.apache.seata.provider.action.SagaActionOne" check="false"
lazy="true"/>

<!-- 第二个TCC参与者 服务订阅 -->
<dubbo:reference id="tccActionTwo" interface="org.apache.seata.action.TccActionTwo" check="false"
<dubbo:reference id="sagaActionTwo" interface="org.apache.seata.provider.action.SagaActionTwo" check="false"
lazy="true"/>

<bean id="tccTransactionService" class="org.apache.seata.service.TccTransactionService"/>
<bean id="sagaAnnotationTransactionService" class="org.apache.seata.consumer.service.SagaAnnotationTransactionService"/>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-all</artifactId>
<version>2.1.0</version>
<version>2.3.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.apache.seata.e2e.E2EUtil.isInE2ETest;

Expand Down Expand Up @@ -49,18 +50,29 @@ public static void main(String[] args) throws Exception {
// }));

// mockZKServer();
if (isInE2ETest()) {
// wait seata-server
Thread.sleep(2000);
}

new AnnotationConfigApplicationContext(DubboProviderStarter.class);
// if (isInE2ETest()) {
// // wait seata-server
// Thread.sleep(2000);
// }
//
// new AnnotationConfigApplicationContext(DubboProviderStarter.class);
//
// LOGGER.info("dubbo provider started");
// //keep run
// Thread.currentThread().join();

mockZKServer();

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring/seata-dubbo-provider.xml");
classPathXmlApplicationContext.start();

LOGGER.info("dubbo provider started");
//keep run
Thread.currentThread().join();
}


private static void mockZKServer() throws Exception {
server = new TestingServer(2181, true);
server.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 org.apache.seata.provider.action;

import org.apache.seata.rm.tcc.api.BusinessActionContext;

/**
* SagaActionOne
*/
public interface SagaActionOne {

boolean prepare(BusinessActionContext actionContext, int a);

boolean compensation(BusinessActionContext actionContext);
}
Loading

0 comments on commit d4fb80d

Please sign in to comment.