From 85ba9e56546027ff10d8e5720c28aa7b300e3c9f Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 24 Jul 2020 06:57:48 +0300 Subject: [PATCH] Fix TQueue binlog replaying. GitOrigin-RevId: 172d70847206253de981d38fed329276b2f778e0 --- tddb/td/db/TQueue.cpp | 16 ++++++++++------ tddb/td/db/TQueue.h | 2 +- test/tqueue.cpp | 4 +++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tddb/td/db/TQueue.cpp b/tddb/td/db/TQueue.cpp index f3fddf97c201..4d82f6389e25 100644 --- a/tddb/td/db/TQueue.cpp +++ b/tddb/td/db/TQueue.cpp @@ -98,9 +98,12 @@ class TQueueImpl : public TQueue { bool do_push(QueueId queue_id, RawEvent &&raw_event) override { CHECK(raw_event.event_id.is_valid()); + // raw_event.data can be empty when replaying binlog + if (raw_event.data.size() > MAX_EVENT_LENGTH) { + return false; + } auto &q = queues_[queue_id]; - if (q.events.size() >= MAX_QUEUE_EVENTS || raw_event.data.empty() || raw_event.data.size() > MAX_EVENT_LENGTH || - q.total_event_length > MAX_TOTAL_EVENT_LENGTH - raw_event.data.size()) { + if (q.events.size() >= MAX_QUEUE_EVENTS || q.total_event_length > MAX_TOTAL_EVENT_LENGTH - raw_event.data.size()) { return false; } if (q.events.empty() || q.events.back().event_id < raw_event.event_id) { @@ -116,16 +119,17 @@ class TQueueImpl : public TQueue { } Result push(QueueId queue_id, string data, double expires_at, int64 extra, EventId hint_new_id) override { - auto &q = queues_[queue_id]; - if (q.events.size() >= MAX_QUEUE_EVENTS) { - return Status::Error("Queue is full"); - } if (data.empty()) { return Status::Error("Data is empty"); } if (data.size() > MAX_EVENT_LENGTH) { return Status::Error("Data is too big"); } + + auto &q = queues_[queue_id]; + if (q.events.size() >= MAX_QUEUE_EVENTS) { + return Status::Error("Queue is full"); + } if (q.total_event_length > MAX_TOTAL_EVENT_LENGTH - data.size()) { return Status::Error("Queue size is too big"); } diff --git a/tddb/td/db/TQueue.h b/tddb/td/db/TQueue.h index 2ae88bebfa4b..2c6843ad0720 100644 --- a/tddb/td/db/TQueue.h +++ b/tddb/td/db/TQueue.h @@ -128,7 +128,7 @@ class TQueueBinlog : public TQueue::StorageCallback { uint64 push(QueueId queue_id, const RawEvent &event) override; void pop(uint64 logevent_id) override; - Status replay(const BinlogEvent &binlog_event, TQueue &q) const; + Status replay(const BinlogEvent &binlog_event, TQueue &q) const TD_WARN_UNUSED_RESULT; void set_binlog(std::shared_ptr binlog) { binlog_ = std::move(binlog); diff --git a/test/tqueue.cpp b/test/tqueue.cpp index ca1d321a52dc..38a6c14c70eb 100644 --- a/test/tqueue.cpp +++ b/test/tqueue.cpp @@ -88,7 +88,9 @@ class TestTQueue { binlog_ = td::TQueue::create(); auto tqueue_binlog = td::make_unique>(); auto binlog = std::make_shared(); - binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { tqueue_binlog->replay(event, *binlog_); }) + binlog + ->init(binlog_path().str(), + [&](const td::BinlogEvent &event) { tqueue_binlog->replay(event, *binlog_).ignore(); }) .ensure(); tqueue_binlog->set_binlog(std::move(binlog)); binlog_->set_callback(std::move(tqueue_binlog));