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

Will throw an exception if generating more than 65,535 flakes in a millisecond #2

Closed
danielcompton opened this issue Aug 20, 2014 · 5 comments · Fixed by #3
Closed

Comments

@danielcompton
Copy link
Contributor

While it is very unlikely to happen, if 65,535 flakes are generated in a millisecond, then an exception will be thrown. Would it be better to check this and wait until the start of the next millisecond to generate the next flake?

I'm not 100% sure this is even an issue, if the caller is aware that this may throw an exception then they could prepare for it and catch it to retry.

(PartialFlake. (System/currentTimeMillis) (p/inc (Short/MAX_VALUE)))
IllegalArgumentException Value out of range for short: 32768  clojure.lang.RT.shortCast (RT.java:1040)
@xpe
Copy link

xpe commented Aug 21, 2014

@danielcompton I skimmed the code, but I'm not sure which exception you are talking about. Would you mind pointing it out?

My thoughts on your note: Your suggestion sounds reasonable, but the current implementation sounds reasonable too. My guess is that if "overflow" happens, it probably is truly exceptional -- and an application would want to know about it and fail. The alternative is wait a millisecond -- but if you are making a system writing that many ID's, milliseconds may matter -- and you might end up with a backlog. Better to fail fast and let the application figure out what to do.

P.S. Just as a reference, from http://boundary.com/blog/2012/01/12/flake-a-decentralized-k-ordered-unique-id-generator-in-erlang/

This means that each server can produce 2^16 – 1 unique ids per millisecond without overflow.

@danielcompton
Copy link
Contributor Author

@xpe that exception doesn't happen in the code, I made it happen by simulating what would happen if we were to inc a flake which had Short/MAX_VALUE as it's sequence value.

I agree 65,000 flakes/millisecond is a lot, it's equivalent to 65 million flakes/second.

@maxcountryman
Copy link
Owner

@danielcompton my feeling is, as @xpe mentioned, it's better to allow the application to decide how to handle this situation.

@danielcompton
Copy link
Contributor Author

@maxcountryman is it worth documenting it as an extremely unlikely edge case then?

@maxcountryman
Copy link
Owner

Sure, more documentation couldn't hurt.

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 a pull request may close this issue.

3 participants