Server.cpp 5.9 KB
Newer Older
G
groot 已提交
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
Y
Yu Kun 已提交
6
#include <thread>
G
groot 已提交
7
#include "Server.h"
Y
Yu Kun 已提交
8
#include "server/grpc_impl/GrpcMilvusServer.h"
G
groot 已提交
9
#include "utils/Log.h"
G
groot 已提交
10 11
#include "utils/SignalUtil.h"
#include "utils/TimeRecorder.h"
Y
yu yunfeng 已提交
12
#include "metrics/Metrics.h"
G
groot 已提交
13

G
groot 已提交
14 15 16 17
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <csignal>
Y
yu yunfeng 已提交
18
//#include <numaif.h>
G
groot 已提交
19 20
#include <unistd.h>
#include <string.h>
W
wxyu 已提交
21
#include <src/scheduler/SchedInst.h>
22
#include "knowhere/index/vector_index/gpu_ivf.h"
G
groot 已提交
23

Y
yu yunfeng 已提交
24
#include "metrics/Metrics.h"
S
starlord 已提交
25
#include "DBWrapper.h"
Y
yu yunfeng 已提交
26

G
groot 已提交
27
namespace zilliz {
J
jinhai 已提交
28
namespace milvus {
G
groot 已提交
29 30 31 32 33 34 35 36
namespace server {

Server*
Server::Instance() {
    static Server server;
    return &server;
}

G
groot 已提交
37
Server::Server() {
G
groot 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

}
Server::~Server() {

}

void
Server::Init(int64_t daemonized, const std::string& pid_filename, const std::string& config_filename) {
    daemonized_ = daemonized;
    pid_filename_ = pid_filename;
    config_filename_ = config_filename;
}

void
Server::Daemonize() {
    if (daemonized_ == 0) {
        return;
    }

J
jinhai 已提交
57
    SERVER_LOG_INFO << "Milvus server run in daemonize mode";
G
groot 已提交
58 59 60

//    std::string log_path(GetLogDirFullPath());
//    log_path += "zdb_server.(INFO/WARNNING/ERROR/CRITICAL)";
G
groot 已提交
61
//    SERVER_LOG_INFO << "Log will be exported to: " + log_path);
G
groot 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    pid_t pid = 0;

    // Fork off the parent process
    pid = fork();

    // An error occurred
    if (pid < 0) {
        exit(EXIT_FAILURE);
    }

    // Success: terminate parent
    if (pid > 0) {
        exit(EXIT_SUCCESS);
    }

    // On success: The child process becomes session leader
    if (setsid() < 0) {
        exit(EXIT_FAILURE);
    }

    // Ignore signal sent from child to parent process
    signal(SIGCHLD, SIG_IGN);

    // Fork off for the second time
    pid = fork();

    // An error occurred
    if (pid < 0) {
        exit(EXIT_FAILURE);
    }

    // Terminate the parent
    if (pid > 0) {
        exit(EXIT_SUCCESS);
    }

    // Set new file permissions
    umask(0);

    // Change the working directory to root
    int ret = chdir("/");
    if(ret != 0){
        return;
    }

    // Close all open fd
    for (long fd = sysconf(_SC_OPEN_MAX); fd > 0; fd--) {
        close(fd);
    }

G
groot 已提交
113
    SERVER_LOG_INFO << "Redirect stdin/stdout/stderr to /dev/null";
G
groot 已提交
114 115 116 117 118 119 120 121 122

    // Redirect stdin/stdout/stderr to /dev/null
    stdin = fopen("/dev/null", "r");
    stdout = fopen("/dev/null", "w+");
    stderr = fopen("/dev/null", "w+");
    // Try to write PID of daemon to lockfile
    if (!pid_filename_.empty()) {
        pid_fd = open(pid_filename_.c_str(), O_RDWR | O_CREAT, 0640);
        if (pid_fd < 0) {
G
groot 已提交
123
            SERVER_LOG_INFO << "Can't open filename: " + pid_filename_ + ", Error: " + strerror(errno);
G
groot 已提交
124 125 126
            exit(EXIT_FAILURE);
        }
        if (lockf(pid_fd, F_TLOCK, 0) < 0) {
G
groot 已提交
127
            SERVER_LOG_INFO << "Can't lock filename: " + pid_filename_ + ", Error: " + strerror(errno);
G
groot 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
            exit(EXIT_FAILURE);
        }

        std::string pid_file_context = std::to_string(getpid());
        ssize_t res = write(pid_fd, pid_file_context.c_str(), pid_file_context.size());
        if(res != 0){
            return;
        }
    }
}

int
Server::Start() {
Y
yu yunfeng 已提交
141

G
groot 已提交
142 143 144 145 146 147 148 149 150 151 152 153
    if (daemonized_) {
        Daemonize();
    }

    do {
        try {
            // Read config file
            if(LoadConfig() != SERVER_SUCCESS) {
                return 1;
            }

            //log path is defined by LoadConfig, so InitLog must be called after LoadConfig
G
groot 已提交
154 155
            ServerConfig &config = ServerConfig::GetInstance();
            ConfigNode server_config = config.GetConfig(CONFIG_SERVER);
G
groot 已提交
156 157 158 159 160

            // Handle Signal
            signal(SIGINT, SignalUtil::HandleSignal);
            signal(SIGHUP, SignalUtil::HandleSignal);
            signal(SIGTERM, SignalUtil::HandleSignal);
Y
yu yunfeng 已提交
161
            server::Metrics::GetInstance().Init();
Y
yu yunfeng 已提交
162
            server::SystemInfo::GetInstance().Init();
S
starlord 已提交
163

G
groot 已提交
164
            std::cout << "Milvus server start successfully." << std::endl;
G
groot 已提交
165
            StartService();
G
groot 已提交
166 167

        } catch(std::exception& ex){
J
jinhai 已提交
168
            SERVER_LOG_ERROR << "Milvus server encounter exception: " << std::string(ex.what())
G
groot 已提交
169
                             << "Is another server instance running?";
G
groot 已提交
170 171 172 173 174 175 176 177 178 179
            break;
        }
    } while(false);

    Stop();
    return 0;
}

void
Server::Stop() {
G
groot 已提交
180
    std::cout << "Milvus server is going to shutdown ..." << std::endl;
G
groot 已提交
181 182 183 184 185

    // Unlock and close lockfile
    if (pid_fd != -1) {
        int ret = lockf(pid_fd, F_ULOCK, 0);
        if(ret != 0){
G
groot 已提交
186
            std::cout << "Can't lock file: " << strerror(errno) << std::endl;
J
jinhai 已提交
187
            exit(0);
G
groot 已提交
188 189 190
        }
        ret = close(pid_fd);
        if(ret != 0){
G
groot 已提交
191
            std::cout << "Can't close file: " << strerror(errno) << std::endl;
J
jinhai 已提交
192
            exit(0);
G
groot 已提交
193 194 195 196 197 198 199
        }
    }

    // Try to delete lockfile
    if (!pid_filename_.empty()) {
        int ret = unlink(pid_filename_.c_str());
        if(ret != 0){
G
groot 已提交
200
            std::cout << "Can't unlink file: " << strerror(errno) << std::endl;
J
jinhai 已提交
201
            exit(0);
G
groot 已提交
202 203 204 205 206 207 208
        }
    }

    running_ = 0;

    StopService();

G
groot 已提交
209
    std::cout << "Milvus server is closed!" << std::endl;
G
groot 已提交
210 211 212 213 214
}


ServerError
Server::LoadConfig() {
G
groot 已提交
215
    ServerConfig::GetInstance().LoadConfigFile(config_filename_);
216 217 218 219
    ServerError err = ServerConfig::GetInstance().ValidateConfig();
    if(err != SERVER_SUCCESS){
        exit(0);
    }
G
groot 已提交
220 221 222 223 224 225

    return SERVER_SUCCESS;
}

void
Server::StartService() {
S
starlord 已提交
226 227
    engine::StartSchedulerService();
    DBWrapper::GetInstance().StartService();
Y
Yu Kun 已提交
228
    grpc::GrpcMilvusServer::StartService();
G
groot 已提交
229 230 231 232
}

void
Server::StopService() {
Y
Yu Kun 已提交
233
    grpc::GrpcMilvusServer::StopService();
S
starlord 已提交
234 235
    DBWrapper::GetInstance().StopService();
    engine::StopSchedulerService();
236
    knowhere::FaissGpuResourceMgr::GetInstance().Free(); // free gpu resource.
G
groot 已提交
237 238 239 240 241
}

}
}
}