-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove random_label in favor of uuid. (#4709)
The random label work broke the python tests because it can generate the same URI under certain conditions. --- TYPE: NO_HISTORY DESC: Remove random_label in favor of uuid.
- Loading branch information
Showing
12 changed files
with
388 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ commence(object_library array) | |
baseline | ||
fragment | ||
generic_tile_io | ||
uuid | ||
vfs | ||
) | ||
if(TILEDB_STATS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @file compile_uuid_main.cc | ||
* | ||
* @section LICENSE | ||
* | ||
* The MIT License | ||
* | ||
* @copyright Copyright (c) 2021 TileDB, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include "../uuid.h" | ||
|
||
int main() { | ||
(void)tiledb::sm::uuid::generate_uuid(nullptr, false); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* @file unit_uuid.cc | ||
* | ||
* @section LICENSE | ||
* | ||
* The MIT License | ||
* | ||
* @copyright Copyright (c) 2018-2022 TileDB, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @section DESCRIPTION | ||
* | ||
* Tests the UUID utility functions. | ||
*/ | ||
|
||
#include <test/support/tdb_catch.h> | ||
#include <set> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include "tiledb/sm/global_state/global_state.h" | ||
#include "tiledb/sm/misc/uuid.h" | ||
|
||
using namespace tiledb::sm; | ||
|
||
std::mutex catch2_macro_mutex; | ||
|
||
// A thread-safe variant of the REQUIRE macro. | ||
#define REQUIRE_SAFE(a) \ | ||
{ \ | ||
std::lock_guard<std::mutex> lock(catch2_macro_mutex); \ | ||
REQUIRE(a); \ | ||
} | ||
|
||
void cancel_all_tasks(StorageManager*) { | ||
} | ||
|
||
TEST_CASE("UUID: Test generate", "[uuid]") { | ||
SECTION("- Serial") { | ||
std::string uuid0, uuid1, uuid2; | ||
REQUIRE(uuid::generate_uuid(&uuid0).ok()); | ||
REQUIRE(uuid0.length() == 36); | ||
REQUIRE(uuid::generate_uuid(&uuid1).ok()); | ||
REQUIRE(uuid1.length() == 36); | ||
REQUIRE(uuid0 != uuid1); | ||
|
||
REQUIRE(uuid::generate_uuid(&uuid2, false).ok()); | ||
REQUIRE(uuid2.length() == 32); | ||
} | ||
|
||
SECTION("- Threaded") { | ||
const unsigned nthreads = 20; | ||
std::vector<std::string> uuids(nthreads); | ||
std::vector<std::thread> threads; | ||
for (unsigned i = 0; i < nthreads; i++) { | ||
threads.emplace_back([&uuids, i]() { | ||
std::string& uuid = uuids[i]; | ||
REQUIRE_SAFE(uuid::generate_uuid(&uuid).ok()); | ||
REQUIRE_SAFE(uuid.length() == 36); | ||
}); | ||
} | ||
for (auto& t : threads) { | ||
t.join(); | ||
} | ||
// Check uniqueness | ||
std::set<std::string> uuid_set; | ||
uuid_set.insert(uuids.begin(), uuids.end()); | ||
REQUIRE(uuid_set.size() == uuids.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* @file uuid.cc | ||
* | ||
* @section LICENSE | ||
* | ||
* The MIT License | ||
* | ||
* @copyright Copyright (c) 2018-2023 TileDB, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @section DESCRIPTION | ||
* | ||
* This file defines a platform-independent UUID generator. | ||
*/ | ||
|
||
#include <mutex> | ||
#include <vector> | ||
|
||
#include "tiledb/sm/misc/uuid.h" | ||
|
||
#ifdef _WIN32 | ||
#include <Rpc.h> | ||
#else | ||
#include <openssl/err.h> | ||
#include <openssl/rand.h> | ||
#include <cstdio> | ||
#endif | ||
|
||
using namespace tiledb::common; | ||
|
||
namespace tiledb::sm::uuid { | ||
|
||
/** Mutex to guard UUID generation. */ | ||
static std::mutex uuid_mtx; | ||
|
||
#ifdef _WIN32 | ||
|
||
/** | ||
* Generate a UUID using Win32 RPC API. | ||
*/ | ||
Status generate_uuid_win32(std::string* uuid_str) { | ||
if (uuid_str == nullptr) | ||
return Status_UtilsError("Null UUID string argument"); | ||
|
||
UUID uuid; | ||
RPC_STATUS rc = UuidCreate(&uuid); | ||
if (rc != RPC_S_OK) | ||
return Status_UtilsError("Unable to generate Win32 UUID: creation error"); | ||
|
||
char* buf = nullptr; | ||
rc = UuidToStringA(&uuid, reinterpret_cast<RPC_CSTR*>(&buf)); | ||
if (rc != RPC_S_OK) | ||
return Status_UtilsError( | ||
"Unable to generate Win32 UUID: string conversion error"); | ||
|
||
*uuid_str = std::string(buf); | ||
|
||
rc = RpcStringFreeA(reinterpret_cast<RPC_CSTR*>(&buf)); | ||
if (rc != RPC_S_OK) | ||
return Status_UtilsError("Unable to generate Win32 UUID: free error"); | ||
|
||
return Status::Ok(); | ||
} | ||
|
||
#else | ||
|
||
/** | ||
* Generate a UUID using OpenSSL. | ||
* | ||
* Initially from: https://gist.github.com/kvelakur/9069c9896577c3040030 | ||
* "Generating a Version 4 UUID using OpenSSL" | ||
*/ | ||
Status generate_uuid_openssl(std::string* uuid_str) { | ||
if (uuid_str == nullptr) | ||
return Status_UtilsError("Null UUID string argument"); | ||
|
||
union { | ||
struct { | ||
uint32_t time_low; | ||
uint16_t time_mid; | ||
uint16_t time_hi_and_version; | ||
uint8_t clk_seq_hi_res; | ||
uint8_t clk_seq_low; | ||
uint8_t node[6]; | ||
}; | ||
uint8_t __rnd[16]; | ||
} uuid; | ||
|
||
int rc = RAND_bytes(uuid.__rnd, sizeof(uuid)); | ||
if (rc < 1) { | ||
char err_msg[256]; | ||
ERR_error_string_n(ERR_get_error(), err_msg, sizeof(err_msg)); | ||
return Status_UtilsError( | ||
"Cannot generate random bytes with OpenSSL: " + std::string(err_msg)); | ||
} | ||
|
||
// Refer Section 4.2 of RFC-4122 | ||
// https://tools.ietf.org/html/rfc4122#section-4.2 | ||
uuid.clk_seq_hi_res = (uint8_t)((uuid.clk_seq_hi_res & 0x3F) | 0x80); | ||
uuid.time_hi_and_version = | ||
(uint16_t)((uuid.time_hi_and_version & 0x0FFF) | 0x4000); | ||
|
||
// Format the UUID as a string. | ||
char buf[128]; | ||
rc = snprintf( | ||
buf, | ||
sizeof(buf), | ||
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", | ||
uuid.time_low, | ||
uuid.time_mid, | ||
uuid.time_hi_and_version, | ||
uuid.clk_seq_hi_res, | ||
uuid.clk_seq_low, | ||
uuid.node[0], | ||
uuid.node[1], | ||
uuid.node[2], | ||
uuid.node[3], | ||
uuid.node[4], | ||
uuid.node[5]); | ||
|
||
if (rc < 0) | ||
return Status_UtilsError("Error formatting UUID string"); | ||
|
||
*uuid_str = std::string(buf); | ||
|
||
return Status::Ok(); | ||
} | ||
|
||
#endif | ||
|
||
Status generate_uuid(std::string* uuid, bool hyphenate) { | ||
if (uuid == nullptr) | ||
return Status_UtilsError("Null UUID string argument"); | ||
|
||
std::string uuid_str; | ||
{ | ||
// OpenSSL is not threadsafe, so grab a lock here. We are locking in the | ||
// Windows case as well just to be careful. | ||
std::unique_lock<std::mutex> lck(uuid_mtx); | ||
#ifdef _WIN32 | ||
RETURN_NOT_OK(generate_uuid_win32(&uuid_str)); | ||
#else | ||
RETURN_NOT_OK(generate_uuid_openssl(&uuid_str)); | ||
#endif | ||
} | ||
|
||
uuid->clear(); | ||
for (unsigned i = 0; i < uuid_str.length(); i++) { | ||
if (uuid_str[i] == '-' && !hyphenate) | ||
continue; | ||
uuid->push_back(uuid_str[i]); | ||
} | ||
|
||
return Status::Ok(); | ||
} | ||
|
||
} // namespace tiledb::sm::uuid |
Oops, something went wrong.