Skip to content

Commit afb6302

Browse files
committed
分布式事务demo:TCC模式
1 parent 4806b67 commit afb6302

23 files changed

+1081
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package code.distribution.tcc.common;
2+
3+
/**
4+
* 〈一句话功能简述〉<p>
5+
* 〈功能详细描述〉
6+
*
7+
* @author zixiao
8+
* @date 2020/1/8
9+
*/
10+
public enum BranchState {
11+
12+
INITIAL,
13+
14+
ONE_PHASE_OK,
15+
16+
TWO_COMMIT_OK,
17+
18+
TWO_COMMITTING,
19+
20+
TWO_ROLLBACK_OK,
21+
22+
TWO_ROLLING_BACK,
23+
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package code.distribution.tcc.common;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* 〈一句话功能简述〉<p>
7+
* 〈功能详细描述〉
8+
*
9+
* @author zixiao
10+
* @date 2020/1/7
11+
*/
12+
@Data
13+
public class BranchTx {
14+
15+
private String id;
16+
17+
private BranchState state;
18+
19+
private TxMethod methods;
20+
21+
public BranchTx(String id, BranchState state, TxMethod methods) {
22+
this.id = id;
23+
this.state = state;
24+
this.methods = methods;
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package code.distribution.tcc.common;
2+
3+
/**
4+
* 〈一句话功能简述〉<p>
5+
* 〈功能详细描述〉
6+
*
7+
* @author zixiao
8+
* @date 2020/1/7
9+
*/
10+
public enum GlobalState {
11+
12+
PROCESSING,
13+
14+
// ONE_PHASE_OK,
15+
//
16+
// ONE_PHASE_FAIL,
17+
18+
TWO_COMMITTING,
19+
20+
TWO_COMMITTED,
21+
22+
TWO_ROLLING_BACK,
23+
24+
TWO_ROLLED_BACK
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package code.distribution.tcc.common;
2+
3+
import code.distribution.at.common.GlobalStatus;
4+
import code.distribution.at.utils.Xid;
5+
import lombok.Data;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* 〈一句话功能简述〉<p>
12+
* 〈功能详细描述〉
13+
*
14+
* @author zixiao
15+
* @date 2020/1/7
16+
*/
17+
@Data
18+
public class GlobalTx {
19+
20+
private String xid;
21+
22+
private GlobalState state;
23+
24+
private List<BranchTx> branches;
25+
26+
public GlobalTx() {
27+
xid = Xid.newXid();
28+
state = GlobalState.PROCESSING;
29+
branches = new ArrayList<>(4);
30+
}
31+
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package code.distribution.tcc.common;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* 〈一句话功能简述〉<p>
7+
* 〈功能详细描述〉
8+
*
9+
* @author zixiao
10+
* @date 2020/1/7
11+
*/
12+
@Target({ElementType.METHOD})
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Documented
15+
public @interface TccAction {
16+
17+
/**
18+
* 预占方法
19+
* @return
20+
*/
21+
String try1();
22+
23+
/**
24+
* 确认方法
25+
*/
26+
String confirm();
27+
28+
/**
29+
* 回滚方法
30+
*/
31+
String cancel();
32+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package code.distribution.tcc.common;
2+
3+
import lombok.Data;
4+
import java.io.Serializable;
5+
import java.util.Date;
6+
7+
/**
8+
* 〈事务控制表〉<p>
9+
* 〈功能详细描述〉
10+
*
11+
* @author zixiao
12+
* @date 2020/1/7
13+
*/
14+
@Data
15+
public class TxFlowDo implements Serializable {
16+
17+
private String txId;
18+
19+
private String branchId;
20+
21+
/**
22+
* 初始,已提交,已回滚
23+
*/
24+
private TxState state;
25+
26+
private Date createTime;
27+
28+
private Date modifyTime;
29+
30+
public TxFlowDo(String txId, String branchId, TxState state) {
31+
this.txId = txId;
32+
this.branchId = branchId;
33+
this.state = state;
34+
this.createTime = new Date();
35+
}
36+
37+
public static TxFlowDo buildInit(String txId, String branchId){
38+
return new TxFlowDo(txId, branchId, TxState.INIT);
39+
}
40+
41+
public static TxFlowDo buildCommit(String txId, String branchId){
42+
return new TxFlowDo(txId, branchId, TxState.COMMIT);
43+
}
44+
45+
public static TxFlowDo buildRollback(String txId, String branchId){
46+
return new TxFlowDo(txId, branchId, TxState.ROLLBACK);
47+
}
48+
49+
public static void main(String[] args) {
50+
TxFlowDo txFlowDo = TxFlowDo.buildCommit("txId", "branchId");
51+
System.out.println(txFlowDo);
52+
}
53+
54+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package code.distribution.tcc.common;
2+
3+
import lombok.Data;
4+
import org.apache.commons.lang3.reflect.MethodUtils;
5+
6+
import java.io.Serializable;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
10+
/**
11+
* 〈一句话功能简述〉<p>
12+
* 〈功能详细描述〉
13+
*
14+
* @author zixiao
15+
* @date 2020/1/7
16+
*/
17+
@Data
18+
public class TxMethod implements Serializable {
19+
20+
private Object object;
21+
22+
private Method tryMethod;
23+
24+
private Method confirmMethod;
25+
26+
private Method cancelMethod;
27+
28+
private Object[] args;
29+
30+
public TxMethod(Object object, Method tryMethod, Method confirmMethod, Method cancelMethod, Object[] args) {
31+
this.object = object;
32+
this.tryMethod = tryMethod;
33+
this.confirmMethod = confirmMethod;
34+
this.cancelMethod = cancelMethod;
35+
this.args = args;
36+
}
37+
38+
public boolean doTry(){
39+
return invoke(tryMethod);
40+
}
41+
42+
public boolean doConfirm(){
43+
return invoke(confirmMethod);
44+
}
45+
46+
public boolean doCancel(){
47+
return invoke(cancelMethod);
48+
}
49+
50+
private boolean invoke(Method method){
51+
try {
52+
method.invoke(object, args);
53+
return true;
54+
} catch (IllegalAccessException e) {
55+
return false;
56+
} catch (InvocationTargetException e) {
57+
return false;
58+
}
59+
}
60+
61+
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package code.distribution.tcc.common;
2+
3+
/**
4+
* 〈事务状态〉<p>
5+
* 〈功能详细描述〉
6+
*
7+
* @author zixiao
8+
* @date 2020/1/7
9+
*/
10+
public enum TxState {
11+
12+
INIT, COMMIT, ROLLBACK
13+
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package code.distribution.tcc.exception;
2+
3+
/**
4+
* 〈一句话功能简述〉<p>
5+
* 〈功能详细描述〉
6+
*
7+
* @author zixiao
8+
* @date 2020/1/8
9+
*/
10+
public class TccException extends RuntimeException {
11+
12+
public TccException(String message) {
13+
super(message);
14+
}
15+
16+
public TccException(String message, Throwable cause) {
17+
super(message, cause);
18+
}
19+
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* 〈TCC模式的分布式事务〉<p>
3+
* 1 空回滚,try没执行,允许cancel空操作
4+
* 2 防悬挂,先cancel后,再try导致资源不释放
5+
* 3 幂等控制,多次confirm/cancel
6+
*
7+
* @author zixiao
8+
* @date 2020/1/7
9+
*/
10+
package code.distribution.tcc;

0 commit comments

Comments
 (0)