2021-01-01 14:43:11 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-19 15:29:22 +00:00
|
|
|
#include "PostgreSQLConnection.h"
|
2021-02-03 16:13:18 +00:00
|
|
|
#include "PostgreSQLReplicaMetadata.h"
|
2021-02-06 12:28:42 +00:00
|
|
|
#include "pqxx/pqxx"
|
|
|
|
|
|
|
|
#include <Core/BackgroundSchedulePool.h>
|
2021-01-27 21:46:19 +00:00
|
|
|
#include <common/logger_useful.h>
|
2021-01-31 19:03:03 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Storages/PostgreSQL/insertPostgreSQLValue.h>
|
2021-02-08 19:32:30 +00:00
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
2021-01-01 14:43:11 +00:00
|
|
|
|
|
|
|
|
2021-02-06 12:28:42 +00:00
|
|
|
namespace DB
|
2021-01-27 21:46:19 +00:00
|
|
|
{
|
|
|
|
|
2021-01-01 14:43:11 +00:00
|
|
|
class PostgreSQLReplicaConsumer
|
|
|
|
{
|
|
|
|
public:
|
2021-02-08 19:32:30 +00:00
|
|
|
using Storages = std::unordered_map<String, StoragePtr>;
|
|
|
|
|
2021-01-01 14:43:11 +00:00
|
|
|
PostgreSQLReplicaConsumer(
|
2021-01-27 21:46:19 +00:00
|
|
|
std::shared_ptr<Context> context_,
|
2021-02-04 17:17:16 +00:00
|
|
|
PostgreSQLConnectionPtr connection_,
|
2021-01-01 14:43:11 +00:00
|
|
|
const std::string & replication_slot_name_,
|
|
|
|
const std::string & publication_name_,
|
2021-02-03 16:13:18 +00:00
|
|
|
const std::string & metadata_path,
|
2021-02-06 12:28:42 +00:00
|
|
|
const std::string & start_lsn,
|
2021-01-31 19:03:03 +00:00
|
|
|
const size_t max_block_size_,
|
2021-02-08 19:32:30 +00:00
|
|
|
Storages storages_);
|
2021-01-01 14:43:11 +00:00
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
/// Start reading WAL from current_lsn position. Initial data sync from created snapshot already done.
|
|
|
|
void startSynchronization();
|
|
|
|
void stopSynchronization();
|
2021-01-01 14:43:11 +00:00
|
|
|
|
|
|
|
private:
|
2021-01-31 19:03:03 +00:00
|
|
|
/// Executed by wal_reader_task. A separate thread reads wal and advances lsn to last commited position
|
|
|
|
/// after rows were written via copyData.
|
|
|
|
void replicationStream();
|
|
|
|
void stopReplicationStream();
|
2021-01-27 21:46:19 +00:00
|
|
|
|
2021-02-03 16:13:18 +00:00
|
|
|
enum class PostgreSQLQuery
|
|
|
|
{
|
|
|
|
INSERT,
|
|
|
|
UPDATE,
|
|
|
|
DELETE
|
|
|
|
};
|
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
bool readFromReplicationSlot();
|
2021-02-08 19:32:30 +00:00
|
|
|
void syncTables(std::shared_ptr<pqxx::nontransaction> tx, const std::unordered_set<std::string> & tables_to_sync);
|
|
|
|
String advanceLSN(std::shared_ptr<pqxx::nontransaction> ntx);
|
2021-01-27 21:46:19 +00:00
|
|
|
|
2021-02-08 19:32:30 +00:00
|
|
|
void processReplicationMessage(
|
|
|
|
const char * replication_message, size_t size, std::unordered_set<std::string> & tables_to_sync);
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2021-02-08 19:32:30 +00:00
|
|
|
struct BufferData
|
|
|
|
{
|
|
|
|
ExternalResultDescription description;
|
|
|
|
MutableColumns columns;
|
|
|
|
/// Needed for insertPostgreSQLValue() method to parse array
|
|
|
|
std::unordered_map<size_t, PostgreSQLArrayInfo> array_info;
|
|
|
|
|
|
|
|
BufferData(const Block block)
|
|
|
|
{
|
|
|
|
description.init(block);
|
|
|
|
columns = description.sample_block.cloneEmptyColumns();
|
|
|
|
for (const auto idx : ext::range(0, description.sample_block.columns()))
|
|
|
|
if (description.types[idx].first == ExternalResultDescription::ValueType::vtArray)
|
|
|
|
preparePostgreSQLArrayInfo(array_info, idx, description.sample_block.getByPosition(idx).type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using Buffers = std::unordered_map<String, BufferData>;
|
|
|
|
|
|
|
|
void insertDefaultValue(BufferData & buffer, size_t column_idx);
|
|
|
|
void insertValue(BufferData & buffer, const std::string & value, size_t column_idx);
|
|
|
|
void readTupleData(BufferData & buffer, const char * message, size_t & pos, PostgreSQLQuery type, bool old_value = false);
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
/// Methods to parse replication message data.
|
2021-01-19 15:29:22 +00:00
|
|
|
void readString(const char * message, size_t & pos, size_t size, String & result);
|
|
|
|
Int64 readInt64(const char * message, size_t & pos);
|
|
|
|
Int32 readInt32(const char * message, size_t & pos);
|
|
|
|
Int16 readInt16(const char * message, size_t & pos);
|
|
|
|
Int8 readInt8(const char * message, size_t & pos);
|
2021-01-27 15:29:28 +00:00
|
|
|
|
2021-01-01 14:43:11 +00:00
|
|
|
Poco::Logger * log;
|
2021-01-27 21:46:19 +00:00
|
|
|
std::shared_ptr<Context> context;
|
2021-01-01 14:43:11 +00:00
|
|
|
const std::string replication_slot_name;
|
|
|
|
const std::string publication_name;
|
2021-02-03 16:13:18 +00:00
|
|
|
PostgreSQLReplicaMetadata metadata;
|
2021-01-01 14:43:11 +00:00
|
|
|
|
2021-02-04 17:17:16 +00:00
|
|
|
PostgreSQLConnectionPtr connection;
|
2021-01-01 14:43:11 +00:00
|
|
|
|
2021-02-06 12:28:42 +00:00
|
|
|
std::string current_lsn, final_lsn;
|
2021-02-08 19:32:30 +00:00
|
|
|
const size_t max_block_size;
|
|
|
|
std::string table_to_insert;
|
|
|
|
|
2021-01-27 15:29:28 +00:00
|
|
|
BackgroundSchedulePool::TaskHolder wal_reader_task;
|
2021-01-27 21:46:19 +00:00
|
|
|
std::atomic<bool> stop_synchronization = false;
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2021-02-08 19:32:30 +00:00
|
|
|
Storages storages;
|
|
|
|
Buffers buffers;
|
2021-01-01 14:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|