2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
|
|
|
#include "td/db/DbKey.h"
|
|
|
|
|
|
|
|
#include "td/utils/AesCtrByteFlow.h"
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/BufferedFd.h"
|
|
|
|
#include "td/utils/ByteFlow.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/crypto.h"
|
2021-06-23 02:22:23 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/FileFd.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2020-06-10 20:48:11 +02:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-02 16:21:31 +01:00
|
|
|
|
2021-06-16 04:23:22 +02:00
|
|
|
extern int32 VERBOSITY_NAME(binlog);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct BinlogInfo {
|
2018-10-26 16:11:20 +02:00
|
|
|
bool was_created{false};
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 last_event_id{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_encrypted{false};
|
|
|
|
bool wrong_password{false};
|
|
|
|
bool is_opened{false};
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class BinlogReader;
|
|
|
|
class BinlogEventsProcessor;
|
|
|
|
class BinlogEventsBuffer;
|
2018-07-18 03:30:29 +02:00
|
|
|
} // namespace detail
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class Binlog {
|
|
|
|
public:
|
2022-12-30 21:13:40 +01:00
|
|
|
enum class Error : int { WrongPassword = -1037284 };
|
2018-12-31 20:04:05 +01:00
|
|
|
Binlog();
|
|
|
|
Binlog(const Binlog &other) = delete;
|
|
|
|
Binlog &operator=(const Binlog &other) = delete;
|
|
|
|
Binlog(Binlog &&other) = delete;
|
|
|
|
Binlog &operator=(Binlog &&other) = delete;
|
|
|
|
~Binlog();
|
|
|
|
|
|
|
|
using Callback = std::function<void(const BinlogEvent &)>;
|
|
|
|
Status init(string path, const Callback &callback, DbKey db_key = DbKey::empty(), DbKey old_db_key = DbKey::empty(),
|
|
|
|
int32 dummy = -1, const Callback &debug_callback = Callback()) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 next_event_id() {
|
|
|
|
return ++last_event_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 next_event_id(int32 shift) {
|
|
|
|
auto res = last_event_id_ + 1;
|
|
|
|
last_event_id_ += shift;
|
2018-12-31 20:04:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 peek_next_event_id() const {
|
|
|
|
return last_event_id_ + 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return fd_.empty();
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:48:11 +02:00
|
|
|
uint64 add(int32 type, const Storer &storer) {
|
2023-01-16 10:47:37 +01:00
|
|
|
auto event_id = next_event_id();
|
|
|
|
add_raw_event(BinlogEvent::create_raw(event_id, type, 0, storer), {});
|
|
|
|
return event_id;
|
2020-06-10 20:48:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 rewrite(uint64 event_id, int32 type, const Storer &storer) {
|
|
|
|
auto seq_no = next_event_id();
|
|
|
|
add_raw_event(BinlogEvent::create_raw(event_id, type, BinlogEvent::Flags::Rewrite, storer), {});
|
2020-06-10 20:48:11 +02:00
|
|
|
return seq_no;
|
|
|
|
}
|
|
|
|
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 erase(uint64 event_id) {
|
|
|
|
auto seq_no = next_event_id();
|
|
|
|
add_raw_event(
|
|
|
|
BinlogEvent::create_raw(event_id, BinlogEvent::ServiceTypes::Empty, BinlogEvent::Flags::Rewrite, EmptyStorer()),
|
|
|
|
{});
|
2020-06-10 20:48:11 +02:00
|
|
|
return seq_no;
|
|
|
|
}
|
2018-06-28 17:12:20 +02:00
|
|
|
|
2023-02-12 01:18:22 +01:00
|
|
|
uint64 erase_batch(vector<uint64> event_ids) {
|
|
|
|
if (event_ids.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-10 15:14:14 +01:00
|
|
|
auto seq_no = next_event_id(0);
|
|
|
|
for (auto event_id : event_ids) {
|
|
|
|
erase(event_id);
|
|
|
|
}
|
|
|
|
return seq_no;
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:12:20 +02:00
|
|
|
void add_raw_event(BufferSlice &&raw_event, BinlogDebugInfo info) {
|
|
|
|
add_event(BinlogEvent(std::move(raw_event), info));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void add_event(BinlogEvent &&event);
|
|
|
|
void sync();
|
|
|
|
void flush();
|
|
|
|
void lazy_flush();
|
|
|
|
double need_flush_since() const {
|
|
|
|
return need_flush_since_;
|
|
|
|
}
|
|
|
|
void change_key(DbKey new_db_key);
|
|
|
|
|
|
|
|
Status close(bool need_sync = true) TD_WARN_UNUSED_RESULT;
|
2020-07-14 19:54:38 +02:00
|
|
|
void close(Promise<> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
Status close_and_destroy() TD_WARN_UNUSED_RESULT;
|
|
|
|
static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
CSlice get_path() const {
|
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
BinlogInfo get_info() const { // works even after binlog was closed
|
|
|
|
return info_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BufferedFdBase<FileFd> fd_;
|
|
|
|
ChainBufferWriter buffer_writer_;
|
|
|
|
ChainBufferReader buffer_reader_;
|
2018-10-26 16:11:20 +02:00
|
|
|
detail::BinlogReader *binlog_reader_ptr_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
BinlogInfo info_;
|
|
|
|
DbKey db_key_;
|
|
|
|
bool db_key_used_ = false;
|
|
|
|
DbKey old_db_key_;
|
|
|
|
enum class EncryptionType { None, AesCtr } encryption_type_ = EncryptionType::None;
|
|
|
|
|
|
|
|
// AesCtrEncryption
|
2023-02-01 21:46:54 +01:00
|
|
|
string aes_ctr_key_salt_;
|
2021-12-01 14:01:27 +01:00
|
|
|
UInt256 aes_ctr_key_;
|
2018-12-31 20:04:05 +01:00
|
|
|
AesCtrState aes_ctr_state_;
|
|
|
|
|
|
|
|
bool byte_flow_flag_ = false;
|
|
|
|
ByteFlowSource byte_flow_source_;
|
|
|
|
ByteFlowSink byte_flow_sink_;
|
|
|
|
AesCtrByteFlow aes_xcode_byte_flow_;
|
|
|
|
|
|
|
|
int64 fd_size_{0};
|
|
|
|
uint64 fd_events_{0};
|
|
|
|
string path_;
|
2023-02-01 21:46:54 +01:00
|
|
|
vector<BinlogEvent> pending_events_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<detail::BinlogEventsProcessor> processor_;
|
|
|
|
unique_ptr<detail::BinlogEventsBuffer> events_buffer_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool in_flush_events_buffer_{false};
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 last_event_id_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
double need_flush_since_ = 0;
|
2018-02-16 10:13:04 +01:00
|
|
|
bool need_sync_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
enum class State { Empty, Load, Reindex, Run } state_{State::Empty};
|
|
|
|
|
2021-10-18 18:26:14 +02:00
|
|
|
static Result<FileFd> open_binlog(const string &path, int32 flags);
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t flush_events_buffer(bool force);
|
|
|
|
void do_add_event(BinlogEvent &&event);
|
|
|
|
void do_event(BinlogEvent &&event);
|
|
|
|
Status load_binlog(const Callback &callback, const Callback &debug_callback = Callback()) TD_WARN_UNUSED_RESULT;
|
|
|
|
void do_reindex();
|
|
|
|
|
|
|
|
void update_encryption(Slice key, Slice iv);
|
|
|
|
void reset_encryption();
|
|
|
|
void update_read_encryption();
|
|
|
|
void update_write_encryption();
|
2019-01-02 16:21:31 +01:00
|
|
|
|
|
|
|
string debug_get_binlog_data(int64 begin_offset, int64 end_offset);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-01-02 16:21:31 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|