-
Notifications
You must be signed in to change notification settings - Fork 2
DistributedId 分布式Id
RockyLOMO edited this page Apr 14, 2022
·
4 revisions
处理了时钟回拨的情况
@Test
public void snowflake() {
Set<Long> set = new HashSet<>();
int len = 1 << 12;
System.out.println(len);
Snowflake snowflake = Snowflake.DEFAULT;
for (int i = 0; i < len; i++) {
Tasks.run(() -> {
assert set.add(snowflake.nextId());
});
}
}
基于这篇文章思路改进。
When using murmurHash3 128-bits, the x86 and x64 versions do not produce the same values. 故采用CRC64.
CRC64碰撞率低,2亿数据量没有碰撞,就算有碰撞那概率比写bug的概率还低~
@Test
public void codec() {
for (int i = 0; i < 10; i++) {
long ts = System.nanoTime();
assert App.orderedUUID(ts, i).equals(App.orderedUUID(ts, i));
}
EntityDatabase db = EntityDatabase.DEFAULT;
db.createMapping(CollisionEntity.class);
db.dropMapping(CollisionEntity.class);
db.createMapping(CollisionEntity.class);
int c = 200000000;
AtomicInteger collision = new AtomicInteger();
invoke("codec", i -> {
long id = CrcModel.CRC64_ECMA_182.getCRC((UUID.randomUUID().toString() + i).getBytes(StandardCharsets.UTF_8)).getCrc();
CollisionEntity po = db.findById(CollisionEntity.class, id);
if (po != null) {
log.warn("collision: {}", collision.incrementAndGet());
return;
}
po = new CollisionEntity();
po.setId(id);
db.save(po, true);
}, c);
assert db.count(new EntityQueryLambda<>(CollisionEntity.class)) == c;
}