Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SnowFlake algorithm. #1065

Merged

Conversation

Allen-dududu
Copy link
Contributor

The idea of Optimize SnowFlate algorithm comes from Seata which is an open-source distributed transaction solution of Ali. It also uses snowflake to generate Id, but they upgraded the algorithm to avoid time step back exception.

FYI: https://seata.io/zh-cn/blog/seata-analysis-UUID-generator.html

@Allen-dududu
Copy link
Contributor Author

Unbind time from the operating system timestamp. The generator only gets the current system timestamp at initialization as the initial timestamp, but is no longer in sync with the system timestamp. Its subsequent increment is driven only by the increment of the serial number. For example, if the current sequence number is 4095 and the next request comes in, the sequence number +1 overflows the 12-bit space, the sequence number is reset to zero, and the overflow carry is added to the timestamp, making the timestamp +1

@yang-xiaodong yang-xiaodong self-requested a review January 10, 2022 02:23
@yang-xiaodong
Copy link
Member

Have you tested this algorithm?

Why is the value of recreated instance always begin with 4989988387126509569?

@Allen-dududu
Copy link
Contributor Author

Sorry for that. I forgot to init the first timestamp and sequence, Fixed now.

src/DotNetCore.CAP/Internal/SnowflakeId.cs Outdated Show resolved Hide resolved
src/DotNetCore.CAP/Internal/SnowflakeId.cs Outdated Show resolved Hide resolved
src/DotNetCore.CAP/Internal/SnowflakeId.cs Outdated Show resolved Hide resolved
src/DotNetCore.CAP/Internal/SnowflakeId.cs Outdated Show resolved Hide resolved
Copy link
Member

@yang-xiaodong yang-xiaodong left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks

@yang-xiaodong yang-xiaodong added this to the 6.1.0 milestone Jan 10, 2022
@yang-xiaodong yang-xiaodong merged commit e28fe60 into dotnetcore:master Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants