Skip to content

Commit

Permalink
refactor: modify the RPC protocol, remove the client's resolution of …
Browse files Browse the repository at this point in the history
…xid to be stateless (#769)
  • Loading branch information
slievrly authored Apr 14, 2019
1 parent a9c9d01 commit f37eed2
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 322 deletions.
15 changes: 0 additions & 15 deletions common/src/main/java/com/alibaba/fescar/common/XID.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,6 @@ public static long getTransactionId(String xid) {
return Long.parseLong(xid.substring(idx + 1));
}

/**
* Gets server address.
*
* @param xid the xid
* @return the server address
*/
public static String getServerAddress(String xid) {
if (xid == null) {
return null;
}

int idx = xid.lastIndexOf(":");
return xid.substring(0, idx);
}

/**
* Gets port.
*
Expand Down
9 changes: 0 additions & 9 deletions common/src/test/java/com/alibaba/fescar/common/XIDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ public void testGenerateXID() {
assertThat(XID.generateXID(tranId)).isEqualTo(XID.getIpAddress() + ":" + XID.getPort() + ":" + tranId);
}

/**
* Test get server address.
*/
@Test
public void testGetServerAddress() {
assertThat(XID.getServerAddress(null)).isNull();
assertThat(XID.getServerAddress("127.0.0.1:8080:8577662204289747564")).isEqualTo("127.0.0.1:8080");
}

/**
* Test get transaction id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,56 @@
*/
public abstract class AbstractBranchEndResponse extends AbstractTransactionResponse {

/**
* The Xid.
*/
protected String xid;

/**
* The Branch id.
*/
protected long branchId;
/**
* The Branch status.
*/
protected BranchStatus branchStatus;

/**
* Gets xid.
*
* @return the xid
*/
public String getXid() {
return xid;
}

/**
* Sets xid.
*
* @param xid the xid
*/
public void setXid(String xid) {
this.xid = xid;
}

/**
* Gets branch id.
*
* @return the branch id
*/
public long getBranchId() {
return branchId;
}

/**
* Sets branch id.
*
* @param branchId the branch id
*/
public void setBranchId(long branchId) {
this.branchId = branchId;
}

/**
* Gets branch status.
*
Expand All @@ -55,12 +100,29 @@ public void setBranchStatus(BranchStatus branchStatus) {
@Override
protected void doEncode() {
super.doEncode();
if (this.xid != null) {
byte[] bs = xid.getBytes(UTF8);
byteBuffer.putShort((short)bs.length);
if (bs.length > 0) {
byteBuffer.put(bs);
}
} else {
byteBuffer.putShort((short)0);
}
byteBuffer.putLong(this.branchId);
byteBuffer.put((byte)branchStatus.getCode());
}

@Override
public void decode(ByteBuffer byteBuffer) {
super.decode(byteBuffer);
short xidLen = byteBuffer.getShort();
if (xidLen > 0) {
byte[] bs = new byte[xidLen];
byteBuffer.get(bs);
this.setXid(new String(bs, UTF8));
}
branchId = byteBuffer.getLong();
branchStatus = BranchStatus.get(byteBuffer.get());
}

Expand All @@ -77,6 +139,12 @@ public boolean decode(ByteBuf in) {
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("xid=");
result.append(xid);
result.append(",");
result.append("branchId=");
result.append(branchId);
result.append(",");
result.append("branchStatus=");
result.append(branchStatus);
result.append(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,29 @@
*/
public abstract class AbstractGlobalEndRequest extends AbstractTransactionRequestToTC implements MergedMessage {

/**
* The Transaction id.
*/
protected long transactionId;
private String xid;

/**
* The Extra data.
*/
protected String extraData;

/**
* Gets transaction id.
* Gets xid.
*
* @return the transaction id
* @return the xid
*/
public long getTransactionId() {
return transactionId;
public String getXid() {
return xid;
}

/**
* Sets transaction id.
* Sets xid.
*
* @param transactionId the transaction id
* @param xid the xid
*/
public void setTransactionId(long transactionId) {
this.transactionId = transactionId;
public void setXid(String xid) {
this.xid = xid;
}

/**
Expand All @@ -76,7 +73,16 @@ public void setExtraData(String extraData) {
@Override
public byte[] encode() {
ByteBuffer byteBuffer = ByteBuffer.allocate(256);
byteBuffer.putLong(this.transactionId);
// 1. xid
if (this.xid != null) {
byte[] bs = xid.getBytes(UTF8);
byteBuffer.putShort((short)bs.length);
if (bs.length > 0) {
byteBuffer.put(bs);
}
} else {
byteBuffer.putShort((short)0);
}
if (this.extraData != null) {
byte[] bs = extraData.getBytes(UTF8);
byteBuffer.putShort((short)bs.length);
Expand All @@ -95,7 +101,12 @@ public byte[] encode() {

@Override
public void decode(ByteBuffer byteBuffer) {
this.transactionId = byteBuffer.getLong();
short xidLen = byteBuffer.getShort();
if (xidLen > 0) {
byte[] bs = new byte[xidLen];
byteBuffer.get(bs);
this.setXid(new String(bs, UTF8));
}
short len = byteBuffer.getShort();
if (len > 0) {
byte[] bs = new byte[len];
Expand All @@ -107,8 +118,8 @@ public void decode(ByteBuffer byteBuffer) {
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("transactionId=");
result.append(transactionId);
result.append("xid=");
result.append(xid);
result.append(",");
result.append("extraData=");
result.append(extraData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BranchRegisterRequest extends AbstractTransactionRequestToTC implem

private static final long serialVersionUID = 1242711598812634704L;

private long transactionId;
private String xid;

private BranchType branchType = BranchType.AT;

Expand All @@ -42,21 +42,21 @@ public class BranchRegisterRequest extends AbstractTransactionRequestToTC implem
private String applicationData;

/**
* Gets transaction id.
* Gets xid.
*
* @return the transaction id
* @return the xid
*/
public long getTransactionId() {
return transactionId;
public String getXid() {
return xid;
}

/**
* Sets transaction id.
* Sets xid.
*
* @param transactionId the transaction id
* @param xid the xid
*/
public void setTransactionId(long transactionId) {
this.transactionId = transactionId;
public void setXid(String xid) {
this.xid = xid;
}

/**
Expand Down Expand Up @@ -118,10 +118,20 @@ public short getTypeCode() {
return TYPE_BRANCH_REGISTER;
}

/**
* Gets application data.
*
* @return the application data
*/
public String getApplicationData() {
return applicationData;
}

/**
* Sets application data.
*
* @param applicationData the application data
*/
public void setApplicationData(String applicationData) {
this.applicationData = applicationData;
}
Expand All @@ -145,8 +155,16 @@ public byte[] encode() {
}
byteBuffer = ByteBuffer.allocate(byteLenth + 1024);

// 1. Transaction Id
byteBuffer.putLong(this.transactionId);
// 1. xid
if (this.xid != null) {
byte[] bs = xid.getBytes(UTF8);
byteBuffer.putShort((short)bs.length);
if (bs.length > 0) {
byteBuffer.put(bs);
}
} else {
byteBuffer.putShort((short)0);
}
// 2. Branch Type
byteBuffer.put((byte)this.branchType.ordinal());
// 3. Resource Id
Expand Down Expand Up @@ -188,7 +206,12 @@ public byte[] encode() {

@Override
public void decode(ByteBuffer byteBuffer) {
this.transactionId = byteBuffer.getLong();
short xidLen = byteBuffer.getShort();
if (xidLen > 0) {
byte[] bs = new byte[xidLen];
byteBuffer.get(bs);
this.setXid(new String(bs, UTF8));
}
this.branchType = BranchType.get(byteBuffer.get());
short len = byteBuffer.getShort();
if (len > 0) {
Expand Down Expand Up @@ -220,8 +243,8 @@ public AbstractTransactionResponse handle(RpcContext rpcContext) {
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("transactionId=");
result.append(transactionId);
result.append("xid=");
result.append(xid);
result.append(",");
result.append("branchType=");
result.append(branchType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,8 @@
public class BranchRegisterResponse extends AbstractTransactionResponse implements Serializable {

private static final long serialVersionUID = 8317040433102745774L;

private long transactionId;

private long branchId;

/**
* Gets transaction id.
*
* @return the transaction id
*/
public long getTransactionId() {
return transactionId;
}

/**
* Sets transaction id.
*
* @param transactionId the transaction id
*/
public void setTransactionId(long transactionId) {
this.transactionId = transactionId;
}

/**
* Gets branch id.
*
Expand All @@ -78,25 +57,20 @@ public short getTypeCode() {
@Override
protected void doEncode() {
super.doEncode();
byteBuffer.putLong(transactionId);
byteBuffer.putLong(branchId);

}

@Override
public void decode(ByteBuffer byteBuffer) {
super.decode(byteBuffer);
this.transactionId = byteBuffer.getLong();
this.branchId = byteBuffer.getLong();
}

@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("BranchRegisterResponse: transactionId=");
result.append(transactionId);
result.append(",");
result.append("branchId=");
result.append("BranchRegisterResponse: branchId=");
result.append(branchId);
result.append(",");
result.append("result code =");
Expand Down
Loading

0 comments on commit f37eed2

Please sign in to comment.