-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
nsqd: switch to monotonic clocks for ID generation #1249
Conversation
1. nsqd/guid.go: uses pseudo-random generate method. will not fail to wait-retry loop when time gone backwards 2. time diff: use time.Time for diff. with monotonic clock to measure elapsed time [1] 3. pqueue(s): use time.Time for heap / priority compare 4. fix tests. See also: [1] golang Monotonic Clocks: https://golang.org/pkg/time
manually test:open a server
pub
sub
change time
expected resultchange server time will not break the nsqd data stream. |
previously discussed in #1124 Looks like good stuff. I'm not sure about the random ID algo ... but it might be time to break previous behavior and make the switch. I'll properly review eventually ... |
UUID generate.A similar algorithm is UUIDv4 (rfc4122) generate alg. see this for an implement. We may read rand.Reader for a better seed either. But i think nanosecond mixs The nodeIDAfter a futher dig in this day, The The This may be better:
UUID generate improvement with such a small nodeIDThe code uses nanosecond for rand seed, It is unlikely to have any collision in production. However, here's some improvement to discuss.
|
Did a little digging on this. The historical requirements here are two-fold:
Regarding (1), channels re-use the ID generated when the message is published to a topic. IDs are persisted via the diskqueue, which results in (2). The current algorithm satisfies (2) by assuming that time only ever moves forward, and generates IDs that are roughly time sorted. IMO, the "right" answer here is an actual GUID (across all The question then becomes: is breaking backwards compatibility to be able to handle time going backwards actually worth it? The argument in favor is that that you already can't really depend on these message IDs outside of NSQ, given the scarcely documented and limited use of The argument against seems to be that we're worried that newly generated message IDs may conflict with diskqueue persisted messages upon upgrade? Is that all? |
If we do not need ID to be time sorted, use rand.Reader to generate it could certainly make the ID globally unique (or at least make the probability of conflict to be negligible. ) (edited) I've found some information about the probabilities of collision randomly generated values: In our case, a collision of id is just like a collision of hash. If a nsq cluster have about 2 million message storage, the probabilities of new generate id collision will be 1 in 10million. refer the article for detail, and there's a picture of collision probabilities here. |
Isn't that a good solution? |
See also:
[1] golang Monotonic Clocks: https://golang.org/pkg/time