2019-08-07 13:01:22 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/db/TQueue.h"
|
|
|
|
|
2019-09-06 17:55:19 +02:00
|
|
|
#include "td/db/binlog/Binlog.h"
|
|
|
|
#include "td/db/binlog/BinlogHelper.h"
|
2020-06-11 15:43:26 +02:00
|
|
|
#include "td/db/binlog/BinlogInterface.h"
|
2019-09-06 17:55:19 +02:00
|
|
|
|
2020-06-11 15:43:26 +02:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/port/Clocks.h"
|
2019-08-07 13:01:22 +02:00
|
|
|
#include "td/utils/Random.h"
|
2020-06-11 15:43:26 +02:00
|
|
|
#include "td/utils/StorerBase.h"
|
|
|
|
#include "td/utils/Time.h"
|
2019-08-07 13:01:22 +02:00
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-09-06 17:55:19 +02:00
|
|
|
#include "td/utils/VectorQueue.h"
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2020-06-11 15:43:26 +02:00
|
|
|
#include <algorithm>
|
2019-08-07 13:01:22 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
using EventId = TQueue::EventId;
|
|
|
|
|
2019-08-07 17:29:47 +02:00
|
|
|
static constexpr int32 MAX_DELAY = 7 * 86400;
|
|
|
|
static constexpr size_t MAX_EVENT_LEN = 65536 * 8;
|
|
|
|
static constexpr size_t MAX_QUEUE_EVENTS = 1000000;
|
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
EventId::EventId() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<EventId> EventId::from_int32(int32 id) {
|
|
|
|
if (!is_valid(id)) {
|
2020-06-11 15:43:26 +02:00
|
|
|
return Status::Error("Invalid ID");
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
return EventId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventId EventId::create_random() {
|
2019-08-07 17:29:47 +02:00
|
|
|
return from_int32(Random::fast_uint32() % (MAX_ID / 2) + 10 + MAX_QUEUE_EVENTS).move_as_ok();
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 EventId::value() const {
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<EventId> EventId::next() const {
|
|
|
|
return from_int32(id_ + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<EventId> EventId::advance(size_t offset) const {
|
|
|
|
TRY_RESULT(new_id, narrow_cast_safe<int32>(id_ + offset));
|
|
|
|
return from_int32(new_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventId::empty() const {
|
|
|
|
return id_ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventId::operator==(const EventId &other) const {
|
|
|
|
return id_ == other.id_;
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:12:16 +02:00
|
|
|
bool EventId::operator!=(const EventId &other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:06:00 +02:00
|
|
|
bool EventId::operator<(const EventId &other) const {
|
|
|
|
return id_ < other.id_;
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const EventId id) {
|
|
|
|
return sb << "EventId{" << id.value() << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
EventId::EventId(int32 id) : id_(id) {
|
|
|
|
CHECK(is_valid(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventId::is_valid(int32 id) {
|
|
|
|
return 0 <= id && id < MAX_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
class TQueueImpl : public TQueue {
|
|
|
|
public:
|
|
|
|
void set_callback(unique_ptr<Callback> callback) override {
|
|
|
|
callback_ = std::move(callback);
|
|
|
|
}
|
|
|
|
unique_ptr<Callback> extract_callback() override {
|
|
|
|
return std::move(callback_);
|
|
|
|
}
|
|
|
|
|
2019-08-07 17:29:47 +02:00
|
|
|
void emulate_restart() override {
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
void do_push(QueueId queue_id, RawEvent &&raw_event) override {
|
2019-08-27 17:06:00 +02:00
|
|
|
//LOG(ERROR) << "Push " << queue_id << " " << raw_event.event_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
CHECK(!raw_event.event_id.empty());
|
2019-08-09 19:29:17 +02:00
|
|
|
if (raw_event.logevent_id == 0 && callback_) {
|
2019-08-07 13:01:22 +02:00
|
|
|
raw_event.logevent_id = callback_->push(queue_id, raw_event);
|
|
|
|
}
|
|
|
|
auto &q = queues_[queue_id];
|
|
|
|
q.tail_id = raw_event.event_id.next().move_as_ok();
|
|
|
|
q.events.push(std::move(raw_event));
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:43:26 +02:00
|
|
|
Result<EventId> push(QueueId queue_id, string data, double expires_at, EventId new_id = EventId(),
|
2019-09-06 17:55:19 +02:00
|
|
|
int64 extra = 0) override {
|
2019-08-07 13:01:22 +02:00
|
|
|
auto &q = queues_[queue_id];
|
2019-08-07 17:36:37 +02:00
|
|
|
if (q.events.size() >= MAX_QUEUE_EVENTS) {
|
|
|
|
return Status::Error("Queue is full");
|
|
|
|
}
|
2019-08-09 19:29:17 +02:00
|
|
|
if (data.empty()) {
|
2020-06-11 15:43:26 +02:00
|
|
|
return Status::Error("Data is empty");
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
EventId event_id;
|
|
|
|
while (true) {
|
2019-08-07 17:29:47 +02:00
|
|
|
if (q.tail_id.empty()) {
|
2019-08-07 13:01:22 +02:00
|
|
|
q.tail_id = new_id.empty() ? EventId::create_random() : new_id;
|
|
|
|
}
|
|
|
|
event_id = q.tail_id;
|
|
|
|
CHECK(!event_id.empty());
|
|
|
|
if (event_id.next().is_ok()) {
|
|
|
|
break;
|
|
|
|
}
|
2019-08-09 19:29:17 +02:00
|
|
|
for (auto &e : q.events.as_mutable_span()) {
|
|
|
|
try_pop(queue_id, e, EventId{}, EventId{}, 0, true);
|
|
|
|
}
|
2019-08-07 17:29:47 +02:00
|
|
|
q.tail_id = {};
|
2019-08-09 19:29:17 +02:00
|
|
|
q.events = {};
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RawEvent raw_event;
|
|
|
|
raw_event.event_id = event_id;
|
2019-08-09 19:29:17 +02:00
|
|
|
raw_event.data = std::move(data);
|
2020-06-11 15:43:26 +02:00
|
|
|
raw_event.expires_at = expires_at;
|
2019-09-06 17:55:19 +02:00
|
|
|
raw_event.extra = extra;
|
2019-08-07 13:01:22 +02:00
|
|
|
do_push(queue_id, std::move(raw_event));
|
|
|
|
return event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventId get_head(QueueId queue_id) const override {
|
|
|
|
auto it = queues_.find(queue_id);
|
|
|
|
if (it == queues_.end()) {
|
|
|
|
return EventId();
|
|
|
|
}
|
|
|
|
auto &q = it->second;
|
|
|
|
if (q.events.empty()) {
|
2019-08-07 17:29:47 +02:00
|
|
|
return q.tail_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
return q.events.front().event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventId get_tail(QueueId queue_id) const override {
|
|
|
|
auto it = queues_.find(queue_id);
|
|
|
|
if (it == queues_.end()) {
|
|
|
|
return EventId();
|
|
|
|
}
|
|
|
|
auto &q = it->second;
|
|
|
|
return q.tail_id;
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:06:00 +02:00
|
|
|
void forget(QueueId queue_id, EventId event_id) override {
|
|
|
|
auto q_it = queues_.find(queue_id);
|
|
|
|
if (q_it == queues_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &q = q_it->second;
|
|
|
|
auto from_events = q.events.as_mutable_span();
|
|
|
|
auto it = std::lower_bound(from_events.begin(), from_events.end(), event_id,
|
|
|
|
[](auto &event, EventId event_id) { return event.event_id < event_id; });
|
|
|
|
if (it == from_events.end() || !(it->event_id == event_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try_pop(queue_id, *it, {}, q.tail_id, 0, true /*force*/);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> get(QueueId queue_id, EventId from_id, bool forget_previous, double now,
|
2020-06-11 17:12:16 +02:00
|
|
|
MutableSpan<Event> &result_events) override {
|
2019-08-27 17:06:00 +02:00
|
|
|
//LOG(ERROR) << "Get " << queue_id << " " << from_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
auto it = queues_.find(queue_id);
|
|
|
|
if (it == queues_.end()) {
|
2020-06-11 17:12:16 +02:00
|
|
|
result_events.truncate(0);
|
2019-08-07 13:01:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto &q = it->second;
|
2019-08-07 18:04:48 +02:00
|
|
|
// Some sanity checks
|
|
|
|
if (from_id.value() > q.tail_id.value() + 10) {
|
2020-06-11 15:43:26 +02:00
|
|
|
return Status::Error("Specified from_id is in the future");
|
2019-08-07 18:04:48 +02:00
|
|
|
}
|
|
|
|
if (from_id.value() < q.tail_id.value() - narrow_cast<int32>(MAX_QUEUE_EVENTS) * 2) {
|
2020-06-11 15:43:26 +02:00
|
|
|
return Status::Error("Specified from_id is in the past");
|
2019-08-07 18:04:48 +02:00
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2019-08-09 19:29:17 +02:00
|
|
|
auto from_events = q.events.as_mutable_span();
|
2019-08-07 17:13:10 +02:00
|
|
|
size_t ready_n = 0;
|
2019-08-09 19:29:17 +02:00
|
|
|
size_t i = 0;
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2019-08-09 19:29:17 +02:00
|
|
|
while (true) {
|
|
|
|
from_events = q.events.as_mutable_span();
|
|
|
|
ready_n = 0;
|
2019-08-27 17:06:00 +02:00
|
|
|
size_t first_i = 0;
|
|
|
|
if (!forget_previous) {
|
|
|
|
first_i = std::lower_bound(from_events.begin(), from_events.end(), from_id,
|
|
|
|
[](auto &event, EventId event_id) { return event.event_id < event_id; }) -
|
|
|
|
from_events.begin();
|
|
|
|
}
|
|
|
|
//LOG(ERROR) << tag("first_i", first_i) << tag("size", from_events.size());
|
|
|
|
for (i = first_i; i < from_events.size(); i++) {
|
2019-08-09 19:29:17 +02:00
|
|
|
auto &from = from_events[i];
|
2019-08-27 17:06:00 +02:00
|
|
|
try_pop(queue_id, from, forget_previous ? from_id : EventId{}, q.tail_id, now);
|
2019-08-09 19:29:17 +02:00
|
|
|
if (from.data.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:12:16 +02:00
|
|
|
if (ready_n == result_events.size()) {
|
2019-08-09 19:29:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:06:00 +02:00
|
|
|
if (from.event_id < from_id) {
|
|
|
|
// should not happend
|
|
|
|
UNREACHABLE();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:12:16 +02:00
|
|
|
auto &to = result_events[ready_n];
|
2019-08-09 19:29:17 +02:00
|
|
|
to.data = from.data;
|
|
|
|
to.id = from.event_id;
|
2020-06-11 15:43:26 +02:00
|
|
|
to.expires_at = from.expires_at;
|
2019-09-06 17:55:19 +02:00
|
|
|
to.extra = from.extra;
|
2019-08-09 19:29:17 +02:00
|
|
|
ready_n++;
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2019-08-09 19:29:17 +02:00
|
|
|
// compactify skipped events
|
2019-08-27 17:06:00 +02:00
|
|
|
if ((first_i + ready_n) * 2 < i) {
|
2019-08-09 19:29:17 +02:00
|
|
|
compactify(q.events, i);
|
|
|
|
continue;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
2019-08-09 19:29:17 +02:00
|
|
|
|
|
|
|
break;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
2019-08-09 19:29:17 +02:00
|
|
|
|
2020-06-11 17:12:16 +02:00
|
|
|
result_events.truncate(ready_n);
|
2019-08-09 19:29:17 +02:00
|
|
|
size_t left_n = from_events.size() - i;
|
2019-08-07 17:13:10 +02:00
|
|
|
return ready_n + left_n;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:29:17 +02:00
|
|
|
void run_gc(double now) override {
|
|
|
|
for (auto &it : queues_) {
|
|
|
|
for (auto &e : it.second.events.as_mutable_span()) {
|
|
|
|
try_pop(it.first, e, EventId(), it.second.tail_id, now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
private:
|
|
|
|
struct Queue {
|
|
|
|
EventId tail_id;
|
|
|
|
VectorQueue<RawEvent> events;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_map<QueueId, Queue> queues_;
|
|
|
|
unique_ptr<Callback> callback_;
|
|
|
|
|
2019-08-09 19:29:17 +02:00
|
|
|
void compactify(VectorQueue<RawEvent> &events, size_t prefix) {
|
|
|
|
auto processed = events.as_mutable_span().substr(0, prefix);
|
|
|
|
auto removed_n =
|
|
|
|
processed.rend() - std::remove_if(processed.rbegin(), processed.rend(), [](auto &e) { return e.data.empty(); });
|
|
|
|
events.pop_n(removed_n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void try_pop(QueueId queue_id, RawEvent &event, EventId from_id, EventId tail_id, double now, bool force = false) {
|
2020-06-11 15:43:26 +02:00
|
|
|
//LOG(ERROR) << event.expires_at << " < " << now << " = " << (event.expires_at < now) << " "
|
2019-08-27 17:06:00 +02:00
|
|
|
//<< (event.event_id.value() < from_id.value()) << " " << force << " " << event.data.empty();
|
2020-06-11 15:43:26 +02:00
|
|
|
bool should_drop =
|
|
|
|
event.expires_at < now || event.event_id.value() < from_id.value() || force || event.data.empty();
|
2019-08-09 19:29:17 +02:00
|
|
|
if (!callback_ || event.logevent_id == 0) {
|
|
|
|
if (should_drop) {
|
|
|
|
event.data = {};
|
2019-08-07 17:13:45 +02:00
|
|
|
}
|
2019-08-09 19:29:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!should_drop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:06:00 +02:00
|
|
|
//LOG(ERROR) << "Drop " << queue_id << " " << event.event_id;
|
2019-08-09 19:29:17 +02:00
|
|
|
if (event.event_id.value() + 1 == tail_id.value()) {
|
|
|
|
if (!event.data.empty()) {
|
|
|
|
event.data = {};
|
|
|
|
callback_->push(queue_id, event);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback_->pop(event.logevent_id);
|
|
|
|
event.logevent_id = 0;
|
|
|
|
event.data = {};
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-06-11 17:12:16 +02:00
|
|
|
|
|
|
|
unique_ptr<TQueue> TQueue::create() {
|
|
|
|
return make_unique<TQueueImpl>();
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TQueueLogEvent : public Storer {
|
|
|
|
TQueueLogEvent() = default;
|
|
|
|
int64 queue_id;
|
|
|
|
int32 event_id;
|
2020-06-11 15:43:26 +02:00
|
|
|
int32 expires_at;
|
2019-08-07 13:01:22 +02:00
|
|
|
Slice data;
|
2019-09-06 17:55:19 +02:00
|
|
|
int64 extra;
|
2019-08-07 13:01:22 +02:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &&storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(queue_id, storer);
|
|
|
|
store(event_id, storer);
|
2020-06-11 15:43:26 +02:00
|
|
|
store(expires_at, storer);
|
2019-08-07 13:01:22 +02:00
|
|
|
store(data, storer);
|
2019-09-06 17:55:19 +02:00
|
|
|
if (extra != 0) {
|
|
|
|
store(extra, storer);
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
2019-09-06 17:55:19 +02:00
|
|
|
void parse(ParserT &&parser, int32 has_extra) {
|
2019-08-07 13:01:22 +02:00
|
|
|
using td::parse;
|
|
|
|
parse(queue_id, parser);
|
|
|
|
parse(event_id, parser);
|
2020-06-11 15:43:26 +02:00
|
|
|
parse(expires_at, parser);
|
2019-08-07 13:01:22 +02:00
|
|
|
data = parser.template fetch_string<Slice>();
|
2019-09-06 17:55:19 +02:00
|
|
|
if (has_extra == 0) {
|
|
|
|
extra = 0;
|
|
|
|
} else {
|
|
|
|
parse(extra, parser);
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const override {
|
|
|
|
TlStorerCalcLength storer;
|
|
|
|
store(storer);
|
|
|
|
return storer.get_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t store(uint8 *ptr) const override {
|
|
|
|
TlStorerUnsafe storer(ptr);
|
|
|
|
store(storer);
|
|
|
|
return static_cast<size_t>(storer.get_buf() - ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-07 18:14:36 +02:00
|
|
|
template <class BinlogT>
|
|
|
|
TQueueBinlog<BinlogT>::TQueueBinlog() {
|
|
|
|
diff_ = Clocks::system() - Time::now();
|
|
|
|
}
|
2020-06-11 17:12:16 +02:00
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
template <class BinlogT>
|
2020-06-11 15:52:39 +02:00
|
|
|
uint64 TQueueBinlog<BinlogT>::push(QueueId queue_id, const RawEvent &event) {
|
2019-08-07 13:01:22 +02:00
|
|
|
TQueueLogEvent log_event;
|
|
|
|
log_event.queue_id = queue_id;
|
|
|
|
log_event.event_id = event.event_id.value();
|
2020-06-11 15:43:26 +02:00
|
|
|
log_event.expires_at = static_cast<int32>(event.expires_at + diff_);
|
2019-08-07 13:01:22 +02:00
|
|
|
log_event.data = event.data;
|
2019-09-06 17:55:19 +02:00
|
|
|
log_event.extra = event.extra;
|
2019-08-09 19:29:17 +02:00
|
|
|
if (event.logevent_id == 0) {
|
2020-06-11 17:12:16 +02:00
|
|
|
return binlog_->add(magic_ + (log_event.extra != 0), log_event);
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
2019-09-06 17:55:19 +02:00
|
|
|
binlog_->rewrite(event.logevent_id, magic_ + (log_event.extra != 0), log_event);
|
2019-08-09 19:29:17 +02:00
|
|
|
return event.logevent_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BinlogT>
|
2020-06-11 15:52:39 +02:00
|
|
|
void TQueueBinlog<BinlogT>::pop(uint64 logevent_id) {
|
2019-08-07 13:01:22 +02:00
|
|
|
binlog_->erase(logevent_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BinlogT>
|
|
|
|
Status TQueueBinlog<BinlogT>::replay(const BinlogEvent &binlog_event, TQueue &q) {
|
|
|
|
TQueueLogEvent event;
|
|
|
|
TlParser parser(binlog_event.data_);
|
2019-09-06 17:55:19 +02:00
|
|
|
event.parse(parser, binlog_event.type_ - magic_);
|
2019-08-07 13:01:22 +02:00
|
|
|
TRY_STATUS(parser.get_status());
|
|
|
|
TRY_RESULT(event_id, EventId::from_int32(event.event_id));
|
|
|
|
RawEvent raw_event;
|
|
|
|
raw_event.logevent_id = binlog_event.id_;
|
|
|
|
raw_event.event_id = event_id;
|
2020-06-11 15:43:26 +02:00
|
|
|
raw_event.expires_at = event.expires_at - diff_ + 1;
|
2019-08-07 13:01:22 +02:00
|
|
|
raw_event.data = event.data.str();
|
2019-09-06 17:55:19 +02:00
|
|
|
raw_event.extra = event.extra;
|
2019-08-07 13:01:22 +02:00
|
|
|
q.do_push(event.queue_id, std::move(raw_event));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
template class TQueueBinlog<BinlogInterface>;
|
|
|
|
template class TQueueBinlog<Binlog>;
|
|
|
|
|
2020-06-11 17:12:16 +02:00
|
|
|
uint64 TQueueMemoryStorage::push(QueueId queue_id, const RawEvent &event) {
|
2019-08-09 19:29:17 +02:00
|
|
|
auto logevent_id = event.logevent_id == 0 ? next_logevent_id_++ : event.logevent_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
events_[logevent_id] = std::make_pair(queue_id, event);
|
|
|
|
return logevent_id;
|
|
|
|
}
|
2020-06-11 17:12:16 +02:00
|
|
|
void TQueueMemoryStorage::pop(uint64 logevent_id) {
|
2019-08-07 13:01:22 +02:00
|
|
|
events_.erase(logevent_id);
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:12:16 +02:00
|
|
|
void TQueueMemoryStorage::replay(TQueue &q) {
|
2019-08-07 13:01:22 +02:00
|
|
|
for (auto e : events_) {
|
|
|
|
auto x = e.second;
|
|
|
|
x.second.logevent_id = e.first;
|
|
|
|
q.do_push(x.first, std::move(x.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|