Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

UUID.generate collisions when called too often #21

Open
chuckremes opened this issue Jan 7, 2012 · 5 comments
Open

UUID.generate collisions when called too often #21

chuckremes opened this issue Jan 7, 2012 · 5 comments

Comments

@chuckremes
Copy link

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.

@assaf
Copy link
Owner

assaf commented Jan 7, 2012

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.

@chuckremes
Copy link
Author

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.synchronize block. If that block was not synchronized, you'd get duplicates much more frequently, see the all issue with drift.


Reply to this email directly or view it on GitHub:
#21 (comment)

@assaf
Copy link
Owner

assaf commented Jan 7, 2012

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.

@chuckremes
Copy link
Author

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.generate or calling that method on the same UUID object.


Reply to this email directly or view it on GitHub:
#21 (comment)

@assaf
Copy link
Owner

assaf commented Jan 8, 2012

My best guess is that the mutex is not working under that version of JRuby.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants