2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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"
|
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>
|
|
|
|
|
|
|
|
using namespace td;
|
|
|
|
|
|
|
|
template <class ContainerT>
|
|
|
|
static typename ContainerT::value_type &rand_elem(ContainerT &cont) {
|
|
|
|
CHECK(0 < cont.size() && cont.size() <= static_cast<size_t>(std::numeric_limits<int>::max()));
|
|
|
|
return cont[Random::fast(0, static_cast<int>(cont.size()) - 1)];
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:41:31 +02:00
|
|
|
TEST(DB, binlog_encryption_bug) {
|
|
|
|
CSlice binlog_name = "test_binlog";
|
|
|
|
Binlog::destroy(binlog_name).ignore();
|
|
|
|
|
2020-11-18 17:55:42 +01:00
|
|
|
auto cucumber = DbKey::password("cucu'\"mb er");
|
2018-06-07 14:41:31 +02:00
|
|
|
auto empty = DbKey::empty();
|
|
|
|
{
|
|
|
|
Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
binlog
|
|
|
|
.init(
|
|
|
|
binlog_name.str(), [&](const BinlogEvent &x) {}, cucumber)
|
|
|
|
.ensure();
|
2018-06-07 14:41:31 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
binlog
|
|
|
|
.init(
|
|
|
|
binlog_name.str(), [&](const BinlogEvent &x) {}, cucumber)
|
|
|
|
.ensure();
|
2018-06-07 14:41:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TEST(DB, binlog_encryption) {
|
|
|
|
CSlice binlog_name = "test_binlog";
|
|
|
|
Binlog::destroy(binlog_name).ignore();
|
|
|
|
|
|
|
|
auto hello = DbKey::raw_key(std::string(32, 'A'));
|
2020-11-18 17:55:42 +01:00
|
|
|
auto cucumber = DbKey::password("cucu'\"mb er");
|
2018-12-31 20:04:05 +01:00
|
|
|
auto empty = DbKey::empty();
|
|
|
|
auto long_data = string(10000, 'Z');
|
|
|
|
{
|
|
|
|
Binlog binlog;
|
|
|
|
binlog.init(binlog_name.str(), [](const BinlogEvent &x) {}).ensure();
|
2018-06-28 21:20:48 +02:00
|
|
|
binlog.add_raw_event(BinlogEvent::create_raw(binlog.next_id(), 1, 0, create_storer("AAAA")),
|
|
|
|
BinlogDebugInfo{__FILE__, __LINE__});
|
|
|
|
binlog.add_raw_event(BinlogEvent::create_raw(binlog.next_id(), 1, 0, create_storer("BBBB")),
|
|
|
|
BinlogDebugInfo{__FILE__, __LINE__});
|
|
|
|
binlog.add_raw_event(BinlogEvent::create_raw(binlog.next_id(), 1, 0, create_storer(long_data)),
|
|
|
|
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";
|
2018-06-28 21:20:48 +02:00
|
|
|
binlog.add_raw_event(BinlogEvent::create_raw(binlog.next_id(), 1, 0, create_storer("CCCC")),
|
|
|
|
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 = [&] {
|
|
|
|
auto fd = FileFd::open(binlog_name, FileFd::Flags::Write | FileFd::Flags::Append).move_as_ok();
|
|
|
|
fd.write("abacabadaba").ensure();
|
|
|
|
};
|
|
|
|
|
|
|
|
add_suffix();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<string> v;
|
|
|
|
LOG(INFO) << "RESTART";
|
|
|
|
Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
binlog
|
|
|
|
.init(
|
|
|
|
binlog_name.str(), [&](const BinlogEvent &x) { v.push_back(x.data_.str()); }, hello)
|
|
|
|
.ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(v == std::vector<string>({"AAAA", "BBBB", long_data, "CCCC"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
add_suffix();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<string> v;
|
|
|
|
LOG(INFO) << "RESTART";
|
|
|
|
Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
auto status = binlog.init(
|
|
|
|
binlog_name.str(), [&](const BinlogEvent &x) { v.push_back(x.data_.str()); }, cucumber);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
add_suffix();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<string> v;
|
|
|
|
LOG(INFO) << "RESTART";
|
|
|
|
Binlog binlog;
|
2019-09-28 04:14:21 +02:00
|
|
|
auto status = binlog.init(
|
|
|
|
binlog_name.str(), [&](const BinlogEvent &x) { v.push_back(x.data_.str()); }, cucumber, hello);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(v == std::vector<string>({"AAAA", "BBBB", long_data, "CCCC"}));
|
|
|
|
}
|
2021-08-30 22:27:32 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
TEST(DB, sqlite_lfs) {
|
|
|
|
string path = "test_sqlite_db";
|
|
|
|
SqliteDb::destroy(path).ignore();
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::open_with_key(path, true, 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();
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DB, sqlite_encryption) {
|
|
|
|
string path = "test_sqlite_db";
|
|
|
|
SqliteDb::destroy(path).ignore();
|
|
|
|
|
|
|
|
auto empty = DbKey::empty();
|
2020-11-18 17:55:42 +01:00
|
|
|
auto cucumber = DbKey::password("cucu'\"mb er");
|
2018-12-31 20:04:05 +01:00
|
|
|
auto tomato = DbKey::raw_key(string(32, 'a'));
|
|
|
|
|
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::open_with_key(path, true, empty).move_as_ok();
|
2018-10-26 16:11:20 +02:00
|
|
|
db.set_user_version(123).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto kv = 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");
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::open_with_key(path, false, cucumber).ensure_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-22 19:17:37 +02:00
|
|
|
SqliteDb::change_key(path, false, cucumber, empty).ensure();
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::change_key(path, false, cucumber, empty).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::open_with_key(path, false, tomato).ensure_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::open_with_key(path, false, cucumber).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto kv = 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);
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::change_key(path, false, tomato, cucumber).ensure();
|
|
|
|
SqliteDb::change_key(path, false, tomato, cucumber).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::open_with_key(path, false, cucumber).ensure_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::open_with_key(path, false, tomato).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto kv = 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);
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::change_key(path, false, empty, tomato).ensure();
|
|
|
|
SqliteDb::change_key(path, false, empty, tomato).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::open_with_key(path, false, empty).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto kv = 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);
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::open_with_key(path, false, cucumber).ensure_error();
|
|
|
|
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) {
|
|
|
|
string path = "test_sqlite_db";
|
|
|
|
SqliteDb::destroy(path).ignore();
|
|
|
|
auto cucumber = DbKey::password("cucumber");
|
|
|
|
auto empty = DbKey::empty();
|
|
|
|
if (false) {
|
|
|
|
// sqlite_sample_db was generated by the following code using SQLCipher based on SQLite 3.15.2
|
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::change_key(path, true, cucumber, empty).move_as_ok();
|
2020-11-21 19:58:42 +01:00
|
|
|
db.set_user_version(123).ensure();
|
|
|
|
auto kv = SqliteKeyValue();
|
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
|
|
|
kv.set("hello", "world");
|
|
|
|
}
|
|
|
|
LOG(ERROR) << base64_encode(read_file(path).move_as_ok());
|
|
|
|
}
|
|
|
|
write_file(path, base64_decode(Slice(sqlite_sample_db_v3, sqlite_sample_db_v3_size)).move_as_ok()).ensure();
|
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::open_with_key(path, true, cucumber).move_as_ok();
|
2020-11-21 19:58:42 +01:00
|
|
|
auto kv = SqliteKeyValue();
|
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
|
|
|
CHECK(kv.get("hello") == "world");
|
|
|
|
CHECK(db.user_version().ok() == 123);
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::destroy(path).ignore();
|
2020-11-21 19:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DB, sqlite_encryption_migrate_v4) {
|
2020-08-12 17:36:26 +02:00
|
|
|
string path = "test_sqlite_db";
|
|
|
|
SqliteDb::destroy(path).ignore();
|
2020-11-18 17:55:42 +01:00
|
|
|
auto cucumber = DbKey::password("cucu'\"mb er");
|
2020-08-12 17:36:26 +02:00
|
|
|
auto empty = 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
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto db = SqliteDb::change_key(path, true, cucumber, empty).move_as_ok();
|
2020-08-12 17:36:26 +02:00
|
|
|
db.set_user_version(123).ensure();
|
|
|
|
auto kv = SqliteKeyValue();
|
|
|
|
kv.init_with_connection(db.clone(), "kv").ensure();
|
|
|
|
kv.set("hello", "world");
|
|
|
|
}
|
|
|
|
LOG(ERROR) << base64_encode(read_file(path).move_as_ok());
|
|
|
|
}
|
2020-11-21 19:58:42 +01:00
|
|
|
write_file(path, base64_decode(Slice(sqlite_sample_db_v4, sqlite_sample_db_v4_size)).move_as_ok()).ensure();
|
2020-08-12 17:36:26 +02:00
|
|
|
{
|
2021-09-22 18:04:56 +02:00
|
|
|
auto r_db = 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();
|
2020-08-12 17:36:26 +02:00
|
|
|
auto kv = 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
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::destroy(path).ignore();
|
2020-08-12 17:36:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
using SeqNo = uint64;
|
|
|
|
struct DbQuery {
|
2018-10-26 16:11:20 +02:00
|
|
|
enum class Type { Get, Set, Erase } type = Type::Get;
|
2018-12-31 20:04:05 +01:00
|
|
|
SeqNo tid = 0;
|
|
|
|
int32 id = 0;
|
|
|
|
string key;
|
|
|
|
string value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ImplT>
|
|
|
|
class QueryHandler {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ImplT impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SqliteKV {
|
|
|
|
public:
|
|
|
|
string get(string key) {
|
|
|
|
return kv_->get().get(key);
|
|
|
|
}
|
|
|
|
SeqNo set(string key, string value) {
|
|
|
|
kv_->get().set(key, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SeqNo erase(string key) {
|
|
|
|
kv_->get().erase(key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Status init(string name) {
|
2021-09-22 16:20:10 +02:00
|
|
|
auto sql_connection = std::make_shared<SqliteConnectionSafe>(name, DbKey::empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
kv_ = std::make_shared<SqliteKeyValueSafe>("kv", sql_connection);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
kv_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SqliteKeyValueSafe> kv_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BaselineKV {
|
|
|
|
public:
|
|
|
|
string get(string key) {
|
|
|
|
return map_[key];
|
|
|
|
}
|
|
|
|
SeqNo set(string key, string value) {
|
|
|
|
map_[key] = value;
|
|
|
|
return ++current_tid_;
|
|
|
|
}
|
|
|
|
SeqNo erase(string key) {
|
|
|
|
map_.erase(key);
|
|
|
|
return ++current_tid_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<string, string> map_;
|
|
|
|
SeqNo current_tid_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(DB, key_value) {
|
2018-05-19 22:45:13 +02:00
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
keys.push_back(rand_string('a', 'b', Random::fast(1, 10)));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
values.push_back(rand_string('a', 'b', Random::fast(1, 10)));
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:34:01 +01:00
|
|
|
int queries_n = 30000;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<DbQuery> queries(queries_n);
|
|
|
|
for (auto &q : queries) {
|
|
|
|
int op = Random::fast(0, 2);
|
|
|
|
const auto &key = rand_elem(keys);
|
|
|
|
const auto &value = rand_elem(values);
|
|
|
|
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;
|
|
|
|
q.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryHandler<BaselineKV> baseline;
|
|
|
|
QueryHandler<SeqKeyValue> kv;
|
|
|
|
QueryHandler<TsSeqKeyValue> ts_kv;
|
|
|
|
QueryHandler<BinlogKeyValue<Binlog>> new_kv;
|
|
|
|
|
|
|
|
CSlice new_kv_name = "test_new_kv";
|
|
|
|
Binlog::destroy(new_kv_name).ignore();
|
|
|
|
new_kv.impl().init(new_kv_name.str()).ensure();
|
|
|
|
|
|
|
|
QueryHandler<SqliteKeyValue> sqlite_kv;
|
2021-09-22 18:04:56 +02:00
|
|
|
CSlice path = "test_sqlite_kv";
|
|
|
|
SqliteDb::destroy(path).ignore();
|
|
|
|
auto db = SqliteDb::open_with_key(path, true, 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);
|
2020-11-22 15:34:01 +01:00
|
|
|
if (cnt++ % 5000 == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
new_kv.impl().init(new_kv_name.str()).ensure();
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::destroy(path).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) {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
keys.push_back(rand_string('a', 'b', Random::fast(1, 10)));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
values.push_back(rand_string('a', 'b', Random::fast(1, 10)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int threads_n = 4;
|
2020-11-22 15:34:01 +01:00
|
|
|
int queries_n = 10000;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::vector<std::vector<DbQuery>> queries(threads_n, std::vector<DbQuery>(queries_n));
|
|
|
|
for (auto &qs : queries) {
|
|
|
|
for (auto &q : qs) {
|
|
|
|
int op = Random::fast(0, 10);
|
|
|
|
const auto &key = rand_elem(keys);
|
|
|
|
const auto &value = rand_elem(values);
|
|
|
|
if (op > 1) {
|
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 == 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) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Set;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
|
|
|
q.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryHandler<BaselineKV> baseline;
|
|
|
|
QueryHandler<TsSeqKeyValue> ts_kv;
|
|
|
|
|
|
|
|
std::vector<thread> threads(threads_n);
|
|
|
|
std::vector<std::vector<DbQuery>> res(threads_n);
|
|
|
|
for (int i = 0; i < threads_n; i++) {
|
2020-02-26 16:15:19 +01:00
|
|
|
threads[i] = 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::size_t> pos(threads_n);
|
|
|
|
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) {
|
|
|
|
using KeyValue = BinlogKeyValue<ConcurrentBinlog>;
|
|
|
|
// using KeyValue = PersistentKeyValue;
|
|
|
|
// using KeyValue = SqliteKV;
|
2018-05-19 22:45:13 +02:00
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
2021-09-22 18:04:56 +02:00
|
|
|
CSlice path = "test_pmc";
|
|
|
|
Binlog::destroy(path).ignore();
|
|
|
|
SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
keys.push_back(rand_string('a', 'b', Random::fast(1, 10)));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
values.push_back(rand_string('a', 'b', Random::fast(1, 10)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryHandler<BaselineKV> baseline;
|
|
|
|
|
|
|
|
for (int iter = 0; iter < 25; iter++) {
|
|
|
|
int threads_n = 4;
|
|
|
|
int queries_n = 3000 / threads_n;
|
|
|
|
|
|
|
|
std::vector<std::vector<DbQuery>> queries(threads_n, std::vector<DbQuery>(queries_n));
|
|
|
|
for (auto &qs : queries) {
|
|
|
|
for (auto &q : qs) {
|
|
|
|
int op = Random::fast(0, 10);
|
|
|
|
const auto &key = rand_elem(keys);
|
|
|
|
const auto &value = rand_elem(values);
|
|
|
|
if (op > 1) {
|
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 == 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) {
|
2018-10-26 16:11:20 +02:00
|
|
|
q.type = DbQuery::Type::Set;
|
2018-12-31 20:04:05 +01:00
|
|
|
q.key = key;
|
|
|
|
q.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::vector<DbQuery>> res(threads_n);
|
2021-07-04 04:58:54 +02:00
|
|
|
class Worker final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
Worker(ActorShared<> parent, std::shared_ptr<QueryHandler<KeyValue>> kv, const std::vector<DbQuery> *queries,
|
|
|
|
std::vector<DbQuery> *res)
|
|
|
|
: 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:
|
|
|
|
ActorShared<> parent_;
|
|
|
|
std::shared_ptr<QueryHandler<KeyValue>> kv_;
|
|
|
|
const std::vector<DbQuery> *queries_;
|
|
|
|
std::vector<DbQuery> *res_;
|
|
|
|
};
|
2021-07-04 04:58:54 +02:00
|
|
|
class Main final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
Main(int threads_n, const std::vector<std::vector<DbQuery>> *queries, std::vector<std::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++) {
|
|
|
|
create_actor_on_scheduler<Worker>("Worker", i + 1, actor_shared(this, 2), kv_, &queries_->at(i), &res_->at(i))
|
|
|
|
.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();
|
|
|
|
Scheduler::instance()->finish();
|
|
|
|
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_;
|
|
|
|
const std::vector<std::vector<DbQuery>> *queries_;
|
|
|
|
std::vector<std::vector<DbQuery>> *res_;
|
|
|
|
|
|
|
|
std::shared_ptr<QueryHandler<KeyValue>> kv_{new QueryHandler<KeyValue>()};
|
|
|
|
int ref_cnt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
ConcurrentScheduler sched;
|
|
|
|
sched.init(threads_n);
|
|
|
|
sched.create_actor_unsafe<Main>(0, "Main", threads_n, &queries, &res).release();
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
|
|
|
|
std::vector<std::size_t> pos(threads_n);
|
|
|
|
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]++;
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 18:04:56 +02:00
|
|
|
SqliteDb::destroy(path).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|