Skip to content

Commit

Permalink
fix #225 ID's sequence can not reset to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
gaohongtao committed Feb 15, 2017
1 parent 036df56 commit b17c85e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
1 change: 1 addition & 0 deletions sharding-jdbc-doc/content/03-community/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ next = "/03-community/directory-structure"
1. [ISSUE #212](https://github.com/dangdangdotcom/sharding-jdbc/issues/212) 对去缺少数据源规则给出更有意义的提示
1. [ISSUE #214](https://github.com/dangdangdotcom/sharding-jdbc/issues/214) where中 table_name.column_name in (?,?)无法解析表达式
1. [ISSUE #180](https://github.com/dangdangdotcom/sharding-jdbc/issues/180) 批量执行Update返回值不准确
1. [ISSUE #225](https://github.com/dangdangdotcom/sharding-jdbc/issues/225) 自动生成Id最后一位不归零

## 1.4.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,6 @@ public static void setWorkerId(final Long workerId) {
CommonSelfIdGenerator.workerId = workerId;
}

/**
* 获取工作Id的二进制长度.
*
* @return 工作Id的二进制长度
*/
public static long getWorkerIdLength() {
return WORKER_ID_BITS;
}

/**
* 生成Id.
*
Expand All @@ -131,7 +122,7 @@ public synchronized Number generateId() {
long time = clock.millis();
Preconditions.checkState(lastTime <= time, "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastTime, time);
if (lastTime == time) {
if (0L == (++sequence & SEQUENCE_MASK)) {
if (0L == (sequence = ++sequence & SEQUENCE_MASK)) {
time = waitUntilNextTime(time);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,16 @@ public Long call() throws Exception {
public void testMaxSequence() throws Exception {
assertThat(maxId((1 << 12) - 1), is((1L << 12L) - 2));
assertThat(maxId(1 << 12), is((1L << 12L) - 1));
assertThat(maxId((1 << 12) + 1), is((1L << 12L) - 1));
assertThat(maxId(1 << 13), is((1L << 12L) - 1));
assertThat(maxId((1 << 13) + 1), is((1L << 12L) - 1));
assertThat(maxId((1 << 12) + 1), is(1L << 22));
}

private long maxId(final int maxSequence) {
CommonSelfIdGenerator idGenerator = new CommonSelfIdGenerator();
CommonSelfIdGenerator.setClock(new FixClock(maxSequence));
CommonSelfIdGenerator.setClock(new FixClock(1 << 13));
long id = 0;
long preId = 0;
while (id < (1L << 12)) {
preId = id;
for (int i = 0; i < maxSequence; i++) {
id = (Long) idGenerator.generateId();
}
return preId;
return id;
}
}

0 comments on commit b17c85e

Please sign in to comment.