QNJR-GROUP / EasyTransaction

A distribute transaction solution(分布式事务) unified the usage of TCC , SAGA ,FMT (seata/fescar AutoCompensation), reliable message, compensate and so on;
Apache License 2.0
2.36k stars 809 forks source link

DatabaseSnowFlakeIdGenerator has something wrong? #141

Closed hiiloveyou closed 4 years ago

hiiloveyou commented 4 years ago

DatabaseSnowFlakeIdGenerator,the implements of the TrxIdGenerator class , when constructor the new DatabaseSnowFlakeIdGenerator, you did'nt assign the generated hostSeq to the local variable hostSeq , so the local variable hostSeq is always be 0; In multiple-concurrent instance of application,the DatabaseSnowFlakeIdGenerator may generate conflict id; what's more,the algorithe of hostSeq may be hostSeq % ((int) Math.pow(2, SnowFlake.MACHINE_BIT)); instead of hostSeq % (2^SnowFlake.MACHINE_BIT);

the pre code

public class DatabaseSnowFlakeIdGenerator implements TrxIdGenerator {
    private long hostSeq;
    private ConcurrentHashMap<String, SnowFlake> mapSnowFlakers = new ConcurrentHashMap<>();
    public DatabaseSnowFlakeIdGenerator(long hostSeq) {
        hostSeq = hostSeq % (2^SnowFlake.MACHINE_BIT);
    }
    @Override
    public long getCurrentTrxId(String busCode) {
        SnowFlake s = mapSnowFlakers.computeIfAbsent(busCode, k->new SnowFlake(hostSeq));
        return s.nextId();
    }
}

my code

public class ETSnowFlakeIdGenerator implements TrxIdGenerator {

    private long hostSeq;
    private ConcurrentHashMap<String, SnowFlake> mapSnowFlakers = new ConcurrentHashMap<>();
    public ETSnowFlakeIdGenerator(long hostSeq) {
        this.hostSeq = hostSeq % ((int) Math.pow(2, SnowFlake.MACHINE_BIT));
    }
    @Override
    public long getCurrentTrxId(String busCode) {
        SnowFlake s = mapSnowFlakers.computeIfAbsent(busCode, k -> new SnowFlake(hostSeq));
        return s.nextId();
    }
}
skyesx commented 4 years ago

Yes,your are right.Thanks for this issue.

Can you submit a PR for this issue? if you are too busy too do it ,I will fix it later, thanks.

skyesx commented 4 years ago

fix in 1.4.3