forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkey_value.cpp
272 lines (241 loc) · 8.42 KB
/
key_value.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "td/utils/tests.h"
#include "td/db/KeyValueAsync.h"
#include "td/db/KeyValue.h"
#include "td/db/RocksDb.h"
#include "td/utils/benchmark.h"
#include "td/utils/buffer.h"
#include "td/utils/optional.h"
#include "td/utils/UInt.h"
TEST(KeyValue, simple) {
td::Slice db_name = "testdb";
td::RocksDb::destroy(db_name).ignore();
std::unique_ptr<td::KeyValue> kv = std::make_unique<td::RocksDb>(td::RocksDb::open(db_name.str()).move_as_ok());
auto set_value = [&](td::Slice key, td::Slice value) {
kv->begin_transaction();
kv->set(key, value);
kv->commit_transaction();
};
auto ensure_value = [&](td::Slice key, td::Slice value) {
std::string kv_value;
auto status = kv->get(key, kv_value).move_as_ok();
ASSERT_EQ(td::int32(status), td::int32(td::KeyValue::GetStatus::Ok));
ASSERT_EQ(kv_value, value);
};
auto ensure_no_value = [&](td::Slice key) {
std::string kv_value;
auto status = kv->get(key, kv_value).move_as_ok();
ASSERT_EQ(td::int32(status), td::int32(td::KeyValue::GetStatus::NotFound));
};
ensure_no_value("A");
set_value("A", "HELLO");
ensure_value("A", "HELLO");
td::UInt128 x;
std::fill(as_slice(x).begin(), as_slice(x).end(), '1');
x.raw[5] = 0;
set_value(as_slice(x), as_slice(x));
ensure_value(as_slice(x), as_slice(x));
kv.reset();
td::RocksDbOptions options;
options.snapshot_statistics = std::make_shared<td::RocksDbSnapshotStatistics>();
kv = std::make_unique<td::RocksDb>(td::RocksDb::open(db_name.str(), options).move_as_ok());
ensure_value("A", "HELLO");
ensure_value(as_slice(x), as_slice(x));
CHECK(!options.snapshot_statistics->oldest_snapshot_timestamp());
auto snapshot = kv->snapshot();
CHECK(options.snapshot_statistics->oldest_snapshot_timestamp());
auto snapshot2 = kv->snapshot();
snapshot.reset();
CHECK(options.snapshot_statistics->oldest_snapshot_timestamp());
snapshot2.reset();
CHECK(!options.snapshot_statistics->oldest_snapshot_timestamp());
};
TEST(KeyValue, async_simple) {
td::Slice db_name = "testdb";
td::RocksDb::destroy(db_name).ignore();
td::actor::Scheduler scheduler({6});
auto watcher = td::create_shared_destructor([] { td::actor::SchedulerContext::get()->stop(); });
class Worker : public td::actor::Actor {
public:
Worker(std::shared_ptr<td::Destructor> watcher, std::string db_name)
: watcher_(std::move(watcher)), db_name_(std::move(db_name)) {
}
void start_up() override {
loop();
}
void tear_down() override {
}
void loop() override {
if (!kv_) {
kv_ = td::KeyValueAsync<td::UInt128, td::BufferSlice>(
std::make_unique<td::RocksDb>(td::RocksDb::open(db_name_).move_as_ok()));
set_start_at_ = td::Timestamp::now();
}
if (next_set_ && next_set_.is_in_past()) {
for (size_t i = 0; i < 10 && left_cnt_ > 0; i++, left_cnt_--) {
do_set();
}
if (left_cnt_ > 0) {
next_set_ = td::Timestamp::in(0.001);
alarm_timestamp() = next_set_;
} else {
next_set_ = td::Timestamp::never();
set_finish_at_ = td::Timestamp::now();
}
}
}
private:
std::shared_ptr<td::Destructor> watcher_;
td::optional<td::KeyValueAsync<td::UInt128, td::BufferSlice>> kv_;
std::string db_name_;
int left_cnt_ = 10000;
int pending_cnt_ = left_cnt_;
td::Timestamp next_set_ = td::Timestamp::now();
td::Timestamp set_start_at_;
td::Timestamp set_finish_at_;
void do_set() {
td::UInt128 key;
td::Random::secure_bytes(as_slice(key));
td::BufferSlice data(1024);
td::Random::secure_bytes(as_slice(data));
kv_.value().set(key, std::move(data), [actor_id = actor_id(this)](td::Result<td::Unit> res) {
res.ensure();
send_closure(actor_id, &Worker::on_stored);
});
}
void on_stored() {
pending_cnt_--;
if (pending_cnt_ == 0) {
auto now = td::Timestamp::now();
LOG(ERROR) << (now.at() - set_finish_at_.at());
LOG(ERROR) << (set_finish_at_.at() - set_start_at_.at());
stop();
}
}
};
scheduler.run_in_context([watcher = std::move(watcher), &db_name]() mutable {
td::actor::create_actor<Worker>("Worker", watcher, db_name.str()).release();
watcher.reset();
});
scheduler.run();
};
class KeyValueBenchmark : public td::Benchmark {
public:
std::string get_description() const override {
return "kv transation benchmark";
}
void start_up() override {
td::RocksDb::destroy("ttt");
db_ = td::RocksDb::open("ttt").move_as_ok();
}
void tear_down() override {
db_ = {};
}
void run(int n) override {
for (int i = 0; i < n; i++) {
db_.value().begin_transaction();
db_.value().set(PSLICE() << i, PSLICE() << i);
db_.value().commit_transaction();
}
}
private:
td::optional<td::RocksDb> db_;
};
TEST(KeyValue, Bench) {
td::bench(KeyValueBenchmark());
}
TEST(KeyValue, Stress) {
return;
td::Slice db_name = "testdb";
size_t N = 20;
auto db_name_i = [&](size_t i) { return PSTRING() << db_name << i; };
for (size_t i = 0; i < N; i++) {
td::RocksDb::destroy(db_name_i(i)).ignore();
}
td::actor::Scheduler scheduler({6});
auto watcher = td::create_shared_destructor([] { td::actor::SchedulerContext::get()->stop(); });
class Worker : public td::actor::Actor {
public:
Worker(std::shared_ptr<td::Destructor> watcher, std::string db_name)
: watcher_(std::move(watcher)), db_name_(std::move(db_name)) {
}
void start_up() override {
loop();
}
void tear_down() override {
}
void loop() override {
if (stat_at_.is_in_past()) {
stat_at_ = td::Timestamp::in(10);
LOG(ERROR) << db_->stats();
}
if (!kv_) {
db_ = std::make_shared<td::RocksDb>(td::RocksDb::open(db_name_).move_as_ok());
kv_ = td::KeyValueAsync<td::UInt128, td::BufferSlice>(db_);
set_start_at_ = td::Timestamp::now();
}
if (next_set_ && next_set_.is_in_past()) {
for (size_t i = 0; i < 10 && left_cnt_ > 0; i++, left_cnt_--) {
do_set();
}
if (left_cnt_ > 0) {
next_set_ = td::Timestamp::in(0.01);
alarm_timestamp() = next_set_;
} else {
next_set_ = td::Timestamp::never();
set_finish_at_ = td::Timestamp::now();
}
}
}
private:
std::shared_ptr<td::Destructor> watcher_;
std::shared_ptr<td::RocksDb> db_;
td::optional<td::KeyValueAsync<td::UInt128, td::BufferSlice>> kv_;
std::string db_name_;
int left_cnt_ = 1000000000;
int pending_cnt_ = left_cnt_;
td::Timestamp next_set_ = td::Timestamp::now();
td::Timestamp set_start_at_;
td::Timestamp set_finish_at_;
td::Timestamp stat_at_ = td::Timestamp::in(10);
void do_set() {
td::UInt128 key = td::UInt128::zero();
td::Random::secure_bytes(as_slice(key).substr(0, 1));
td::BufferSlice data(1024);
td::Random::secure_bytes(as_slice(data));
kv_.value().set(key, std::move(data), [actor_id = actor_id(this)](td::Result<td::Unit> res) {
res.ensure();
send_closure(actor_id, &Worker::on_stored);
});
}
void on_stored() {
pending_cnt_--;
if (pending_cnt_ == 0) {
auto now = td::Timestamp::now();
LOG(ERROR) << (now.at() - set_finish_at_.at());
LOG(ERROR) << (set_finish_at_.at() - set_start_at_.at());
stop();
}
}
};
scheduler.run_in_context([watcher = std::move(watcher), &db_name_i, &N]() mutable {
for (size_t i = 0; i < N; i++) {
td::actor::create_actor<Worker>("Worker", watcher, db_name_i(i)).release();
}
watcher.reset();
});
scheduler.run();
}