shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

DefaultKeyGenerator 生成器生成的主键不能产生奇数

Open cwpgh opened this issue 8 years ago • 2 comments

主键的最后12位是序列号,该序列号只有在同一秒内才会自增

主键最后的一个二进制位会是0 , 这样就不能产生奇数了

代码如下 sequence 在非同一秒内都是0 public synchronized Number generateKey() { long currentMillis = timeService.getCurrentMillis(); Preconditions.checkState(lastTime <= currentMillis, "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastTime, currentMillis); if (lastTime == currentMillis) { if (0L == (sequence = ++sequence & SEQUENCE_MASK)) { currentMillis = waitUntilNextTime(currentMillis); } } else { sequence = 0; } lastTime = currentMillis; if (log.isDebugEnabled()) { log.debug("{}-{}-{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(lastTime)), workerId, sequence); } return ((currentMillis - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (workerId << WORKER_ID_LEFT_SHIFT_BITS) | sequence; }

cwpgh avatar Jan 22 '18 07:01 cwpgh

请先仔细阅读文档,如无充足时间,请至少阅读下faq

terrymanu avatar Jan 22 '18 13:01 terrymanu

嗯,确实没看faq,我是在测试过程中,发现数据都到一个分片去了

cwpgh avatar Jan 23 '18 01:01 cwpgh