diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/AbstractSnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/AbstractSnowflakeId.java index 4879d95f2c..74d76ae8bf 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/AbstractSnowflakeId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/AbstractSnowflakeId.java @@ -24,16 +24,16 @@ * @author ahoo wang */ public abstract class AbstractSnowflakeId implements SnowflakeId { - + protected final long epoch; protected final int timestampBit; protected final int machineBit; protected final int sequenceBit; - + protected final long maxTimestamp; protected final long maxSequence; - protected final int maxMachine; - + protected final int maxMachineId; + protected final long machineLeft; protected final long timestampLeft; /** @@ -43,7 +43,7 @@ public abstract class AbstractSnowflakeId implements SnowflakeId { private final long sequenceResetThreshold; protected long sequence = 0L; protected long lastTimestamp = -1L; - + public AbstractSnowflakeId(long epoch, int timestampBit, int machineBit, @@ -59,16 +59,16 @@ public AbstractSnowflakeId(long epoch, this.sequenceBit = sequenceBit; this.maxTimestamp = ~(-1L << timestampBit); this.maxSequence = ~(-1L << sequenceBit); - this.maxMachine = ~(-1 << machineBit); + this.maxMachineId = ~(-1 << machineBit); this.machineLeft = sequenceBit; this.timestampLeft = this.machineLeft + machineBit; - if (machineId > this.maxMachine || machineId < 0) { - throw new IllegalArgumentException(Strings.lenientFormat("machineId can't be greater than maxMachine[%s] or less than 0 .", maxMachine)); + if (machineId > this.maxMachineId || machineId < 0) { + throw new IllegalArgumentException(Strings.lenientFormat("machineId[%s] can't be greater than maxMachineId[%s] or less than 0 .", machineId, maxMachineId)); } this.machineId = machineId; this.sequenceResetThreshold = sequenceResetThreshold; } - + protected long nextTime() { long time = getCurrentTime(); while (time <= lastTimestamp) { @@ -76,34 +76,34 @@ protected long nextTime() { } return time; } - + /** * get current timestamp. * * @return current timestamp */ protected abstract long getCurrentTime(); - + @Override public synchronized long generate() { long currentTimestamp = getCurrentTime(); if (currentTimestamp < lastTimestamp) { throw new ClockBackwardsException(lastTimestamp, currentTimestamp); } - + //region Reset sequence based on sequence reset threshold,Optimize the problem of uneven sharding. - + if (currentTimestamp > lastTimestamp - && sequence >= sequenceResetThreshold) { + && sequence >= sequenceResetThreshold) { sequence = 0L; } - + sequence = (sequence + 1) & maxSequence; - + if (sequence == 0L) { currentTimestamp = nextTime(); } - + //endregion lastTimestamp = currentTimestamp; long diffTimestamp = (currentTimestamp - epoch); @@ -111,53 +111,53 @@ public synchronized long generate() { throw new TimestampOverflowException(epoch, diffTimestamp, maxTimestamp); } return diffTimestamp << timestampLeft - | machineId << machineLeft - | sequence; + | machineId << machineLeft + | sequence; } - + @Override public long getEpoch() { return epoch; } - + @Override public int getTimestampBit() { return timestampBit; } - + @Override public int getMachineBit() { return machineBit; } - + @Override public int getSequenceBit() { return sequenceBit; } - + @Override public long getMaxTimestamp() { return maxTimestamp; } - + @Override - public int getMaxMachine() { - return maxMachine; + public int getMaxMachineId() { + return maxMachineId; } - + @Override public long getMaxSequence() { return maxSequence; } - + @Override public long getLastTimestamp() { return lastTimestamp; } - + @Override public int getMachineId() { return (int) machineId; } - + } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java index b028615bf7..497275f896 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/ClockSyncSnowflakeId.java @@ -97,8 +97,8 @@ public long getMaxTimestamp() { } @Override - public int getMaxMachine() { - return actual.getMaxMachine(); + public int getMaxMachineId() { + return actual.getMaxMachineId(); } @Override diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java index fa1e482c3a..ddecd52e44 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/SnowflakeId.java @@ -47,7 +47,7 @@ default boolean isSafeJavascript() { long getMaxTimestamp(); - int getMaxMachine(); + int getMaxMachineId(); long getMaxSequence(); diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java index 78c276d56f..ef2fc765f1 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/StringSnowflakeId.java @@ -56,8 +56,8 @@ public long getMaxTimestamp() { } @Override - public int getMaxMachine() { - return snowflakeId.getMaxMachine(); + public int getMaxMachineId() { + return snowflakeId.getMaxMachineId(); } @Override diff --git a/cosid-core/src/test/java/me/ahoo/cosid/machine/ClockSyncSnowflakeIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/machine/ClockSyncSnowflakeIdTest.java index 4113359121..b59c86cd74 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/machine/ClockSyncSnowflakeIdTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/machine/ClockSyncSnowflakeIdTest.java @@ -67,8 +67,8 @@ void getMaxTimestamp() { } @Test - void getMaxMachine() { - assertThat(clockSyncSnowflakeId.getMaxMachine(), equalTo(1023)); + void getMaxMachineId() { + assertThat(clockSyncSnowflakeId.getMaxMachineId(), equalTo(1023)); } @Test diff --git a/cosid-core/src/test/java/me/ahoo/cosid/snowflake/StringSnowflakeIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/snowflake/StringSnowflakeIdTest.java index 8d68382e8d..f59c808bfa 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/snowflake/StringSnowflakeIdTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/snowflake/StringSnowflakeIdTest.java @@ -47,8 +47,8 @@ void getMaxTimestamp() { } @Test - void getMaxMachine() { - Assertions.assertEquals(delegate.getMaxMachine(), snowflakeId.getMaxMachine()); + void getMaxMachineId() { + Assertions.assertEquals(delegate.getMaxMachineId(), snowflakeId.getMaxMachineId()); } @Test