Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public long millis() {
return lastTimestamp.toEpochMilli();
}

/**
* Sub-miliseconds part of timestamp (micro- and nanoseconds) mapped to 12 bit integral value.
* Calculated as nanos / 1000000 * 4096
*
* @param timestamp
* @return
*/
private static long nanos(Instant timestamp) {
return (long) ((timestamp.getNano() % 1_000_000L) * 0.004096);
}
Expand All @@ -64,9 +71,9 @@ public State getNextState() {
return new State( now, randomSequence() );
}
else {
final long nextSequence = lastSequence + Holder.numberGenerator.nextLong( 0xFFFF_FFFFL );
return nextSequence > MAX_RANDOM_SEQUENCE
? new State( lastTimestamp.plusNanos( 250 ), randomSequence() )
final long nextSequence = randomSequence();
return lastSequence >= nextSequence
? new State( lastTimestamp.plusNanos( 250 ), nextSequence )
Comment on lines +74 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the nextSequence must be greater than the lastSequence to guarantee monotonicity? And the addition of 250 is because that is the lowest sensible value to add due to using only the upper 12 bit of the nanos? Since the highest potential nano value 999_999 has 20 bits which, I would assume that the smallest sensible value to add is 2^8 i.e. 256. Can you please explain the reasoning in a code comment?

: new State( lastTimestamp, nextSequence );
}
}
Expand All @@ -77,7 +84,7 @@ private boolean lastTimestampEarlierThan(Instant now) {
}

private static long randomSequence() {
return Holder.numberGenerator.nextLong( MAX_RANDOM_SEQUENCE );
return Holder.numberGenerator.nextLong( MAX_RANDOM_SEQUENCE + 1 );
}
}

Expand Down Expand Up @@ -118,7 +125,7 @@ public UUID generateUuid(final SharedSessionContractImplementor session) {
| state.nanos() & 0xFFFL,
// LSB bits 0-1 - variant = 4
0x8000_0000_0000_0000L
// LSB bits 2-15 - pseudorandom counter
// LSB bits 2-63 - pseudorandom counter
| state.lastSequence
);
}
Expand Down