ClickHouse/utils/keeper-bench/Runner.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1510 lines
54 KiB
C++
Raw Normal View History

2021-04-13 12:36:47 +00:00
#include "Runner.h"
2024-04-10 10:56:29 +00:00
#include <atomic>
2023-04-07 13:02:42 +00:00
#include <Poco/Util/AbstractConfiguration.h>
2021-04-13 12:36:47 +00:00
2024-05-10 12:15:01 +00:00
#include <Coordination/CoordinationSettings.h>
#include <Coordination/KeeperContext.h>
2024-05-22 14:12:33 +00:00
#include <Coordination/KeeperSnapshotManager.h>
2024-05-10 12:15:01 +00:00
#include <Coordination/KeeperStorage.h>
2024-05-22 14:12:33 +00:00
#include <Core/ColumnWithTypeAndName.h>
#include <Core/ColumnsWithTypeAndName.h>
2024-05-10 12:15:01 +00:00
#include <Disks/DiskLocal.h>
2024-07-15 17:15:45 +00:00
#include <Core/Settings.h>
2024-09-18 12:20:53 +00:00
#include <Formats/FormatFactory.h>
2024-05-22 14:12:33 +00:00
#include <Formats/ReadSchemaUtils.h>
#include <Formats/registerFormats.h>
#include <IO/ReadBuffer.h>
#include <IO/ReadBufferFromFile.h>
2023-04-17 11:25:46 +00:00
#include <IO/WriteBufferFromFile.h>
#include <IO/WriteBufferFromString.h>
#include <IO/copyData.h>
2024-04-10 10:56:29 +00:00
#include <Interpreters/Context.h>
2024-05-22 14:12:33 +00:00
#include <Processors/Formats/IInputFormat.h>
#include <Common/ConcurrentBoundedQueue.h>
#include <Common/Config/ConfigProcessor.h>
#include <Common/EventNotifier.h>
#include <Common/Exception.h>
#include <Common/ZooKeeper/IKeeper.h>
#include <Common/ZooKeeper/ZooKeeperArgs.h>
#include <Common/ZooKeeper/ZooKeeperCommon.h>
#include <Common/ZooKeeper/ZooKeeperConstants.h>
2023-04-14 13:32:08 +00:00
2023-04-14 13:32:08 +00:00
namespace CurrentMetrics
{
extern const Metric LocalThread;
extern const Metric LocalThreadActive;
extern const Metric LocalThreadScheduled;
2023-04-14 13:32:08 +00:00
}
2024-09-18 18:35:52 +00:00
namespace DB::Setting
{
extern const SettingsUInt64 max_block_size;
}
2023-04-06 15:10:58 +00:00
namespace DB::ErrorCodes
2021-04-13 12:36:47 +00:00
{
extern const int CANNOT_BLOCK_SIGNAL;
2023-04-14 13:32:08 +00:00
extern const int BAD_ARGUMENTS;
2024-04-10 10:56:29 +00:00
extern const int LOGICAL_ERROR;
2021-04-13 12:36:47 +00:00
}
2023-04-06 15:10:58 +00:00
Runner::Runner(
2023-04-07 14:14:39 +00:00
std::optional<size_t> concurrency_,
2023-04-06 15:10:58 +00:00
const std::string & config_path,
2024-04-10 10:56:29 +00:00
const std::string & input_request_log_,
2024-05-10 12:15:01 +00:00
const std::string & setup_nodes_snapshot_path_,
2023-04-06 15:10:58 +00:00
const Strings & hosts_strings_,
2023-04-07 14:14:39 +00:00
std::optional<double> max_time_,
std::optional<double> delay_,
std::optional<bool> continue_on_error_,
std::optional<size_t> max_iterations_)
2024-04-10 10:56:29 +00:00
: input_request_log(input_request_log_)
2024-05-10 12:15:01 +00:00
, setup_nodes_snapshot_path(setup_nodes_snapshot_path_)
2024-04-10 10:56:29 +00:00
, info(std::make_shared<Stats>())
2023-04-07 13:02:42 +00:00
{
2023-04-06 15:10:58 +00:00
2023-04-07 15:20:36 +00:00
DB::ConfigProcessor config_processor(config_path, true, false);
2024-04-10 10:56:29 +00:00
DB::ConfigurationPtr config = nullptr;
if (!config_path.empty())
{
config = config_processor.loadConfig().configuration;
if (config->has("generator"))
generator.emplace(*config);
}
else
{
if (input_request_log.empty())
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Both --config and --input_request_log cannot be empty");
if (!std::filesystem::exists(input_request_log))
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "File on path {} does not exist", input_request_log);
}
2021-04-13 12:36:47 +00:00
2023-04-07 13:02:42 +00:00
if (!hosts_strings_.empty())
{
for (const auto & host : hosts_strings_)
connection_infos.push_back({.host = host});
}
else
{
if (!config)
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "No config file or hosts defined");
parseHostsFromConfig(*config);
}
2023-04-07 14:14:39 +00:00
2023-04-14 13:32:08 +00:00
std::cerr << "---- Run options ---- " << std::endl;
2023-04-07 15:20:36 +00:00
static constexpr uint64_t DEFAULT_CONCURRENCY = 1;
2023-04-07 14:14:39 +00:00
if (concurrency_)
concurrency = *concurrency_;
2024-04-10 10:56:29 +00:00
else if (!config)
concurrency = DEFAULT_CONCURRENCY;
2023-04-07 14:14:39 +00:00
else
2023-04-07 15:20:36 +00:00
concurrency = config->getUInt64("concurrency", DEFAULT_CONCURRENCY);
2023-04-14 13:32:08 +00:00
std::cerr << "Concurrency: " << concurrency << std::endl;
2023-04-07 14:14:39 +00:00
2023-04-07 15:20:36 +00:00
static constexpr uint64_t DEFAULT_ITERATIONS = 0;
2023-04-07 14:14:39 +00:00
if (max_iterations_)
max_iterations = *max_iterations_;
2024-04-10 10:56:29 +00:00
else if (!config)
max_iterations = DEFAULT_ITERATIONS;
2023-04-07 14:14:39 +00:00
else
2023-04-07 15:20:36 +00:00
max_iterations = config->getUInt64("iterations", DEFAULT_ITERATIONS);
2023-04-14 13:32:08 +00:00
std::cerr << "Iterations: " << max_iterations << std::endl;
2023-04-07 14:14:39 +00:00
2023-04-07 15:20:36 +00:00
static constexpr double DEFAULT_DELAY = 1.0;
2023-04-07 14:14:39 +00:00
if (delay_)
delay = *delay_;
2024-04-10 10:56:29 +00:00
else if (!config)
delay = DEFAULT_DELAY;
2023-04-07 14:14:39 +00:00
else
2023-04-07 15:20:36 +00:00
delay = config->getDouble("report_delay", DEFAULT_DELAY);
2023-04-14 13:32:08 +00:00
std::cerr << "Report delay: " << delay << std::endl;
2023-04-07 14:14:39 +00:00
2023-04-14 13:32:08 +00:00
static constexpr double DEFAULT_TIME_LIMIT = 0.0;
2023-04-07 14:14:39 +00:00
if (max_time_)
max_time = *max_time_;
2024-04-10 10:56:29 +00:00
else if (!config)
max_time = DEFAULT_TIME_LIMIT;
2023-04-07 14:14:39 +00:00
else
2023-04-07 15:20:36 +00:00
max_time = config->getDouble("timelimit", DEFAULT_TIME_LIMIT);
2023-04-14 13:32:08 +00:00
std::cerr << "Time limit: " << max_time << std::endl;
2023-04-07 14:14:39 +00:00
if (continue_on_error_)
continue_on_error = *continue_on_error_;
2024-04-10 10:56:29 +00:00
else if (!config)
continue_on_error_ = false;
2023-04-07 14:14:39 +00:00
else
2023-04-07 15:20:36 +00:00
continue_on_error = config->getBool("continue_on_error", false);
2023-04-14 13:32:08 +00:00
std::cerr << "Continue on error: " << continue_on_error << std::endl;
2023-04-17 11:25:46 +00:00
2024-04-10 10:56:29 +00:00
if (config)
2023-04-17 11:25:46 +00:00
{
2024-04-10 10:56:29 +00:00
benchmark_context.initializeFromConfig(*config);
static const std::string output_key = "output";
print_to_stdout = config->getBool(output_key + ".stdout", false);
std::cerr << "Printing output to stdout: " << print_to_stdout << std::endl;
static const std::string output_file_key = output_key + ".file";
if (config->has(output_file_key))
2023-04-17 11:25:46 +00:00
{
2024-04-10 10:56:29 +00:00
if (config->has(output_file_key + ".path"))
{
file_output = config->getString(output_file_key + ".path");
output_file_with_timestamp = config->getBool(output_file_key + ".with_timestamp");
}
else
file_output = config->getString(output_file_key);
2023-04-17 11:25:46 +00:00
2024-04-10 10:56:29 +00:00
std::cerr << "Result file path: " << file_output->string() << std::endl;
}
2023-04-17 11:25:46 +00:00
}
2023-04-14 13:32:08 +00:00
std::cerr << "---- Run options ----\n" << std::endl;
2023-04-07 13:02:42 +00:00
}
void Runner::parseHostsFromConfig(const Poco::Util::AbstractConfiguration & config)
{
const auto fill_connection_details = [&](const std::string & key, auto & connection_info)
{
if (config.has(key + ".secure"))
connection_info.secure = config.getBool(key + ".secure");
if (config.has(key + ".session_timeout_ms"))
connection_info.session_timeout_ms = config.getInt(key + ".session_timeout_ms");
if (config.has(key + ".operation_timeout_ms"))
connection_info.operation_timeout_ms = config.getInt(key + ".operation_timeout_ms");
if (config.has(key + ".connection_timeout_ms"))
connection_info.connection_timeout_ms = config.getInt(key + ".connection_timeout_ms");
if (config.has(key + ".use_compression"))
connection_info.use_compression = config.getBool(key + ".use_compression");
2023-04-07 13:02:42 +00:00
};
fill_connection_details("connections", default_connection_info);
Poco::Util::AbstractConfiguration::Keys connections_keys;
config.keys("connections", connections_keys);
for (const auto & key : connections_keys)
{
std::string connection_key = "connections." + key;
auto connection_info = default_connection_info;
if (key.starts_with("host"))
{
connection_info.host = config.getString(connection_key);
connection_infos.push_back(std::move(connection_info));
}
else if (key.starts_with("connection") && key != "connection_timeout_ms")
{
connection_info.host = config.getString(connection_key + ".host");
if (config.has(connection_key + ".sessions"))
connection_info.sessions = config.getUInt64(connection_key + ".sessions");
fill_connection_details(connection_key, connection_info);
connection_infos.push_back(std::move(connection_info));
}
}
}
void Runner::thread(std::vector<std::shared_ptr<Coordination::ZooKeeper>> zookeepers)
2021-04-13 12:36:47 +00:00
{
Coordination::ZooKeeperRequestPtr request;
/// Randomly choosing connection index
pcg64 rng(randomSeed());
std::uniform_int_distribution<size_t> distribution(0, zookeepers.size() - 1);
/// In these threads we do not accept INT signal.
sigset_t sig_set;
if (sigemptyset(&sig_set)
|| sigaddset(&sig_set, SIGINT)
|| pthread_sigmask(SIG_BLOCK, &sig_set, nullptr))
{
2023-12-15 18:25:49 +00:00
throw DB::ErrnoException(DB::ErrorCodes::CANNOT_BLOCK_SIGNAL, "Cannot block signal");
2021-04-13 12:36:47 +00:00
}
while (true)
{
bool extracted = false;
while (!extracted)
{
2023-04-07 14:14:39 +00:00
extracted = queue->tryPop(request, 100);
2021-04-13 12:36:47 +00:00
if (shutdown
|| (max_iterations && requests_executed >= max_iterations))
{
return;
}
}
const auto connection_index = distribution(rng);
auto & zk = zookeepers[connection_index];
auto promise = std::make_shared<std::promise<size_t>>();
auto future = promise->get_future();
2023-04-07 14:51:25 +00:00
Coordination::ResponseCallback callback = [&request, promise](const Coordination::Response & response)
2021-04-13 12:36:47 +00:00
{
2023-04-07 14:51:25 +00:00
bool set_exception = true;
if (response.error == Coordination::Error::ZOK)
{
set_exception = false;
}
else if (response.error == Coordination::Error::ZNONODE)
{
/// remove can fail with ZNONODE because of different order of execution
2023-04-14 13:32:08 +00:00
/// of generated create and remove requests
2023-04-07 14:51:25 +00:00
/// this is okay for concurrent runs
if (dynamic_cast<const Coordination::ZooKeeperRemoveResponse *>(&response))
set_exception = false;
else if (const auto * multi_response = dynamic_cast<const Coordination::ZooKeeperMultiResponse *>(&response))
{
const auto & responses = multi_response->responses;
size_t i = 0;
while (responses[i]->error != Coordination::Error::ZNONODE)
++i;
const auto & multi_request = dynamic_cast<const Coordination::ZooKeeperMultiRequest &>(*request);
if (dynamic_cast<const Coordination::ZooKeeperRemoveRequest *>(&*multi_request.requests[i]))
set_exception = false;
}
}
if (set_exception)
2021-04-13 12:36:47 +00:00
promise->set_exception(std::make_exception_ptr(zkutil::KeeperException(response.error)));
else
promise->set_value(response.bytesSize());
};
Stopwatch watch;
zk->executeGenericRequest(request, callback);
try
{
auto response_size = future.get();
2023-04-14 13:32:08 +00:00
auto microseconds = watch.elapsedMicroseconds();
2021-04-13 12:36:47 +00:00
std::lock_guard lock(mutex);
if (request->isReadRequest())
2023-04-14 13:32:08 +00:00
info->addRead(microseconds, 1, request->bytesSize() + response_size);
2021-04-13 12:36:47 +00:00
else
2023-04-14 13:32:08 +00:00
info->addWrite(microseconds, 1, request->bytesSize() + response_size);
2021-04-13 12:36:47 +00:00
}
catch (...)
{
if (!continue_on_error)
{
shutdown = true;
throw;
}
std::cerr << DB::getCurrentExceptionMessage(true, true /*check embedded stack trace*/) << std::endl;
bool got_expired = false;
for (const auto & connection : zookeepers)
{
if (connection->isExpired())
{
got_expired = true;
break;
}
}
if (got_expired)
{
while (true)
{
try
{
2023-04-07 13:02:42 +00:00
zookeepers = refreshConnections();
break;
}
catch (...)
{
std::cerr << DB::getCurrentExceptionMessage(true, true /*check embedded stack trace*/) << std::endl;
}
}
}
2021-04-13 12:36:47 +00:00
}
++requests_executed;
}
}
2023-04-07 14:14:39 +00:00
bool Runner::tryPushRequestInteractively(Coordination::ZooKeeperRequestPtr && request, DB::InterruptListener & interrupt_listener)
2021-04-13 12:36:47 +00:00
{
bool inserted = false;
while (!inserted)
{
2023-04-07 14:14:39 +00:00
inserted = queue->tryPush(std::move(request), 100);
2021-04-13 12:36:47 +00:00
if (shutdown)
{
/// An exception occurred in a worker
return false;
}
if (max_time > 0 && total_watch.elapsedSeconds() >= max_time)
{
2023-04-14 13:32:08 +00:00
std::cerr << "Stopping launch of queries. Requested time limit is exhausted.\n";
2021-04-13 12:36:47 +00:00
return false;
}
if (interrupt_listener.check())
{
2023-04-14 13:32:08 +00:00
std::cerr << "Stopping launch of queries. SIGINT received." << std::endl;
2021-04-13 12:36:47 +00:00
return false;
}
if (delay > 0 && delay_watch.elapsedSeconds() > delay)
{
printNumberOfRequestsExecuted(requests_executed);
std::lock_guard lock(mutex);
2023-04-14 13:32:08 +00:00
info->report(concurrency);
2021-04-13 12:36:47 +00:00
delay_watch.restart();
}
}
return true;
}
void Runner::runBenchmark()
{
2024-04-10 10:56:29 +00:00
if (generator)
runBenchmarkWithGenerator();
else
runBenchmarkFromLog();
}
struct ZooKeeperRequestBlock
{
explicit ZooKeeperRequestBlock(DB::Block block_)
: block(std::move(block_))
2024-05-10 12:15:01 +00:00
, hostname_idx(block.getPositionByName("hostname"))
, request_event_time_idx(block.getPositionByName("request_event_time"))
, thread_id_idx(block.getPositionByName("thread_id"))
, session_id_idx(block.getPositionByName("session_id"))
, xid_idx(block.getPositionByName("xid"))
2024-04-10 10:56:29 +00:00
, has_watch_idx(block.getPositionByName("has_watch"))
, op_num_idx(block.getPositionByName("op_num"))
, path_idx(block.getPositionByName("path"))
, data_idx(block.getPositionByName("data"))
, is_ephemeral_idx(block.getPositionByName("is_ephemeral"))
, is_sequential_idx(block.getPositionByName("is_sequential"))
2024-05-10 12:15:01 +00:00
, response_event_time_idx(block.getPositionByName("response_event_time"))
2024-04-10 10:56:29 +00:00
, error_idx(block.getPositionByName("error"))
, requests_size_idx(block.getPositionByName("requests_size"))
, version_idx(block.getPositionByName("version"))
{}
size_t rows() const
{
return block.rows();
}
UInt64 getExecutorId(size_t row) const
{
return getSessionId(row);
}
std::string getHostname(size_t row) const
{
return getField(hostname_idx, row).safeGet<std::string>();
}
UInt64 getThreadId(size_t row) const
{
return getField(thread_id_idx, row).safeGet<UInt64>();
}
DB::DateTime64 getRequestEventTime(size_t row) const
{
return getField(request_event_time_idx, row).safeGet<DB::DateTime64>();
}
DB::DateTime64 getResponseEventTime(size_t row) const
{
return getField(response_event_time_idx, row).safeGet<DB::DateTime64>();
}
Int64 getSessionId(size_t row) const
{
return getField(session_id_idx, row).safeGet<Int64>();
}
Int64 getXid(size_t row) const
{
return getField(xid_idx, row).safeGet<Int64>();
}
bool hasWatch(size_t row) const
{
return getField(has_watch_idx, row).safeGet<UInt8>();
}
Coordination::OpNum getOpNum(size_t row) const
{
return static_cast<Coordination::OpNum>(getField(op_num_idx, row).safeGet<Int64>());
}
bool isEphemeral(size_t row) const
{
return getField(is_ephemeral_idx, row).safeGet<UInt8>();
}
bool isSequential(size_t row) const
{
return getField(is_sequential_idx, row).safeGet<UInt8>();
}
std::string getPath(size_t row) const
{
return getField(path_idx, row).safeGet<std::string>();
}
std::string getData(size_t row) const
{
return getField(data_idx, row).safeGet<std::string>();
}
UInt64 getRequestsSize(size_t row) const
{
return getField(requests_size_idx, row).safeGet<UInt64>();
}
std::optional<Int32> getVersion(size_t row) const
{
auto field = getField(version_idx, row);
if (field.isNull())
return std::nullopt;
return static_cast<Int32>(field.safeGet<Int64>());
}
std::optional<Coordination::Error> getError(size_t row) const
{
auto field = getField(error_idx, row);
if (field.isNull())
return std::nullopt;
return static_cast<Coordination::Error>(field.safeGet<Int64>());
}
private:
DB::Field getField(size_t position, size_t row) const
{
DB::Field field;
block.getByPosition(position).column->get(row, field);
return field;
}
DB::Block block;
size_t hostname_idx = 0;
size_t request_event_time_idx = 0;
size_t thread_id_idx = 0;
size_t session_id_idx = 0;
size_t xid_idx = 0;
size_t has_watch_idx = 0;
size_t op_num_idx = 0;
size_t path_idx = 0;
size_t data_idx = 0;
size_t is_ephemeral_idx = 0;
size_t is_sequential_idx = 0;
size_t response_event_time_idx = 0;
size_t error_idx = 0;
size_t requests_size_idx = 0;
size_t version_idx = 0;
};
struct RequestFromLog
{
Coordination::ZooKeeperRequestPtr request;
std::optional<Coordination::Error> expected_result;
2024-05-10 12:15:01 +00:00
std::vector<std::optional<Coordination::Error>> subrequest_expected_results;
2024-04-10 10:56:29 +00:00
int64_t session_id = 0;
size_t executor_id = 0;
bool has_watch = false;
DB::DateTime64 request_event_time;
DB::DateTime64 response_event_time;
std::shared_ptr<Coordination::ZooKeeper> connection;
};
struct ZooKeeperRequestFromLogReader
{
ZooKeeperRequestFromLogReader(const std::string & input_request_log, DB::ContextPtr context)
{
std::optional<DB::FormatSettings> format_settings;
file_read_buf = std::make_unique<DB::ReadBufferFromFile>(input_request_log);
auto compression_method = DB::chooseCompressionMethod(input_request_log, "");
file_read_buf = DB::wrapReadBufferWithCompressionMethod(std::move(file_read_buf), compression_method);
DB::SingleReadBufferIterator read_buffer_iterator(std::move(file_read_buf));
auto [columns_description, format] = DB::detectFormatAndReadSchema(format_settings, read_buffer_iterator, context);
DB::ColumnsWithTypeAndName columns;
columns.reserve(columns_description.size());
for (const auto & column_description : columns_description)
columns.push_back(DB::ColumnWithTypeAndName{column_description.type, column_description.name});
header_block = std::move(columns);
file_read_buf
= DB::wrapReadBufferWithCompressionMethod(std::make_unique<DB::ReadBufferFromFile>(input_request_log), compression_method);
input_format = DB::FormatFactory::instance().getInput(
format,
*file_read_buf,
header_block,
context,
2024-09-18 18:35:52 +00:00
context->getSettingsRef()[DB::Setting::max_block_size],
2024-04-10 10:56:29 +00:00
format_settings,
1,
std::nullopt,
/*is_remote_fs*/ false,
DB::CompressionMethod::None,
false);
Coordination::ACL acl;
acl.permissions = Coordination::ACL::All;
acl.scheme = "world";
acl.id = "anyone";
default_acls.emplace_back(std::move(acl));
}
std::optional<RequestFromLog> getNextRequest(bool for_multi = false)
{
RequestFromLog request_from_log;
if (!current_block)
{
auto chunk = input_format->generate();
if (chunk.empty())
return std::nullopt;
current_block.emplace(header_block.cloneWithColumns(chunk.detachColumns()));
idx_in_block = 0;
}
request_from_log.expected_result = current_block->getError(idx_in_block);
request_from_log.session_id = current_block->getSessionId(idx_in_block);
request_from_log.has_watch = current_block->hasWatch(idx_in_block);
request_from_log.executor_id = current_block->getExecutorId(idx_in_block);
request_from_log.request_event_time = current_block->getRequestEventTime(idx_in_block);
request_from_log.response_event_time = current_block->getResponseEventTime(idx_in_block);
const auto move_row_iterator = [&]
{
if (idx_in_block == current_block->rows() - 1)
current_block.reset();
else
++idx_in_block;
};
auto op_num = current_block->getOpNum(idx_in_block);
switch (op_num)
{
case Coordination::OpNum::Create:
{
auto create_request = std::make_shared<Coordination::ZooKeeperCreateRequest>();
create_request->path = current_block->getPath(idx_in_block);
create_request->data = current_block->getData(idx_in_block);
create_request->is_ephemeral = current_block->isEphemeral(idx_in_block);
create_request->is_sequential = current_block->isSequential(idx_in_block);
request_from_log.request = create_request;
break;
}
case Coordination::OpNum::Set:
{
auto set_request = std::make_shared<Coordination::ZooKeeperSetRequest>();
set_request->path = current_block->getPath(idx_in_block);
set_request->data = current_block->getData(idx_in_block);
if (auto version = current_block->getVersion(idx_in_block))
2024-05-21 14:19:14 +00:00
{
/// we just need to make sure that the request with version that need to fail, fail when replaying
if (request_from_log.expected_result == Coordination::Error::ZBADVERSION)
set_request->version = std::numeric_limits<int32_t>::max();
}
2024-04-10 10:56:29 +00:00
request_from_log.request = set_request;
break;
}
case Coordination::OpNum::Remove:
{
auto remove_request = std::make_shared<Coordination::ZooKeeperRemoveRequest>();
remove_request->path = current_block->getPath(idx_in_block);
if (auto version = current_block->getVersion(idx_in_block))
2024-05-21 14:19:14 +00:00
{
/// we just need to make sure that the request with version that need to fail, fail when replaying
if (request_from_log.expected_result == Coordination::Error::ZBADVERSION)
remove_request->version = std::numeric_limits<int32_t>::max();
}
2024-04-10 10:56:29 +00:00
request_from_log.request = remove_request;
break;
}
case Coordination::OpNum::Check:
2024-05-07 12:40:45 +00:00
case Coordination::OpNum::CheckNotExists:
2024-04-10 10:56:29 +00:00
{
auto check_request = std::make_shared<Coordination::ZooKeeperCheckRequest>();
check_request->path = current_block->getPath(idx_in_block);
if (auto version = current_block->getVersion(idx_in_block))
2024-05-21 14:19:14 +00:00
{
/// we just need to make sure that the request with version that need to fail, fail when replaying
if (request_from_log.expected_result == Coordination::Error::ZBADVERSION)
check_request->version = std::numeric_limits<int32_t>::max();
}
2024-05-07 12:40:45 +00:00
if (op_num == Coordination::OpNum::CheckNotExists)
check_request->not_exists = true;
2024-04-10 10:56:29 +00:00
request_from_log.request = check_request;
break;
}
case Coordination::OpNum::Sync:
{
auto sync_request = std::make_shared<Coordination::ZooKeeperSyncRequest>();
sync_request->path = current_block->getPath(idx_in_block);
request_from_log.request = sync_request;
break;
}
case Coordination::OpNum::Get:
{
auto get_request = std::make_shared<Coordination::ZooKeeperGetRequest>();
get_request->path = current_block->getPath(idx_in_block);
request_from_log.request = get_request;
break;
}
case Coordination::OpNum::SimpleList:
case Coordination::OpNum::FilteredList:
{
auto list_request = std::make_shared<Coordination::ZooKeeperSimpleListRequest>();
list_request->path = current_block->getPath(idx_in_block);
request_from_log.request = list_request;
break;
}
case Coordination::OpNum::Exists:
{
auto exists_request = std::make_shared<Coordination::ZooKeeperExistsRequest>();
exists_request->path = current_block->getPath(idx_in_block);
request_from_log.request = exists_request;
break;
}
case Coordination::OpNum::Multi:
case Coordination::OpNum::MultiRead:
{
if (for_multi)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Nested multi requests are not allowed");
auto requests_size = current_block->getRequestsSize(idx_in_block);
Coordination::Requests requests;
requests.reserve(requests_size);
move_row_iterator();
for (size_t i = 0; i < requests_size; ++i)
{
auto subrequest_from_log = getNextRequest(/*for_multi=*/true);
if (!subrequest_from_log)
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Failed to fetch subrequest for {}, subrequest index {}", op_num, i);
2024-05-10 12:15:01 +00:00
if (!subrequest_from_log->expected_result && request_from_log.expected_result
&& request_from_log.expected_result == Coordination::Error::ZOK)
{
subrequest_from_log->expected_result = Coordination::Error::ZOK;
}
2024-04-10 10:56:29 +00:00
requests.push_back(std::move(subrequest_from_log->request));
if (subrequest_from_log->session_id != request_from_log.session_id)
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Session id mismatch for subrequest in {}, subrequest index {}", op_num, i);
if (subrequest_from_log->executor_id != request_from_log.executor_id)
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Executor id mismatch for subrequest in {}, subrequest index {}", op_num, i);
2024-05-10 12:15:01 +00:00
request_from_log.subrequest_expected_results.push_back(subrequest_from_log->expected_result);
2024-04-10 10:56:29 +00:00
}
request_from_log.request = std::make_shared<Coordination::ZooKeeperMultiRequest>(requests, default_acls);
return request_from_log;
}
default:
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Unsupported operation {} ({})", op_num, static_cast<int64_t>(op_num));
}
move_row_iterator();
return request_from_log;
}
private:
DB::Block header_block;
std::unique_ptr<DB::ReadBuffer> file_read_buf;
DB::InputFormatPtr input_format;
std::optional<ZooKeeperRequestBlock> current_block;
size_t idx_in_block = 0;
Coordination::ACLs default_acls;
};
namespace
{
struct RequestFromLogStats
{
struct Stats
{
std::atomic<size_t> total = 0;
std::atomic<size_t> unexpected_results = 0;
};
Stats write_requests;
Stats read_requests;
};
2024-05-10 12:15:01 +00:00
struct SetupNodeCollector
{
explicit SetupNodeCollector(const std::string & setup_nodes_snapshot_path)
{
if (setup_nodes_snapshot_path.empty())
return;
keeper_context = std::make_shared<DB::KeeperContext>(true, std::make_shared<Coordination::CoordinationSettings>());
keeper_context->setDigestEnabled(true);
keeper_context->setSnapshotDisk(
std::make_shared<DB::DiskLocal>("Keeper-snapshots", setup_nodes_snapshot_path));
snapshot_manager.emplace(1, keeper_context);
auto snapshot_result = snapshot_manager->restoreFromLatestSnapshot();
if (snapshot_result.storage == nullptr)
{
std::cerr << "No initial snapshot found" << std::endl;
initial_storage = std::make_unique<Coordination::KeeperMemoryStorage>(
2024-05-10 12:15:01 +00:00
/* tick_time_ms */ 500, /* superdigest */ "", keeper_context, /* initialize_system_nodes */ false);
initial_storage->initializeSystemNodes();
}
else
{
std::cerr << "Loaded initial nodes from snapshot" << std::endl;
initial_storage = std::move(snapshot_result.storage);
}
}
void processRequest(const RequestFromLog & request_from_log)
{
if (!request_from_log.expected_result.has_value())
return;
2024-05-21 14:19:14 +00:00
2024-05-10 12:15:01 +00:00
auto process_request = [&](const Coordination::ZooKeeperRequest & request, const auto expected_result)
{
const auto & path = request.getPath();
2024-05-21 14:19:14 +00:00
if (nodes_created_during_replay.contains(path))
2024-05-10 12:15:01 +00:00
return;
auto op_num = request.getOpNum();
if (op_num == Coordination::OpNum::Create)
{
if (expected_result == Coordination::Error::ZNODEEXISTS)
{
addExpectedNode(path);
}
else if (expected_result == Coordination::Error::ZOK)
{
2024-05-21 14:19:14 +00:00
nodes_created_during_replay.insert(path);
2024-05-10 12:15:01 +00:00
/// we need to make sure ancestors exist
auto position = path.find_last_of('/');
if (position != 0)
{
auto parent_path = path.substr(0, position);
2024-05-21 14:19:14 +00:00
addExpectedNode(parent_path);
2024-05-10 12:15:01 +00:00
}
}
}
else if (op_num == Coordination::OpNum::Remove)
{
2024-05-21 14:19:14 +00:00
if (expected_result == Coordination::Error::ZOK || expected_result == Coordination::Error::ZBADVERSION)
2024-05-10 12:15:01 +00:00
addExpectedNode(path);
}
else if (op_num == Coordination::OpNum::Set)
{
2024-05-21 14:19:14 +00:00
if (expected_result == Coordination::Error::ZOK || expected_result == Coordination::Error::ZBADVERSION)
2024-05-10 12:15:01 +00:00
addExpectedNode(path);
}
else if (op_num == Coordination::OpNum::Check)
{
2024-05-21 14:19:14 +00:00
if (expected_result == Coordination::Error::ZOK || expected_result == Coordination::Error::ZBADVERSION)
2024-05-10 12:15:01 +00:00
addExpectedNode(path);
}
else if (op_num == Coordination::OpNum::CheckNotExists)
{
2024-05-21 14:19:14 +00:00
if (expected_result == Coordination::Error::ZNODEEXISTS || expected_result == Coordination::Error::ZBADVERSION)
2024-05-10 12:15:01 +00:00
addExpectedNode(path);
}
else if (request.isReadRequest())
{
if (expected_result == Coordination::Error::ZOK)
addExpectedNode(path);
}
};
const auto & request = request_from_log.request;
if (request->getOpNum() == Coordination::OpNum::Multi || request->getOpNum() == Coordination::OpNum::MultiRead)
{
const auto & multi_request = dynamic_cast<const Coordination::ZooKeeperMultiRequest &>(*request);
const auto & subrequests = multi_request.requests;
for (size_t i = 0; i < subrequests.size(); ++i)
{
const auto & zookeeper_request = dynamic_cast<const Coordination::ZooKeeperRequest &>(*subrequests[i]);
const auto subrequest_expected_result = request_from_log.subrequest_expected_results[i];
if (subrequest_expected_result.has_value())
process_request(zookeeper_request, *subrequest_expected_result);
}
}
else
process_request(*request, *request_from_log.expected_result);
}
void addExpectedNode(const std::string & path)
{
std::lock_guard lock(nodes_mutex);
if (initial_storage->container.contains(path))
return;
2024-05-22 14:12:33 +00:00
new_nodes = true;
2024-05-10 12:15:01 +00:00
std::cerr << "Adding expected node " << path << std::endl;
Coordination::Requests create_ops;
size_t pos = 1;
while (true)
{
pos = path.find('/', pos);
if (pos == std::string::npos)
break;
auto request = zkutil::makeCreateRequest(path.substr(0, pos), "", zkutil::CreateMode::Persistent, true);
create_ops.emplace_back(request);
++pos;
}
auto request = zkutil::makeCreateRequest(path, "", zkutil::CreateMode::Persistent, true);
create_ops.emplace_back(request);
auto next_zxid = initial_storage->getNextZXID();
static Coordination::ACLs default_acls = []
{
Coordination::ACL acl;
acl.permissions = Coordination::ACL::All;
acl.scheme = "world";
acl.id = "anyone";
return Coordination::ACLs{std::move(acl)};
}();
auto multi_create_request = std::make_shared<Coordination::ZooKeeperMultiRequest>(create_ops, default_acls);
initial_storage->preprocessRequest(multi_create_request, 1, 0, next_zxid, /* check_acl = */ false);
auto responses = initial_storage->processRequest(multi_create_request, 1, next_zxid, /* check_acl = */ false);
if (responses.size() > 1 || responses[0].response->error != Coordination::Error::ZOK)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Invalid response after trying to create a node {}", responses[0].response->error);
}
void generateSnapshot()
{
std::lock_guard lock(nodes_mutex);
2024-05-22 14:12:33 +00:00
if (!new_nodes)
{
std::cerr << "No new nodes added" << std::endl;
return;
}
std::cerr << "Generating snapshot with starting data" << std::endl;
2024-05-10 12:15:01 +00:00
DB::SnapshotMetadataPtr snapshot_meta = std::make_shared<DB::SnapshotMetadata>(initial_storage->getZXID(), 1, std::make_shared<nuraft::cluster_config>());
DB::KeeperStorageSnapshot<Coordination::KeeperMemoryStorage> snapshot(initial_storage.get(), snapshot_meta);
2024-05-10 12:15:01 +00:00
snapshot_manager->serializeSnapshotToDisk(snapshot);
2024-05-22 14:12:33 +00:00
new_nodes = false;
2024-05-10 12:15:01 +00:00
}
std::mutex nodes_mutex;
DB::KeeperContextPtr keeper_context;
std::shared_ptr<Coordination::KeeperMemoryStorage> initial_storage;
2024-05-21 14:19:14 +00:00
std::unordered_set<std::string> nodes_created_during_replay;
std::optional<Coordination::KeeperSnapshotManager<Coordination::KeeperMemoryStorage>> snapshot_manager;
2024-05-22 14:12:33 +00:00
bool new_nodes = false;
2024-05-10 12:15:01 +00:00
};
2024-04-10 10:56:29 +00:00
void dumpStats(std::string_view type, const RequestFromLogStats::Stats & stats_for_type)
{
std::cerr << fmt::format(
"{} requests: {} total, {} with unexpected results ({:.4}%)",
type,
stats_for_type.total,
stats_for_type.unexpected_results,
2024-05-10 12:15:01 +00:00
stats_for_type.total != 0 ? static_cast<double>(stats_for_type.unexpected_results) / stats_for_type.total * 100 : 0.0)
2024-04-10 10:56:29 +00:00
<< std::endl;
};
void requestFromLogExecutor(std::shared_ptr<ConcurrentBoundedQueue<RequestFromLog>> queue, RequestFromLogStats & request_stats)
{
RequestFromLog request_from_log;
std::optional<std::future<void>> last_request;
while (queue->pop(request_from_log))
{
auto request_promise = std::make_shared<std::promise<void>>();
last_request = request_promise->get_future();
2024-05-10 12:15:01 +00:00
Coordination::ResponseCallback callback = [&,
request_promise,
request = request_from_log.request,
expected_result = request_from_log.expected_result,
subrequest_expected_results = std::move(request_from_log.subrequest_expected_results)](
const Coordination::Response & response) mutable
2024-04-10 10:56:29 +00:00
{
auto & stats = request->isReadRequest() ? request_stats.read_requests : request_stats.write_requests;
stats.total.fetch_add(1, std::memory_order_relaxed);
2024-05-10 12:15:01 +00:00
if (expected_result)
{
if (*expected_result != response.error)
stats.unexpected_results.fetch_add(1, std::memory_order_relaxed);
2024-05-22 14:12:33 +00:00
#if 0
if (*expected_result != response.error)
{
std::cerr << fmt::format(
"Unexpected result for {}\ngot {}, expected {}\n", request->toString(), response.error, *expected_result)
<< std::endl;
if (const auto * multi_response = dynamic_cast<const Coordination::ZooKeeperMultiResponse *>(&response))
{
std::string subresponses;
for (size_t i = 0; i < multi_response->responses.size(); ++i)
{
subresponses += fmt::format("{} = {}\n", i, multi_response->responses[i]->error);
}
std::cerr << "Subresponses\n" << subresponses << std::endl;
}
}
#endif
2024-05-10 12:15:01 +00:00
}
2024-04-10 10:56:29 +00:00
request_promise->set_value();
};
Coordination::WatchCallbackPtr watch;
if (request_from_log.has_watch)
watch = std::make_shared<Coordination::WatchCallback>([](const Coordination::WatchResponse &) {});
request_from_log.connection->executeGenericRequest(request_from_log.request, callback, watch);
}
if (last_request)
last_request->wait();
}
}
void Runner::runBenchmarkFromLog()
{
std::cerr << fmt::format("Running benchmark using requests from {}", input_request_log) << std::endl;
pool.emplace(CurrentMetrics::LocalThread, CurrentMetrics::LocalThreadActive, CurrentMetrics::LocalThreadScheduled, concurrency);
shared_context = DB::Context::createShared();
global_context = DB::Context::createGlobal(shared_context.get());
global_context->makeGlobalContext();
DB::registerFormats();
/// Randomly choosing connection index
pcg64 rng(randomSeed());
std::uniform_int_distribution<size_t> connection_distribution(0, connection_infos.size() - 1);
std::unordered_map<int64_t, std::shared_ptr<Coordination::ZooKeeper>> zookeeper_connections;
auto get_zookeeper_connection = [&](int64_t session_id)
{
if (auto it = zookeeper_connections.find(session_id); it != zookeeper_connections.end() && !it->second->isExpired())
return it->second;
auto connection_idx = connection_distribution(rng);
auto zk_connection = getConnection(connection_infos[connection_idx], connection_idx);
zookeeper_connections.insert_or_assign(session_id, zk_connection);
return zk_connection;
};
RequestFromLogStats stats;
2024-05-10 12:15:01 +00:00
std::optional<SetupNodeCollector> setup_nodes_collector;
if (!setup_nodes_snapshot_path.empty())
setup_nodes_collector.emplace(setup_nodes_snapshot_path);
2024-04-10 10:56:29 +00:00
std::unordered_map<uint64_t, std::shared_ptr<ConcurrentBoundedQueue<RequestFromLog>>> executor_id_to_queue;
2024-05-21 14:19:14 +00:00
SCOPE_EXIT_SAFE({
2024-04-10 10:56:29 +00:00
for (const auto & [executor_id, executor_queue] : executor_id_to_queue)
executor_queue->finish();
pool->wait();
2024-05-22 14:12:33 +00:00
if (setup_nodes_collector)
{
setup_nodes_collector->generateSnapshot();
}
else
{
dumpStats("Write", stats.write_requests);
dumpStats("Read", stats.read_requests);
}
2024-04-10 10:56:29 +00:00
});
auto push_request = [&](RequestFromLog request)
{
if (auto it = executor_id_to_queue.find(request.executor_id); it != executor_id_to_queue.end())
{
auto success = it->second->push(std::move(request));
if (!success)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Failed to push to the executor's queue");
return;
}
2024-05-10 12:15:01 +00:00
auto executor_queue = std::make_shared<ConcurrentBoundedQueue<RequestFromLog>>(std::numeric_limits<uint64_t>::max());
2024-04-10 10:56:29 +00:00
executor_id_to_queue.emplace(request.executor_id, executor_queue);
auto scheduled = pool->trySchedule([&, executor_queue]() mutable
{
requestFromLogExecutor(std::move(executor_queue), stats);
});
if (!scheduled)
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Failed to schedule worker, try to increase concurrency parameter");
auto success = executor_queue->push(std::move(request));
if (!success)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Failed to push to the executor's queue");
};
2024-05-10 12:15:01 +00:00
if (!setup_nodes_collector)
2024-04-10 10:56:29 +00:00
{
auto setup_connection = getConnection(connection_infos[0], 0);
benchmark_context.startup(*setup_connection);
}
ZooKeeperRequestFromLogReader request_reader(input_request_log, global_context);
2024-05-07 12:40:45 +00:00
delay_watch.restart();
2024-04-10 10:56:29 +00:00
while (auto request_from_log = request_reader.getNextRequest())
{
2024-05-10 12:15:01 +00:00
if (setup_nodes_collector)
{
setup_nodes_collector->processRequest(*request_from_log);
}
else
{
request_from_log->connection = get_zookeeper_connection(request_from_log->session_id);
2024-09-02 09:44:21 +00:00
request_from_log->executor_id %= concurrency;
2024-05-10 12:15:01 +00:00
push_request(std::move(*request_from_log));
}
2024-05-07 12:40:45 +00:00
if (delay > 0 && delay_watch.elapsedSeconds() > delay)
{
2024-05-10 12:15:01 +00:00
if (setup_nodes_collector)
setup_nodes_collector->generateSnapshot();
else
{
dumpStats("Write", stats.write_requests);
dumpStats("Read", stats.read_requests);
std::cerr << std::endl;
}
2024-05-07 12:40:45 +00:00
delay_watch.restart();
}
2024-04-10 10:56:29 +00:00
}
}
void Runner::runBenchmarkWithGenerator()
{
pool.emplace(CurrentMetrics::LocalThread, CurrentMetrics::LocalThreadActive, CurrentMetrics::LocalThreadScheduled, concurrency);
queue.emplace(concurrency);
2023-04-07 13:02:42 +00:00
createConnections();
std::cerr << "Preparing to run\n";
2024-04-10 10:56:29 +00:00
benchmark_context.startup(*connections[0]);
2023-04-07 13:02:42 +00:00
generator->startup(*connections[0]);
std::cerr << "Prepared\n";
2023-04-17 11:25:46 +00:00
2023-04-17 15:09:35 +00:00
auto start_timestamp_ms = Poco::Timestamp().epochMicroseconds() / 1000;
2023-04-17 11:25:46 +00:00
2021-04-13 12:36:47 +00:00
try
{
for (size_t i = 0; i < concurrency; ++i)
{
2023-04-07 13:02:42 +00:00
auto thread_connections = connections;
2024-05-10 12:15:01 +00:00
pool->scheduleOrThrowOnError([this, my_connections = std::move(thread_connections)]() mutable { thread(my_connections); });
2021-04-13 12:36:47 +00:00
}
}
catch (...)
{
shutdown = true;
2023-04-07 14:14:39 +00:00
pool->wait();
2021-04-13 12:36:47 +00:00
throw;
}
DB::InterruptListener interrupt_listener;
delay_watch.restart();
/// Push queries into queue
for (size_t i = 0; !max_iterations || i < max_iterations; ++i)
{
if (!tryPushRequestInteractively(generator->generate(), interrupt_listener))
{
shutdown = true;
break;
}
}
2023-04-07 14:14:39 +00:00
pool->wait();
2021-04-13 12:36:47 +00:00
total_watch.stop();
printNumberOfRequestsExecuted(requests_executed);
std::lock_guard lock(mutex);
2023-04-14 13:32:08 +00:00
info->report(concurrency);
2023-04-17 11:25:46 +00:00
DB::WriteBufferFromOwnString out;
info->writeJSON(out, concurrency, start_timestamp_ms);
auto output_string = std::move(out.str());
if (print_to_stdout)
std::cout << output_string << std::endl;
if (file_output)
{
auto path = *file_output;
if (output_file_with_timestamp)
{
auto filename = file_output->filename();
filename = fmt::format("{}_{}{}", filename.stem().generic_string(), start_timestamp_ms, filename.extension().generic_string());
path = file_output->parent_path() / filename;
}
std::cerr << "Storing output to " << path << std::endl;
DB::WriteBufferFromFile file_output_buffer(path);
DB::ReadBufferFromString read_buffer(output_string);
DB::copyData(read_buffer, file_output_buffer);
}
2021-04-13 12:36:47 +00:00
}
2023-04-07 13:02:42 +00:00
void Runner::createConnections()
2021-04-13 12:36:47 +00:00
{
2023-04-07 14:14:39 +00:00
DB::EventNotifier::init();
2023-04-14 13:32:08 +00:00
std::cerr << "---- Creating connections ---- " << std::endl;
2023-04-07 13:02:42 +00:00
for (size_t connection_info_idx = 0; connection_info_idx < connection_infos.size(); ++connection_info_idx)
{
const auto & connection_info = connection_infos[connection_info_idx];
2023-04-14 13:32:08 +00:00
std::cerr << fmt::format("Creating {} session(s) for:\n"
2023-04-07 13:02:42 +00:00
"- host: {}\n"
"- secure: {}\n"
"- session timeout: {}ms\n"
"- operation timeout: {}ms\n"
"- connection timeout: {}ms",
connection_info.sessions,
connection_info.host,
connection_info.secure,
connection_info.session_timeout_ms,
connection_info.operation_timeout_ms,
connection_info.connection_timeout_ms) << std::endl;
for (size_t session = 0; session < connection_info.sessions; ++session)
{
connections.emplace_back(getConnection(connection_info, connection_info_idx));
2023-04-07 13:02:42 +00:00
connections_to_info_map[connections.size() - 1] = connection_info_idx;
}
2021-04-13 12:36:47 +00:00
}
2023-04-14 13:32:08 +00:00
std::cerr << "---- Done creating connections ----\n" << std::endl;
2023-04-07 13:02:42 +00:00
}
2021-04-13 12:36:47 +00:00
std::shared_ptr<Coordination::ZooKeeper> Runner::getConnection(const ConnectionInfo & connection_info, size_t connection_info_idx)
2023-04-07 13:02:42 +00:00
{
2024-06-27 14:39:25 +00:00
zkutil::ShuffleHost host;
host.host = connection_info.host;
host.secure = connection_info.secure;
host.original_index = static_cast<UInt8>(connection_info_idx);
host.address = Poco::Net::SocketAddress{connection_info.host};
zkutil::ShuffleHosts nodes{host};
2023-04-07 13:02:42 +00:00
zkutil::ZooKeeperArgs args;
args.session_timeout_ms = connection_info.session_timeout_ms;
args.connection_timeout_ms = connection_info.connection_timeout_ms;
args.operation_timeout_ms = connection_info.operation_timeout_ms;
args.use_compression = connection_info.use_compression;
2023-04-07 13:02:42 +00:00
return std::make_shared<Coordination::ZooKeeper>(nodes, args, nullptr);
}
2023-04-07 13:02:42 +00:00
std::vector<std::shared_ptr<Coordination::ZooKeeper>> Runner::refreshConnections()
{
std::lock_guard lock(connection_mutex);
for (size_t connection_idx = 0; connection_idx < connections.size(); ++connection_idx)
{
auto & connection = connections[connection_idx];
if (connection->isExpired())
{
const auto & connection_info = connection_infos[connections_to_info_map[connection_idx]];
connection = getConnection(connection_info, connection_idx);
2023-04-07 13:02:42 +00:00
}
}
return connections;
2021-04-13 12:36:47 +00:00
}
2023-04-07 14:14:39 +00:00
Runner::~Runner()
{
2024-04-10 10:56:29 +00:00
if (queue)
queue->clearAndFinish();
2023-04-07 14:14:39 +00:00
shutdown = true;
2024-04-10 10:56:29 +00:00
if (pool)
pool->wait();
2024-05-21 14:19:14 +00:00
try
{
auto connection = getConnection(connection_infos[0], 0);
benchmark_context.cleanup(*connection);
}
catch (...)
{
DB::tryLogCurrentException("While trying to clean nodes");
}
2024-04-10 10:56:29 +00:00
}
namespace
{
void removeRecursive(Coordination::ZooKeeper & zookeeper, const std::string & path)
{
namespace fs = std::filesystem;
auto promise = std::make_shared<std::promise<void>>();
auto future = promise->get_future();
Strings children;
auto list_callback = [promise, &children] (const Coordination::ListResponse & response)
{
children = response.names;
promise->set_value();
};
zookeeper.list(path, Coordination::ListRequestType::ALL, list_callback, nullptr);
future.get();
std::span children_span(children);
while (!children_span.empty())
{
Coordination::Requests ops;
2024-07-04 15:59:07 +00:00
for (size_t i = 0; i < 1000 && !children_span.empty(); ++i)
2024-04-10 10:56:29 +00:00
{
2024-07-04 15:59:07 +00:00
removeRecursive(zookeeper, fs::path(path) / children_span.back());
2024-04-10 10:56:29 +00:00
ops.emplace_back(zkutil::makeRemoveRequest(fs::path(path) / children_span.back(), -1));
children_span = children_span.subspan(0, children_span.size() - 1);
}
auto multi_promise = std::make_shared<std::promise<void>>();
auto multi_future = multi_promise->get_future();
auto multi_callback = [multi_promise] (const Coordination::MultiResponse &)
{
multi_promise->set_value();
};
zookeeper.multi(ops, multi_callback);
multi_future.get();
}
auto remove_promise = std::make_shared<std::promise<void>>();
auto remove_future = remove_promise->get_future();
auto remove_callback = [remove_promise] (const Coordination::RemoveResponse &)
{
remove_promise->set_value();
};
zookeeper.remove(path, -1, remove_callback);
remove_future.get();
}
}
void BenchmarkContext::initializeFromConfig(const Poco::Util::AbstractConfiguration & config)
{
Coordination::ACL acl;
acl.permissions = Coordination::ACL::All;
acl.scheme = "world";
acl.id = "anyone";
default_acls.emplace_back(std::move(acl));
std::cerr << "---- Parsing setup ---- " << std::endl;
static const std::string setup_key = "setup";
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(setup_key, keys);
for (const auto & key : keys)
{
if (key.starts_with("node"))
{
auto node_key = setup_key + "." + key;
auto parsed_root_node = parseNode(node_key, config);
const auto node = root_nodes.emplace_back(parsed_root_node);
if (config.has(node_key + ".repeat"))
{
if (!node->name.isRandom())
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Repeating node creation for key {}, but name is not randomly generated", node_key);
auto repeat_count = config.getUInt64(node_key + ".repeat");
node->repeat_count = repeat_count;
for (size_t i = 1; i < repeat_count; ++i)
root_nodes.emplace_back(node->clone());
}
std::cerr << "Tree to create:" << std::endl;
node->dumpTree();
std::cerr << std::endl;
}
}
std::cerr << "---- Done parsing data setup ----\n" << std::endl;
}
std::shared_ptr<BenchmarkContext::Node> BenchmarkContext::parseNode(const std::string & key, const Poco::Util::AbstractConfiguration & config)
{
auto node = std::make_shared<BenchmarkContext::Node>();
node->name = StringGetter::fromConfig(key + ".name", config);
if (config.has(key + ".data"))
node->data = StringGetter::fromConfig(key + ".data", config);
Poco::Util::AbstractConfiguration::Keys node_keys;
config.keys(key, node_keys);
for (const auto & node_key : node_keys)
{
if (!node_key.starts_with("node"))
continue;
const auto node_key_string = key + "." + node_key;
auto child_node = parseNode(node_key_string, config);
node->children.push_back(child_node);
if (config.has(node_key_string + ".repeat"))
{
if (!child_node->name.isRandom())
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Repeating node creation for key {}, but name is not randomly generated", node_key_string);
auto repeat_count = config.getUInt64(node_key_string + ".repeat");
child_node->repeat_count = repeat_count;
for (size_t i = 1; i < repeat_count; ++i)
node->children.push_back(child_node);
}
}
return node;
}
void BenchmarkContext::Node::dumpTree(int level) const
{
std::string data_string
= data.has_value() ? fmt::format("{}", data->description()) : "no data";
std::string repeat_count_string = repeat_count != 0 ? fmt::format(", repeated {} times", repeat_count) : "";
std::cerr << fmt::format("{}name: {}, data: {}{}", std::string(level, '\t'), name.description(), data_string, repeat_count_string) << std::endl;
for (auto it = children.begin(); it != children.end();)
{
const auto & child = *it;
child->dumpTree(level + 1);
std::advance(it, child->repeat_count != 0 ? child->repeat_count : 1);
}
}
std::shared_ptr<BenchmarkContext::Node> BenchmarkContext::Node::clone() const
{
auto new_node = std::make_shared<Node>();
new_node->name = name;
new_node->data = data;
new_node->repeat_count = repeat_count;
// don't do deep copy of children because we will do clone only for root nodes
new_node->children = children;
return new_node;
}
void BenchmarkContext::Node::createNode(Coordination::ZooKeeper & zookeeper, const std::string & parent_path, const Coordination::ACLs & acls) const
{
auto path = std::filesystem::path(parent_path) / name.getString();
auto promise = std::make_shared<std::promise<void>>();
auto future = promise->get_future();
auto create_callback = [promise] (const Coordination::CreateResponse & response)
{
if (response.error != Coordination::Error::ZOK)
promise->set_exception(std::make_exception_ptr(zkutil::KeeperException(response.error)));
else
promise->set_value();
};
zookeeper.create(path, data ? data->getString() : "", false, false, acls, create_callback);
future.get();
for (const auto & child : children)
child->createNode(zookeeper, path, acls);
}
void BenchmarkContext::startup(Coordination::ZooKeeper & zookeeper)
{
if (root_nodes.empty())
return;
std::cerr << "---- Creating test data ----" << std::endl;
for (const auto & node : root_nodes)
{
auto node_name = node->name.getString();
node->name.setString(node_name);
std::string root_path = std::filesystem::path("/") / node_name;
std::cerr << "Cleaning up " << root_path << std::endl;
removeRecursive(zookeeper, root_path);
node->createNode(zookeeper, "/", default_acls);
}
std::cerr << "---- Created test data ----\n" << std::endl;
}
void BenchmarkContext::cleanup(Coordination::ZooKeeper & zookeeper)
{
if (root_nodes.empty())
return;
std::cerr << "---- Cleaning up test data ----" << std::endl;
for (const auto & node : root_nodes)
{
auto node_name = node->name.getString();
std::string root_path = std::filesystem::path("/") / node_name;
std::cerr << "Cleaning up " << root_path << std::endl;
removeRecursive(zookeeper, root_path);
}
2023-04-07 14:14:39 +00:00
}