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)
|
|
|
|
//
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "data.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/binlog/BinlogHelper.h"
|
2019-01-07 00:44:29 +01:00
|
|
|
#include "td/db/binlog/ConcurrentBinlog.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/BinlogKeyValue.h"
|
2019-10-22 19:01:15 +02:00
|
|
|
#include "td/db/DbKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/SeqKeyValue.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/db/SqliteConnectionSafe.h"
|
2019-04-26 00:47:25 +02:00
|
|
|
#include "td/db/SqliteDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
|
|
|
#include "td/db/SqliteKeyValueSafe.h"
|
|
|
|
#include "td/db/TsSeqKeyValue.h"
|
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
2020-11-23 01:24:36 +01:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2020-08-12 17:36:26 +02:00
|
|
|
#include "td/utils/base64.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-08-12 17:36:26 +02:00
|
|
|
#include "td/utils/filesystem.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/port/thread.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
template <class ContainerT>
|
|
|
|
static typename ContainerT::value_type &rand_elem(ContainerT &cont) {
|
2022-01-07 10:51:50 +01:00
|
|
|
CHECK(0 < cont.size() && cont.size() <= static_cast<std::size_t>(std::numeric_limits<int>::max()));
|
|
|
|
return cont[td::Random::fast(0, static_cast<int>(cont.size()) - 1)];
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 14:41:31 +02:00
|
|
|
TEST(DB, binlog_encryption_bug) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::CSlice binlog_name = "test_binlog";
|
|
|
|
td::Binlog::destroy(binlog_name).ignore();
|
2018-06-07 14:41:31 +02:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
auto cucumber = td::DbKey::password("cucu'\"mb er");
|
|
|
|
auto empty = td::DbKey::empty();
|
2018-06-07 14:41:31 +02:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
binlog
|
|
|
|
.init(
|
2022-01-07 10:51:50 +01:00
|
|
|
binlog_name.str(), [&](const td::BinlogEvent &x) {}, cucumber)
|
2019-09-28 04:14:21 +02:00
|
|
|
.ensure();
|
2018-06-07 14:41:31 +02:00
|
|
|
}
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
binlog
|
|
|
|
.init(
|
2022-01-07 10:51:50 +01:00
|
|
|
binlog_name.str(), [&](const td::BinlogEvent &x) {}, cucumber)
|
2019-09-28 04:14:21 +02:00
|
|
|
.ensure();
|
2018-06-07 14:41:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TEST(DB, binlog_encryption) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::CSlice binlog_name = "test_binlog";
|
|
|
|
td::Binlog::destroy(binlog_name).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
auto hello = td::DbKey::raw_key(td::string(32, 'A'));
|
|
|
|
auto cucumber = td::DbKey::password("cucu'\"mb er");
|
|
|
|
auto empty = td::DbKey::empty();
|
|
|
|
auto long_data = td::string(10000, 'Z');
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Binlog binlog;
|
|
|
|
binlog.init(binlog_name.str(), [](const td::BinlogEvent &x) {}).ensure();
|
2023-01-16 10:47:37 +01:00
|
|
|
binlog.add_raw_event(td::BinlogEvent::create_raw(binlog.next_event_id(), 1, 0, td::create_storer("AAAA")),
|
2022-01-07 10:51:50 +01:00
|
|
|
td::BinlogDebugInfo{__FILE__, __LINE__});
|
2023-01-16 10:47:37 +01:00
|
|
|
binlog.add_raw_event(td::BinlogEvent::create_raw(binlog.next_event_id(), 1, 0, td::create_storer("BBBB")),
|
2022-01-07 10:51:50 +01:00
|
|
|
td::BinlogDebugInfo{__FILE__, __LINE__});
|
2023-01-16 10:47:37 +01:00
|
|
|
binlog.add_raw_event(td::BinlogEvent::create_raw(binlog.next_event_id(), 1, 0, td::create_storer(long_data)),
|
2022-01-07 10:51:50 +01:00
|
|
|
td::BinlogDebugInfo{__FILE__, __LINE__});
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "SET PASSWORD";
|
|
|
|
binlog.change_key(cucumber);
|
|
|
|
binlog.change_key(hello);
|
|
|
|
LOG(INFO) << "OK";
|
2023-01-16 10:47:37 +01:00
|
|
|
binlog.add_raw_event(td::BinlogEvent::create_raw(binlog.next_event_id(), 1, 0, td::create_storer("CCCC")),
|
2022-01-07 10:51:50 +01:00
|
|
|
td::BinlogDebugInfo{__FILE__, __LINE__});
|
2018-12-31 20:04:05 +01:00
|
|
|
binlog.close().ensure();
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:19:59 +01:00
|
|
|
return;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto add_suffix = [&] {
|
2022-01-07 10:51:50 +01:00
|
|
|
auto fd = td::FileFd::open(binlog_name, td::FileFd::Flags::Write | td::FileFd::Flags::Append).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
fd.write("abacabadaba").ensure();
|
|
|
|
};
|
|
|
|
|
|
|
|
add_suffix();
|
|
|
|
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::string> v;
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "RESTART";
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
binlog
|
|
|
|
.init(
|
2023-02-02 01:08:26 +01:00
|
|
|
binlog_name.str(), [&](const td::BinlogEvent &x) { v.push_back(x.get_data().str()); }, hello)
|
2019-09-28 04:14:21 +02:00
|
|
|
.ensure();
|
2022-01-07 10:51:50 +01:00
|
|
|
CHECK(v == td::vector<td::string>({"AAAA", "BBBB", long_data, "CCCC"}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
add_suffix();
|
|
|
|
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::string> v;
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "RESTART";
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
auto status = binlog.init(
|
2023-02-02 01:08:26 +01:00
|
|
|
binlog_name.str(), [&](const td::BinlogEvent &x) { v.push_back(x.get_data().str()); }, cucumber);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
add_suffix();
|
|
|
|
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::string> v;
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "RESTART";
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
auto status = binlog.init(
|
2023-02-02 01:08:26 +01:00
|
|
|
binlog_name.str(), [&](const td::BinlogEvent &x) { v.push_back(x.get_data().str()); }, cucumber, hello);
|
2022-01-07 10:51:50 +01:00
|
|
|
CHECK(v == td::vector<td::string>({"AAAA", "BBBB", long_data, "CCCC"}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-08-30 22:27:32 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
TEST(DB, sqlite_lfs) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string path = "test_sqlite_db";
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
|
|
|
auto db = td::SqliteDb::open_with_key(path, true, td::DbKey::empty()).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
db.exec("PRAGMA journal_mode=WAL").ensure();
|
|
|
|
db.exec("PRAGMA user_version").ensure();
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DB, sqlite_encryption) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string path = "test_sqlite_db";
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
auto empty = td::DbKey::empty();
|
|
|
|
auto cucumber = td::DbKey::password("cucu'\"mb er");
|
|
|
|
auto tomato = td::DbKey::raw_key(td::string(32, 'a'));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::open_with_key(path, true, empty).move_as_ok();
|
2018-10-26 16:11:20 +02:00
|
|
|
db.set_user_version(123).ensure();
|
2022-01-07 10:51:50 +01:00
|
|
|
auto kv = td::SqliteKeyValue();
|
2018-07-17 23:54:30 +02:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
kv.set("a", "b");
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::open_with_key(path, false, cucumber).ensure_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::change_key(path, false, cucumber, empty).ensure();
|
|
|
|
td::SqliteDb::change_key(path, false, cucumber, empty).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::open_with_key(path, false, tomato).ensure_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::open_with_key(path, false, cucumber).move_as_ok();
|
|
|
|
auto kv = td::SqliteKeyValue();
|
2018-07-17 23:54:30 +02:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(kv.get("a") == "b");
|
|
|
|
CHECK(db.user_version().ok() == 123);
|
|
|
|
}
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::change_key(path, false, tomato, cucumber).ensure();
|
|
|
|
td::SqliteDb::change_key(path, false, tomato, cucumber).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::open_with_key(path, false, cucumber).ensure_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::open_with_key(path, false, tomato).move_as_ok();
|
|
|
|
auto kv = td::SqliteKeyValue();
|
2018-07-17 23:54:30 +02:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(kv.get("a") == "b");
|
|
|
|
CHECK(db.user_version().ok() == 123);
|
|
|
|
}
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::change_key(path, false, empty, tomato).ensure();
|
|
|
|
td::SqliteDb::change_key(path, false, empty, tomato).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::open_with_key(path, false, empty).move_as_ok();
|
|
|
|
auto kv = td::SqliteKeyValue();
|
2018-07-17 23:54:30 +02:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(kv.get("a") == "b");
|
|
|
|
CHECK(db.user_version().ok() == 123);
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::open_with_key(path, false, cucumber).ensure_error();
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:58:42 +01:00
|
|
|
TEST(DB, sqlite_encryption_migrate_v3) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string path = "test_sqlite_db";
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
|
|
|
auto cucumber = td::DbKey::password("cucumber");
|
|
|
|
auto empty = td::DbKey::empty();
|
2020-11-21 19:58:42 +01:00
|
|
|
if (false) {
|
|
|
|
// sqlite_sample_db was generated by the following code using SQLCipher based on SQLite 3.15.2
|
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::change_key(path, true, cucumber, empty).move_as_ok();
|
2020-11-21 19:58:42 +01:00
|
|
|
db.set_user_version(123).ensure();
|
2022-01-07 10:51:50 +01:00
|
|
|
auto kv = td::SqliteKeyValue();
|
2020-11-21 19:58:42 +01:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
|
|
|
kv.set("hello", "world");
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
LOG(ERROR) << td::base64_encode(td::read_file(path).move_as_ok());
|
2020-11-21 19:58:42 +01:00
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::write_file(path, td::base64_decode(td::Slice(sqlite_sample_db_v3, sqlite_sample_db_v3_size)).move_as_ok())
|
|
|
|
.ensure();
|
2020-11-21 19:58:42 +01:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::open_with_key(path, true, cucumber).move_as_ok();
|
|
|
|
auto kv = td::SqliteKeyValue();
|
2020-11-21 19:58:42 +01:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
|
|
|
CHECK(kv.get("hello") == "world");
|
|
|
|
CHECK(db.user_version().ok() == 123);
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2020-11-21 19:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DB, sqlite_encryption_migrate_v4) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string path = "test_sqlite_db";
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
|
|
|
auto cucumber = td::DbKey::password("cucu'\"mb er");
|
|
|
|
auto empty = td::DbKey::empty();
|
2020-08-13 15:57:28 +02:00
|
|
|
if (false) {
|
2020-11-21 19:58:42 +01:00
|
|
|
// sqlite_sample_db was generated by the following code using SQLCipher 4.4.0
|
2020-08-12 17:36:26 +02:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto db = td::SqliteDb::change_key(path, true, cucumber, empty).move_as_ok();
|
2020-08-12 17:36:26 +02:00
|
|
|
db.set_user_version(123).ensure();
|
2022-01-07 10:51:50 +01:00
|
|
|
auto kv = td::SqliteKeyValue();
|
2020-08-12 17:36:26 +02:00
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
|
|
|
kv.set("hello", "world");
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
LOG(ERROR) << td::base64_encode(td::read_file(path).move_as_ok());
|
2020-08-12 17:36:26 +02:00
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::write_file(path, td::base64_decode(td::Slice(sqlite_sample_db_v4, sqlite_sample_db_v4_size)).move_as_ok())
|
|
|
|
.ensure();
|
2020-08-12 17:36:26 +02:00
|
|
|
{
|
2022-01-07 10:51:50 +01:00
|
|
|
auto r_db = td::SqliteDb::open_with_key(path, true, cucumber);
|
2020-11-21 19:02:42 +01:00
|
|
|
if (r_db.is_error()) {
|
|
|
|
LOG(ERROR) << r_db.error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto db = r_db.move_as_ok();
|
2022-01-07 10:51:50 +01:00
|
|
|
auto kv = td::SqliteKeyValue();
|
2020-11-21 17:43:48 +01:00
|
|
|
auto status = kv.init_with_connection(db.clone(), "kv");
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << status;
|
|
|
|
} else {
|
|
|
|
CHECK(kv.get("hello") == "world");
|
|
|
|
CHECK(db.user_version().ok() == 123);
|
|
|
|
}
|
2020-08-12 17:36:26 +02:00
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2020-08-12 17:36:26 +02:00
|
|
|
}
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
using SeqNo = td::uint64;
|
2018-12-31 20:04:05 +01:00
|
|
|
struct DbQuery {
|
2023-02-12 01:36:52 +01:00
|
|
|
enum class Type { Get, Set, Erase, EraseBatch } type = Type::Get;
|
2018-12-31 20:04:05 +01:00
|
|
|
SeqNo tid = 0;
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string key;
|
|
|
|
td::string value;
|
2023-02-12 01:36:52 +01:00
|
|
|
|
|
|
|
// for EraseBatch
|
|
|
|
td::vector<td::string> erased_keys;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2023-02-12 01:36:52 +01:00
|
|
|
static td::StringBuilder &operator<<(td::StringBuilder &string_builder, const DbQuery &query) {
|
|
|
|
string_builder << "seq_no = " << query.tid << ": ";
|
|
|
|
switch (query.type) {
|
|
|
|
case DbQuery::Type::Get:
|
|
|
|
return string_builder << "Get " << query.key << " = " << query.value;
|
|
|
|
case DbQuery::Type::Set:
|
|
|
|
return string_builder << "Set " << query.key << " = " << query.value;
|
|
|
|
case DbQuery::Type::Erase:
|
|
|
|
return string_builder << "Del " << query.key;
|
|
|
|
case DbQuery::Type::EraseBatch:
|
|
|
|
return string_builder << "Del " << query.erased_keys;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ImplT>
|
|
|
|
class QueryHandler {
|
2021-12-12 13:32:36 +01:00
|
|
|
public:
|
|
|
|
ImplT &impl() {
|
|
|
|
return impl_;
|
|
|
|
}
|
|
|
|
void do_query(DbQuery &query) {
|
|
|
|
switch (query.type) {
|
|
|
|
case DbQuery::Type::Get:
|
|
|
|
query.value = impl_.get(query.key);
|
|
|
|
return;
|
|
|
|
case DbQuery::Type::Set:
|
|
|
|
impl_.set(query.key, query.value);
|
|
|
|
query.tid = 1;
|
|
|
|
return;
|
|
|
|
case DbQuery::Type::Erase:
|
|
|
|
impl_.erase(query.key);
|
|
|
|
query.tid = 1;
|
|
|
|
return;
|
2023-02-12 01:36:52 +01:00
|
|
|
case DbQuery::Type::EraseBatch:
|
|
|
|
impl_.erase_batch(query.erased_keys);
|
|
|
|
query.tid = 1;
|
|
|
|
return;
|
2021-12-12 13:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ImplT impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ImplT>
|
|
|
|
class SeqQueryHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
ImplT &impl() {
|
|
|
|
return impl_;
|
|
|
|
}
|
|
|
|
void do_query(DbQuery &query) {
|
|
|
|
switch (query.type) {
|
2018-10-26 16:11:20 +02:00
|
|
|
case DbQuery::Type::Get:
|
2018-12-31 20:04:05 +01:00
|
|
|
query.value = impl_.get(query.key);
|
|
|
|
return;
|
2018-10-26 16:11:20 +02:00
|
|
|
case DbQuery::Type::Set:
|
2018-12-31 20:04:05 +01:00
|
|
|
query.tid = impl_.set(query.key, query.value);
|
|
|
|
return;
|
2018-10-26 16:11:20 +02:00
|
|
|
case DbQuery::Type::Erase:
|
2018-12-31 20:04:05 +01:00
|
|
|
query.tid = impl_.erase(query.key);
|
|
|
|
return;
|
2023-02-12 01:36:52 +01:00
|
|
|
case DbQuery::Type::EraseBatch:
|
|
|
|
query.tid = impl_.erase_batch(query.erased_keys);
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ImplT impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SqliteKV {
|
|
|
|
public:
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string get(const td::string &key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return kv_->get().get(key);
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
SeqNo set(const td::string &key, const td::string &value) {
|
2018-12-31 20:04:05 +01:00
|
|
|
kv_->get().set(key, value);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
SeqNo erase(const td::string &key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
kv_->get().erase(key);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-12 01:36:52 +01:00
|
|
|
SeqNo erase_batch(td::vector<td::string> keys) {
|
|
|
|
for (auto &key : keys) {
|
|
|
|
kv_->get().erase(key);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Status init(const td::string &name) {
|
|
|
|
auto sql_connection = std::make_shared<td::SqliteConnectionSafe>(name, td::DbKey::empty());
|
|
|
|
kv_ = std::make_shared<td::SqliteKeyValueSafe>("kv", sql_connection);
|
|
|
|
return td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
kv_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-07 10:51:50 +01:00
|
|
|
std::shared_ptr<td::SqliteKeyValueSafe> kv_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class BaselineKV {
|
|
|
|
public:
|
2022-01-07 10:51:50 +01:00
|
|
|
td::string get(const td::string &key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return map_[key];
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
SeqNo set(const td::string &key, td::string value) {
|
2021-10-21 11:51:16 +02:00
|
|
|
map_[key] = std::move(value);
|
2018-12-31 20:04:05 +01:00
|
|
|
return ++current_tid_;
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
SeqNo erase(const td::string &key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
map_.erase(key);
|
|
|
|
return ++current_tid_;
|
|
|
|
}
|
2023-02-12 01:36:52 +01:00
|
|
|
SeqNo erase_batch(td::vector<td::string> keys) {
|
|
|
|
for (auto &key : keys) {
|
|
|
|
map_.erase(key);
|
|
|
|
}
|
|
|
|
SeqNo result = current_tid_ + 1;
|
|
|
|
current_tid_ += map_.size();
|
|
|
|
return result;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2022-01-07 10:51:50 +01:00
|
|
|
std::map<td::string, td::string> map_;
|
2018-12-31 20:04:05 +01:00
|
|
|
SeqNo current_tid_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(DB, key_value) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::string> keys;
|
|
|
|
td::vector<td::string> values;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
keys.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-12-12 13:32:36 +01:00
|
|
|
for (int i = 0; i < 10; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
values.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-27 23:15:18 +02:00
|
|
|
int queries_n = 1000;
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<DbQuery> queries(queries_n);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &q : queries) {
|
2023-02-12 01:36:52 +01:00
|
|
|
int op = td::Random::fast(0, 3);
|
2018-12-31 20:04:05 +01:00
|
|
|
const auto &key = rand_elem(keys);
|
|
|
|
if (op == 0) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Get;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
|
|
|
} else if (op == 1) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Erase;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
|
|
|
} else if (op == 2) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Set;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
2023-02-12 01:36:52 +01:00
|
|
|
q.value = rand_elem(values);
|
|
|
|
} else if (op == 3) {
|
|
|
|
q.type = DbQuery::Type::EraseBatch;
|
|
|
|
q.erased_keys.resize(td::Random::fast(0, 3));
|
|
|
|
for (auto &erased_key : q.erased_keys) {
|
|
|
|
erased_key = rand_elem(keys);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryHandler<BaselineKV> baseline;
|
2022-01-07 10:51:50 +01:00
|
|
|
QueryHandler<td::SeqKeyValue> kv;
|
|
|
|
QueryHandler<td::TsSeqKeyValue> ts_kv;
|
|
|
|
QueryHandler<td::BinlogKeyValue<td::Binlog>> new_kv;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::CSlice new_kv_name = "test_new_kv";
|
|
|
|
td::Binlog::destroy(new_kv_name).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
new_kv.impl().init(new_kv_name.str()).ensure();
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
QueryHandler<td::SqliteKeyValue> sqlite_kv;
|
|
|
|
td::CSlice path = "test_sqlite_kv";
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
|
|
|
auto db = td::SqliteDb::open_with_key(path, true, td::DbKey::empty()).move_as_ok();
|
2021-09-22 16:05:52 +02:00
|
|
|
sqlite_kv.impl().init_with_connection(std::move(db), "KV").ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
for (auto &q : queries) {
|
|
|
|
DbQuery a = q;
|
|
|
|
DbQuery b = q;
|
|
|
|
DbQuery c = q;
|
|
|
|
DbQuery d = q;
|
|
|
|
DbQuery e = q;
|
|
|
|
baseline.do_query(a);
|
|
|
|
kv.do_query(b);
|
|
|
|
ts_kv.do_query(c);
|
|
|
|
sqlite_kv.do_query(d);
|
|
|
|
new_kv.do_query(e);
|
|
|
|
ASSERT_EQ(a.value, b.value);
|
|
|
|
ASSERT_EQ(a.value, c.value);
|
|
|
|
ASSERT_EQ(a.value, d.value);
|
|
|
|
ASSERT_EQ(a.value, e.value);
|
2022-07-27 23:15:18 +02:00
|
|
|
if (cnt++ % 200 == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
new_kv.impl().init(new_kv_name.str()).ensure();
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::destroy(path).ignore();
|
|
|
|
td::Binlog::destroy(new_kv_name).ignore();
|
2021-12-12 20:34:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DB, key_value_set_all) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::string> keys;
|
|
|
|
td::vector<td::string> values;
|
2021-12-12 20:34:19 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
keys.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2021-12-12 20:34:19 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
values.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2021-12-12 20:34:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteKeyValue sqlite_kv;
|
|
|
|
td::CSlice sqlite_kv_name = "test_sqlite_kv";
|
|
|
|
td::SqliteDb::destroy(sqlite_kv_name).ignore();
|
|
|
|
auto db = td::SqliteDb::open_with_key(sqlite_kv_name, true, td::DbKey::empty()).move_as_ok();
|
2021-12-12 20:34:19 +01:00
|
|
|
sqlite_kv.init_with_connection(std::move(db), "KV").ensure();
|
|
|
|
|
|
|
|
BaselineKV kv;
|
|
|
|
|
|
|
|
int queries_n = 100;
|
|
|
|
while (queries_n-- > 0) {
|
2022-01-07 10:51:50 +01:00
|
|
|
int cnt = td::Random::fast(0, 10);
|
2022-02-07 20:41:07 +01:00
|
|
|
td::FlatHashMap<td::string, td::string> key_values;
|
2021-12-12 20:34:19 +01:00
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
auto key = rand_elem(keys);
|
|
|
|
auto value = rand_elem(values);
|
|
|
|
key_values[key] = value;
|
|
|
|
kv.set(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite_kv.set_all(key_values);
|
|
|
|
|
|
|
|
for (auto &key : keys) {
|
|
|
|
CHECK(kv.get(key) == sqlite_kv.get(key));
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::destroy(sqlite_kv_name).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
2020-11-22 15:34:01 +01:00
|
|
|
TEST(DB, thread_key_value) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::string> keys;
|
|
|
|
td::vector<td::string> values;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
keys.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
values.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int threads_n = 4;
|
2020-11-22 15:34:01 +01:00
|
|
|
int queries_n = 10000;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::vector<DbQuery>> queries(threads_n, td::vector<DbQuery>(queries_n));
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &qs : queries) {
|
|
|
|
for (auto &q : qs) {
|
2022-01-07 10:51:50 +01:00
|
|
|
int op = td::Random::fast(0, 10);
|
2018-12-31 20:04:05 +01:00
|
|
|
const auto &key = rand_elem(keys);
|
2023-02-12 01:36:52 +01:00
|
|
|
if (op == 0) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Erase;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
|
|
|
} else if (op == 1) {
|
2023-02-12 01:36:52 +01:00
|
|
|
q.type = DbQuery::Type::EraseBatch;
|
|
|
|
q.erased_keys.resize(td::Random::fast(0, 3));
|
|
|
|
for (auto &erased_key : q.erased_keys) {
|
|
|
|
erased_key = rand_elem(keys);
|
|
|
|
}
|
|
|
|
} else if (op <= 6) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Set;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
2023-02-12 01:36:52 +01:00
|
|
|
q.value = rand_elem(values);
|
|
|
|
} else {
|
|
|
|
q.type = DbQuery::Type::Get;
|
|
|
|
q.key = key;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryHandler<BaselineKV> baseline;
|
2022-01-07 10:51:50 +01:00
|
|
|
SeqQueryHandler<td::TsSeqKeyValue> ts_kv;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::thread> threads(threads_n);
|
|
|
|
td::vector<td::vector<DbQuery>> res(threads_n);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < threads_n; i++) {
|
2021-12-12 13:32:36 +01:00
|
|
|
threads[i] = td::thread([&ts_kv, &queries, &res, i] {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto q : queries[i]) {
|
|
|
|
ts_kv.do_query(q);
|
|
|
|
res[i].push_back(q);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<std::size_t> pos(threads_n);
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
|
|
|
bool was = false;
|
|
|
|
for (int i = 0; i < threads_n; i++) {
|
|
|
|
auto p = pos[i];
|
|
|
|
if (p == res[i].size()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto &q = res[i][p];
|
|
|
|
if (q.tid == 0) {
|
2018-10-26 16:11:20 +02:00
|
|
|
if (q.type == DbQuery::Type::Get) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto nq = q;
|
|
|
|
baseline.do_query(nq);
|
|
|
|
if (nq.value == q.value) {
|
|
|
|
was = true;
|
|
|
|
pos[i]++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
was = true;
|
|
|
|
pos[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int best = -1;
|
|
|
|
SeqNo best_tid = 0;
|
|
|
|
for (int i = 0; i < threads_n; i++) {
|
|
|
|
auto p = pos[i];
|
|
|
|
if (p == res[i].size()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was = true;
|
|
|
|
auto &q = res[i][p];
|
|
|
|
if (q.tid != 0) {
|
|
|
|
if (best == -1 || q.tid < best_tid) {
|
|
|
|
best = i;
|
|
|
|
best_tid = q.tid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!was) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(best != -1);
|
|
|
|
baseline.do_query(res[best][pos[best]]);
|
|
|
|
pos[best]++;
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 15:34:01 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
TEST(DB, persistent_key_value) {
|
2022-01-07 10:51:50 +01:00
|
|
|
using KeyValue = td::BinlogKeyValue<td::ConcurrentBinlog>;
|
|
|
|
// using KeyValue = td::SqliteKeyValue;
|
|
|
|
td::vector<td::string> keys;
|
|
|
|
td::vector<td::string> values;
|
|
|
|
td::CSlice path = "test_pmc";
|
|
|
|
td::Binlog::destroy(path).ignore();
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
keys.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
values.push_back(td::rand_string('a', 'b', td::Random::fast(1, 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryHandler<BaselineKV> baseline;
|
|
|
|
|
|
|
|
for (int iter = 0; iter < 25; iter++) {
|
|
|
|
int threads_n = 4;
|
|
|
|
int queries_n = 3000 / threads_n;
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::vector<DbQuery>> queries(threads_n, td::vector<DbQuery>(queries_n));
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &qs : queries) {
|
|
|
|
for (auto &q : qs) {
|
2022-01-07 10:51:50 +01:00
|
|
|
int op = td::Random::fast(0, 10);
|
2018-12-31 20:04:05 +01:00
|
|
|
const auto &key = rand_elem(keys);
|
2023-02-12 01:36:52 +01:00
|
|
|
if (op == 0) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Erase;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
|
|
|
} else if (op == 1) {
|
2023-02-12 01:36:52 +01:00
|
|
|
q.type = DbQuery::Type::EraseBatch;
|
|
|
|
q.erased_keys.resize(td::Random::fast(0, 3));
|
|
|
|
for (auto &erased_key : q.erased_keys) {
|
|
|
|
erased_key = rand_elem(keys);
|
|
|
|
}
|
|
|
|
} else if (op <= 6) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Set;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
2023-02-12 01:36:52 +01:00
|
|
|
q.value = rand_elem(values);
|
|
|
|
} else {
|
|
|
|
q.type = DbQuery::Type::Get;
|
|
|
|
q.key = key;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<td::vector<DbQuery>> res(threads_n);
|
|
|
|
class Worker final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-01-07 10:51:50 +01:00
|
|
|
Worker(td::ActorShared<> parent, std::shared_ptr<SeqQueryHandler<KeyValue>> kv,
|
|
|
|
const td::vector<DbQuery> *queries, td::vector<DbQuery> *res)
|
2018-12-31 20:04:05 +01:00
|
|
|
: parent_(std::move(parent)), kv_(std::move(kv)), queries_(queries), res_(res) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto q : *queries_) {
|
|
|
|
kv_->do_query(q);
|
|
|
|
res_->push_back(q);
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-07 10:51:50 +01:00
|
|
|
td::ActorShared<> parent_;
|
2021-12-12 13:32:36 +01:00
|
|
|
std::shared_ptr<SeqQueryHandler<KeyValue>> kv_;
|
2022-01-07 10:51:50 +01:00
|
|
|
const td::vector<DbQuery> *queries_;
|
|
|
|
td::vector<DbQuery> *res_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-01-07 10:51:50 +01:00
|
|
|
class Main final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-01-07 10:51:50 +01:00
|
|
|
Main(int threads_n, const td::vector<td::vector<DbQuery>> *queries, td::vector<td::vector<DbQuery>> *res)
|
2018-10-26 16:11:20 +02:00
|
|
|
: threads_n_(threads_n), queries_(queries), res_(res), ref_cnt_(threads_n) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Start up";
|
2018-12-31 20:04:05 +01:00
|
|
|
kv_->impl().init("test_pmc").ensure();
|
|
|
|
for (int i = 0; i < threads_n_; i++) {
|
2022-01-07 10:51:50 +01:00
|
|
|
td::create_actor_on_scheduler<Worker>("Worker", i + 1, actor_shared(this, 2), kv_, &queries_->at(i),
|
|
|
|
&res_->at(i))
|
2018-12-31 20:04:05 +01:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Tear down";
|
2018-12-31 20:04:05 +01:00
|
|
|
// kv_->impl().close();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Hang up";
|
2018-12-31 20:04:05 +01:00
|
|
|
ref_cnt_--;
|
|
|
|
if (ref_cnt_ == 0) {
|
|
|
|
kv_->impl().close();
|
2022-01-07 10:51:50 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "BAD HANGUP";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int threads_n_;
|
2022-01-07 10:51:50 +01:00
|
|
|
const td::vector<td::vector<DbQuery>> *queries_;
|
|
|
|
td::vector<td::vector<DbQuery>> *res_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-12-12 13:32:36 +01:00
|
|
|
std::shared_ptr<SeqQueryHandler<KeyValue>> kv_{new SeqQueryHandler<KeyValue>()};
|
2018-12-31 20:04:05 +01:00
|
|
|
int ref_cnt_;
|
|
|
|
};
|
|
|
|
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.create_actor_unsafe<Main>(0, "Main", threads_n, &queries, &res).release();
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
|
2022-01-07 10:51:50 +01:00
|
|
|
td::vector<std::size_t> pos(threads_n);
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
|
|
|
bool was = false;
|
|
|
|
for (int i = 0; i < threads_n; i++) {
|
|
|
|
auto p = pos[i];
|
|
|
|
if (p == res[i].size()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto &q = res[i][p];
|
|
|
|
if (q.tid == 0) {
|
2018-10-26 16:11:20 +02:00
|
|
|
if (q.type == DbQuery::Type::Get) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto nq = q;
|
|
|
|
baseline.do_query(nq);
|
|
|
|
if (nq.value == q.value) {
|
|
|
|
was = true;
|
|
|
|
pos[i]++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
was = true;
|
|
|
|
pos[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-12 01:36:52 +01:00
|
|
|
LOG(DEBUG) << pos;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int best = -1;
|
|
|
|
SeqNo best_tid = 0;
|
|
|
|
for (int i = 0; i < threads_n; i++) {
|
|
|
|
auto p = pos[i];
|
|
|
|
if (p == res[i].size()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was = true;
|
|
|
|
auto &q = res[i][p];
|
2023-02-12 01:36:52 +01:00
|
|
|
LOG(DEBUG) << i << ' ' << p << ' ' << q;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (q.tid != 0) {
|
|
|
|
if (best == -1 || q.tid < best_tid) {
|
|
|
|
best = i;
|
|
|
|
best_tid = q.tid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!was) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(best != -1);
|
|
|
|
baseline.do_query(res[best][pos[best]]);
|
|
|
|
pos[best]++;
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 10:51:50 +01:00
|
|
|
td::SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|