uuid
uuid copied to clipboard
UUID.generate collisions when called too often
Under JRuby I am calling UUID.generate quite often. I see a case in my logs where two successive calls returns the same UUID. Looking at the code, I see this comment:
# The clock must be monotonically increasing. The clock resolution is at
# best 100 ns (UUID spec), but practically may be lower (on my setup,
# around 1ms). If this method is called too fast, we don't have a
# monotonically increasing clock, so the solution is to just wait.
#
# It is possible for the clock to be adjusted backwards, in which case we
# would end up blocking for a long time. When backward clock is detected,
# we prevent duplicates by asking for a new sequence number and continue
# with the new clock.
Judging by the comment, I assume that it is possible that a backward clock could be detected twice in a row, so asking for a new sequence number in that situation will still generate the same UUID when UUID.state_file=nil. If this assumption is correct, would it be reasonable to randomize the next sequence number?
e.g.
if self.class.state_file
....
else
@sequence += rand(999)
end
...
If that is acceptable, I could provide a patch.
Backward clock happens when the system clock is adjusted backwards which is not likely to happen often enough for you to notice. That detection is done in mutex.synchronize block. If that block was not synchronized, you'd get duplicates much more frequently, see the all issue with drift.
So it sounds like my guess for why the dupes occur is in error. However, it does dupe fairly often even though I use this synchronized version everywhere. Aside from "don't call it so often" is there a fix or should I work around it?
Sent from my iPhone
On Jan 7, 2012, at 3:32 PM, Assaf [email protected] wrote:
Backward clock happens when the system clock is adjusted backwards which is not likely to happen often enough for you to notice. That detection is done in
mutex.synchronizeblock. If that block was not synchronized, you'd get duplicates much more frequently, see the all issue withdrift.
Reply to this email directly or view it on GitHub: https://github.com/assaf/uuid/issues/21#issuecomment-3398390
Frequency is not the issue, it either works or doesn't. I assume you're calling UUID.generate or calling that method on the same UUID object.
I am calling UUID.generate (based on our last conversation). It isn't working because I sometimes get duplicates. What do you suggest?
cr
On Jan 7, 2012, at 4:18 PM, Assaf Arkin wrote:
Frequency is not the issue, it either works or doesn't. I assume you're calling
UUID.generateor calling that method on the same UUID object.
Reply to this email directly or view it on GitHub: https://github.com/assaf/uuid/issues/21#issuecomment-3398689
My best guess is that the mutex is not working under that version of JRuby.