2021-12-09 10:39:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <Storages/MergeTree/RequestResponse.h>
|
|
|
|
|
2022-11-14 05:09:03 +00:00
|
|
|
|
2021-12-09 10:39:28 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2023-10-12 20:34:26 +00:00
|
|
|
struct Progress;
|
|
|
|
using ProgressCallback = std::function<void(const Progress & progress)>;
|
2021-12-09 10:39:28 +00:00
|
|
|
|
2023-02-03 13:34:18 +00:00
|
|
|
/// The main class to spread mark ranges across replicas dynamically
|
2021-12-09 10:39:28 +00:00
|
|
|
class ParallelReplicasReadingCoordinator
|
|
|
|
{
|
|
|
|
public:
|
2023-02-03 13:34:18 +00:00
|
|
|
class ImplInterface;
|
|
|
|
|
|
|
|
explicit ParallelReplicasReadingCoordinator(size_t replicas_count_);
|
2021-12-09 10:39:28 +00:00
|
|
|
~ParallelReplicasReadingCoordinator();
|
2023-02-03 13:34:18 +00:00
|
|
|
|
|
|
|
void handleInitialAllRangesAnnouncement(InitialAllRangesAnnouncement);
|
|
|
|
ParallelReadResponse handleRequest(ParallelReadRequest request);
|
|
|
|
|
2023-05-31 20:10:33 +00:00
|
|
|
/// Called when some replica is unavailable and we skipped it.
|
|
|
|
/// This is needed to "finalize" reading state e.g. spread all the marks using
|
|
|
|
/// consistent hashing, because otherwise coordinator will continue working in
|
|
|
|
/// "pending" state waiting for the unavailable replica to send the announcement.
|
|
|
|
void markReplicaAsUnavailable(size_t replica_number);
|
|
|
|
|
2023-10-12 20:34:26 +00:00
|
|
|
/// needed to report total rows to read
|
|
|
|
void setProgressCallback(ProgressCallback callback);
|
|
|
|
|
2021-12-09 10:39:28 +00:00
|
|
|
private:
|
2023-02-03 13:34:18 +00:00
|
|
|
void initialize();
|
|
|
|
|
2023-04-05 11:32:46 +00:00
|
|
|
std::mutex mutex;
|
2023-02-03 13:34:18 +00:00
|
|
|
size_t replicas_count{0};
|
2023-04-05 11:32:46 +00:00
|
|
|
CoordinationMode mode{CoordinationMode::Default};
|
2023-02-03 13:34:18 +00:00
|
|
|
std::atomic<bool> initialized{false};
|
|
|
|
std::unique_ptr<ImplInterface> pimpl;
|
2023-10-17 20:09:39 +00:00
|
|
|
ProgressCallback progress_callback; // store the callback only to bypass it to coordinator implementation
|
2023-12-22 20:54:52 +00:00
|
|
|
std::set<size_t> replicas_used;
|
2023-12-19 15:41:51 +00:00
|
|
|
|
|
|
|
/// To initialize `pimpl` we need to know the coordinator mode. We can know it only from initial announcement or regular request.
|
|
|
|
/// The problem is `markReplicaAsUnavailable` might be called before any of these requests happened.
|
|
|
|
/// In this case we will remember the numbers of unavailable replicas and apply this knowledge later on initialization.
|
|
|
|
std::vector<size_t> unavailable_nodes_registered_before_initialization;
|
2021-12-09 10:39:28 +00:00
|
|
|
};
|
|
|
|
|
2022-06-02 09:46:33 +00:00
|
|
|
using ParallelReplicasReadingCoordinatorPtr = std::shared_ptr<ParallelReplicasReadingCoordinator>;
|
|
|
|
|
2021-12-09 10:39:28 +00:00
|
|
|
}
|