Commit f5e4c461 authored by Sergei Golubchik's avatar Sergei Golubchik

post-merge changes

* remove duplicate test file
* move all uuidv7 tests into plugin/type_uuid/mysql-test/type_uuid/
* remove mysys/ changes
* auto my_random_bytes() fallback - removes duplicate code from uuid,
  and fixes all other users of my_random_bytes() that don't check
  the return value (because, perhaps, they don't need crypto-strong
  random bytes)
* End of 11.6 -> 11.7 in tests
* clarify the warning text
* UUID_VERSION_MASK()/UUID_VARIANT_MASK() must not depend on the version
* allow 4x more monotonic uuidv7 per millisecond - instead of stretching
  1000 microseconds over 12 bits, let's use extra 2 bits as a counter
parent 89e0944d
......@@ -471,8 +471,7 @@ PSI_mutex_key key_BITMAP_mutex, key_IO_CACHE_append_buffer_lock,
key_THR_LOCK_lock, key_THR_LOCK_malloc,
key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net,
key_THR_LOCK_open, key_THR_LOCK_threads,
key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_LOCK_uuid_generator,
key_LOCK_uuid_v7_generator;
key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_LOCK_uuid_generator;
static PSI_mutex_info all_mysys_mutexes[]=
{
......@@ -499,8 +498,7 @@ static PSI_mutex_info all_mysys_mutexes[]=
{ &key_THR_LOCK_threads, "THR_LOCK_threads", PSI_FLAG_GLOBAL},
{ &key_TMPDIR_mutex, "TMPDIR_mutex", PSI_FLAG_GLOBAL},
{ &key_THR_LOCK_myisam_mmap, "THR_LOCK_myisam_mmap", PSI_FLAG_GLOBAL},
{ &key_LOCK_uuid_generator, "LOCK_uuid_generator", PSI_FLAG_GLOBAL },
{ &key_LOCK_uuid_v7_generator, "LOCK_uuid_v7_generator", PSI_FLAG_GLOBAL }
{ &key_LOCK_uuid_generator, "LOCK_uuid_generator", PSI_FLAG_GLOBAL }
};
PSI_cond_key key_COND_timer, key_IO_CACHE_SHARE_cond,
......
......@@ -48,8 +48,7 @@ extern PSI_mutex_key key_BITMAP_mutex, key_IO_CACHE_append_buffer_lock,
key_THR_LOCK_lock, key_THR_LOCK_malloc,
key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net,
key_THR_LOCK_open, key_THR_LOCK_threads, key_LOCK_uuid_generator,
key_LOCK_uuid_v7_generator, key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap,
key_LOCK_timer;
key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_LOCK_timer;
extern PSI_cond_key key_COND_timer, key_IO_CACHE_SHARE_cond,
key_IO_CACHE_SHARE_cond_writer, key_my_thread_var_suspend,
......
......@@ -27,7 +27,8 @@
#include <my_crypt.h>
#include <ssl_compat.h>
#include <cstdint>
#include <random>
#define CTX_ALIGN 16
......@@ -359,10 +360,25 @@ unsigned int my_aes_ctx_size(enum my_aes_mode)
return MY_AES_CTX_SIZE;
}
static std::mt19937 rnd;
int my_random_bytes(uchar *buf, int num)
{
if (RAND_bytes(buf, num) != 1)
{ /* shouldn't happen */
uchar *end= buf + num - 3;
uint r= rnd();
for (; buf < end; buf+= 4, r= rnd())
int4store(buf, r);
switch (num % 4)
{
case 0: break;
case 1: *buf= rnd(); break;
case 2: r=rnd(); int2store(buf, r); break;
case 3: r=rnd(); int3store(buf, r); break;
}
return MY_AES_OPENSSL_ERROR;
}
return MY_AES_OK;
}
......
SET debug_dbug="+d,simulate_uuidv4_my_random_bytes_failure";
#
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation
#
CREATE TABLE t1 (
......@@ -12,22 +9,22 @@ unique key(u)
);
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
Warnings:
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv4: RANDOM_BYTES() failed, using fallback
SELECT COUNT(DISTINCT u) AS distinct_uuid_count FROM t1;
distinct_uuid_count
16
......@@ -38,7 +35,5 @@ FROM t1 GROUP BY is_correct_version_and_revision;
is_correct_version_and_revision COUNT(*)
1 16
DROP TABLE t1;
#
# End of 11.6 tests
#
# End of 11.7 tests
SET debug_dbug="";
......@@ -4,10 +4,6 @@ SET debug_dbug="+d,simulate_uuidv4_my_random_bytes_failure";
source include/have_sequence.inc;
--echo #
--echo # Start of 11.6 tests
--echo #
--echo #
--echo # MDEV-11339 Support UUID v4 generation
--echo #
......@@ -25,8 +21,6 @@ SELECT
FROM t1 GROUP BY is_correct_version_and_revision;
DROP TABLE t1;
--echo #
--echo # End of 11.6 tests
--echo #
--echo # End of 11.7 tests
SET debug_dbug="";
#
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation
#
SELECT
......@@ -30,6 +27,4 @@ PLUGIN_DESCRIPTION Function UUIDv4()
PLUGIN_LICENSE GPL
PLUGIN_MATURITY Experimental
PLUGIN_AUTH_VERSION 1.0
#
# End of 11.6 tests
#
# End of 11.7 tests
--echo #
--echo # Start of 11.6 tests
--echo #
--echo #
--echo # MDEV-11339 Support UUID v4 generation
--echo #
......@@ -25,6 +21,4 @@ WHERE PLUGIN_TYPE='FUNCTION'
ORDER BY PLUGIN_NAME;
--horizontal_results
--echo #
--echo # End of 11.6 tests
--echo #
--echo # End of 11.7 tests
#
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation
#
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv4(), unique key(u));
......@@ -10,6 +7,4 @@ select count(distinct u) AS distinct_uuid_count from t1;
distinct_uuid_count
100
drop table t1;
#
# End of 11.6 tests
#
# End of 11.7 tests
source include/have_sequence.inc;
--echo #
--echo # Start of 11.6 tests
--echo #
--echo #
--echo # MDEV-11339 Support UUID v4 generation
--echo #
......@@ -13,6 +9,4 @@ insert into t1(a) select seq from seq_1_to_100;
select count(distinct u) AS distinct_uuid_count from t1;
drop table t1;
--echo #
--echo # End of 11.6 tests
--echo #
--echo # End of 11.7 tests
#
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation
#
SELECT UUIDv4() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
is_correct_version_and_revision
1
#
# End of 11.6 tests
#
# End of 11.7 tests
--echo #
--echo # Start of 11.6 tests
--echo #
--echo #
--echo # MDEV-11339 Support UUID v4 generation
--echo #
SELECT UUIDv4() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
--echo #
--echo # End of 11.6 tests
--echo #
--echo # End of 11.7 tests
......@@ -12,22 +12,22 @@ unique key(u)
);
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
Warnings:
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
Note 1105 UUIDv7: RANDOM_BYTES() failed, using fallback
SELECT COUNT(DISTINCT u) AS distinct_uuid_count FROM t1;
distinct_uuid_count
16
......
......@@ -202,7 +202,19 @@ int uuid_init(void*)
int uuidv7_init(void*)
{
mysql_mutex_init(0, &LOCK_uuid_v7_generator, MY_MUTEX_INIT_FAST);
#ifdef HAVE_PSI_INTERFACE
static PSI_mutex_key key_LOCK_uuid_v7_generator;
static PSI_mutex_info psi_mutexes[]=
{
{ &key_LOCK_uuid_v7_generator, "LOCK_uuid_v7_generator", PSI_FLAG_GLOBAL }
};
mysql_mutex_register("uuidv7", psi_mutexes, 1);
#else
#define key_LOCK_uuid_v7_generator 0
#endif
mysql_mutex_init(key_LOCK_uuid_v7_generator, &LOCK_uuid_v7_generator,
MY_MUTEX_INIT_FAST);
return 0;
}
......
......@@ -18,6 +18,9 @@
#include "sql_type_fixedbin_storage.h"
static constexpr uchar UUID_VERSION_MASK() { return 0x0F; }
static constexpr uchar UUID_VARIANT_MASK() { return 0x3F; }
template <bool force_swap>
class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
{
......
......@@ -18,7 +18,7 @@
/*
Implements Universal Unique Identifiers version 4, as described in
draft-ietf-uuidrev-rfc4122bis-14.
RFC 9562.
Field Octet # Note
random_a 0-5 Random CSPRNG 48 bits.
......@@ -36,14 +36,11 @@
*/
#include "sql_type_uuid.h"
#include "my_rnd.h"
class UUIDv4: public Type_handler_uuid_new::Fbt
{
static constexpr uchar UUID_VERSION() { return 0x40; }
static constexpr uchar UUID_VERSION_MASK() { return 0x0F; }
static constexpr uchar UUID_VARIANT() { return 0x80; }
static constexpr uchar UUID_VARIANT_MASK() { return 0x3F; }
static void inject_version_and_variant(uchar *to)
{
......@@ -51,13 +48,6 @@ class UUIDv4: public Type_handler_uuid_new::Fbt
to[8]= ((to[8] & UUID_VARIANT_MASK()) | UUID_VARIANT());
}
// Construct using a my_rnd()-based fallback method
static void construct_fallback(char *to)
{
for (uint i= 0; i < 4; i++)
int4store(&to[i * 4], (uint32) (my_rnd(&sql_rand)*0xFFFFFFFF));
}
static void construct(char *to)
{
bool error= my_random_bytes((uchar*) to, 16) != MY_AES_OK;
......@@ -67,8 +57,7 @@ class UUIDv4: public Type_handler_uuid_new::Fbt
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_NOTE,
ER_UNKNOWN_ERROR,
"UUIDv4 generation failed; using a my_rnd fallback");
construct_fallback(to);
"UUIDv4: RANDOM_BYTES() failed, using fallback");
}
/*
We have random bytes at to[6] and to[8].
......
......@@ -41,7 +41,6 @@
*/
#include "sql_type_uuid.h"
#include <my_rnd.h>
#include <m_string.h>
#include <myisampack.h> /* mi_int2store, mi_int6store */
#include <errmsg.h>
......@@ -52,10 +51,7 @@ extern mysql_mutex_t LOCK_uuid_v7_generator;
class UUIDv7: public Type_handler_uuid_new::Fbt
{
static constexpr uchar UUID_VERSION() { return 0x70; }
static constexpr uchar UUID_VERSION_MASK() { return 0x0F; }
static constexpr uchar UUID_VARIANT() { return 0x80; }
static constexpr uchar UUID_VARIANT_MASK() { return 0x3F; }
static constexpr double MICROSECONDS_TO_12BIT_MAPPING_FACTOR() { return 4.096; }
static void inject_version_and_variant(uchar *to)
{
......@@ -63,13 +59,6 @@ class UUIDv7: public Type_handler_uuid_new::Fbt
to[8]= ((to[8] & UUID_VARIANT_MASK()) | UUID_VARIANT());
}
// Construct using a my_rnd()-based fallback method
static void construct_fallback(char *to)
{
for (uint i= 0; i < 4; i++)
int4store(&to[i * 4], (uint32) (my_rnd(&sql_rand)*0xFFFFFFFF));
}
static void construct(char *to)
{
bool error= my_random_bytes((uchar*) to+8, 8) != MY_AES_OK;
......@@ -79,23 +68,22 @@ class UUIDv7: public Type_handler_uuid_new::Fbt
{
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_NOTE,
ER_UNKNOWN_ERROR,
"UUIDv7 generation failed; using a my_rnd fallback");
construct_fallback(to);
"UUIDv7: RANDOM_BYTES() failed, using fallback");
}
uint64 tv= my_hrtime().val;
/*
We have 12 bits for to ensure monotonocity. Let's store microseconds
there (from 0 to 999) as described in section 6.2, Method 3 of RFC 9562,
and use two remaining bits as a counter, thus allowing 4000 UUIDv7
values to be generated within one millisecond.
*/
uint64 tv= my_hrtime().val * 4;
mysql_mutex_lock(&LOCK_uuid_v7_generator);
last_uuidv7_timestamp= tv= MY_MAX(last_uuidv7_timestamp+1, tv);
mysql_mutex_unlock(&LOCK_uuid_v7_generator);
mi_int6store(to, tv / 1000);
/**
Map all the possible microseconds values (from 0 to 999) to all the
values that can be represented in 12 bits (from 0 to 4095) as
described in section 6.2, Method 3 of RFC 9562.
*/
mi_int2store(to+6, MICROSECONDS_TO_12BIT_MAPPING_FACTOR() * (tv % 1000));
mi_int6store(to, tv / 4000);
mi_int2store(to+6, tv % 4000);
/*
Let's inject proper version and variant to make it good UUIDv7.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment