Skip to content

Commit bc6b821

Browse files
committed
server: refactor init time
1 parent 2c9b4a5 commit bc6b821

File tree

6 files changed

+108
-59
lines changed

6 files changed

+108
-59
lines changed

src/modelbox/common/utils.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ Status ChownToUser(const std::string &user, const std::string &path) {
304304
Status RunAsUser(const std::string &user) {
305305
struct __user_cap_data_struct cap;
306306
struct __user_cap_header_struct header;
307+
#ifdef _LINUX_CAPABILITY_VERSION_3
308+
header.version = _LINUX_CAPABILITY_VERSION_3;
309+
#else
307310
header.version = _LINUX_CAPABILITY_VERSION;
311+
#endif
308312
header.pid = 0;
309313
uid_t uid = 0;
310314
gid_t gid = 0;

src/modelbox/server/include/modelbox/server/plugin.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,29 @@ class Plugin {
3636
virtual ~Plugin() = default;
3737

3838
/**
39-
* @brief Server init
40-
* @param config server configuration
39+
* @brief plugin init
40+
* @param config plugin configuration
4141
* @return init result
4242
*/
4343
virtual bool Init(std::shared_ptr<modelbox::Configuration> config) = 0;
4444

4545
/**
46-
* @brief Start server
46+
* @brief Start plugin
4747
* @return start result
4848
*/
4949
virtual bool Start() = 0;
5050

5151
/**
52-
* @brief Stop server
52+
* @brief Stop plugin
5353
* @return stop result
5454
*/
5555
virtual bool Stop() = 0;
56+
57+
/**
58+
* @brief Check plugin status
59+
* @return check result
60+
*/
61+
virtual bool Check() { return true; }
5662
};
5763

5864
using PluginRecvMsgFunc = std::function<void(
@@ -102,10 +108,9 @@ class PluginMsgRouter {
102108
modelbox::ThreadPool thread_pool_{2, -1, 100};
103109
};
104110

105-
106111
} // namespace modelbox
107112

108-
extern "C" {
113+
extern "C" {
109114

110115
#if defined(__clang__)
111116
#pragma clang diagnostic push

src/modelbox/server/main.cc

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
#define MODELBOX_SERVER_LOG_PATH "/var/log/modelbox/modelbox.log"
4242
#define MODELBOX_SERVER_PID_FILE "/var/run/modelbox.pid"
43-
#define MODELBOX_MAX_INIT_TIME (60 * 10)
43+
#define MODELBOX_MAX_INIT_TIME (60 * 12)
4444

4545
static int g_sig_list[] = {
4646
SIGIO, SIGPWR, SIGSTKFLT, SIGPROF, SIGINT, SIGTERM,
@@ -213,62 +213,62 @@ void modelbox_hung_check(const std::shared_ptr<modelbox::Server> &server) {
213213
}
214214

215215
int modelbox_run(const std::shared_ptr<modelbox::Server> &server) {
216-
bool is_server_init = true;
217-
std::shared_ptr<modelbox::TimerTask> heart_beattask =
218-
std::make_shared<modelbox::TimerTask>();
219-
heart_beattask->Callback(modelbox_hung_check, server);
220-
221-
auto future = std::async(
222-
std::launch::async, [heart_beattask, &is_server_init, server]() {
223-
if (app_monitor_init(nullptr, nullptr) != 0) {
224-
return;
225-
}
226-
227-
int count = 0;
228-
while (is_server_init == true) { // NOLINT
229-
sleep(1);
230-
if (count >= MODELBOX_MAX_INIT_TIME) {
231-
MBLOG_WARN << "exceed max init time, " << MODELBOX_MAX_INIT_TIME
232-
<< " seconds";
233-
break;
216+
int retval = 0;
217+
auto server_init_timer = std::make_shared<modelbox::TimerTask>([]() {
218+
MBLOG_INFO << "server init timeout, you may change the init timeout "
219+
"value by setting the init_timeout in modelbox.conf";
220+
modelbox::kServerTimer->Stop();
221+
modelbox::Abort("server init timeout");
222+
});
223+
224+
auto future =
225+
std::async(std::launch::async, [server, &retval, &server_init_timer]() {
226+
modelbox::Status ret;
227+
Defer {
228+
if (!ret) {
229+
modelbox::kServerTimer->Stop();
230+
retval = 1;
234231
}
232+
};
235233

236-
count++;
237-
238-
if (count % app_monitor_heartbeat_interval() != 0) {
239-
continue;
240-
}
234+
ret = server->Init();
235+
if (!ret) {
236+
MBLOG_ERROR << "server init failed !";
237+
return 1;
238+
}
241239

242-
modelbox_hung_check(server);
240+
ret = server->Start();
241+
if (!ret) {
242+
MBLOG_ERROR << "server start failed !";
243+
return 1;
243244
}
244245

245-
sleep(1);
246+
server_init_timer->Stop();
247+
server_init_timer = nullptr;
246248

247-
MBLOG_INFO << "start manager heartbeat";
248-
modelbox::kServerTimer->Schedule(
249-
heart_beattask, 0, 1000 * app_monitor_heartbeat_interval(), true);
249+
return 0;
250250
});
251251

252-
auto ret = server->Init();
253-
if (!ret) {
254-
MBLOG_ERROR << "server init failed !";
255-
return 1;
252+
if (app_monitor_init(nullptr, nullptr) == 0) {
253+
MBLOG_INFO << "start manager heartbeat";
254+
std::shared_ptr<modelbox::TimerTask> heart_beattask =
255+
std::make_shared<modelbox::TimerTask>();
256+
heart_beattask->Callback(modelbox_hung_check, server);
257+
modelbox::kServerTimer->Schedule(
258+
heart_beattask, 0, 1000 * app_monitor_heartbeat_interval(), true);
256259
}
257260

258-
ret = server->Start();
259-
if (!ret) {
260-
MBLOG_ERROR << "server start failed !";
261-
return 1;
262-
}
263-
264-
is_server_init = false;
265-
future.get();
261+
auto init_timeout = modelbox::kConfig->GetUint32("server.init_timeout",
262+
MODELBOX_MAX_INIT_TIME);
263+
modelbox::kServerTimer->Schedule(server_init_timer, 1000 * init_timeout, 0,
264+
false);
266265

267266
// run timer loop.
268267
modelbox::kServerTimer->Run();
269268

269+
future.get();
270270
server->Stop();
271-
return 0;
271+
return retval;
272272
}
273273

274274
static void onexit() {}

src/modelbox/server/server.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ modelbox::Status Server::Start() {
7070
MBLOG_ERROR << "Plugin, start failed, " << plugin->PluginFile();
7171
return modelbox::STATUS_FAULT;
7272
}
73+
plugin->SetInit(true);
7374
last_plugin = plugin;
7475
}
7576
} catch (const std::exception &e) {
@@ -90,6 +91,9 @@ modelbox::Status Server::Start() {
9091
modelbox::Status Server::Stop() {
9192
MBLOG_INFO << "app server stop";
9293
for (auto &plugin : plugins_) {
94+
if (plugin->IsInit() == false) {
95+
continue;
96+
}
9397
plugin->Stop();
9498
}
9599

src/modelbox/server/server_plugin.cc

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ std::shared_ptr<ServerPlugin> ServerPlugin::MakePlugin(
5353
return std::make_shared<DlPlugin>(plugin_path);
5454
}
5555

56-
Status ServerPlugin::Check() {
57-
return STATUS_OK;
58-
}
56+
modelbox::Status ServerPlugin::Check() { return modelbox::STATUS_OK; }
57+
58+
std::string ServerPlugin::PluginFile() { return plugin_path_; }
59+
60+
bool ServerPlugin::IsInit() { return is_init_; }
61+
62+
void ServerPlugin::SetInit(bool init) { is_init_ = init; }
5963

6064
DlPlugin::DlPlugin(const std::string &plugin_path)
6165
: ServerPlugin(plugin_path) {}
@@ -106,24 +110,49 @@ modelbox::Status DlPlugin::Init(
106110
return {modelbox::STATUS_FAULT, errmsg};
107111
}
108112

109-
plugin_ = create_plugin_func();
110-
if (plugin_ == nullptr) {
113+
auto plugin = create_plugin_func();
114+
if (plugin == nullptr) {
111115
MBLOG_ERROR << "create plugin failed";
112116
return modelbox::STATUS_FAULT;
113117
}
114118

115-
if (!plugin_->Init(config)) {
116-
MBLOG_ERROR << "init plugin " << plugin_path_ << " failed";
119+
if (!plugin->Init(config)) {
117120
return modelbox::STATUS_FAULT;
118121
}
119122

123+
plugin_ = plugin;
120124
ret = modelbox::STATUS_OK;
121125

122126
return ret;
123127
}
124128

125-
modelbox::Status DlPlugin::Start() { return plugin_->Start(); }
129+
modelbox::Status DlPlugin::Start() {
130+
if (plugin_ == nullptr) {
131+
MBLOG_ERROR << "plugin is null";
132+
return modelbox::STATUS_FAULT;
133+
}
134+
135+
return plugin_->Start();
136+
}
126137

127-
modelbox::Status DlPlugin::Stop() { return plugin_->Stop(); }
138+
modelbox::Status DlPlugin::Stop() {
139+
if (plugin_ == nullptr) {
140+
return modelbox::STATUS_FAULT;
141+
}
142+
143+
return plugin_->Stop();
144+
}
145+
146+
modelbox::Status DlPlugin::Check() {
147+
if (plugin_ == nullptr) {
148+
return modelbox::STATUS_FAULT;
149+
}
150+
151+
if (plugin_->Check() == false) {
152+
return STATUS_FAULT;
153+
}
154+
155+
return STATUS_OK;
156+
}
128157

129158
} // namespace modelbox

src/modelbox/server/server_plugin.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ class ServerPlugin {
3939
virtual modelbox::Status Stop() = 0;
4040
virtual modelbox::Status Check();
4141

42-
inline std::string PluginFile() { return plugin_path_; };
43-
4442
static std::shared_ptr<ServerPlugin> MakePlugin(
4543
const std::string &plugin_path);
4644

45+
std::string PluginFile();
46+
4747
protected:
4848
std::string plugin_path_;
49+
50+
private:
51+
friend class Server;
52+
bool IsInit();
53+
void SetInit(bool init);
54+
bool is_init_{false};
4955
};
5056

5157
/**
@@ -60,6 +66,7 @@ class DlPlugin : public ServerPlugin {
6066
std::shared_ptr<modelbox::Configuration> config) override;
6167
modelbox::Status Start() override;
6268
modelbox::Status Stop() override;
69+
modelbox::Status Check() override;
6370

6471
private:
6572
std::shared_ptr<Plugin> plugin_;

0 commit comments

Comments
 (0)