From a2d7a4072c8508124682e178fb6c47837e45bd36 Mon Sep 17 00:00:00 2001 From: groot Date: Mon, 10 Jun 2019 18:38:34 +0800 Subject: [PATCH 1/3] fix license check bug Former-commit-id: c1ad0e1db515ecc079ecad2bae31209ff70d95fa --- cpp/CHANGELOG.md | 1 + cpp/CMakeLists.txt | 8 +++---- cpp/src/license/LicenseCheck.cpp | 41 ++++++++++++++++++++++++++++---- cpp/src/license/LicenseCheck.h | 10 +++----- cpp/src/server/Server.cpp | 3 +++ 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/cpp/CHANGELOG.md b/cpp/CHANGELOG.md index feafe74a64..34d80abbbf 100644 --- a/cpp/CHANGELOG.md +++ b/cpp/CHANGELOG.md @@ -24,6 +24,7 @@ Please mark all change in change log and use the ticket from JIRA. - MS-32 - Fix thrift error - MS-34 - Fix prometheus-cpp thirdparty +- MS-67 - Fix license check bug ## Improvement diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 7362dc7597..a1b78571f8 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -82,10 +82,10 @@ endif() if(CMAKE_BUILD_TYPE STREQUAL "Release") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC -DELPP_THREAD_SAFE -fopenmp") - # if (GPU_VERSION STREQUAL "ON") - # set(ENABLE_LICENSE "ON") - # add_definitions("-DENABLE_LICENSE") - # endif () + if (GPU_VERSION STREQUAL "ON") + set(ENABLE_LICENSE "ON") + add_definitions("-DENABLE_LICENSE") + endif () else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -fPIC -DELPP_THREAD_SAFE -fopenmp") endif() diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp index ca8fb4f930..52bdd2c2fa 100644 --- a/cpp/src/license/LicenseCheck.cpp +++ b/cpp/src/license/LicenseCheck.cpp @@ -1,5 +1,7 @@ #include "LicenseCheck.h" #include +#include + #include #include //#include @@ -13,6 +15,16 @@ namespace zilliz { namespace vecwise { namespace server { +using IO_SERVICE = boost::asio::io_service; + +namespace { +IO_SERVICE& GetIOService() { + static IO_SERVICE io; + return io; +} + + +} // Part 1: Legality check @@ -84,11 +96,30 @@ LicenseCheck::AlterFile(const std::string &license_file_path, ServerError LicenseCheck::StartCountingDown(const std::string &license_file_path) { - if (!LicenseLibrary::IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; - boost::asio::io_service io; - boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); - t.async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, &t)); - io.run(); + if (!LicenseLibrary::IsFileExistent(license_file_path)) { + printf("license file not exist\n"); + exit(1); + } + + //create a thread to run AlterFile + std::thread io_thread([&]() { + boost::asio::io_service& io = GetIOService(); + boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); + t.async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, &t)); + io.run();//this thread will block here + }); + io_thread.detach(); + + return SERVER_SUCCESS; +} + +ServerError +LicenseCheck::StopCountingDown() { + boost::asio::io_service& io = GetIOService(); + if(!io.stopped()) { + io.stop(); + } + return SERVER_SUCCESS; } diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h index 9a22f57f5a..866e630356 100644 --- a/cpp/src/license/LicenseCheck.h +++ b/cpp/src/license/LicenseCheck.h @@ -14,13 +14,6 @@ namespace server { class LicenseCheck { public: - static LicenseCheck & - GetInstance() { - static LicenseCheck instance; - return instance; - }; - - // Part 1: Legality check static ServerError LegalityCheck(const std::string &license_file_path); @@ -36,6 +29,9 @@ class LicenseCheck { static ServerError StartCountingDown(const std::string &license_file_path); + static ServerError + StopCountingDown(); + private: diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index 5829f3e6a1..adede6eb2a 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -217,6 +217,9 @@ Server::Stop() { StopService(); +#ifdef ENABLE_LICENSE + server::LicenseCheck::StopCountingDown(); +#endif SERVER_LOG_INFO << "Vecwise server closed"; } From 51c9ffbaaf5e1bee6b3fc1f9d272fcdc636820b0 Mon Sep 17 00:00:00 2001 From: groot Date: Mon, 10 Jun 2019 19:16:44 +0800 Subject: [PATCH 2/3] fix license check bug Former-commit-id: 69114c03a3482ab0835e209b7c680d9b2a0f684c --- cpp/src/license/LicenseCheck.cpp | 47 ++++++++++++++++---------------- cpp/src/license/LicenseCheck.h | 35 ++++++++++++++---------- cpp/src/server/Server.cpp | 8 ++---- 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp index 52bdd2c2fa..e23eace2cf 100644 --- a/cpp/src/license/LicenseCheck.cpp +++ b/cpp/src/license/LicenseCheck.cpp @@ -9,25 +9,18 @@ #include #include #include +#include +#include namespace zilliz { namespace vecwise { namespace server { -using IO_SERVICE = boost::asio::io_service; - -namespace { -IO_SERVICE& GetIOService() { - static IO_SERVICE io; - return io; -} - +LicenseCheck::LicenseCheck() { } -// Part 1: Legality check - ServerError LicenseCheck::LegalityCheck(const std::string &license_file_path) { @@ -81,14 +74,16 @@ LicenseCheck::AlterFile(const std::string &license_file_path, const boost::system::error_code &ec, boost::asio::deadline_timer *pt) { - ServerError err = LegalityCheck(license_file_path); - if(err!=SERVER_SUCCESS) - { + ServerError err = LicenseCheck::LegalityCheck(license_file_path); + if(err!=SERVER_SUCCESS) { + printf("license file check error\n"); exit(1); } + printf("---runing---\n"); pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); - pt->async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, pt)); + pt->async_wait(boost::bind(LicenseCheck::AlterFile, license_file_path, boost::asio::placeholders::error, pt)); + return SERVER_SUCCESS; } @@ -102,22 +97,26 @@ LicenseCheck::StartCountingDown(const std::string &license_file_path) { } //create a thread to run AlterFile - std::thread io_thread([&]() { - boost::asio::io_service& io = GetIOService(); - boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); - t.async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, &t)); - io.run();//this thread will block here - }); - io_thread.detach(); + if(counting_thread_ == nullptr) { + counting_thread_ = std::make_shared([&]() { + boost::asio::deadline_timer t(io_service_, boost::posix_time::hours(1)); + t.async_wait(boost::bind(LicenseCheck::AlterFile, license_file_path, boost::asio::placeholders::error, &t)); + io_service_.run();//this thread will block here + }); + } return SERVER_SUCCESS; } ServerError LicenseCheck::StopCountingDown() { - boost::asio::io_service& io = GetIOService(); - if(!io.stopped()) { - io.stop(); + if(!io_service_.stopped()) { + io_service_.stop(); + } + + if(counting_thread_ != nullptr) { + counting_thread_->join(); + counting_thread_ = nullptr; } return SERVER_SUCCESS; diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h index 866e630356..15b39cab43 100644 --- a/cpp/src/license/LicenseCheck.h +++ b/cpp/src/license/LicenseCheck.h @@ -4,36 +4,43 @@ #include "LicenseLibrary.h" #include -#include -#include +#include +#include namespace zilliz { namespace vecwise { namespace server { class LicenseCheck { - public: - // Part 1: Legality check +private: + LicenseCheck(); + +public: + static LicenseCheck & + GetInstance() { + static LicenseCheck instance; + return instance; + }; + static ServerError LegalityCheck(const std::string &license_file_path); + ServerError + StartCountingDown(const std::string &license_file_path); - // Part 2: Timing check license + ServerError + StopCountingDown(); + +private: static ServerError AlterFile(const std::string &license_file_path, const boost::system::error_code &ec, boost::asio::deadline_timer *pt); - - static ServerError - StartCountingDown(const std::string &license_file_path); - - static ServerError - StopCountingDown(); - - private: - +private: + boost::asio::io_service io_service_; + std::shared_ptr counting_thread_; }; diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index adede6eb2a..8480c2450c 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -159,15 +159,13 @@ Server::Start() { ConfigNode license_config = config.GetConfig(CONFIG_LICENSE); std::string license_file_path = license_config.GetValue(CONFIG_LICENSE_PATH); SERVER_LOG_INFO << "License path: " << license_file_path; + if(server::LicenseCheck::LegalityCheck(license_file_path) != SERVER_SUCCESS) { SERVER_LOG_ERROR << "License check failed"; exit(1); } - if(server::LicenseCheck::StartCountingDown(license_file_path) != SERVER_SUCCESS) { - SERVER_LOG_ERROR << "License counter start error"; - exit(1); - } + server::LicenseCheck::GetInstance().StartCountingDown(license_file_path); #endif // Handle Signal @@ -218,7 +216,7 @@ Server::Stop() { StopService(); #ifdef ENABLE_LICENSE - server::LicenseCheck::StopCountingDown(); + server::LicenseCheck::GetInstance().StopCountingDown(); #endif SERVER_LOG_INFO << "Vecwise server closed"; From 24498cd466a83659bc6b6f6a94e12e2788611346 Mon Sep 17 00:00:00 2001 From: groot Date: Mon, 10 Jun 2019 19:25:51 +0800 Subject: [PATCH 3/3] fix license check bug Former-commit-id: 7b0fb269f41dd6c1b3bf415a51e9f97260b8ab80 --- cpp/src/license/LicenseCheck.cpp | 4 ++++ cpp/src/license/LicenseCheck.h | 1 + 2 files changed, 5 insertions(+) diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp index e23eace2cf..5f9efe0c38 100644 --- a/cpp/src/license/LicenseCheck.cpp +++ b/cpp/src/license/LicenseCheck.cpp @@ -21,6 +21,10 @@ LicenseCheck::LicenseCheck() { } +LicenseCheck::~LicenseCheck() { + StopCountingDown(); +} + ServerError LicenseCheck::LegalityCheck(const std::string &license_file_path) { diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h index 15b39cab43..c660f345b4 100644 --- a/cpp/src/license/LicenseCheck.h +++ b/cpp/src/license/LicenseCheck.h @@ -15,6 +15,7 @@ namespace server { class LicenseCheck { private: LicenseCheck(); + ~LicenseCheck(); public: static LicenseCheck &