2017-08-31 21:11:25 +00:00
|
|
|
#include <gtest/gtest.h>
|
2017-11-24 20:40:14 +00:00
|
|
|
|
2019-10-04 13:32:21 +00:00
|
|
|
#include <Common/Exception.h>
|
2018-11-27 16:45:45 +00:00
|
|
|
#include <Common/RWLock.h>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2019-01-11 19:12:36 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/phdr_cache.h>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <random>
|
2017-09-09 23:17:38 +00:00
|
|
|
#include <pcg_random.hpp>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace DB;
|
|
|
|
|
2019-09-01 01:32:44 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int DEADLOCK_AVOIDED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2020-03-09 02:10:20 +00:00
|
|
|
TEST(Common, RWLock1)
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
2020-06-01 17:07:27 +00:00
|
|
|
/// Tests with threads require this, because otherwise
|
|
|
|
/// when tested under Memory Sanitizer,
|
|
|
|
/// it tries to obtain stack trace on 'free' invocation at thread exit,
|
|
|
|
/// but cannot do that due to infinite recursion.
|
|
|
|
/// Alternative solution: disable PHDR Cache under memory sanitizer.
|
|
|
|
updatePHDRCache();
|
|
|
|
|
2018-01-18 20:33:16 +00:00
|
|
|
constexpr int cycles = 1000;
|
2017-09-04 12:49:49 +00:00
|
|
|
const std::vector<size_t> pool_sizes{1, 2, 4, 8};
|
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
static std::atomic<int> readers{0};
|
|
|
|
static std::atomic<int> writers{0};
|
|
|
|
|
2018-11-27 16:45:45 +00:00
|
|
|
static auto fifo_lock = RWLockImpl::create();
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 15:14:05 +00:00
|
|
|
static thread_local std::random_device rd;
|
2017-09-09 23:17:38 +00:00
|
|
|
static thread_local pcg64 gen(rd());
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2018-01-10 00:04:08 +00:00
|
|
|
auto func = [&] (size_t threads, int round)
|
|
|
|
{
|
2018-06-20 15:21:42 +00:00
|
|
|
for (int i = 0; i < cycles; ++i)
|
2017-09-04 12:49:49 +00:00
|
|
|
{
|
2018-11-27 16:45:45 +00:00
|
|
|
auto type = (std::uniform_int_distribution<>(0, 9)(gen) >= round) ? RWLockImpl::Read : RWLockImpl::Write;
|
2017-09-04 12:49:49 +00:00
|
|
|
auto sleep_for = std::chrono::duration<int, std::micro>(std::uniform_int_distribution<>(1, 100)(gen));
|
|
|
|
|
2019-02-28 18:15:10 +00:00
|
|
|
auto lock = fifo_lock->getLock(type, RWLockImpl::NO_QUERY);
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2018-11-27 16:45:45 +00:00
|
|
|
if (type == RWLockImpl::Write)
|
2017-09-04 12:49:49 +00:00
|
|
|
{
|
|
|
|
++writers;
|
|
|
|
|
|
|
|
ASSERT_EQ(writers, 1);
|
|
|
|
ASSERT_EQ(readers, 0);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(sleep_for);
|
|
|
|
|
|
|
|
--writers;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++readers;
|
|
|
|
|
|
|
|
ASSERT_EQ(writers, 0);
|
|
|
|
ASSERT_GE(readers, 1);
|
|
|
|
ASSERT_LE(readers, threads);
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
std::this_thread::sleep_for(sleep_for);
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
--readers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto pool_size : pool_sizes)
|
|
|
|
{
|
|
|
|
for (int round = 0; round < 10; ++round)
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
2017-09-04 12:49:49 +00:00
|
|
|
Stopwatch watch(CLOCK_MONOTONIC_COARSE);
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
std::list<std::thread> threads;
|
2017-10-25 18:39:10 +00:00
|
|
|
for (size_t thread = 0; thread < pool_size; ++thread)
|
2017-09-04 12:49:49 +00:00
|
|
|
threads.emplace_back([=] () { func(pool_size, round); });
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
for (auto & thread : threads)
|
|
|
|
thread.join();
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
auto total_time = watch.elapsedSeconds();
|
|
|
|
std::cout << "Threads " << pool_size << ", round " << round << ", total_time " << std::setprecision(2) << total_time << "\n";
|
2017-08-31 21:11:25 +00:00
|
|
|
}
|
2017-09-04 12:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 02:10:20 +00:00
|
|
|
TEST(Common, RWLockRecursive)
|
2017-09-04 12:49:49 +00:00
|
|
|
{
|
2020-06-01 17:07:27 +00:00
|
|
|
updatePHDRCache();
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
constexpr auto cycles = 10000;
|
|
|
|
|
2018-11-27 16:45:45 +00:00
|
|
|
static auto fifo_lock = RWLockImpl::create();
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2017-09-04 18:18:45 +00:00
|
|
|
static thread_local std::random_device rd;
|
2017-09-09 23:17:38 +00:00
|
|
|
static thread_local pcg64 gen(rd());
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2018-01-10 00:04:08 +00:00
|
|
|
std::thread t1([&] ()
|
|
|
|
{
|
2017-09-04 12:49:49 +00:00
|
|
|
for (int i = 0; i < 2 * cycles; ++i)
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
2019-09-02 01:00:58 +00:00
|
|
|
auto lock = fifo_lock->getLock(RWLockImpl::Write, "q1");
|
2017-09-04 12:49:49 +00:00
|
|
|
|
|
|
|
auto sleep_for = std::chrono::duration<int, std::micro>(std::uniform_int_distribution<>(1, 100)(gen));
|
|
|
|
std::this_thread::sleep_for(sleep_for);
|
|
|
|
}
|
|
|
|
});
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2018-01-10 00:04:08 +00:00
|
|
|
std::thread t2([&] ()
|
|
|
|
{
|
2017-09-04 12:49:49 +00:00
|
|
|
for (int i = 0; i < cycles; ++i)
|
|
|
|
{
|
2019-09-02 01:00:58 +00:00
|
|
|
auto lock1 = fifo_lock->getLock(RWLockImpl::Read, "q2");
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
auto sleep_for = std::chrono::duration<int, std::micro>(std::uniform_int_distribution<>(1, 100)(gen));
|
2017-08-31 21:11:25 +00:00
|
|
|
std::this_thread::sleep_for(sleep_for);
|
|
|
|
|
2019-09-02 01:00:58 +00:00
|
|
|
auto lock2 = fifo_lock->getLock(RWLockImpl::Read, "q2");
|
2017-09-09 23:17:38 +00:00
|
|
|
|
2020-10-16 11:58:47 +00:00
|
|
|
#ifndef ABORT_ON_LOGICAL_ERROR
|
|
|
|
/// It throws LOGICAL_ERROR
|
2019-09-02 01:00:58 +00:00
|
|
|
EXPECT_ANY_THROW({fifo_lock->getLock(RWLockImpl::Write, "q2");});
|
2020-10-16 11:58:47 +00:00
|
|
|
#endif
|
2017-08-31 21:11:25 +00:00
|
|
|
}
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2019-09-02 01:00:58 +00:00
|
|
|
fifo_lock->getLock(RWLockImpl::Write, "q2");
|
2017-09-04 12:49:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
2017-08-31 21:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2020-03-09 02:10:20 +00:00
|
|
|
TEST(Common, RWLockDeadlock)
|
2019-09-01 01:32:44 +00:00
|
|
|
{
|
2020-06-01 17:07:27 +00:00
|
|
|
updatePHDRCache();
|
|
|
|
|
2019-09-01 01:32:44 +00:00
|
|
|
static auto lock1 = RWLockImpl::create();
|
|
|
|
static auto lock2 = RWLockImpl::create();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* q1: r1 r2
|
|
|
|
* q2: w1
|
|
|
|
* q3: r2 r1
|
|
|
|
* q4: w2
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::thread t1([&] ()
|
|
|
|
{
|
|
|
|
auto holder1 = lock1->getLock(RWLockImpl::Read, "q1");
|
|
|
|
usleep(100000);
|
|
|
|
usleep(100000);
|
|
|
|
usleep(100000);
|
2020-04-06 21:35:20 +00:00
|
|
|
usleep(100000);
|
2019-09-01 01:32:44 +00:00
|
|
|
try
|
|
|
|
{
|
2020-04-07 00:06:48 +00:00
|
|
|
auto holder2 = lock2->getLock(RWLockImpl::Read, "q1", std::chrono::milliseconds(100));
|
2020-04-06 21:35:20 +00:00
|
|
|
if (!holder2)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Locking attempt timed out! Possible deadlock avoided. Client should retry.",
|
|
|
|
ErrorCodes::DEADLOCK_AVOIDED);
|
|
|
|
}
|
2019-09-01 01:32:44 +00:00
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
if (e.code() != ErrorCodes::DEADLOCK_AVOIDED)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
std::thread t2([&] ()
|
|
|
|
{
|
|
|
|
usleep(100000);
|
|
|
|
auto holder1 = lock1->getLock(RWLockImpl::Write, "q2");
|
|
|
|
});
|
|
|
|
|
|
|
|
std::thread t3([&] ()
|
|
|
|
{
|
|
|
|
usleep(100000);
|
|
|
|
usleep(100000);
|
|
|
|
auto holder2 = lock2->getLock(RWLockImpl::Read, "q3");
|
|
|
|
usleep(100000);
|
|
|
|
usleep(100000);
|
2020-04-06 21:35:20 +00:00
|
|
|
usleep(100000);
|
2019-09-01 01:32:44 +00:00
|
|
|
try
|
|
|
|
{
|
2020-04-07 00:06:48 +00:00
|
|
|
auto holder1 = lock1->getLock(RWLockImpl::Read, "q3", std::chrono::milliseconds(100));
|
2020-04-06 21:35:20 +00:00
|
|
|
if (!holder1)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Locking attempt timed out! Possible deadlock avoided. Client should retry.",
|
|
|
|
ErrorCodes::DEADLOCK_AVOIDED);
|
|
|
|
}
|
2019-09-01 01:32:44 +00:00
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
if (e.code() != ErrorCodes::DEADLOCK_AVOIDED)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
std::thread t4([&] ()
|
|
|
|
{
|
|
|
|
usleep(100000);
|
|
|
|
usleep(100000);
|
|
|
|
usleep(100000);
|
|
|
|
auto holder2 = lock2->getLock(RWLockImpl::Write, "q4");
|
|
|
|
});
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
t3.join();
|
|
|
|
t4.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-09 02:10:20 +00:00
|
|
|
TEST(Common, RWLockPerfTestReaders)
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
2020-06-01 17:07:27 +00:00
|
|
|
updatePHDRCache();
|
|
|
|
|
2018-01-18 20:33:16 +00:00
|
|
|
constexpr int cycles = 100000; // 100k
|
2017-09-01 15:05:23 +00:00
|
|
|
const std::vector<size_t> pool_sizes{1, 2, 4, 8};
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2018-11-27 16:45:45 +00:00
|
|
|
static auto fifo_lock = RWLockImpl::create();
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2017-09-01 15:05:23 +00:00
|
|
|
for (auto pool_size : pool_sizes)
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
|
|
|
Stopwatch watch(CLOCK_MONOTONIC_COARSE);
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
auto func = [&] ()
|
|
|
|
{
|
|
|
|
for (auto i = 0; i < cycles; ++i)
|
|
|
|
{
|
2019-02-28 18:15:10 +00:00
|
|
|
auto lock = fifo_lock->getLock(RWLockImpl::Read, RWLockImpl::NO_QUERY);
|
2017-09-04 12:49:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::list<std::thread> threads;
|
2017-10-25 18:39:10 +00:00
|
|
|
for (size_t thread = 0; thread < pool_size; ++thread)
|
2017-09-04 12:49:49 +00:00
|
|
|
threads.emplace_back(func);
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
for (auto & thread : threads)
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
auto total_time = watch.elapsedSeconds();
|
2017-09-04 12:49:49 +00:00
|
|
|
std::cout << "Threads " << pool_size << ", total_time " << std::setprecision(2) << total_time << "\n";
|
2017-08-31 21:11:25 +00:00
|
|
|
}
|
|
|
|
}
|