forked from eBay/HomeBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomeblks_impl.cpp
More file actions
270 lines (232 loc) · 11.7 KB
/
homeblks_impl.cpp
File metadata and controls
270 lines (232 loc) · 11.7 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
/*********************************************************************************
* 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 <algorithm>
#include <iomgr/io_environment.hpp>
#include <homestore/homestore.hpp>
#include <homestore/replication_service.hpp>
#include <sisl/options/options.h>
#include "homeblks_impl.hpp"
#include "listener.hpp"
SISL_OPTION_GROUP(homeblocks,
(executor_type, "", "executor", "Executor to use for Future deferal",
::cxxopts::value< std::string >()->default_value("immediate"), "immediate|cpu|io"));
SISL_LOGGING_DEF(HOMEBLOCKS_LOG_MODS)
namespace homeblocks {
extern std::shared_ptr< HomeBlocks > init_homeblocks(std::weak_ptr< HomeBlocksApplication >&& application) {
LOGI("Initializing HomeBlocks");
auto inst = std::make_shared< HomeBlocksImpl >(std::move(application));
inst->init_homestore();
inst->init_cp();
return inst;
}
HomeBlocksStats HomeBlocksImpl::get_stats() const {
HomeBlocksStats s;
return s;
}
HomeBlocksImpl::~HomeBlocksImpl() {
homestore::hs()->shutdown();
homestore::HomeStore::reset_instance();
iomanager.stop();
}
HomeBlocksImpl::HomeBlocksImpl(std::weak_ptr< HomeBlocksApplication >&& application) :
_application(std::move(application)), sb_{HB_META_NAME} {
auto exe_type = SISL_OPTIONS["executor"].as< std::string >();
std::transform(exe_type.begin(), exe_type.end(), exe_type.begin(), ::tolower);
if ("immediate" == exe_type) [[likely]]
executor_ = &folly::QueuedImmediateExecutor::instance();
else if ("io" == exe_type)
executor_ = folly::getGlobalIOExecutor();
else if ("cpu" == exe_type)
executor_ = folly::getGlobalCPUExecutor();
else
RELEASE_ASSERT(false, "Unknown Folly Executor type: [{}]", exe_type);
LOGI("initialized with [executor={}]", exe_type);
}
DevType HomeBlocksImpl::get_device_type(std::string const& devname) {
const iomgr::drive_type dtype = iomgr::DriveInterface::get_drive_type(devname);
if (dtype == iomgr::drive_type::block_hdd || dtype == iomgr::drive_type::file_on_hdd) { return DevType::HDD; }
if (dtype == iomgr::drive_type::file_on_nvme || dtype == iomgr::drive_type::block_nvme) { return DevType::NVME; }
return DevType::UNSUPPORTED;
}
// repl application to init homestore
class HBReplApp : public homestore::ReplApplication {
public:
HBReplApp(homestore::repl_impl_type impl_type, bool tl_consistency, HomeBlocksImpl* hb,
std::weak_ptr< HomeBlocksApplication > ho_app) :
impl_type_(impl_type), tl_consistency_(tl_consistency), hb_(hb), ho_app_(ho_app) {}
// TODO: make this override after the base class in homestore adds a virtual destructor
virtual ~HBReplApp() = default;
// overrides
homestore::repl_impl_type get_impl_type() const override { return impl_type_; }
bool need_timeline_consistency() const override { return tl_consistency_; }
// this will be called by homestore when create_repl_dev is called;
std::shared_ptr< homestore::ReplDevListener > create_repl_dev_listener(homestore::group_id_t group_id) override {
return std::make_shared< HBListener >(hb_);
#if 0
std::scoped_lock lock_guard(_repl_sm_map_lock);
auto [it, inserted] = _repl_sm_map.emplace(group_id, nullptr);
if (inserted) { it->second = std::make_shared< ReplicationStateMachine >(hb_); }
return it->second;
#endif
}
void on_repl_devs_init_completed() override { hb_->on_init_complete(); }
std::pair< std::string, uint16_t > lookup_peer(homestore::replica_id_t uuid) const override {
// r1: should never come here;
RELEASE_ASSERT(false, "Unexpected to be called.");
return std::make_pair("", 0);
}
homestore::replica_id_t get_my_repl_id() const override { return hb_->our_uuid(); }
private:
homestore::repl_impl_type impl_type_;
bool tl_consistency_; // indicates whether this application needs timeline consistency;
HomeBlocksImpl* hb_;
std::weak_ptr< HomeBlocksApplication > ho_app_;
#if 0
std::map< homestore::group_id_t, std::shared_ptr< HBListener> > _repl_sm_map;
std::mutex _repl_sm_map_lock;
#endif
};
void HomeBlocksImpl::get_dev_info(shared< HomeBlocksApplication > app, std::vector< homestore::dev_info >& dev_info,
bool& has_data_dev, bool& has_fast_dev) {
for (auto const& dev : app->devices()) {
auto input_dev_type = dev.type;
auto detected_type = get_device_type(dev.path.string());
LOGD("Device {} detected as {}", dev.path.string(), detected_type);
auto final_type = (dev.type == DevType::AUTO_DETECT) ? detected_type : input_dev_type;
if (final_type == DevType::UNSUPPORTED) {
LOGW("Device {} is not supported, skipping", dev.path.string());
continue;
}
if (input_dev_type != DevType::AUTO_DETECT && detected_type != final_type) {
LOGW("Device {} detected as {}, but input type is {}, using input type", dev.path.string(), detected_type,
input_dev_type);
}
auto hs_type = (final_type == DevType::HDD) ? homestore::HSDevType::Data : homestore::HSDevType::Fast;
if (hs_type == homestore::HSDevType::Data) { has_data_dev = true; }
if (hs_type == homestore::HSDevType::Fast) { has_fast_dev = true; }
dev_info.emplace_back(std::filesystem::canonical(dev.path).string(), hs_type);
}
}
void HomeBlocksImpl::init_homestore() {
auto app = _application.lock();
RELEASE_ASSERT(app, "HomeObjectApplication lifetime unexpected!");
LOGI("Starting iomgr with {} threads, spdk: {}", app->threads(), false);
ioenvironment.with_iomgr(iomgr::iomgr_params{.num_threads = app->threads(), .is_spdk = app->spdk_mode()})
.with_http_server();
const uint64_t app_mem_size = app->app_mem_size() * 1024 * 1024 * 1024;
LOGI("Initialize and start HomeStore with app_mem_size = {}", app_mem_size);
std::vector< homestore::dev_info > device_info;
bool has_data_dev{false}, has_fast_dev{false};
get_dev_info(app, device_info, has_data_dev, has_fast_dev);
RELEASE_ASSERT(device_info.size() != 0, "No supported devices found!");
using namespace homestore;
// Note: timeline_consistency doesn't matter as we are using solo repl dev;
auto repl_app =
std::make_shared< HBReplApp >(repl_impl_type::solo, false /*timeline_consistency*/, this, _application);
bool need_format = homestore::hs()
->with_index_service(std::make_unique< HBIndexSvcCB >(this))
.with_repl_data_service(repl_app) // chunk selector defaulted to round_robine
.start(hs_input_params{.devices = device_info, .app_mem_size = app_mem_size},
[this]() { register_metablk_cb(); });
if (need_format) {
LOGI("We are starting for the first time. Formatting HomeStore. ");
if (has_data_dev && has_fast_dev) {
// NOTE: chunk_size, num_chunks only has to specify one, can be deduced from each other.
homestore::hs()->format_and_start({
{HS_SERVICE::META, hs_format_params{.dev_type = HSDevType::Fast, .size_pct = 9.0}},
{HS_SERVICE::LOG,
hs_format_params{
.dev_type = HSDevType::Fast, .size_pct = 45.0, .num_chunks = 0, .chunk_size = 32 * Mi}},
{HS_SERVICE::INDEX, hs_format_params{.dev_type = HSDevType::Fast, .size_pct = 45.0}},
{HS_SERVICE::REPLICATION,
hs_format_params{.dev_type = HSDevType::Data,
.size_pct = 95.0,
.num_chunks = 0, // num_chunks will be deduced from chunk_size
.chunk_size = HS_CHUNK_SIZE,
.block_size = DATA_BLK_SIZE}},
});
} else {
auto run_on_type = has_fast_dev ? homestore::HSDevType::Fast : homestore::HSDevType::Data;
LOGD("Running with Single mode, all service on {}", run_on_type);
homestore::hs()->format_and_start({
{HS_SERVICE::META, hs_format_params{.dev_type = run_on_type, .size_pct = 5.0}},
{HS_SERVICE::LOG,
hs_format_params{.dev_type = run_on_type, .size_pct = 10.0, .num_chunks = 0, .chunk_size = 32 * Mi}},
{HS_SERVICE::INDEX, hs_format_params{.dev_type = run_on_type, .size_pct = 5.0}},
{HS_SERVICE::REPLICATION,
hs_format_params{.dev_type = run_on_type,
.size_pct = 75.0,
.num_chunks = 0, // num_chunks will be deduced from chunk_size;
.chunk_size = HS_CHUNK_SIZE,
.block_size = DATA_BLK_SIZE}},
});
}
// repl_app->on_repl_devs_init_completed();
superblk_init();
}
recovery_done_ = true;
LOGI("Initialize and start HomeStore is successfully");
}
void HomeBlocksImpl::superblk_init() {
sb_.create(sizeof(homeblks_sb_t));
sb_->magic = HB_SB_MAGIC;
sb_->version = HB_SB_VER;
sb_->boot_cnt = 0;
sb_->init_flag(0);
sb_.write();
}
void HomeBlocksImpl::on_hb_meta_blk_found(sisl::byte_view const& buf, void* cookie) {
sb_.load(buf, cookie);
// sb verification
RELEASE_ASSERT_EQ(sb_->version, HB_SB_VER);
RELEASE_ASSERT_EQ(sb_->magic, HB_SB_MAGIC);
if (sb_->test_flag(SB_FLAGS_GRACEFUL_SHUTDOWN)) {
// if it is a gracefuln shutdown, this flag should be set again in shutdown routine;
sb_->clear_flag(SB_FLAGS_GRACEFUL_SHUTDOWN);
LOGI("System was shutdown gracefully");
} else {
LOGI("System experienced sudden crash since last boot");
}
++sb_->boot_cnt;
// avoid doing sb meta blk write in callback which will cause deadlock;
// the 1st CP should flush all dirty SB before taking traffic;
}
void HomeBlocksImpl::register_metablk_cb() {
// register some callbacks for metadata recovery;
using namespace homestore;
// HomeBlks SB
homestore::hs()->meta_service().register_handler(
HB_META_NAME,
[this](homestore::meta_blk* mblk, sisl::byte_view buf, size_t size) {
on_hb_meta_blk_found(std::move(buf), voidptr_cast(mblk));
},
nullptr /*recovery_comp_cb*/, true /* do_crc */);
}
void HomeBlocksImpl::on_init_complete() {
// this is called after HomeStore all recovery completed.
// Add anything that needs to be done here.
using namespace homestore;
// Volume SB
homestore::hs()->meta_service().register_handler(
Volume::VOL_META_NAME,
[this](homestore::meta_blk* mblk, sisl::byte_view buf, size_t size) {
on_vol_meta_blk_found(std::move(buf), voidptr_cast(mblk));
},
nullptr /*recovery_comp_cb*/, true /* do_crc */,
std::optional< meta_subtype_vec_t >({homestore::hs()->repl_service().get_meta_blk_name()}));
homestore::hs()->meta_service().read_sub_sb(Volume::VOL_META_NAME);
}
void HomeBlocksImpl::init_cp() {}
} // namespace homeblocks