2019-08-07 13:01:22 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2019-08-07 13:01:22 +02: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)
|
|
|
|
//
|
|
|
|
#include "td/db/TQueue.h"
|
|
|
|
|
2019-09-06 17:55:19 +02:00
|
|
|
#include "td/db/binlog/Binlog.h"
|
2020-06-12 02:02:20 +02:00
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
2019-09-06 17:55:19 +02:00
|
|
|
#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
|
|
|
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2020-06-11 22:54:56 +02:00
|
|
|
#include "td/utils/misc.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"
|
2022-10-13 15:04:24 +02:00
|
|
|
#include "td/utils/Time.h"
|
2020-06-13 04:13:11 +02:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-08-07 13:01:22 +02:00
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
2020-08-07 05:06:29 +02:00
|
|
|
#include <set>
|
2019-08-07 13:01:22 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
using EventId = TQueue::EventId;
|
|
|
|
|
|
|
|
EventId::EventId() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<EventId> EventId::from_int32(int32 id) {
|
2020-06-11 22:54:56 +02:00
|
|
|
if (!is_valid_id(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);
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:54:56 +02:00
|
|
|
bool EventId::is_valid() const {
|
|
|
|
return !empty() && is_valid_id(id_);
|
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_;
|
|
|
|
}
|
|
|
|
|
2021-10-18 18:26:14 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, EventId id) {
|
2020-06-11 22:54:56 +02:00
|
|
|
return string_builder << "EventId{" << id.value() << "}";
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EventId::EventId(int32 id) : id_(id) {
|
2020-06-11 22:54:56 +02:00
|
|
|
CHECK(is_valid_id(id));
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 22:54:56 +02:00
|
|
|
bool EventId::is_valid_id(int32 id) {
|
2019-08-07 13:01:22 +02:00
|
|
|
return 0 <= id && id < MAX_ID;
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TQueueImpl final : public TQueue {
|
2020-07-13 22:48:15 +02:00
|
|
|
static constexpr size_t MAX_EVENT_LENGTH = 65536 * 8;
|
2022-11-10 19:18:13 +01:00
|
|
|
static constexpr size_t MAX_QUEUE_EVENTS = 100000;
|
|
|
|
static constexpr size_t MAX_TOTAL_EVENT_LENGTH = 1 << 27;
|
2020-06-12 02:02:20 +02:00
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_callback(unique_ptr<StorageCallback> callback) final {
|
2019-08-07 13:01:22 +02:00
|
|
|
callback_ = std::move(callback);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
unique_ptr<StorageCallback> extract_callback() final {
|
2019-08-07 13:01:22 +02:00
|
|
|
return std::move(callback_);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool do_push(QueueId queue_id, RawEvent &&raw_event) final {
|
2020-06-11 22:54:56 +02:00
|
|
|
CHECK(raw_event.event_id.is_valid());
|
2020-07-24 05:57:48 +02:00
|
|
|
// raw_event.data can be empty when replaying binlog
|
2022-02-09 22:59:52 +01:00
|
|
|
if (raw_event.data.size() > MAX_EVENT_LENGTH || queue_id == 0) {
|
2020-07-24 05:57:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
auto &q = queues_[queue_id];
|
2020-08-07 05:06:29 +02:00
|
|
|
if (q.events.size() >= MAX_QUEUE_EVENTS || q.total_event_length > MAX_TOTAL_EVENT_LENGTH - raw_event.data.size() ||
|
|
|
|
raw_event.expires_at <= 0) {
|
2020-07-24 04:47:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-24 23:37:45 +02:00
|
|
|
auto event_id = raw_event.event_id;
|
|
|
|
if (event_id < q.tail_id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-25 02:11:03 +02:00
|
|
|
if (!q.events.empty()) {
|
|
|
|
auto it = q.events.end();
|
|
|
|
--it;
|
|
|
|
if (it->second.data.empty()) {
|
2020-09-22 01:15:09 +02:00
|
|
|
if (callback_ != nullptr && it->second.log_event_id != 0) {
|
|
|
|
callback_->pop(it->second.log_event_id);
|
2020-07-25 02:11:03 +02:00
|
|
|
}
|
|
|
|
q.events.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 05:06:29 +02:00
|
|
|
if (q.events.empty() && !raw_event.data.empty()) {
|
|
|
|
schedule_queue_gc(queue_id, q, raw_event.expires_at);
|
|
|
|
}
|
2020-07-25 02:11:03 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
if (raw_event.log_event_id == 0 && callback_ != nullptr) {
|
|
|
|
raw_event.log_event_id = callback_->push(queue_id, raw_event);
|
2020-06-12 02:02:20 +02:00
|
|
|
}
|
2020-07-24 23:37:45 +02:00
|
|
|
q.tail_id = event_id.next().move_as_ok();
|
|
|
|
q.total_event_length += raw_event.data.size();
|
|
|
|
q.events.emplace(event_id, std::move(raw_event));
|
|
|
|
return true;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<EventId> push(QueueId queue_id, string data, int32 expires_at, int64 extra, EventId hint_new_id) final {
|
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
|
|
|
}
|
2020-07-13 22:48:15 +02:00
|
|
|
if (data.size() > MAX_EVENT_LENGTH) {
|
2020-06-12 02:02:20 +02:00
|
|
|
return Status::Error("Data is too big");
|
|
|
|
}
|
2022-02-09 22:59:52 +01:00
|
|
|
if (queue_id == 0) {
|
|
|
|
return Status::Error("Queue identifier is invalid");
|
|
|
|
}
|
2020-07-24 05:57:48 +02:00
|
|
|
|
|
|
|
auto &q = queues_[queue_id];
|
|
|
|
if (q.events.size() >= MAX_QUEUE_EVENTS) {
|
|
|
|
return Status::Error("Queue is full");
|
|
|
|
}
|
2020-07-13 22:48:15 +02:00
|
|
|
if (q.total_event_length > MAX_TOTAL_EVENT_LENGTH - data.size()) {
|
|
|
|
return Status::Error("Queue size is too big");
|
|
|
|
}
|
2020-08-16 01:57:31 +02:00
|
|
|
if (expires_at <= 0) {
|
|
|
|
return Status::Error("Failed to add already expired event");
|
|
|
|
}
|
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()) {
|
2020-06-12 02:02:20 +02:00
|
|
|
if (hint_new_id.empty()) {
|
2022-11-10 19:18:13 +01:00
|
|
|
q.tail_id = EventId::from_int32(
|
|
|
|
Random::fast(2 * max(static_cast<int>(MAX_QUEUE_EVENTS), 1000000) + 1, EventId::MAX_ID / 2))
|
|
|
|
.move_as_ok();
|
2020-06-12 02:02:20 +02:00
|
|
|
} else {
|
|
|
|
q.tail_id = hint_new_id;
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
event_id = q.tail_id;
|
2020-06-11 23:46:47 +02:00
|
|
|
CHECK(event_id.is_valid());
|
2019-08-07 13:01:22 +02:00
|
|
|
if (event_id.next().is_ok()) {
|
|
|
|
break;
|
|
|
|
}
|
2020-07-24 23:37:45 +02:00
|
|
|
for (auto it = q.events.begin(); it != q.events.end();) {
|
|
|
|
pop(q, queue_id, it, {});
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
2020-06-11 22:54:56 +02:00
|
|
|
q.tail_id = EventId();
|
2020-06-11 23:46:47 +02:00
|
|
|
CHECK(hint_new_id.next().is_ok());
|
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;
|
2020-06-12 02:02:20 +02:00
|
|
|
bool is_added = do_push(queue_id, std::move(raw_event));
|
|
|
|
CHECK(is_added);
|
2019-08-07 13:01:22 +02:00
|
|
|
return event_id;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
EventId get_head(QueueId queue_id) const final {
|
2019-08-07 13:01:22 +02:00
|
|
|
auto it = queues_.find(queue_id);
|
|
|
|
if (it == queues_.end()) {
|
|
|
|
return EventId();
|
|
|
|
}
|
2020-08-26 20:36:29 +02:00
|
|
|
return get_queue_head(it->second);
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
EventId get_tail(QueueId queue_id) const final {
|
2019-08-07 13:01:22 +02:00
|
|
|
auto it = queues_.find(queue_id);
|
|
|
|
if (it == queues_.end()) {
|
|
|
|
return EventId();
|
|
|
|
}
|
|
|
|
auto &q = it->second;
|
|
|
|
return q.tail_id;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void forget(QueueId queue_id, EventId event_id) final {
|
2019-08-27 17:06:00 +02:00
|
|
|
auto q_it = queues_.find(queue_id);
|
|
|
|
if (q_it == queues_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &q = q_it->second;
|
2020-07-24 23:37:45 +02:00
|
|
|
auto it = q.events.find(event_id);
|
|
|
|
if (it == q.events.end()) {
|
2019-08-27 17:06:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-24 23:37:45 +02:00
|
|
|
pop(q, queue_id, it, q.tail_id);
|
2019-08-27 17:06:00 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:04:45 +01:00
|
|
|
void clear(QueueId queue_id, size_t keep_count) final {
|
2022-11-08 15:27:02 +01:00
|
|
|
auto queue_it = queues_.find(queue_id);
|
|
|
|
if (queue_it == queues_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &q = queue_it->second;
|
|
|
|
auto size = get_size(q);
|
2022-11-08 15:04:45 +01:00
|
|
|
if (size <= keep_count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 10:20:48 +01:00
|
|
|
auto start_time = Time::now();
|
|
|
|
auto total_event_length = q.total_event_length;
|
|
|
|
|
2022-11-08 15:27:02 +01:00
|
|
|
auto end_it = q.events.end();
|
2022-11-21 10:20:48 +01:00
|
|
|
for (size_t i = 0; i < keep_count; i++) {
|
2022-11-08 15:27:02 +01:00
|
|
|
--end_it;
|
|
|
|
}
|
|
|
|
for (auto it = q.events.begin(); it != end_it;) {
|
|
|
|
pop(q, queue_id, it, q.tail_id);
|
|
|
|
}
|
2022-11-21 10:20:48 +01:00
|
|
|
|
|
|
|
auto clear_time = Time::now() - start_time;
|
|
|
|
if (clear_time > 0.1) {
|
|
|
|
LOG(WARNING) << "Cleared " << (size - keep_count) << " TQueue events with total size "
|
|
|
|
<< (total_event_length - q.total_event_length) << " in " << time << " seconds";
|
|
|
|
}
|
2022-11-08 15:04:45 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 03:02:54 +02:00
|
|
|
Result<size_t> get(QueueId queue_id, EventId from_id, bool forget_previous, int32 unix_time_now,
|
2021-07-03 22:51:36 +02:00
|
|
|
MutableSpan<Event> &result_events) final {
|
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
|
|
|
}
|
2020-08-28 13:53:22 +02:00
|
|
|
if (from_id.value() < get_queue_head(q).value() - static_cast<int32>(MAX_QUEUE_EVENTS)) {
|
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
|
|
|
|
2020-08-07 05:06:29 +02:00
|
|
|
do_get(queue_id, q, from_id, forget_previous, unix_time_now, result_events);
|
2020-08-07 21:14:42 +02:00
|
|
|
return get_size(q);
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 15:04:24 +02:00
|
|
|
std::pair<int64, bool> run_gc(int32 unix_time_now) final {
|
2020-08-07 18:36:23 +02:00
|
|
|
int64 deleted_events = 0;
|
2022-10-13 15:04:24 +02:00
|
|
|
auto max_finish_time = Time::now() + 0.05;
|
|
|
|
int64 counter = 0;
|
2020-08-07 05:06:29 +02:00
|
|
|
while (!queue_gc_at_.empty()) {
|
|
|
|
auto it = queue_gc_at_.begin();
|
|
|
|
if (it->first >= unix_time_now) {
|
|
|
|
break;
|
2020-07-24 01:42:54 +02:00
|
|
|
}
|
2020-08-07 05:06:29 +02:00
|
|
|
auto queue_id = it->second;
|
|
|
|
auto &q = queues_[queue_id];
|
|
|
|
CHECK(q.gc_at == it->first);
|
2020-08-07 13:03:39 +02:00
|
|
|
int32 new_gc_at = 0;
|
2020-08-07 05:06:29 +02:00
|
|
|
|
|
|
|
if (!q.events.empty()) {
|
2020-08-07 18:36:23 +02:00
|
|
|
size_t size_before = get_size(q);
|
2022-10-13 14:22:06 +02:00
|
|
|
for (auto event_it = q.events.begin(); event_it != q.events.end();) {
|
|
|
|
auto &event = event_it->second;
|
2022-10-13 15:04:24 +02:00
|
|
|
if ((++counter & 128) == 0 && Time::now() >= max_finish_time) {
|
|
|
|
if (new_gc_at == 0) {
|
|
|
|
new_gc_at = event.expires_at;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-10-13 14:22:06 +02:00
|
|
|
if (event.expires_at < unix_time_now || event.data.empty()) {
|
|
|
|
pop(q, queue_id, event_it, q.tail_id);
|
|
|
|
} else {
|
|
|
|
if (new_gc_at != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_gc_at = event.expires_at;
|
|
|
|
++event_it;
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 18:36:23 +02:00
|
|
|
size_t size_after = get_size(q);
|
|
|
|
CHECK(size_after <= size_before);
|
|
|
|
deleted_events += size_before - size_after;
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
2020-08-07 13:03:39 +02:00
|
|
|
schedule_queue_gc(queue_id, q, new_gc_at);
|
2022-10-13 15:04:24 +02:00
|
|
|
if (Time::now() >= max_finish_time) {
|
|
|
|
return {deleted_events, false};
|
|
|
|
}
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
2022-10-13 15:04:24 +02:00
|
|
|
return {deleted_events, true};
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t get_size(QueueId queue_id) const final {
|
2020-06-24 14:11:39 +02:00
|
|
|
auto it = queues_.find(queue_id);
|
|
|
|
if (it == queues_.end()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-07 18:36:23 +02:00
|
|
|
return get_size(it->second);
|
2020-06-24 14:11:39 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void close(Promise<> promise) final {
|
2020-07-24 23:37:45 +02:00
|
|
|
if (callback_ != nullptr) {
|
|
|
|
callback_->close(std::move(promise));
|
|
|
|
callback_ = nullptr;
|
|
|
|
}
|
2020-07-14 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
private:
|
|
|
|
struct Queue {
|
|
|
|
EventId tail_id;
|
2020-07-24 23:37:45 +02:00
|
|
|
std::map<EventId, RawEvent> events;
|
2020-07-13 22:48:15 +02:00
|
|
|
size_t total_event_length = 0;
|
2020-08-07 05:06:29 +02:00
|
|
|
int32 gc_at = 0;
|
2019-08-07 13:01:22 +02:00
|
|
|
};
|
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<QueueId, Queue> queues_;
|
2020-08-07 05:06:29 +02:00
|
|
|
std::set<std::pair<int32, QueueId>> queue_gc_at_;
|
2020-06-11 22:54:56 +02:00
|
|
|
unique_ptr<StorageCallback> callback_;
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2020-08-26 20:36:29 +02:00
|
|
|
static EventId get_queue_head(const Queue &q) {
|
|
|
|
if (q.events.empty()) {
|
|
|
|
return q.tail_id;
|
|
|
|
}
|
|
|
|
return q.events.begin()->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_size(const Queue &q) {
|
2020-08-07 18:36:23 +02:00
|
|
|
if (q.events.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return q.events.size() - (q.events.rbegin()->second.data.empty() ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2020-07-24 23:37:45 +02:00
|
|
|
void pop(Queue &q, QueueId queue_id, std::map<EventId, RawEvent>::iterator &it, EventId tail_id) {
|
|
|
|
auto &event = it->second;
|
2020-09-22 01:15:09 +02:00
|
|
|
if (callback_ == nullptr || event.log_event_id == 0) {
|
2020-07-24 23:37:45 +02:00
|
|
|
remove_event(q, it);
|
2019-08-09 19:29:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:54:56 +02:00
|
|
|
if (event.event_id.next().ok() == tail_id) {
|
2019-08-09 19:29:17 +02:00
|
|
|
if (!event.data.empty()) {
|
2020-07-13 22:48:15 +02:00
|
|
|
clear_event_data(q, event);
|
2019-08-09 19:29:17 +02:00
|
|
|
callback_->push(queue_id, event);
|
|
|
|
}
|
2020-07-24 23:37:45 +02:00
|
|
|
++it;
|
2019-08-09 19:29:17 +02:00
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
callback_->pop(event.log_event_id);
|
2020-07-24 23:37:45 +02:00
|
|
|
remove_event(q, it);
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 14:11:39 +02:00
|
|
|
|
2020-07-24 23:37:45 +02:00
|
|
|
static void remove_event(Queue &q, std::map<EventId, RawEvent>::iterator &it) {
|
|
|
|
q.total_event_length -= it->second.data.size();
|
|
|
|
it = q.events.erase(it);
|
|
|
|
}
|
|
|
|
|
2020-07-13 22:48:15 +02:00
|
|
|
static void clear_event_data(Queue &q, RawEvent &event) {
|
|
|
|
q.total_event_length -= event.data.size();
|
|
|
|
event.data = {};
|
|
|
|
}
|
|
|
|
|
2020-08-07 05:06:29 +02:00
|
|
|
void do_get(QueueId queue_id, Queue &q, EventId from_id, bool forget_previous, int32 unix_time_now,
|
|
|
|
MutableSpan<Event> &result_events) {
|
2020-07-24 23:37:45 +02:00
|
|
|
if (forget_previous) {
|
|
|
|
for (auto it = q.events.begin(); it != q.events.end() && it->first < from_id;) {
|
|
|
|
pop(q, queue_id, it, q.tail_id);
|
2020-06-24 14:11:39 +02:00
|
|
|
}
|
2020-07-24 23:37:45 +02:00
|
|
|
}
|
2020-06-24 14:11:39 +02:00
|
|
|
|
2020-07-24 23:37:45 +02:00
|
|
|
size_t ready_n = 0;
|
|
|
|
for (auto it = q.events.lower_bound(from_id); it != q.events.end();) {
|
|
|
|
auto &event = it->second;
|
2020-08-06 03:02:54 +02:00
|
|
|
if (event.expires_at < unix_time_now || event.data.empty()) {
|
2020-07-24 23:37:45 +02:00
|
|
|
pop(q, queue_id, it, q.tail_id);
|
|
|
|
} else {
|
|
|
|
CHECK(!(event.event_id < from_id));
|
2020-06-24 14:11:39 +02:00
|
|
|
if (ready_n == result_events.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &to = result_events[ready_n];
|
2020-07-24 23:37:45 +02:00
|
|
|
to.data = event.data;
|
|
|
|
to.id = event.event_id;
|
|
|
|
to.expires_at = event.expires_at;
|
|
|
|
to.extra = event.extra;
|
2020-06-24 14:11:39 +02:00
|
|
|
ready_n++;
|
2020-07-24 23:37:45 +02:00
|
|
|
++it;
|
2020-06-24 14:11:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result_events.truncate(ready_n);
|
2020-08-07 05:06:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void schedule_queue_gc(QueueId queue_id, Queue &q, int32 gc_at) {
|
2020-08-07 13:03:39 +02:00
|
|
|
if (q.gc_at != 0) {
|
2020-10-08 22:04:40 +02:00
|
|
|
bool is_deleted = queue_gc_at_.erase({q.gc_at, queue_id}) > 0;
|
2020-08-07 13:03:39 +02:00
|
|
|
CHECK(is_deleted);
|
|
|
|
}
|
2020-08-07 05:06:29 +02:00
|
|
|
q.gc_at = gc_at;
|
2020-08-07 13:03:39 +02:00
|
|
|
if (q.gc_at != 0) {
|
|
|
|
bool is_inserted = queue_gc_at_.emplace(gc_at, queue_id).second;
|
|
|
|
CHECK(is_inserted);
|
|
|
|
}
|
2020-06-24 14:11:39 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
struct TQueueLogEvent final : public Storer {
|
2019-08-07 13:01:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t size() const final {
|
2019-08-07 13:01:22 +02:00
|
|
|
TlStorerCalcLength storer;
|
|
|
|
store(storer);
|
|
|
|
return storer.get_length();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t store(uint8 *ptr) const final {
|
2019-08-07 13:01:22 +02:00
|
|
|
TlStorerUnsafe storer(ptr);
|
|
|
|
store(storer);
|
|
|
|
return static_cast<size_t>(storer.get_buf() - ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-08-06 03:02:54 +02:00
|
|
|
log_event.expires_at = event.expires_at;
|
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;
|
2020-08-06 03:02:54 +02:00
|
|
|
auto magic = BINLOG_EVENT_TYPE + (log_event.extra != 0);
|
2020-09-22 01:15:09 +02:00
|
|
|
if (event.log_event_id == 0) {
|
2020-06-12 02:02:20 +02:00
|
|
|
return binlog_->add(magic, log_event);
|
2019-08-09 19:29:17 +02:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_->rewrite(event.log_event_id, magic, log_event);
|
|
|
|
return event.log_event_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BinlogT>
|
2020-09-22 01:15:09 +02:00
|
|
|
void TQueueBinlog<BinlogT>::pop(uint64 log_event_id) {
|
|
|
|
binlog_->erase(log_event_id);
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BinlogT>
|
2020-06-12 02:02:20 +02:00
|
|
|
Status TQueueBinlog<BinlogT>::replay(const BinlogEvent &binlog_event, TQueue &q) const {
|
2019-08-07 13:01:22 +02:00
|
|
|
TQueueLogEvent event;
|
|
|
|
TlParser parser(binlog_event.data_);
|
2020-08-06 03:02:54 +02:00
|
|
|
int32 has_extra = binlog_event.type_ - BINLOG_EVENT_TYPE;
|
2020-06-12 02:02:20 +02:00
|
|
|
if (has_extra != 0 && has_extra != 1) {
|
|
|
|
return Status::Error("Wrong magic");
|
|
|
|
}
|
|
|
|
event.parse(parser, has_extra);
|
|
|
|
parser.fetch_end();
|
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;
|
2020-09-22 01:15:09 +02:00
|
|
|
raw_event.log_event_id = binlog_event.id_;
|
2019-08-07 13:01:22 +02:00
|
|
|
raw_event.event_id = event_id;
|
2020-08-06 03:02:54 +02:00
|
|
|
raw_event.expires_at = event.expires_at;
|
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;
|
2020-06-12 02:02:20 +02:00
|
|
|
if (!q.do_push(event.queue_id, std::move(raw_event))) {
|
|
|
|
return Status::Error("Failed to add event");
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-07-14 18:10:26 +02:00
|
|
|
template <class BinlogT>
|
2020-07-14 19:54:38 +02:00
|
|
|
void TQueueBinlog<BinlogT>::close(Promise<> promise) {
|
2020-07-14 18:10:26 +02:00
|
|
|
binlog_->close(std::move(promise));
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
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) {
|
2020-09-22 01:15:09 +02:00
|
|
|
auto log_event_id = event.log_event_id == 0 ? next_log_event_id_++ : event.log_event_id;
|
|
|
|
events_[log_event_id] = std::make_pair(queue_id, event);
|
|
|
|
return log_event_id;
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
2020-06-12 02:02:20 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
void TQueueMemoryStorage::pop(uint64 log_event_id) {
|
|
|
|
events_.erase(log_event_id);
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 02:02:20 +02:00
|
|
|
void TQueueMemoryStorage::replay(TQueue &q) const {
|
|
|
|
for (auto &e : events_) {
|
2019-08-07 13:01:22 +02:00
|
|
|
auto x = e.second;
|
2020-09-22 01:15:09 +02:00
|
|
|
x.second.log_event_id = e.first;
|
2020-06-12 02:02:20 +02:00
|
|
|
bool is_added = q.do_push(x.first, std::move(x.second));
|
|
|
|
CHECK(is_added);
|
2019-08-07 13:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 19:54:38 +02:00
|
|
|
void TQueueMemoryStorage::close(Promise<> promise) {
|
2020-08-06 03:02:54 +02:00
|
|
|
events_.clear();
|
2020-07-14 18:10:26 +02:00
|
|
|
promise.set_value({});
|
|
|
|
}
|
2019-08-07 13:01:22 +02:00
|
|
|
|
|
|
|
} // namespace td
|