rocksdb/utilities/persistent_cache/hash_table_test.cc
Peter Dillinger 86bb0c0d1b Use C++20 in public API, fix CI (#13915)
Summary:
A follow-up to https://github.com/facebook/rocksdb/issues/13904 which was incomplete in updating CI jobs to support C++20 because the C++20 usage was only in tests. Here we add subtle C++20 usage in the public API ("using enum" feature in db.h) to force the issue.

A lot of the work for this PR was in updating the Ubuntu22 docker image, for earlier compiler/runtime versions supporting C++20, and generating a new Ubuntu24 docker image, for later compiler/runtime versions. The Ubuntu22 image needed to be updated because there are incompatibilities with clang-13 + c++20 + libstdc++ for gcc 11, seen on these examples

```
#include <chrono>

int main(int argc, char *argv[]) {
  std::chrono::microseconds d = {}; return 0;
}
```

and

```
#include <coroutine>

int main() { return 0; }
```

The second was causing recurring failures in build-linux-clang-13-asan-ubsan-with-folly, now fixed.

So we have to install clang's libc++ to compile with clang-13. I haven't been able to get this to work with some of the libraries like benchmark, glog, and/or gflags, but I'm able to compile core RocksDB with clang-13. On this docker image, an extra compiler parameter is needed to compile with gcc and glog because it's built from source perhaps not perfectly, because the ubuntu package transitively conflicts with libc++.

The Ubuntu24 image seems to be low-drama and generally work for testing out newer compiler versions. The mingw build uses Ubuntu24 because the mingw package on Ubuntu22 uses a gcc version that is too old.

And the mass of other code changes are trying to work around new warnings, mostly from clang-analyze, which I upgraded to clang-18 in CI.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/13915

Test Plan: CI, including temporarily including the nightly jobs in the PR jobs in earlier revisions to test and stabilize

Reviewed By: archang19

Differential Revision: D81933067

Pulled By: pdillinger

fbshipit-source-id: 7e33823006a79d5f3cf5bc1d625f0a3c08a7d74c
2025-09-08 13:11:28 -07:00

161 lines
3.9 KiB
C++

// Copyright (c) 2013, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
//
#include "utilities/persistent_cache/hash_table.h"
#include <cstdlib>
#include <iostream>
#include <set>
#include <string>
#include "db/db_test_util.h"
#include "memory/arena.h"
#include "test_util/testharness.h"
#include "util/random.h"
#include "utilities/persistent_cache/hash_table_evictable.h"
namespace ROCKSDB_NAMESPACE {
struct HashTableTest : public testing::Test {
~HashTableTest() override { map_.Clear(&HashTableTest::ClearNode); }
struct Node {
Node() = default;
explicit Node(const uint64_t key, const std::string& val = std::string())
: key_(key), val_(val) {}
uint64_t key_ = 0;
std::string val_;
};
struct Equal {
bool operator()(const Node& lhs, const Node& rhs) {
return lhs.key_ == rhs.key_;
}
};
struct Hash {
uint64_t operator()(const Node& node) {
return std::hash<uint64_t>()(node.key_);
}
};
static void ClearNode(Node /*node*/) {}
HashTable<Node, Hash, Equal> map_;
};
struct EvictableHashTableTest : public testing::Test {
~EvictableHashTableTest() override {
map_.Clear(&EvictableHashTableTest::ClearNode);
}
struct Node : LRUElement<Node> {
Node() = default;
explicit Node(const uint64_t key, const std::string& val = std::string())
: key_(key), val_(val) {}
uint64_t key_ = 0;
std::string val_;
std::atomic<uint32_t> refs_{0};
};
struct Equal {
bool operator()(const Node* lhs, const Node* rhs) {
return lhs->key_ == rhs->key_;
}
};
struct Hash {
uint64_t operator()(const Node* node) {
return std::hash<uint64_t>()(node->key_);
}
};
static void ClearNode(Node* /*node*/) {}
EvictableHashTable<Node, Hash, Equal> map_;
};
TEST_F(HashTableTest, TestInsert) {
const uint64_t max_keys = 1024 * 1024;
// insert
for (uint64_t k = 0; k < max_keys; ++k) {
map_.Insert(Node(k, std::string(1000, k % 255)));
}
// verify
for (uint64_t k = 0; k < max_keys; ++k) {
Node val;
port::RWMutex* rlock = nullptr;
assert(map_.Find(Node(k), &val, &rlock));
rlock->ReadUnlock();
assert(val.val_ == std::string(1000, k % 255));
}
}
TEST_F(HashTableTest, TestErase) {
const uint64_t max_keys = 1024 * 1024;
// insert
for (uint64_t k = 0; k < max_keys; ++k) {
map_.Insert(Node(k, std::string(1000, k % 255)));
}
auto rand = Random64(time(nullptr));
// erase a few keys randomly
std::set<uint64_t> erased;
for (int i = 0; i < 1024; ++i) {
uint64_t k = rand.Next() % max_keys;
if (erased.find(k) != erased.end()) {
continue;
}
assert(map_.Erase(Node(k), /*ret=*/nullptr));
erased.insert(k);
}
// verify
for (uint64_t k = 0; k < max_keys; ++k) {
Node val;
port::RWMutex* rlock = nullptr;
bool status = map_.Find(Node(k), &val, &rlock);
if (erased.find(k) == erased.end()) {
assert(status);
rlock->ReadUnlock();
assert(val.val_ == std::string(1000, k % 255));
} else {
assert(!status);
}
}
}
TEST_F(EvictableHashTableTest, TestEvict) {
#ifndef __clang_analyzer__
const uint64_t max_keys = 1024 * 1024;
// insert
for (uint64_t k = 0; k < max_keys; ++k) {
map_.Insert(new Node(k, std::string(1000, k % 255)));
}
// verify
for (uint64_t k = 0; k < max_keys; ++k) {
Node* val = map_.Evict();
// unfortunately we can't predict eviction value since it is from any one of
// the lock stripe
assert(val);
assert(val->val_ == std::string(1000, val->key_ % 255));
delete val;
}
#endif // __clang_analyzer__
}
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}