ClickHouse/utils/keeper-bench/Generator.h

200 lines
6.4 KiB
C++
Raw Normal View History

2021-04-12 08:10:23 +00:00
#pragma once
#include <Common/ZooKeeper/KeeperException.h>
#include <Common/ZooKeeper/ZooKeeper.h>
2021-04-12 15:40:42 +00:00
#include <Common/ZooKeeper/ZooKeeperImpl.h>
2021-04-12 08:10:23 +00:00
#include <Common/ZooKeeper/ZooKeeperCommon.h>
#include <functional>
#include <optional>
2021-04-13 11:55:08 +00:00
#include <pcg-random/pcg_random.hpp>
2023-04-06 15:10:58 +00:00
#include <Poco/Util/AbstractConfiguration.h>
2021-04-13 11:55:08 +00:00
#include <Common/randomSeed.h>
2023-04-06 15:10:58 +00:00
struct NumberGetter
2021-04-12 08:10:23 +00:00
{
2023-04-06 15:10:58 +00:00
static NumberGetter fromConfig(const std::string & key, const Poco::Util::AbstractConfiguration & config, std::optional<uint64_t> default_value = std::nullopt);
uint64_t getNumber() const;
std::string description() const;
private:
struct NumberRange
2021-04-13 11:55:08 +00:00
{
2023-04-06 15:10:58 +00:00
uint64_t min_value;
uint64_t max_value;
};
2021-04-13 11:55:08 +00:00
2023-04-06 15:10:58 +00:00
std::variant<uint64_t, NumberRange> value;
2021-04-12 08:10:23 +00:00
};
2023-04-06 15:10:58 +00:00
struct StringGetter
2021-04-12 08:10:23 +00:00
{
2023-04-06 15:10:58 +00:00
explicit StringGetter(NumberGetter number_getter)
: value(std::move(number_getter))
2021-04-12 08:10:23 +00:00
{}
2023-04-06 15:10:58 +00:00
StringGetter() = default;
2021-04-12 08:10:23 +00:00
2023-04-06 15:10:58 +00:00
static StringGetter fromConfig(const std::string & key, const Poco::Util::AbstractConfiguration & config);
void setString(std::string name);
std::string getString() const;
std::string description() const;
bool isRandom() const;
2021-04-12 08:10:23 +00:00
private:
2023-04-06 15:10:58 +00:00
std::variant<std::string, NumberGetter> value;
2021-04-12 08:10:23 +00:00
};
2021-04-13 11:55:08 +00:00
2023-04-06 19:17:35 +00:00
struct PathGetter
{
static PathGetter fromConfig(const std::string & key, const Poco::Util::AbstractConfiguration & config);
std::string getPath() const;
std::string description() const;
void initialize(Coordination::ZooKeeper & zookeeper);
private:
std::vector<std::string> parent_paths;
bool initialized = false;
std::vector<std::string> paths;
mutable std::uniform_int_distribution<size_t> path_picker;
};
2023-04-06 15:10:58 +00:00
struct RequestGenerator
2021-04-13 11:55:08 +00:00
{
2023-04-06 15:10:58 +00:00
virtual ~RequestGenerator() = default;
2021-04-13 11:55:08 +00:00
2023-04-06 15:10:58 +00:00
void getFromConfig(const std::string & key, const Poco::Util::AbstractConfiguration & config);
2021-04-13 11:55:08 +00:00
2023-04-06 15:10:58 +00:00
Coordination::ZooKeeperRequestPtr generate(const Coordination::ACLs & acls);
2021-04-13 11:55:08 +00:00
2023-04-06 15:10:58 +00:00
std::string description();
2023-04-06 19:17:35 +00:00
void startup(Coordination::ZooKeeper & zookeeper);
2023-04-07 13:02:42 +00:00
size_t getWeight() const;
2023-04-06 15:10:58 +00:00
private:
virtual void getFromConfigImpl(const std::string & key, const Poco::Util::AbstractConfiguration & config) = 0;
virtual std::string descriptionImpl() = 0;
virtual Coordination::ZooKeeperRequestPtr generateImpl(const Coordination::ACLs & acls) = 0;
2023-04-06 19:17:35 +00:00
virtual void startupImpl(Coordination::ZooKeeper &) {}
2023-04-07 13:02:42 +00:00
size_t weight = 1;
2021-04-13 11:55:08 +00:00
};
2023-04-07 13:02:42 +00:00
using RequestGeneratorPtr = std::shared_ptr<RequestGenerator>;
2021-04-13 11:55:08 +00:00
2023-04-06 15:10:58 +00:00
struct CreateRequestGenerator final : public RequestGenerator
{
CreateRequestGenerator();
2021-04-13 11:55:08 +00:00
private:
2023-04-06 15:10:58 +00:00
void getFromConfigImpl(const std::string & key, const Poco::Util::AbstractConfiguration & config) override;
std::string descriptionImpl() override;
Coordination::ZooKeeperRequestPtr generateImpl(const Coordination::ACLs & acls) override;
2023-04-06 19:17:35 +00:00
void startupImpl(Coordination::ZooKeeper & zookeeper) override;
2021-04-13 11:55:08 +00:00
2023-04-06 19:17:35 +00:00
PathGetter parent_path;
2023-04-06 15:10:58 +00:00
StringGetter name;
std::optional<StringGetter> data;
2022-01-20 17:06:34 +00:00
2023-04-07 14:51:25 +00:00
std::optional<double> remove_factor;
2023-04-06 15:10:58 +00:00
pcg64 rng;
std::uniform_real_distribution<double> remove_picker;
2022-01-20 17:06:34 +00:00
2023-04-06 15:10:58 +00:00
std::unordered_set<std::string> paths_created;
2022-01-20 17:06:34 +00:00
};
2023-04-06 19:17:35 +00:00
struct SetRequestGenerator final : public RequestGenerator
{
private:
void getFromConfigImpl(const std::string & key, const Poco::Util::AbstractConfiguration & config) override;
std::string descriptionImpl() override;
Coordination::ZooKeeperRequestPtr generateImpl(const Coordination::ACLs & acls) override;
void startupImpl(Coordination::ZooKeeper & zookeeper) override;
PathGetter path;
StringGetter data;
};
struct GetRequestGenerator final : public RequestGenerator
{
private:
void getFromConfigImpl(const std::string & key, const Poco::Util::AbstractConfiguration & config) override;
std::string descriptionImpl() override;
Coordination::ZooKeeperRequestPtr generateImpl(const Coordination::ACLs & acls) override;
void startupImpl(Coordination::ZooKeeper & zookeeper) override;
PathGetter path;
};
struct ListRequestGenerator final : public RequestGenerator
{
private:
void getFromConfigImpl(const std::string & key, const Poco::Util::AbstractConfiguration & config) override;
std::string descriptionImpl() override;
Coordination::ZooKeeperRequestPtr generateImpl(const Coordination::ACLs & acls) override;
void startupImpl(Coordination::ZooKeeper & zookeeper) override;
PathGetter path;
};
2023-04-07 13:02:42 +00:00
struct RequestGetter
{
2023-04-07 15:20:36 +00:00
explicit RequestGetter(std::vector<RequestGeneratorPtr> request_generators_);
RequestGetter() = default;
2023-04-07 13:02:42 +00:00
static RequestGetter fromConfig(const std::string & key, const Poco::Util::AbstractConfiguration & config, bool for_multi = false);
RequestGeneratorPtr getRequestGenerator() const;
std::string description() const;
void startup(Coordination::ZooKeeper & zookeeper);
const std::vector<RequestGeneratorPtr> & requestGenerators() const;
private:
std::vector<RequestGeneratorPtr> request_generators;
std::vector<size_t> weights;
mutable std::uniform_int_distribution<size_t> request_generator_picker;
};
struct MultiRequestGenerator final : public RequestGenerator
{
private:
void getFromConfigImpl(const std::string & key, const Poco::Util::AbstractConfiguration & config) override;
std::string descriptionImpl() override;
Coordination::ZooKeeperRequestPtr generateImpl(const Coordination::ACLs & acls) override;
void startupImpl(Coordination::ZooKeeper & zookeeper) override;
std::optional<NumberGetter> size;
RequestGetter request_getter;
};
2023-04-06 15:10:58 +00:00
class Generator
2023-03-24 14:37:51 +00:00
{
public:
2023-04-06 15:10:58 +00:00
explicit Generator(const Poco::Util::AbstractConfiguration & config);
2023-03-24 14:37:51 +00:00
2023-04-06 15:10:58 +00:00
void startup(Coordination::ZooKeeper & zookeeper);
Coordination::ZooKeeperRequestPtr generate();
2023-04-07 14:51:25 +00:00
void cleanup(Coordination::ZooKeeper & zookeeper);
2023-03-24 14:37:51 +00:00
private:
2023-04-06 15:10:58 +00:00
struct Node
{
StringGetter name;
std::optional<StringGetter> data;
std::vector<std::shared_ptr<Node>> children;
2023-04-14 13:32:08 +00:00
size_t repeat_count = 0;
std::shared_ptr<Node> clone() const;
2023-04-06 15:10:58 +00:00
void createNode(Coordination::ZooKeeper & zookeeper, const std::string & parent_path, const Coordination::ACLs & acls) const;
void dumpTree(int level = 0) const;
};
2023-03-24 14:37:51 +00:00
2023-04-06 15:10:58 +00:00
static std::shared_ptr<Node> parseNode(const std::string & key, const Poco::Util::AbstractConfiguration & config);
2022-01-20 17:06:34 +00:00
2023-04-06 15:10:58 +00:00
std::uniform_int_distribution<size_t> request_picker;
std::vector<std::shared_ptr<Node>> root_nodes;
2023-04-07 13:02:42 +00:00
RequestGetter request_getter;
2023-04-06 15:10:58 +00:00
Coordination::ACLs default_acls;
};
2023-04-06 10:25:37 +00:00
2023-04-07 15:20:36 +00:00
std::optional<Generator> getGenerator(const std::string & name);