public synchronized long nextId() {
long timestamp = this.timeGen();
if (this.lastTimestamp == timestamp) {
this.sequence = this.sequence + 1 & this.sequenceMask;
if (this.sequence == 0) {
timestamp = this.tilNextMillis(this.lastTimestamp);
}
}
else {
this.sequence = 0;
}
if (timestamp < this.lastTimestamp) {
throw new InvalidSystemClock(String.format(
"Clock moved backwards. Refusing to generate id for %d milliseconds", this.lastTimestamp - timestamp));
}
this.lastTimestamp = timestamp;
//其中的workerId为broker的brokerId,这样保证在客户端消费者重复消息过滤时,不会因为两个broker有同样的topic并且生成了同样的其他数时造成消息错误过滤
return timestamp - twepoch << this.timestampLeftShift | this.workerId << this.workerIdShift | this.sequence;
}
metaq的msgid生成规则
最新推荐文章于 2024-08-26 20:55:02 发布