Skip to content

Commit

Permalink
Easytransaction version 1.4.0 (#133)
Browse files Browse the repository at this point in the history
* upgrade Fescar0.3.1 to Seata 0.8.1
* support ET run without Zookeeper
	* implement DB based Master selector
	* implement DB based StringCodec
	* implement DB based snowflaker
* fix #128
  • Loading branch information
skyesx authored Oct 7, 2019
1 parent 621bcbc commit f13a082
Show file tree
Hide file tree
Showing 58 changed files with 2,079 additions and 143 deletions.
8 changes: 4 additions & 4 deletions easytrans-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
</dependency>

<dependency>
<groupId>com.alibaba.fescar</groupId>
<artifactId>fescar-rm-datasource</artifactId>
<version>0.3.1</version>
</dependency>
<groupId>io.seata</groupId>
<artifactId>seata-rm-datasource</artifactId>
<version>0.8.1</version>
</dependency>

<!-- for override a spring class method that imported common-logging -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public boolean process(LogProcessContext logCtx){
for(int i = 0;i < orderedContents.size() ; i++){
Content content = orderedContents.get(i);
//check log order
LOG.info("log content item {}, cid {}, tostring : {}", i,content.getcId(),content.toString());
Assert.isTrue(content.getcId() != null && content.getcId().equals(i + 1),"content list did not sort or contentId is null");

Class<? extends LogProcessor> proccessorClass = ContentType.getById(content.getLogType()).getProccessorClass();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.yiqiniu.easytrans.idgen.impl;

public class SnowFlake {
/**
* modified from https://github.com/beyondfengyu/SnowFlake
*/


/**
* 每一部分占用的位数
*/
private final static long TIMESTAMP_SECOND_BIT = 32;
private final static long SEQUENCE_BIT = 16; //序列号占用的位数 4096
public final static long MACHINE_BIT = 64 - TIMESTAMP_SECOND_BIT - SEQUENCE_BIT; //机器标识占用的位数 32
/**
* 每一部分的最大值
*/
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);

/**
* 每一部分向左的位移
*/
private final static long TIMESTMP_LEFT = SEQUENCE_BIT;
private final static long MACHINE_LEFT = TIMESTMP_LEFT + MACHINE_BIT;

private long machineId; //机器标识
private long sequence = 0L; //序列号
private long lastStmp = -1L;//上一次时间戳

public SnowFlake(long machineId) {
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
}
this.machineId = machineId;
}

/**
* 产生下一个ID
*
* @return
*/
public synchronized long nextId() {
long currStmp = getCurrentSecond();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}

if (currStmp == lastStmp) {
//相同秒内,序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE;
//同一秒的序列数已经达到最大
if (sequence == 0L) {
throw new RuntimeException("max tp/ms reached!");
}
} else {
//不同毫秒内,序列号置为0
sequence = 0L;
}

lastStmp = currStmp;

return machineId << MACHINE_LEFT // 机器标识部分
| currStmp << TIMESTMP_LEFT // 时间戳部分
| sequence; //序列号部分
}


private long getCurrentSecond() {
long mill = System.currentTimeMillis() / 1000;
return mill;
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -36,76 +36,6 @@ public long getCurrentTrxId(String busCode) {
}


/**
* modified from https://github.com/beyondfengyu/SnowFlake
*/
public static class SnowFlake {

/**
* 每一部分占用的位数
*/
private final static long TIMESTAMP_SECOND_BIT = 32;
private final static long SEQUENCE_BIT = 16; //序列号占用的位数 4096
private final static long MACHINE_BIT = 64 - TIMESTAMP_SECOND_BIT - SEQUENCE_BIT; //机器标识占用的位数 32
/**
* 每一部分的最大值
*/
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);

/**
* 每一部分向左的位移
*/
private final static long TIMESTMP_LEFT = SEQUENCE_BIT;
private final static long MACHINE_LEFT = TIMESTMP_LEFT + MACHINE_BIT;

private long machineId; //机器标识
private long sequence = 0L; //序列号
private long lastStmp = -1L;//上一次时间戳

public SnowFlake(long machineId) {
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
}
this.machineId = machineId;
}

/**
* 产生下一个ID
*
* @return
*/
public synchronized long nextId() {
long currStmp = getCurrentSecond();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}

if (currStmp == lastStmp) {
//相同秒内,序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE;
//同一秒的序列数已经达到最大
if (sequence == 0L) {
throw new RuntimeException("max tp/ms reached!");
}
} else {
//不同毫秒内,序列号置为0
sequence = 0L;
}

lastStmp = currStmp;

return machineId << MACHINE_LEFT // 机器标识部分
| currStmp << TIMESTMP_LEFT // 时间戳部分
| sequence; //序列号部分
}


private long getCurrentSecond() {
long mill = System.currentTimeMillis() / 1000;
return mill;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import java.util.Date;
import java.util.List;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import org.springframework.util.CollectionUtils;

import com.yiqiniu.easytrans.core.ConsistentGuardian;
import com.yiqiniu.easytrans.log.TransactionLogReader;
import com.yiqiniu.easytrans.log.vo.LogCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public boolean equals(Object obj) {
return true;
}


@Override
public String toString() {
return "TransactionId [appId=" + appId + ", busCode=" + busCode + ", trxId=" + trxId + "]";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fescar.core.context.RootContext;
import com.alibaba.fescar.core.exception.TransactionException;
import com.alibaba.fescar.core.model.Resource;
import com.alibaba.fescar.rm.datasource.DataSourceManager;
import com.yiqiniu.easytrans.core.EasytransConstant;
import com.yiqiniu.easytrans.filter.MetaDataFilter;
import com.yiqiniu.easytrans.protocol.TransactionId;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.BranchType;
import io.seata.core.model.Resource;
import io.seata.rm.DefaultResourceManager;

public abstract class AbstractAutoCpsMethod<P extends AutoCpsMethodRequest<R>, R extends Serializable> implements AutoCpsMethod<P, R> {

{
Expand Down Expand Up @@ -49,7 +51,7 @@ public final void doAutoCpsCommit(P param) {
if (ds instanceof Resource) {
Resource rs = (Resource) ds;
try {
DataSourceManager.get().branchCommit(getFescarXid(transactionId), callSeq, rs.getResourceId(), null);
DefaultResourceManager.get().branchCommit(BranchType.AT,getFescarXid(transactionId), callSeq, rs.getResourceId(), null);
} catch (TransactionException e) {
LOGGER.error("transaction commit exception occour , code:" + e.getCode(), e);
throw new RuntimeException("transaction exception", e);
Expand All @@ -74,7 +76,7 @@ public final void doAutoCpsRollback(P param) {
if (ds instanceof Resource) {
Resource rs = (Resource) ds;
try {
DataSourceManager.get().branchRollback(getFescarXid(transactionId), callSeq, rs.getResourceId(), null);
DefaultResourceManager.get().branchRollback(BranchType.AT,getFescarXid(transactionId), callSeq, rs.getResourceId(), null);
} catch (TransactionException e) {
LOGGER.error("transaction roll back exception occour , code:" + e.getCode(), e);
throw new RuntimeException("transaction exception", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.util.concurrent.Callable;

import com.alibaba.fescar.core.context.RootContext;
import io.seata.core.context.RootContext;


/**
* 用于执行本地事务时,确保更新、Select for update等操作能获取正确值
Expand Down
Loading

0 comments on commit f13a082

Please sign in to comment.