Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random UUID non blocking

By using Blockhound io.projectreactor.tools blockhound-junit-platform i found out that UUID.randomUUID is a blocking call which is a problem for us since we are using Spring boot Webflux version 2.2.2.RELEASE

Is there any other way to get a random uuid in a non blocking way or is there any other java library recommended for non blocking generation of randomized strings.

Stack trace from blockhound:

java.lang.Error: Blocking call! java.io.FileInputStream#readBytes
at reactor.blockhound.BlockHound$Builder.lambda$new$0(BlockHound.java:196) ~[blockhound-1.0.1.RELEASE.jar:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain][ExceptionHandlingWebHandler]
Stack trace:
    at reactor.blockhound.BlockHound$Builder.lambda$new$0(BlockHound.java:196) ~[blockhound-1.0.1.RELEASE.jar:na]
    at reactor.blockhound.BlockHound$Builder.lambda$install$6(BlockHound.java:318) ~[blockhound-1.0.1.RELEASE.jar:na]
    at reactor.blockhound.BlockHoundRuntime.checkBlocking(BlockHoundRuntime.java:46) ~[na:na]
    at java.base/java.io.FileInputStream.readBytes(FileInputStream.java) ~[na:na]
    at java.base/java.io.FileInputStream.read(FileInputStream.java:279) ~[na:na]
    at java.base/java.io.FilterInputStream.read(FilterInputStream.java:133) ~[na:na]
    at java.base/sun.security.provider.NativePRNG$RandomIO.readFully(NativePRNG.java:424) ~[na:na]
    at java.base/sun.security.provider.NativePRNG$RandomIO.ensureBufferValid(NativePRNG.java:526) ~[na:na]
    at java.base/sun.security.provider.NativePRNG$RandomIO.implNextBytes(NativePRNG.java:545) ~[na:na]
    at java.base/sun.security.provider.NativePRNG.engineNextBytes(NativePRNG.java:220) ~[na:na]
    at java.base/java.security.SecureRandom.nextBytes(SecureRandom.java:741) ~[na:na]
    at java.base/java.util.UUID.randomUUID(UUID.java:150) ~[na:na]
like image 823
linkebon Avatar asked Feb 06 '20 13:02

linkebon


People also ask

How do you generate a random UUID?

In Java, the randomUUID() static method is used to generate a random UUID. The method internally uses SecureRandom class, which provides a cryptographically strong random number generator. Every UUID is associated with a version number. The version number describes how the UUID was generated.

Is generating UUID slow?

Java on Linux uses the /dev/urandom generator, which can be rather slow if there is not much activity (e.g. user input or network traffic) on that system. The claim was 350ms to generate a single UUID.

Is UUID randomUUID cryptographically secure?

As it happens, Java's UUID. randomUUID() method does use a cryptographic RNG, so it should be safe in that regard.


1 Answers

This is a list of three examples on how to use uuid-creator to reduce thread contention on Linux.

An additional example uses ulid-creator to generate Monotonic ULIDs and then turn them into RFC-4122 UUID (standard). Monotonic ULID generation is very fast.

The benchmark code is available on a GitHub Gist.

Example 1: using UuidCreator and SHA1PRNG

How to setup the SHA1PRNG algorithm to be used by UuidCreator:

# Append to /etc/environment or ~/.profile
# Use the the algorithm SHA1PRNG for SecureRandom
export UUIDCREATOR_SECURERANDOM="SHA1PRNG"
// generate a random-based UUID
UUID uuid = UuidCreator.getRandomBased();

UuidCreator uses a fixed pool of SecureRandom. The variable UUIDCREATOR_SECURERANDOM tells UuidCreator to use another SecureRandom algorithm, instead of NativePRNG on Linux. UuidCreator along with SHA1PRNG algorithm generates UUIDs with less thread contention (less-blocking) than UUID.randomUUID().

See the benchmark using 4 threads:

----------------------------------------------------------------------
Benchmark                      Mode  Cnt      Score     Error   Units
----------------------------------------------------------------------
UUID.randomUUID()             thrpt    5   1423,060 ±  30,125  ops/ms
UuidCreator.getRandomBased()  thrpt    5  10616,016 ± 281,486  ops/ms
----------------------------------------------------------------------

Example 2: using ThreadLocalRandom

How to implement a UUID generator using ThreadLocalRandom:

public class UuidGenerator {

    private static final RandomBasedFactory UUID_FACTORY;

    static {
        UUID_FACTORY = new RandomBasedFactory((int length) -> {
            final byte[] bytes = new byte[length];
            ThreadLocalRandom.current().nextBytes(bytes);
            return bytes;
        });
    }

    public static UUID generate() {
        return UUID_FACTORY.create();
    }
}

ThreadLocalRandom is a fast (and unsafe) random generator without thread contention (non-blocking).

See the benchmark using 4 threads:

----------------------------------------------------------------------
Benchmark                     Mode  Cnt      Score      Error   Units
----------------------------------------------------------------------
UUID.randomUUID()            thrpt    5   1423,060 ±   30,125  ops/ms
UuidGenerator.generate()     thrpt    5  85390,979 ± 1564,589  ops/ms
----------------------------------------------------------------------

Example 3: using RandomBasedFactory[] and SHA1PRNG

How to implement a UUID generator using ana array of RandomBasedFactory and SHA1PRNG algorithm:

    public static class UuidGenerator {

        private static final int SIZE = 8; // you choose
        private static final RandomBasedFactory[] FACTORIES;

        static {
            FACTORIES = new RandomBasedFactory[SIZE];
            try {
                for (int i = 0; i < FACTORIES.length; i++) {
                    // SHA1PRNG or DRBG can be used to reduce thread contention.
                    SecureRandom argument = SecureRandom.getInstance("SHA1PRNG");
                    FACTORIES[i] = new RandomBasedFactory(argument);
                }
            } catch (NoSuchAlgorithmException e) {
                // oops!
            }
        }

        public static UUID generate() {
            // calculate the factory index given the current thread ID
            final int index = (int) Thread.currentThread().getId() % SIZE;
            return FACTORIES[index].create();
        }
    }

An array of RandomBasedFactory with and SHA1PRNG algorithm can generate UUIDs with less thread contention (less-blocking).

See the benchmark using 4 threads:

----------------------------------------------------------------------
Benchmark                     Mode  Cnt      Score      Error   Units
----------------------------------------------------------------------
UUID.randomUUID()            thrpt    5   1423,060 ±   30,125  ops/ms
UuidGenerator.generate()     thrpt    5  10048,747 ±  195,209  ops/ms
----------------------------------------------------------------------

Example 4: using UlidCreator and Monotonic ULIDs

How to use ulid-creator to generate RFC-4122 UUIDs from Monotonic ULIDs:

// create a Monotonic ULID and convert it to RFC-4122 UUID v4
UUID uuid = UlidCreator.getMonotonicUlid().toRfc4122().toUuid();

See the benchmark using 4 threads:

-----------------------------------------------------------------------
Benchmark                       Mode  Cnt     Score      Error   Units
-----------------------------------------------------------------------
UUID.randomUUID()              thrpt    5  1423,060 ±   30,125  ops/ms
UlidCreator.getMonotonicUlid() thrpt    5  7391,917 ±  871,799  ops/ms
-----------------------------------------------------------------------

EDIT: added the 4th example that uses Monotonic ULID.

like image 174
fabiolimace Avatar answered Sep 17 '22 14:09

fabiolimace