distributed transaction solution - Seata

 Distributed Transaction Solution
GTS(Global Transaction Service)
ACID
XA(eXtended Architecture) X/Open. TM(Transaction Manager), RM(Resource Manager), and Application Manager.
XA->2PC(Prepare, commit, rollback)->TCC(Try,Confirm,Canccel)->3PC(CanCommit,PreCommit,DoCommit)

Saga , 1987 year, by Hector and Kenneth for Long lived transaction.Princeton University

Seata

TXC->GTS->Seata->Seata GA

TC(Transaction Coordinator),TM(Transaction Manager),RM(Resource Manager).
http://seata.io/en-us/docs/overview/what-is-seata.html

XID - Transaction ID

TC - Transaction Coordinator

Maintain status of global and branch transactions, drive the global commit or rollback.

TM - Transaction Manager

Define the scope of global transaction: begin a global transaction, commit or rollback a global transaction.

RM - Resource Manager

Manage resources that branch transactions working on, talk to TC for registering branch transactions and reporting status of branch transactions, and drive the branch transaction commit or rollback.

watch?v=IWhUqwuTKg8&list=PLmOn9nNkQxJGVG1ktTV4SedFWuyef_Pi0&index=139

Hash : user_id % 10 are number of tables. user_id==985 must be in table 5.

1. Twitter IDS

https://developer.twitter.com/en/docs/twitter-ids, Twitter IDs are unique 64-bit unsigned integers, which are based on time, instead of being sequential. The full ID is composed of a timestamp, a worker number, and a sequence number. Twitter developed an internal service known as “Snowflake” in order to consistently generate these IDs.

Numbers as large as 64-bits can cause issues with programming languages that represent integers with fewer than 64-bits. An example of this is JavaScript, where integers are limited to 53-bits in size. In order to provide a workaround for this, in the original designs of the Twitter API (v1/1.1), ID values were returned in two formats: both as integers, and as strings.

{"id": 10765432100123456789, "id_str": "10765432100123456789"}

If you run the command  (10765432100123456789).toString() in a browser JavaScript console, the result will be "10765432100123458000" - the 64-bit integer loses accuracy as a result of the translation (this is sometimes called “munging” - a destructive change to a piece of data).

In Twitter APIs up to version 1.1, you should always use the string representation of the number to avoid losing accuracy.

In newer versions of the API, all large integer values are represented as strings by default.

2. uid-generator of baidu
https://github.com/baidu/uid-generator

https://github.com/beyondfengyu/SnowFlake/blob/master/SnowFlake.java

*/ twitter snowflake -- java
*/
public class SnowFlake {
  private final static long START_STMP = 1480166465631L; //41 bits //2016-11-26 21:21:05
//1623990673000L; //2021-06-18 12:31:13

  private final static long SEQUENCE_BIT = 12; // 12 bits : 4096

  private final static long MACHINE_BIT = 5;     // 5 bits : 32
  private final static long DATACENTER_BIT = 5;  // 5 bits
private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);//32
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);//32
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
//1bit+41bits+5bits+5bits+12bits
private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;

private long datacenterId;
private long machineId; 
private long sequence = 0L;
private long lastStmp = -1L;

public SnowFlake(long datacenterId, long machineId) {
if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
}
   if (machineId > MAX_MACHINE_NUM || machineId < 0) {
      throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or  less than 0");
  }
    this.datacenterId = datacenterId;
    this.machineId = machineId;
  }
public synchronized long nextId() {
   long currStmp = getNewstmp();
   if (currStmp < lastStmp) {
     throw new RuntimeException("Clock moved backwards. Refusing to generate id");
   }
    if (currStmp == lastStmp) {
      sequence = (sequence + 1) & MAX_SEQUENCE;
      if (sequence == 0L) {
          currStmp = getNextMill();
      }
    } else {
       sequence = 0L;
    }
    lastStmp = currStmp;
    return (currStmp - START_STMP) << TIMESTMP_LEFT
      | datacenterId << DATACENTER_LEFT
      | machineId << MACHINE_LEFT
      | sequence;
  }
private long getNextMill() {
   long mill = getNewstmp();
   while (mill <= lastStmp) {
      mill = getNewstmp();
   }
   return mill;
}

private long getNewstmp() {
   return System.currentTimeMillis();
}
public static void main(String[] args) {
   SnowFlake snowFlake = new SnowFlake(2, 3);
   for (int i = 0; i < (1 << 12); i++) {
   System.out.println(snowFlake.nextId());
}
}
}

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE