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();
}
}
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 ofhostSeq % (2^SnowFlake.MACHINE_BIT);
the pre code
my code