forked from eBay/HomeBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_volume_io.cpp
More file actions
344 lines (288 loc) · 12 KB
/
test_volume_io.cpp
File metadata and controls
344 lines (288 loc) · 12 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*********************************************************************************
* Modifications Copyright 2017-2019 eBay Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*********************************************************************************/
#include <string>
#include <boost/icl/interval_set.hpp>
#include <folly/init/Init.h>
#include <gtest/gtest.h>
#include <sisl/options/options.h>
#include <sisl/flip/flip_client.hpp>
#include <iomgr/iomgr_flip.hpp>
#include <homeblks/home_blks.hpp>
#include <homeblks/volume_mgr.hpp>
#include <volume/volume.hpp>
#include "test_common.hpp"
SISL_LOGGING_INIT(HOMEBLOCKS_LOG_MODS)
SISL_OPTION_GROUP(test_volume_io_setup,
(num_vols, "", "num_vols", "number of volumes", ::cxxopts::value< uint32_t >()->default_value("2"),
"number"),
(num_blks, "", "num_blks", "number of volumes", ::cxxopts::value< uint32_t >()->default_value("1"),
"number"));
SISL_OPTIONS_ENABLE(logging, test_common_setup, test_volume_io_setup, homeblocks)
SISL_LOGGING_DECL(test_volume_io)
std::unique_ptr< test_common::HBTestHelper > g_helper;
using namespace homeblocks;
class VolumeIOImpl {
public:
VolumeIOImpl() { create_volume(); }
private:
VolumeInfo gen_vol_info(uint32_t vol_idx) {
VolumeInfo vol_info;
vol_info.name = "vol_" + std::to_string(vol_idx);
vol_info.size_bytes = 1024 * 1024 * 1024;
vol_info.page_size = 4096;
vol_info.id = hb_utils::gen_random_uuid();
return vol_info;
}
public:
void create_volume() {
auto vinfo = gen_vol_info(m_volume_id_++);
m_vol_name = vinfo.name;
m_vol_id = vinfo.id;
auto vol_mgr = g_helper->inst()->volume_manager();
auto ret = vol_mgr->create_volume(std::move(vinfo)).get();
ASSERT_TRUE(ret);
m_vol_ptr = vol_mgr->lookup_volume(m_vol_id);
ASSERT_TRUE(m_vol_ptr != nullptr);
}
void remove_volume() {
auto vol_mgr = g_helper->inst()->volume_manager();
auto ret = vol_mgr->remove_volume(m_vol_id).get();
ASSERT_TRUE(ret);
}
void reset() {
auto vol_mgr = g_helper->inst()->volume_manager();
m_vol_ptr = vol_mgr->lookup_volume(m_vol_id);
ASSERT_TRUE(m_vol_ptr != nullptr);
}
void get_random_non_overlapping_lba(lba_t& start_lba, uint32_t& nblks, uint64_t max_blks) {
if (start_lba != 0 && nblks != 0) {
lba_t end_lba = start_lba + nblks - 1;
auto new_range = boost::icl::interval< int >::closed(start_lba, end_lba);
// For user provided lba and nblks, check if they are already in flight.
std::lock_guard lock(m_mutex);
ASSERT_TRUE(m_inflight_ios.find(new_range) == m_inflight_ios.end());
m_inflight_ios.insert(new_range);
return;
}
do {
// Generate lba which are not overlapped with the inflight ios, otherwise
// we cant decide which io completed last and cant verify the data.
start_lba = rand() % max_blks;
nblks = std::max(1, rand() % 64);
lba_t end_lba = start_lba + nblks - 1;
auto new_range = boost::icl::interval< int >::closed(start_lba, end_lba);
std::lock_guard lock(m_mutex);
if (m_inflight_ios.find(new_range) == m_inflight_ios.end()) {
m_inflight_ios.insert(new_range);
break;
}
} while (true);
}
auto build_random_data(lba_t& start_lba, uint32_t& nblks) {
// Write upto 1-64 nblks * 4k = 256k size.
auto info = m_vol_ptr->info();
uint64_t page_size = info->page_size;
uint64_t max_blks = info->size_bytes / page_size;
get_random_non_overlapping_lba(start_lba, nblks, max_blks);
nblks = std::min(static_cast< uint64_t >(nblks), max_blks - static_cast< uint64_t >(start_lba));
auto data_size = nblks * page_size;
auto data = sisl::make_byte_array(data_size, 512);
auto data_bytes = data->bytes();
for (uint64_t i = 0, lba = start_lba; i < nblks; i++) {
uint64_t data_pattern = ((long long)rand() << 32) | rand();
test_common::HBTestHelper::fill_data_buf(data_bytes, page_size, data_pattern);
data_bytes += page_size;
{
// Store the lba to pattern mapping
std::lock_guard lock(m_mutex);
m_lba_data[lba] = data_pattern;
}
LOGDEBUG("Generate data vol={} lba={} pattern={}", m_vol_name, lba, data_pattern);
lba++;
}
return data;
}
void generate_io_single(shared< VolumeIOImpl > vol, lba_t start_lba = 0, uint32_t nblks = 0, bool wait = true) {
// Generate a single io with start lba and nblks.
test_common::Waiter waiter(1);
auto fut = waiter.start([this, vol, start_lba, nblks, &waiter]() mutable {
auto data = build_random_data(start_lba, nblks);
vol_interface_req_ptr req(new vol_interface_req{data->bytes(), start_lba, nblks});
auto vol_mgr = g_helper->inst()->volume_manager();
vol_mgr->write(m_vol_ptr, req)
.via(&folly::InlineExecutor::instance())
.thenValue([this, data, req, &waiter](auto&& result) {
ASSERT_FALSE(result.hasError());
{
std::lock_guard lock(m_mutex);
m_inflight_ios.erase(boost::icl::interval< int >::closed(req->lba, req->lba + req->nlbas - 1));
}
waiter.one_complete();
});
});
if (wait) { std::move(fut).get(); }
}
auto generate_io(lba_t start_lba = 0, uint32_t nblks = 0) {
auto data = build_random_data(start_lba, nblks);
vol_interface_req_ptr req(new vol_interface_req{data->bytes(), start_lba, nblks});
auto vol_mgr = g_helper->inst()->volume_manager();
vol_mgr->write(m_vol_ptr, req)
.via(&folly::InlineExecutor::instance())
.thenValue([this, req, data](auto&& result) {
ASSERT_FALSE(result.hasError());
{
std::lock_guard lock(m_mutex);
m_inflight_ios.erase(boost::icl::interval< int >::closed(req->lba, req->lba + req->nlbas - 1));
}
g_helper->runner().next_task();
});
}
void verify_all_data() {
for (auto& [lba, data_pattern] : m_lba_data) {
auto buffer = iomanager.iobuf_alloc(512, 4096);
vol_interface_req_ptr req(new vol_interface_req{buffer, lba, 1});
auto vol_mgr = g_helper->inst()->volume_manager();
vol_mgr->read(m_vol_ptr, req).get();
test_common::HBTestHelper::validate_data_buf(buffer, 4096, data_pattern);
LOGDEBUG("Verify data vol={} lba={} pattern={} {}", m_vol_name, lba, data_pattern,
*r_cast< uint64_t* >(buffer));
iomanager.iobuf_free(buffer);
}
}
#ifdef _PRERELEASE
void set_flip_point(const std::string flip_name) {
flip::FlipCondition null_cond;
flip::FlipFrequency freq;
freq.set_count(1);
freq.set_percent(100);
m_fc.inject_noreturn_flip(flip_name, {null_cond}, freq);
LOGI("Flip {} set", flip_name);
}
#endif
private:
#ifdef _PRERELEASE
flip::FlipClient m_fc{iomgr_flip::instance()};
#endif
std::mutex m_mutex;
std::string m_vol_name;
VolumePtr m_vol_ptr;
volume_id_t m_vol_id;
static inline uint32_t m_volume_id_{1};
// Mapping from lba to data patttern.
std::map< lba_t, uint64_t > m_lba_data;
boost::icl::interval_set< int > m_inflight_ios;
};
class VolumeIOTest : public ::testing::Test {
public:
void SetUp() override {
for (uint32_t i = 0; i < SISL_OPTIONS["num_vols"].as< uint32_t >(); i++) {
m_vols_impl.emplace_back(std::make_shared< VolumeIOImpl >());
}
}
void TearDown() override {
for (auto& vol : m_vols_impl) {
vol->remove_volume();
}
}
void generate_io_single(shared< VolumeIOImpl > vol, lba_t start_lba = 0, uint32_t nblks = 0, bool wait = true) {
vol->generate_io_single(vol, start_lba, nblks, wait);
}
void generate_io(shared< VolumeIOImpl > vol = nullptr, lba_t start_lba = 0, uint32_t nblks = 0, bool wait = true) {
// Generate a io based on num_io and qdepth with start lba and nblks.
g_helper->runner().set_task([this, vol, start_lba, nblks]() mutable {
if (vol == nullptr) {
// Get a random volume.
vol = m_vols_impl[rand() % m_vols_impl.size()];
}
vol->generate_io(start_lba, nblks);
});
if (wait) { g_helper->runner().execute().get(); }
LOGINFO("IO completed");
}
void verify_all_data(shared< VolumeIOImpl > vol_impl = nullptr) {
if (vol_impl) {
vol_impl->verify_all_data();
return;
}
for (auto& vol_impl : m_vols_impl) {
vol_impl->verify_all_data();
}
}
void restart(int shutdown_delay) {
g_helper->restart(shutdown_delay);
for (auto& vol_impl : m_vols_impl) {
vol_impl->reset();
}
}
std::vector< shared< VolumeIOImpl > >& volume_list() { return m_vols_impl; }
private:
std::vector< shared< VolumeIOImpl > > m_vols_impl;
};
TEST_F(VolumeIOTest, SingleVolumeWriteData) {
// Write and verify fixed LBA range to single volume multiple times.
auto vol = volume_list().back();
uint32_t nblks = 100;
lba_t start_lba = 1;
uint32_t num_iter = 1;
LOGINFO("Write and verify data with num_iter={} start={} nblks={}", num_iter, start_lba, nblks);
for (uint32_t i = 0; i < num_iter; i++) {
generate_io_single(vol, start_lba, nblks);
verify_all_data(vol);
}
// Verify data after restart.
restart(5);
LOGINFO("Verify data");
verify_all_data(vol);
// Write and verify again on same LBA range to single volume multiple times.
LOGINFO("Write and verify data with num_iter={} start={} nblks={}", num_iter, start_lba, nblks);
for (uint32_t i = 0; i < num_iter; i++) {
generate_io_single(vol, start_lba, nblks);
}
verify_all_data(vol);
LOGINFO("SingleVolumeWriteData test done.");
}
TEST_F(VolumeIOTest, MultipleVolumeWriteData) {
LOGINFO("Write data randomly on num_vols={} num_io={}", SISL_OPTIONS["num_vols"].as< uint32_t >(),
SISL_OPTIONS["num_io"].as< uint64_t >());
generate_io();
LOGINFO("Verify data");
verify_all_data();
restart(5);
LOGINFO("Verify data again");
verify_all_data();
LOGINFO("Write data randomly");
generate_io();
LOGINFO("Verify data");
verify_all_data();
LOGINFO("MultipleVolumeWriteData test done.");
}
int main(int argc, char* argv[]) {
int parsed_argc = argc;
char** orig_argv = argv;
std::vector< std::string > args;
for (int i = 0; i < argc; ++i) {
args.emplace_back(argv[i]);
}
::testing::InitGoogleTest(&parsed_argc, argv);
SISL_OPTIONS_LOAD(parsed_argc, argv, logging, test_common_setup, test_volume_io_setup, homeblocks);
spdlog::set_pattern("[%D %T%z] [%^%l%$] [%n] [%t] %v");
parsed_argc = 1;
auto f = ::folly::Init(&parsed_argc, &argv, true);
g_helper = std::make_unique< test_common::HBTestHelper >("test_volume_io", args, orig_argv);
g_helper->setup();
auto ret = RUN_ALL_TESTS();
g_helper->teardown();
return ret;
}