From 1f5850a4a7314e3193ef6001e96d3fd6211770a6 Mon Sep 17 00:00:00 2001 From: Wang Xiangyu Date: Wed, 16 Sep 2020 14:48:24 +0800 Subject: [PATCH] close lockfile when server stop (#3737) * close lockfile when server stop Signed-off-by: Wang Xiangyu * fix something error Signed-off-by: Wang Xiangyu Signed-off-by: shengjun.li --- core/src/server/Server.cpp | 7 ++++++- core/src/server/Server.h | 5 ++++- core/src/server/init/Directory.cpp | 18 +++++++++++++----- core/src/server/init/Directory.h | 5 +++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/core/src/server/Server.cpp b/core/src/server/Server.cpp index 1781ce1bb3..b1994a863e 100644 --- a/core/src/server/Server.cpp +++ b/core/src/server/Server.cpp @@ -177,7 +177,7 @@ Server::Start() { STATUS_CHECK(Directory::Access(config.storage.path(), wal_path, config.logs.path())); if (config.system.lock.enable()) { - STATUS_CHECK(Directory::Lock(config.storage.path(), wal_path)); + STATUS_CHECK(Directory::Lock(config.storage.path(), wal_path, fd_list_)); } } @@ -214,6 +214,11 @@ void Server::Stop() { std::cerr << "Milvus server is going to shutdown ..." << std::endl; + for (auto fd : fd_list_) { + LOG_SERVER_DEBUG_C << "close directory lock file descriptor: " << fd; + close(fd); + } + /* Unlock and close lockfile */ if (pid_fd_ != -1) { int ret = lockf(pid_fd_, F_ULOCK, 0); diff --git a/core/src/server/Server.h b/core/src/server/Server.h index 307c7c6e34..c5ff94ee02 100644 --- a/core/src/server/Server.h +++ b/core/src/server/Server.h @@ -12,6 +12,8 @@ #pragma once #include +#include + #include "config/ServerConfig.h" #include "utils/Status.h" @@ -57,7 +59,8 @@ class Server { int pid_fd_ = -1; std::string pid_filename_; std::string config_filename_; - // ConfigMgrPtr config_mgr_; + /* Used for lock work directory */ + std::vector fd_list_; }; // Server } // namespace milvus::server diff --git a/core/src/server/init/Directory.cpp b/core/src/server/init/Directory.cpp index f3617b299a..9112ee8c06 100644 --- a/core/src/server/init/Directory.cpp +++ b/core/src/server/init/Directory.cpp @@ -33,10 +33,17 @@ Directory::Initialize(const std::string& storage_path, const std::string& wal_pa } Status -Directory::Lock(const std::string& storage_path, const std::string& wal_path) { +Directory::Lock(const std::string& storage_path, const std::string& wal_path, std::vector& fd_list) { try { - lock(storage_path); - lock(wal_path); + auto storage_fd = lock(storage_path); + if (storage_fd > 0) { + fd_list.push_back(storage_fd); + } + + auto wal_fd = lock(wal_path); + if (wal_fd > 0) { + fd_list.push_back(wal_fd); + } } catch (std::exception& ex) { return Status(SERVER_UNEXPECTED_ERROR, ex.what()); } @@ -72,10 +79,10 @@ Directory::init(const std::string& path) { } } -void +int64_t Directory::lock(const std::string& path) { if (path.empty()) { - return; + return -1; } std::string lock_path = path + "/lock"; auto fd = open(lock_path.c_str(), O_RDWR | O_CREAT | O_NOFOLLOW, 0640); @@ -113,6 +120,7 @@ Directory::lock(const std::string& path) { close(fd); throw std::runtime_error(msg); } + return fd; } void diff --git a/core/src/server/init/Directory.h b/core/src/server/init/Directory.h index ef650a7f29..a7ddf768c0 100644 --- a/core/src/server/init/Directory.h +++ b/core/src/server/init/Directory.h @@ -12,6 +12,7 @@ #pragma once #include +#include #include "utils/Status.h" @@ -23,7 +24,7 @@ class Directory { Initialize(const std::string& storage_path, const std::string& wal_path, const std::string& log_path); static Status - Lock(const std::string& storage_path, const std::string& wal_path); + Lock(const std::string& storage_path, const std::string& wal_path, std::vector& fd_list); static Status Access(const std::string& storage_path, const std::string& wal_path, const std::string& log_path); @@ -32,7 +33,7 @@ class Directory { static void init(const std::string& path); - static void + static int64_t lock(const std::string& path); static void