2020-10-15 10:22:02 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <Storages/MergeTree/BackgroundJobsExecutor.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/tests/gtest_global_context.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <chrono>
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric BackgroundPoolTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace DB;
|
|
|
|
|
|
|
|
static std::atomic<Int64> counter{0};
|
|
|
|
|
|
|
|
class TestJobExecutor : public IBackgroundJobExecutor
|
|
|
|
{
|
|
|
|
public:
|
2021-04-10 23:33:54 +00:00
|
|
|
explicit TestJobExecutor(ContextPtr local_context)
|
2020-10-15 10:22:02 +00:00
|
|
|
:IBackgroundJobExecutor(
|
2021-04-10 23:33:54 +00:00
|
|
|
local_context,
|
2020-10-16 10:18:56 +00:00
|
|
|
BackgroundTaskSchedulingSettings{},
|
2020-10-15 10:22:02 +00:00
|
|
|
{PoolConfig{PoolType::MERGE_MUTATE, 4, CurrentMetrics::BackgroundPoolTask}})
|
|
|
|
{}
|
|
|
|
|
|
|
|
protected:
|
2020-10-16 10:18:56 +00:00
|
|
|
String getBackgroundTaskName() const override
|
2020-10-15 10:22:02 +00:00
|
|
|
{
|
2020-10-16 10:18:56 +00:00
|
|
|
return "TestTask";
|
2020-10-15 10:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<JobAndPool> getBackgroundJob() override
|
|
|
|
{
|
2021-02-11 11:46:18 +00:00
|
|
|
return JobAndPool{[] { std::this_thread::sleep_for(1s); counter++; return true; }, PoolType::MERGE_MUTATE};
|
2020-10-15 10:22:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using TestExecutorPtr = std::unique_ptr<TestJobExecutor>;
|
|
|
|
|
|
|
|
TEST(BackgroundExecutor, TestMetric)
|
|
|
|
{
|
2020-10-15 13:57:50 +00:00
|
|
|
const auto & context_holder = getContext();
|
2020-10-15 10:22:02 +00:00
|
|
|
std::vector<TestExecutorPtr> executors;
|
|
|
|
for (size_t i = 0; i < 100; ++i)
|
2021-04-10 23:33:54 +00:00
|
|
|
executors.emplace_back(std::make_unique<TestJobExecutor>(context_holder.context));
|
2020-10-15 10:22:02 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; ++i)
|
|
|
|
executors[i]->start();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; ++i)
|
|
|
|
{
|
|
|
|
EXPECT_TRUE(CurrentMetrics::values[CurrentMetrics::BackgroundPoolTask].load() <= 4);
|
|
|
|
std::this_thread::sleep_for(200ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; ++i)
|
|
|
|
executors[i]->finish();
|
|
|
|
|
|
|
|
/// Sanity check
|
|
|
|
EXPECT_TRUE(counter > 50);
|
|
|
|
}
|