Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 7 uuid bug?

Tags:

java

uuid

scala

I believe I may have found a UUID multi-threading bug in Oracle Java7 Update 5 and I was wondering if anyone could confirm with an independent example. Basically what happens is if I call UUID.Random() concurrently (I'm using 64 threads), I get a collision quickly most of the time).

However, If the UUID generator is warmed up by running a test beforehand that just tosses a bunch of UUIDs it seems to work fine. Here's a gist in scala:

https://gist.github.com/3318973

Important to note that I'm inserting into a mutable Set that I'm synchronizing from 64 different threads. The main thread blocks waiting for the workers to finish and then checks that the set size is what it should be. Collisions are printed as they occur.

thanks! Adam

like image 575
jadamcrain Avatar asked Aug 10 '12 23:08

jadamcrain


People also ask

Is UUID immutable java?

Class UUID. A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers.

Is UUID length fixed?

UUIDs are fixed length. UUIDs are 128-bits in binary. (32 hex digits x 4 bits per hex digit = 128-bits).

Are UUID collisions possible?

A collision is possible but the total number of unique keys generated is so large that the possibility of a collision is almost zero. As per Wikipedia, the number of UUIDs generated to have atleast 1 collision is 2.71 quintillion. This is equivalent to generating around 1 billion UUIDs per second for about 85 years.

What is UUID randomUUID()?

The randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.


2 Answers

It could be this:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7071826

It's fixed in 7u6, which should be out soon.

like image 187
Stuart Marks Avatar answered Oct 22 '22 20:10

Stuart Marks


I did a quick check with Java 1.7.0_05 (no scala or scalatest involved) and could not reproduce any UUID collisions whatsoever. My basic setup is the same, with 128 threads throwing 10000 uids at a synchronized set each. This is very likely an issue with your testcode, which i can't doublecheck on this machine atm.

like image 2
sarcan Avatar answered Oct 22 '22 18:10

sarcan