未验证 提交 4ab92f30 编写于 作者: B BossZou 提交者: GitHub

Add config to switch off web server (#2137)

* Add a config parameter to switch off http server (fix #2057)
Signed-off-by: NYhz <yinghao.zou@zilliz.com>

* modify web enable config name
Signed-off-by: NYhz <yinghao.zou@zilliz.com>

* fix cmake bug & output exanption msg
Signed-off-by: NYhz <yinghao.zou@zilliz.com>

* rename web enbale config as web_enable
Signed-off-by: NYhz <yinghao.zou@zilliz.com>

* optimize code
Signed-off-by: NYhz <yinghao.zou@zilliz.com>

* log out errno
Signed-off-by: Nyhz <413554850@qq.com>
上级 1b25bace
......@@ -19,6 +19,7 @@ Please mark all change in change log and use the issue from GitHub
- \#1962 Add api HasPartition
- \#1965 FAISS/NSG/HNSW/ANNOY use unified distance calculation algorithm
- \#2054 Check if CPU instruction sets are illegal
- \#2057 Add a config parameter to switch off http server
- \#2059 Add lock file avoid multiple instances modifying data at the same time
- \#2064 Warn when use SQLite as metadata management
- \#2111 Check GPU environment before start server
......
......@@ -322,7 +322,7 @@ else ()
endif ()
if (DEFINED ENV{MILVUS_OATPP_URL})
set(MILVUS_OATPP_URL "$ENV{MILVUS_OATPP_URL}")
set(OATPP_SOURCE_URL "$ENV{MILVUS_OATPP_URL}")
else ()
# set(OATPP_SOURCE_URL "https://github.com/oatpp/oatpp/archive/${OATPP_VERSION}.tar.gz")
set(OATPP_SOURCE_URL "https://github.com/BossZou/oatpp/archive/master.zip")
......
......@@ -23,6 +23,8 @@ version: 0.3
#----------------------+------------------------------------------------------------+------------+-----------------+
# time_zone | Use UTC-x or UTC+x to specify a time zone. | Timezone | UTC+8 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# web_enable | Enable web server or not. | Boolean | true |
#----------------------+------------------------------------------------------------+------------+-----------------+
# web_port | Port that Milvus web server monitors. | Integer | 19121 |
# | Port range (1024, 65535) | | |
#----------------------+------------------------------------------------------------+------------+-----------------+
......@@ -31,6 +33,7 @@ server_config:
port: 19530
deploy_mode: single
time_zone: UTC+8
web_enable: true
web_port: 19121
#----------------------+------------------------------------------------------------+------------+-----------------+
......
......@@ -23,6 +23,8 @@ version: 0.3
#----------------------+------------------------------------------------------------+------------+-----------------+
# time_zone | Use UTC-x or UTC+x to specify a time zone. | Timezone | UTC+8 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# web_enable | Enable web server or not. | Boolean | true |
#----------------------+------------------------------------------------------------+------------+-----------------+
# web_port | Port that Milvus web server monitors. | Integer | 19121 |
# | Port range (1024, 65535) | | |
#----------------------+------------------------------------------------------------+------------+-----------------+
......@@ -31,6 +33,7 @@ server_config:
port: 19530
deploy_mode: single
time_zone: UTC+8
web_enable: true
web_port: 19121
#----------------------+------------------------------------------------------------+------------+-----------------+
......
......@@ -50,6 +50,8 @@ const char* CONFIG_SERVER_DEPLOY_MODE = "deploy_mode";
const char* CONFIG_SERVER_DEPLOY_MODE_DEFAULT = "single";
const char* CONFIG_SERVER_TIME_ZONE = "time_zone";
const char* CONFIG_SERVER_TIME_ZONE_DEFAULT = "UTC+8";
const char* CONFIG_SERVER_WEB_ENABLE = "web_enable";
const char* CONFIG_SERVER_WEB_ENABLE_DEFAULT = "true";
const char* CONFIG_SERVER_WEB_PORT = "web_port";
const char* CONFIG_SERVER_WEB_PORT_DEFAULT = "19121";
......@@ -241,6 +243,9 @@ Config::ValidateConfig() {
std::string server_time_zone;
CONFIG_CHECK(GetServerConfigTimeZone(server_time_zone));
bool server_web_enable;
CONFIG_CHECK(GetServerConfigWebEnable(server_web_enable));
std::string server_web_port;
CONFIG_CHECK(GetServerConfigWebPort(server_web_port));
......@@ -372,6 +377,7 @@ Config::ResetDefaultConfig() {
CONFIG_CHECK(SetServerConfigPort(CONFIG_SERVER_PORT_DEFAULT));
CONFIG_CHECK(SetServerConfigDeployMode(CONFIG_SERVER_DEPLOY_MODE_DEFAULT));
CONFIG_CHECK(SetServerConfigTimeZone(CONFIG_SERVER_TIME_ZONE_DEFAULT));
CONFIG_CHECK(SetServerConfigWebEnable(CONFIG_SERVER_WEB_ENABLE_DEFAULT));
CONFIG_CHECK(SetServerConfigWebPort(CONFIG_SERVER_WEB_PORT_DEFAULT));
/* db config */
......@@ -860,6 +866,11 @@ Config::CheckServerConfigTimeZone(const std::string& value) {
return Status::OK();
}
Status
Config::CheckServerConfigWebEnable(const std::string& value) {
return ValidationUtil::ValidateStringIsBool(value);
}
Status
Config::CheckServerConfigWebPort(const std::string& value) {
if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
......@@ -1603,6 +1614,13 @@ Config::GetServerConfigTimeZone(std::string& value) {
return CheckServerConfigTimeZone(value);
}
Status
Config::GetServerConfigWebEnable(bool& value) {
std::string str = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_WEB_ENABLE, CONFIG_SERVER_WEB_ENABLE_DEFAULT);
CONFIG_CHECK(CheckServerConfigWebEnable(str));
return StringHelpFunctions::ConvertToBoolean(str, value);
}
Status
Config::GetServerConfigWebPort(std::string& value) {
value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_WEB_PORT, CONFIG_SERVER_WEB_PORT_DEFAULT);
......@@ -1972,6 +1990,12 @@ Config::SetServerConfigTimeZone(const std::string& value) {
return SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, value);
}
Status
Config::SetServerConfigWebEnable(const std::string& value) {
CONFIG_CHECK(CheckServerConfigWebEnable(value));
return SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_WEB_ENABLE, value);
}
Status
Config::SetServerConfigWebPort(const std::string& value) {
CONFIG_CHECK(CheckServerConfigWebPort(value));
......
......@@ -46,6 +46,8 @@ extern const char* CONFIG_SERVER_DEPLOY_MODE;
extern const char* CONFIG_SERVER_DEPLOY_MODE_DEFAULT;
extern const char* CONFIG_SERVER_TIME_ZONE;
extern const char* CONFIG_SERVER_TIME_ZONE_DEFAULT;
extern const char* CONFIG_SERVER_WEB_ENABLE;
extern const char* CONFIG_SERVER_WEB_ENABLE_DEFAULT;
extern const char* CONFIG_SERVER_WEB_PORT;
extern const char* CONFIG_SERVER_WEB_PORT_DEFAULT;
......@@ -208,6 +210,8 @@ class Config {
Status
CheckServerConfigTimeZone(const std::string& value);
Status
CheckServerConfigWebEnable(const std::string& value);
Status
CheckServerConfigWebPort(const std::string& value);
/* db config */
......@@ -319,6 +323,8 @@ class Config {
Status
GetServerConfigTimeZone(std::string& value);
Status
GetServerConfigWebEnable(bool& value);
Status
GetServerConfigWebPort(std::string& value);
/* db config */
......@@ -422,6 +428,8 @@ class Config {
Status
SetServerConfigTimeZone(const std::string& value);
Status
SetServerConfigWebEnable(const std::string& value);
Status
SetServerConfigWebPort(const std::string& value);
/* db config */
......
......@@ -22,7 +22,10 @@ namespace web {
void
WebServer::Start() {
if (nullptr == thread_ptr_) {
auto& config = Config::GetInstance();
bool enable = true;
config.GetServerConfigWebEnable(enable);
if (enable && nullptr == thread_ptr_) {
thread_ptr_ = std::make_shared<std::thread>(&WebServer::StartService, this);
}
}
......@@ -44,23 +47,21 @@ WebServer::StartService() {
Config& config = Config::GetInstance();
std::string port;
CONFIG_CHECK(config.GetServerConfigWebPort(port));
{
AppComponent components = AppComponent(std::stoi(port));
auto user_controller = WebController::createShared();
/* create ApiControllers and add endpoints to router */
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
auto user_controller = WebController::createShared();
auto router = components.http_router_.getObject();
user_controller->addEndpointsToRouter(router);
/* Get connection handler component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, connection_handler);
auto connection_handler = components.server_connection_handler_.getObject();
/* Get connection provider component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connection_provider);
auto connection_provider = components.server_connection_provider_.getObject();
/* create server */
auto server = oatpp::network::server::Server(connection_provider, connection_handler);
......@@ -77,12 +78,9 @@ WebServer::StartService() {
// start synchronously
server.run();
connection_handler->stop();
stop_thread.join();
}
oatpp::base::Environment::destroy();
return Status::OK();
......
......@@ -42,7 +42,8 @@ class AppComponent {
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(this->port_);
} catch (std::exception& e) {
std::string error_msg = "Cannot bind http port " + std::to_string(this->port_) +
". Check if the port is already used";
": " + e.what() +
" (errno:" + std::to_string(errno) + "details: " + strerror(errno) + ").";
std::cout << error_msg << std::endl;
throw std::runtime_error(error_msg);
}
......@@ -58,7 +59,7 @@ class AppComponent {
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, server_connection_handler_)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());
......
......@@ -120,7 +120,8 @@ LogCpuInfo() {
/*CPU information*/
std::fstream fcpu("/proc/cpuinfo", std::ios::in);
if (!fcpu.is_open()) {
LOG_SERVER_WARNING_ << "Cannot obtain CPU information. Open file /proc/cpuinfo fail: " << strerror(errno);
LOG_SERVER_WARNING_ << "Cannot obtain CPU information. Open file /proc/cpuinfo fail: " << strerror(errno)
<< "(errno: " << errno << ")";
return;
}
std::stringstream cpu_info_ss;
......
......@@ -524,6 +524,7 @@ static const char* CONTROLLER_TEST_VALID_CONFIG_STR =
" port: 19530\n"
" deploy_mode: single\n"
" time_zone: UTC+8\n"
" web_enable: true\n"
" web_port: 19121\n"
"\n"
"#----------------------+------------------------------------------------------------+------------+----------------"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册