ClickHouse/utils/keeper-bench/Generator.h

108 lines
3.0 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>
#include <Common/randomSeed.h>
2021-04-12 08:10:23 +00:00
std::string generateRandomPath(const std::string & prefix, size_t length = 5);
std::string generateRandomData(size_t size);
class IGenerator
{
public:
2021-04-13 11:55:08 +00:00
IGenerator()
{
Coordination::ACL acl;
acl.permissions = Coordination::ACL::All;
acl.scheme = "world";
acl.id = "anyone";
default_acls.emplace_back(std::move(acl));
}
2021-04-12 15:40:42 +00:00
virtual void startup(Coordination::ZooKeeper & /*zookeeper*/) {}
virtual Coordination::ZooKeeperRequestPtr generate() = 0;
2021-04-13 11:55:08 +00:00
2021-04-12 08:10:23 +00:00
virtual ~IGenerator() = default;
2021-04-13 11:55:08 +00:00
Coordination::ACLs default_acls;
2021-04-12 08:10:23 +00:00
};
class CreateRequestGenerator final : public IGenerator
{
public:
explicit CreateRequestGenerator(
2021-04-13 11:55:08 +00:00
std::string path_prefix_ = "/create_generator",
2021-04-12 08:10:23 +00:00
std::optional<uint64_t> path_length_ = std::nullopt,
std::optional<uint64_t> data_size_ = std::nullopt)
: path_prefix(path_prefix_)
, path_length(path_length_)
, data_size(data_size_)
{}
2021-04-13 11:55:08 +00:00
void startup(Coordination::ZooKeeper & zookeeper) override;
2021-04-12 15:40:42 +00:00
Coordination::ZooKeeperRequestPtr generate() override;
2021-04-12 08:10:23 +00:00
private:
std::string path_prefix;
std::optional<uint64_t> path_length;
std::optional<uint64_t> data_size;
std::unordered_set<std::string> paths_created;
};
2021-04-13 11:55:08 +00:00
class GetRequestGenerator final : public IGenerator
{
public:
explicit GetRequestGenerator(
std::string path_prefix_ = "/get_generator",
std::optional<uint64_t> num_nodes_ = std::nullopt,
std::optional<uint64_t> nodes_data_size_ = std::nullopt)
: path_prefix(path_prefix_)
, num_nodes(num_nodes_)
, nodes_data_size(nodes_data_size_)
, rng(randomSeed())
, distribution(0, num_nodes ? *num_nodes - 1 : 0)
{}
void startup(Coordination::ZooKeeper & zookeeper) override;
Coordination::ZooKeeperRequestPtr generate() override;
private:
std::string path_prefix;
std::optional<uint64_t> num_nodes;
std::optional<uint64_t> nodes_data_size;
std::vector<std::string> paths_to_get;
pcg64 rng;
std::uniform_int_distribution<size_t> distribution;
};
class ListRequestGenerator final : public IGenerator
{
public:
explicit ListRequestGenerator(
std::string path_prefix_ = "/list_generator",
std::optional<uint64_t> num_nodes_ = std::nullopt,
std::optional<uint64_t> paths_length_ = std::nullopt)
: path_prefix(path_prefix_)
, num_nodes(num_nodes_)
, paths_length(paths_length_)
{}
void startup(Coordination::ZooKeeper & zookeeper) override;
Coordination::ZooKeeperRequestPtr generate() override;
private:
std::string path_prefix;
std::optional<uint64_t> num_nodes;
std::optional<uint64_t> paths_length;
};
std::unique_ptr<IGenerator> getGenerator(const std::string & name);