close lockfile when server stop (#3737)

* close lockfile when server stop

Signed-off-by: Wang Xiangyu <xy.wang@zilliz.com>

* fix something error

Signed-off-by: Wang Xiangyu <xy.wang@zilliz.com>
Signed-off-by: shengjun.li <shengjun.li@zilliz.com>
pull/3805/head
Wang Xiangyu 2020-09-16 14:48:24 +08:00 committed by shengjun.li
parent 3b8a69a3e2
commit 1f5850a4a7
4 changed files with 26 additions and 9 deletions

View File

@ -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);

View File

@ -12,6 +12,8 @@
#pragma once
#include <string>
#include <vector>
#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<int64_t> fd_list_;
}; // Server
} // namespace milvus::server

View File

@ -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<int64_t>& 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

View File

@ -12,6 +12,7 @@
#pragma once
#include <string>
#include <vector>
#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<int64_t>& 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