2019-08-05 17:50:55 +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-05 17:50:55 +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)
|
|
|
|
//
|
2019-08-07 13:01:22 +02:00
|
|
|
#include "td/db/binlog/Binlog.h"
|
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
|
|
|
#include "td/db/binlog/BinlogHelper.h"
|
|
|
|
#include "td/db/TQueue.h"
|
|
|
|
|
2020-08-04 20:37:47 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2020-12-22 13:51:57 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-08-05 17:50:55 +02:00
|
|
|
#include "td/utils/int_types.h"
|
2020-12-22 13:51:57 +01:00
|
|
|
#include "td/utils/logging.h"
|
2020-08-04 21:48:23 +02:00
|
|
|
#include "td/utils/Random.h"
|
2019-08-05 17:50:55 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-08-05 17:50:55 +02:00
|
|
|
#include "td/utils/Span.h"
|
|
|
|
#include "td/utils/tests.h"
|
2022-11-08 15:04:45 +01:00
|
|
|
#include "td/utils/Time.h"
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2020-12-22 13:51:57 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2019-08-05 17:50:55 +02:00
|
|
|
TEST(TQueue, hands) {
|
2020-06-11 23:46:47 +02:00
|
|
|
td::TQueue::Event events[100];
|
|
|
|
auto events_span = td::MutableSpan<td::TQueue::Event>(events, 100);
|
2019-08-05 17:50:55 +02:00
|
|
|
|
2020-06-11 23:46:47 +02:00
|
|
|
auto tqueue = td::TQueue::create();
|
2019-08-05 17:50:55 +02:00
|
|
|
auto qid = 12;
|
2019-08-07 13:01:22 +02:00
|
|
|
ASSERT_EQ(true, tqueue->get_head(qid).empty());
|
|
|
|
ASSERT_EQ(true, tqueue->get_tail(qid).empty());
|
2020-08-07 05:06:29 +02:00
|
|
|
tqueue->push(qid, "hello", 1, 0, td::TQueue::EventId());
|
2019-08-07 13:01:22 +02:00
|
|
|
auto head = tqueue->get_head(qid);
|
2020-06-12 02:02:20 +02:00
|
|
|
auto tail = tqueue->get_tail(qid);
|
|
|
|
ASSERT_EQ(head.next().ok(), tail);
|
2019-08-27 17:06:00 +02:00
|
|
|
ASSERT_EQ(1u, tqueue->get(qid, head, true, 0, events_span).move_as_ok());
|
2020-06-12 02:02:20 +02:00
|
|
|
ASSERT_EQ(1u, tqueue->get(qid, head, true, 0, events_span).move_as_ok());
|
2020-07-24 23:37:45 +02:00
|
|
|
ASSERT_EQ(1u, tqueue->get(qid, tail, false, 0, events_span).move_as_ok());
|
2020-06-12 02:02:20 +02:00
|
|
|
ASSERT_EQ(1u, tqueue->get(qid, head, true, 0, events_span).move_as_ok());
|
|
|
|
ASSERT_EQ(0u, tqueue->get(qid, tail, true, 0, events_span).move_as_ok());
|
|
|
|
ASSERT_EQ(0u, tqueue->get(qid, head, true, 0, events_span).move_as_ok());
|
2019-08-06 16:30:15 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 20:24:28 +02:00
|
|
|
class TestTQueue {
|
|
|
|
public:
|
2020-06-11 23:46:47 +02:00
|
|
|
using EventId = td::TQueue::EventId;
|
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
static td::CSlice binlog_path() {
|
|
|
|
return td::CSlice("tqueue_binlog");
|
2019-08-06 20:24:28 +02:00
|
|
|
}
|
2020-06-12 02:53:04 +02:00
|
|
|
|
2019-08-06 20:24:28 +02:00
|
|
|
TestTQueue() {
|
2020-06-11 23:46:47 +02:00
|
|
|
baseline_ = td::TQueue::create();
|
2019-08-07 13:01:22 +02:00
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
memory_ = td::TQueue::create();
|
2020-06-11 23:46:47 +02:00
|
|
|
auto memory_storage = td::make_unique<td::TQueueMemoryStorage>();
|
2019-08-06 20:24:28 +02:00
|
|
|
memory_storage_ = memory_storage.get();
|
2019-08-07 13:01:22 +02:00
|
|
|
memory_->set_callback(std::move(memory_storage));
|
2019-08-06 20:24:28 +02:00
|
|
|
|
2020-06-11 23:46:47 +02:00
|
|
|
binlog_ = td::TQueue::create();
|
|
|
|
auto tqueue_binlog = td::make_unique<td::TQueueBinlog<td::Binlog>>();
|
|
|
|
td::Binlog::destroy(binlog_path()).ensure();
|
|
|
|
auto binlog = std::make_shared<td::Binlog>();
|
|
|
|
binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { UNREACHABLE(); }).ensure();
|
2020-06-12 02:53:04 +02:00
|
|
|
tqueue_binlog->set_binlog(std::move(binlog));
|
2019-08-07 13:01:22 +02:00
|
|
|
binlog_->set_callback(std::move(tqueue_binlog));
|
2019-08-06 20:24:28 +02:00
|
|
|
}
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2020-08-06 03:02:54 +02:00
|
|
|
void restart(td::Random::Xorshift128plus &rnd, td::int32 now) {
|
2019-08-09 19:29:17 +02:00
|
|
|
if (rnd.fast(0, 10) == 0) {
|
|
|
|
baseline_->run_gc(now);
|
|
|
|
}
|
2019-08-07 17:29:47 +02:00
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
memory_->extract_callback().release();
|
2020-06-11 23:46:47 +02:00
|
|
|
auto memory_storage = td::unique_ptr<td::TQueueMemoryStorage>(memory_storage_);
|
|
|
|
memory_ = td::TQueue::create();
|
2019-08-07 13:01:22 +02:00
|
|
|
memory_storage->replay(*memory_);
|
|
|
|
memory_->set_callback(std::move(memory_storage));
|
2019-08-09 19:29:17 +02:00
|
|
|
if (rnd.fast(0, 10) == 0) {
|
|
|
|
memory_->run_gc(now);
|
|
|
|
}
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
if (rnd.fast(0, 30) != 0) {
|
2019-08-06 20:24:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2020-06-11 22:54:56 +02:00
|
|
|
LOG(INFO) << "Restart binlog";
|
2020-06-11 23:46:47 +02:00
|
|
|
binlog_ = td::TQueue::create();
|
|
|
|
auto tqueue_binlog = td::make_unique<td::TQueueBinlog<td::Binlog>>();
|
|
|
|
auto binlog = std::make_shared<td::Binlog>();
|
2020-07-24 05:57:48 +02:00
|
|
|
binlog
|
|
|
|
->init(binlog_path().str(),
|
|
|
|
[&](const td::BinlogEvent &event) { tqueue_binlog->replay(event, *binlog_).ignore(); })
|
2019-08-06 20:24:28 +02:00
|
|
|
.ensure();
|
2020-06-12 02:53:04 +02:00
|
|
|
tqueue_binlog->set_binlog(std::move(binlog));
|
2019-08-07 13:01:22 +02:00
|
|
|
binlog_->set_callback(std::move(tqueue_binlog));
|
2020-06-12 02:53:04 +02:00
|
|
|
if (rnd.fast(0, 2) == 0) {
|
2019-08-09 19:29:17 +02:00
|
|
|
binlog_->run_gc(now);
|
|
|
|
}
|
2019-08-06 20:24:28 +02:00
|
|
|
}
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
EventId push(td::TQueue::QueueId queue_id, const td::string &data, td::int32 expires_at, EventId new_id = EventId()) {
|
2020-06-11 23:46:47 +02:00
|
|
|
auto a_id = baseline_->push(queue_id, data, expires_at, 0, new_id).move_as_ok();
|
|
|
|
auto b_id = memory_->push(queue_id, data, expires_at, 0, new_id).move_as_ok();
|
|
|
|
auto c_id = binlog_->push(queue_id, data, expires_at, 0, new_id).move_as_ok();
|
2019-08-06 20:24:28 +02:00
|
|
|
ASSERT_EQ(a_id, b_id);
|
|
|
|
ASSERT_EQ(a_id, c_id);
|
|
|
|
return a_id;
|
|
|
|
}
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2020-08-06 03:02:54 +02:00
|
|
|
void check_head_tail(td::TQueue::QueueId qid) {
|
2019-08-09 19:29:17 +02:00
|
|
|
//ASSERT_EQ(baseline_->get_head(qid), memory_->get_head(qid));
|
|
|
|
//ASSERT_EQ(baseline_->get_head(qid), binlog_->get_head(qid));
|
2019-08-07 13:01:22 +02:00
|
|
|
ASSERT_EQ(baseline_->get_tail(qid), memory_->get_tail(qid));
|
|
|
|
ASSERT_EQ(baseline_->get_tail(qid), binlog_->get_tail(qid));
|
2019-08-06 20:24:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 03:02:54 +02:00
|
|
|
void check_get(td::TQueue::QueueId qid, td::Random::Xorshift128plus &rnd, td::int32 now) {
|
2020-06-11 23:46:47 +02:00
|
|
|
td::TQueue::Event a[10];
|
|
|
|
td::MutableSpan<td::TQueue::Event> a_span(a, 10);
|
|
|
|
td::TQueue::Event b[10];
|
|
|
|
td::MutableSpan<td::TQueue::Event> b_span(b, 10);
|
|
|
|
td::TQueue::Event c[10];
|
|
|
|
td::MutableSpan<td::TQueue::Event> c_span(c, 10);
|
2019-08-06 20:24:28 +02:00
|
|
|
|
2019-08-07 13:01:22 +02:00
|
|
|
auto a_from = baseline_->get_head(qid);
|
2019-08-09 19:29:17 +02:00
|
|
|
//auto b_from = memory_->get_head(qid);
|
|
|
|
//auto c_from = binlog_->get_head(qid);
|
|
|
|
//ASSERT_EQ(a_from, b_from);
|
|
|
|
//ASSERT_EQ(a_from, c_from);
|
2019-08-06 16:30:15 +02:00
|
|
|
|
|
|
|
auto tmp = a_from.advance(rnd.fast(-10, 10));
|
|
|
|
if (tmp.is_ok()) {
|
|
|
|
a_from = tmp.move_as_ok();
|
|
|
|
}
|
2019-08-27 17:06:00 +02:00
|
|
|
baseline_->get(qid, a_from, true, now, a_span).move_as_ok();
|
|
|
|
memory_->get(qid, a_from, true, now, b_span).move_as_ok();
|
|
|
|
binlog_->get(qid, a_from, true, now, c_span).move_as_ok();
|
2019-08-07 17:13:10 +02:00
|
|
|
ASSERT_EQ(a_span.size(), b_span.size());
|
|
|
|
ASSERT_EQ(a_span.size(), c_span.size());
|
|
|
|
for (size_t i = 0; i < a_span.size(); i++) {
|
2019-08-06 20:24:28 +02:00
|
|
|
ASSERT_EQ(a_span[i].id, b_span[i].id);
|
|
|
|
ASSERT_EQ(a_span[i].id, c_span[i].id);
|
|
|
|
ASSERT_EQ(a_span[i].data, b_span[i].data);
|
|
|
|
ASSERT_EQ(a_span[i].data, c_span[i].data);
|
2019-08-06 16:30:15 +02:00
|
|
|
}
|
2019-08-06 20:24:28 +02:00
|
|
|
}
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2019-08-06 20:24:28 +02:00
|
|
|
private:
|
2020-06-11 23:46:47 +02:00
|
|
|
td::unique_ptr<td::TQueue> baseline_;
|
|
|
|
td::unique_ptr<td::TQueue> memory_;
|
|
|
|
td::unique_ptr<td::TQueue> binlog_;
|
|
|
|
td::TQueueMemoryStorage *memory_storage_{nullptr};
|
2019-08-06 20:24:28 +02:00
|
|
|
};
|
2019-08-06 16:30:15 +02:00
|
|
|
|
2019-08-06 20:24:28 +02:00
|
|
|
TEST(TQueue, random) {
|
2020-06-11 23:46:47 +02:00
|
|
|
using EventId = td::TQueue::EventId;
|
|
|
|
td::Random::Xorshift128plus rnd(123);
|
2020-06-12 02:53:04 +02:00
|
|
|
auto next_queue_id = [&rnd] {
|
2019-08-07 13:01:22 +02:00
|
|
|
return rnd.fast(1, 10);
|
|
|
|
};
|
2020-06-12 02:53:04 +02:00
|
|
|
auto next_first_id = [&rnd] {
|
|
|
|
if (rnd.fast(0, 3) == 0) {
|
|
|
|
return EventId::from_int32(EventId::MAX_ID - 20).move_as_ok();
|
|
|
|
}
|
|
|
|
return EventId::from_int32(rnd.fast(1000000000, 1500000000)).move_as_ok();
|
2019-08-06 20:24:28 +02:00
|
|
|
};
|
2020-06-12 02:53:04 +02:00
|
|
|
|
2019-08-06 20:24:28 +02:00
|
|
|
TestTQueue q;
|
2020-08-07 05:06:29 +02:00
|
|
|
td::int32 now = 1000;
|
2019-08-06 20:24:28 +02:00
|
|
|
auto push_event = [&] {
|
|
|
|
auto data = PSTRING() << rnd();
|
2020-06-12 02:53:04 +02:00
|
|
|
if (rnd.fast(0, 10000) == 0) {
|
|
|
|
data = td::string(1 << 19, '\0');
|
|
|
|
}
|
|
|
|
q.push(next_queue_id(), data, now + rnd.fast(-10, 10) * 10 + 5, next_first_id());
|
2019-08-09 19:29:17 +02:00
|
|
|
};
|
|
|
|
auto inc_now = [&] {
|
|
|
|
now += 10;
|
2019-08-06 20:24:28 +02:00
|
|
|
};
|
2019-08-07 13:01:22 +02:00
|
|
|
auto check_head_tail = [&] {
|
2020-08-06 03:02:54 +02:00
|
|
|
q.check_head_tail(next_queue_id());
|
2019-08-07 13:01:22 +02:00
|
|
|
};
|
|
|
|
auto restart = [&] {
|
2019-09-06 17:55:19 +02:00
|
|
|
q.restart(rnd, now);
|
2019-08-07 13:01:22 +02:00
|
|
|
};
|
|
|
|
auto get = [&] {
|
2020-06-12 02:53:04 +02:00
|
|
|
q.check_get(next_queue_id(), rnd, now);
|
2019-08-07 13:01:22 +02:00
|
|
|
};
|
2020-06-11 23:46:47 +02:00
|
|
|
td::RandomSteps steps({{push_event, 100}, {check_head_tail, 10}, {get, 40}, {inc_now, 5}, {restart, 1}});
|
2020-06-11 22:54:56 +02:00
|
|
|
for (int i = 0; i < 100000; i++) {
|
2019-08-06 16:30:15 +02:00
|
|
|
steps.step(rnd);
|
|
|
|
}
|
2019-08-05 17:50:55 +02:00
|
|
|
}
|
2020-08-04 20:37:47 +02:00
|
|
|
|
|
|
|
TEST(TQueue, memory_leak) {
|
2020-08-04 21:48:23 +02:00
|
|
|
return;
|
2020-08-04 20:37:47 +02:00
|
|
|
auto tqueue = td::TQueue::create();
|
|
|
|
auto tqueue_binlog = td::make_unique<td::TQueueBinlog<td::Binlog>>();
|
|
|
|
std::string binlog_path = "test_tqueue.binlog";
|
|
|
|
td::Binlog::destroy(binlog_path).ensure();
|
|
|
|
auto binlog = std::make_shared<td::Binlog>();
|
|
|
|
binlog->init(binlog_path, [&](const td::BinlogEvent &event) { UNREACHABLE(); }).ensure();
|
|
|
|
tqueue_binlog->set_binlog(std::move(binlog));
|
|
|
|
tqueue->set_callback(std::move(tqueue_binlog));
|
|
|
|
|
2020-08-06 03:02:54 +02:00
|
|
|
td::int32 now = 0;
|
2020-08-04 20:37:47 +02:00
|
|
|
std::vector<td::TQueue::EventId> ids;
|
|
|
|
td::Random::Xorshift128plus rnd(123);
|
|
|
|
int i = 0;
|
|
|
|
while (true) {
|
|
|
|
auto id = tqueue->push(1, "a", now + 600000, 0, {}).move_as_ok();
|
|
|
|
ids.push_back(id);
|
2020-08-04 21:48:23 +02:00
|
|
|
if (ids.size() > static_cast<std::size_t>(rnd()) % 100000) {
|
|
|
|
auto it = static_cast<std::size_t>(rnd()) % ids.size();
|
2020-08-04 20:37:47 +02:00
|
|
|
std::swap(ids.back(), ids[it]);
|
|
|
|
tqueue->forget(1, ids.back());
|
|
|
|
ids.pop_back();
|
|
|
|
}
|
2020-08-06 03:02:54 +02:00
|
|
|
now++;
|
2020-08-04 20:37:47 +02:00
|
|
|
if (i++ % 100000 == 0) {
|
|
|
|
LOG(ERROR) << td::BufferAllocator::get_buffer_mem() << " " << tqueue->get_size(1) << " "
|
|
|
|
<< td::BufferAllocator::get_buffer_slice_size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 15:04:45 +01:00
|
|
|
|
|
|
|
TEST(TQueue, clear) {
|
|
|
|
auto tqueue = td::TQueue::create();
|
|
|
|
|
|
|
|
auto start_time = td::Time::now();
|
|
|
|
td::int32 now = 0;
|
|
|
|
td::vector<td::TQueue::EventId> ids;
|
|
|
|
td::Random::Xorshift128plus rnd(123);
|
|
|
|
for (size_t i = 0; i < 1000000; i++) {
|
2022-11-23 21:14:51 +01:00
|
|
|
tqueue->push(1, td::string(td::Random::fast(100, 500), 'a'), now + 600000, 0, {}).ensure();
|
2022-11-08 15:04:45 +01:00
|
|
|
}
|
|
|
|
auto tail_id = tqueue->get_tail(1);
|
|
|
|
auto clear_start_time = td::Time::now();
|
|
|
|
size_t keep_count = td::Random::fast(0, 2);
|
|
|
|
tqueue->clear(1, keep_count);
|
|
|
|
auto finish_time = td::Time::now();
|
|
|
|
LOG(INFO) << "Added TQueue events in " << clear_start_time - start_time << " seconds and cleared them in "
|
|
|
|
<< finish_time - clear_start_time << " seconds";
|
|
|
|
CHECK(tqueue->get_size(1) == keep_count);
|
|
|
|
CHECK(tqueue->get_head(1).advance(keep_count).ok() == tail_id);
|
|
|
|
CHECK(tqueue->get_tail(1) == tail_id);
|
|
|
|
}
|