Server.cpp 6.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

Y
Yu Kun 已提交
18
#include <thread>
G
groot 已提交
19
#include "Server.h"
Y
yudong.cai 已提交
20
#include "server/grpc_impl/GrpcServer.h"
G
groot 已提交
21
#include "utils/Log.h"
22
#include "utils/LogUtil.h"
G
groot 已提交
23 24
#include "utils/SignalUtil.h"
#include "utils/TimeRecorder.h"
Y
yu yunfeng 已提交
25
#include "metrics/Metrics.h"
G
groot 已提交
26

G
groot 已提交
27 28 29 30
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <csignal>
Y
yu yunfeng 已提交
31
//#include <numaif.h>
G
groot 已提交
32 33
#include <unistd.h>
#include <string.h>
W
wxyu 已提交
34
#include <src/scheduler/SchedInst.h>
X
xiaojun.lin 已提交
35
#include "src/wrapper/KnowhereResource.h"
G
groot 已提交
36

Y
yu yunfeng 已提交
37
#include "metrics/Metrics.h"
S
starlord 已提交
38
#include "DBWrapper.h"
Y
yu yunfeng 已提交
39

40

G
groot 已提交
41
namespace zilliz {
J
jinhai 已提交
42
namespace milvus {
G
groot 已提交
43 44
namespace server {

45
Server &
G
groot 已提交
46 47
Server::Instance() {
    static Server server;
S
starlord 已提交
48
    return server;
G
groot 已提交
49 50
}

G
groot 已提交
51
Server::Server() {
G
groot 已提交
52 53 54 55 56 57 58

}
Server::~Server() {

}

void
59 60 61 62
Server::Init(int64_t daemonized,
             const std::string &pid_filename,
             const std::string &config_filename,
             const std::string &log_config_file) {
G
groot 已提交
63 64 65
    daemonized_ = daemonized;
    pid_filename_ = pid_filename;
    config_filename_ = config_filename;
66
    log_config_file_ = log_config_file;
G
groot 已提交
67 68 69 70 71 72 73 74
}

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

75
    std::cout << "Milvus server run in daemonize mode";
G
groot 已提交
76 77 78

//    std::string log_path(GetLogDirFullPath());
//    log_path += "zdb_server.(INFO/WARNNING/ERROR/CRITICAL)";
G
groot 已提交
79
//    SERVER_LOG_INFO << "Log will be exported to: " + log_path);
G
groot 已提交
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 113 114 115 116 117 118 119 120 121

    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("/");
122
    if (ret != 0) {
G
groot 已提交
123 124 125 126 127 128 129 130
        return;
    }

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

131
    std::cout << "Redirect stdin/stdout/stderr to /dev/null";
G
groot 已提交
132 133 134 135 136 137 138 139 140

    // 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) {
S
starlord 已提交
141
            std::cerr << "Can't open filename: " + pid_filename_ + ", Error: " + strerror(errno);
G
groot 已提交
142 143 144
            exit(EXIT_FAILURE);
        }
        if (lockf(pid_fd, F_TLOCK, 0) < 0) {
S
starlord 已提交
145
            std::cerr << "Can't lock filename: " + pid_filename_ + ", Error: " + strerror(errno);
G
groot 已提交
146 147 148 149 150
            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());
151
        if (res != 0) {
G
groot 已提交
152 153 154 155 156
            return;
        }
    }
}

Y
yudong.cai 已提交
157
void
G
groot 已提交
158 159 160 161 162
Server::Start() {
    if (daemonized_) {
        Daemonize();
    }

Y
yudong.cai 已提交
163 164 165 166 167 168
    try {
        /* Read config file */
        if (LoadConfig() != SERVER_SUCCESS) {
            std::cerr << "Milvus server fail to load config file" << std::endl;
            return;
        }
G
groot 已提交
169

Y
yudong.cai 已提交
170 171 172
        /* log path is defined in Config file, so InitLog must be called after LoadConfig */
        ServerConfig &config = ServerConfig::GetInstance();
        ConfigNode server_config = config.GetConfig(CONFIG_SERVER);
G
groot 已提交
173

Y
yudong.cai 已提交
174 175 176 177 178 179
        std::string time_zone = server_config.GetValue(CONFIG_TIME_ZONE, "UTC+8");
        if (time_zone.length() == 3) {
            time_zone = "CUT";
        } else {
            int time_bias = std::stoi(time_zone.substr(3, std::string::npos));
            if (time_bias == 0)
180
                time_zone = "CUT";
Y
yudong.cai 已提交
181 182
            else if (time_bias > 0) {
                time_zone = "CUT" + std::to_string(-time_bias);
183
            } else {
Y
yudong.cai 已提交
184
                time_zone = "CUT+" + std::to_string(-time_bias);
185
            }
Y
yudong.cai 已提交
186
        }
187

Y
yudong.cai 已提交
188 189 190 191 192
        if (setenv("TZ", time_zone.c_str(), 1) != 0) {
            std::cerr << "Fail to setenv" << std::endl;
            return;
        }
        tzset();
S
starlord 已提交
193

Y
yudong.cai 已提交
194
        InitLog(log_config_file_);
G
groot 已提交
195

Y
yudong.cai 已提交
196 197
        server::Metrics::GetInstance().Init();
        server::SystemInfo::GetInstance().Init();
198

Y
yudong.cai 已提交
199
        StartService();
200
        std::cout << "Milvus server start successfully." << std::endl;
G
groot 已提交
201

Y
yudong.cai 已提交
202 203 204
    } catch (std::exception &ex) {
        std::cerr << "Milvus server encounter exception: " << ex.what();
    }
G
groot 已提交
205 206 207 208
}

void
Server::Stop() {
S
starlord 已提交
209
    std::cerr << "Milvus server is going to shutdown ..." << std::endl;
G
groot 已提交
210

Y
yudong.cai 已提交
211
    /* Unlock and close lockfile */
G
groot 已提交
212 213
    if (pid_fd != -1) {
        int ret = lockf(pid_fd, F_ULOCK, 0);
214
        if (ret != 0) {
S
starlord 已提交
215
            std::cerr << "Can't lock file: " << strerror(errno) << std::endl;
J
jinhai 已提交
216
            exit(0);
G
groot 已提交
217 218
        }
        ret = close(pid_fd);
219
        if (ret != 0) {
S
starlord 已提交
220
            std::cerr << "Can't close file: " << strerror(errno) << std::endl;
J
jinhai 已提交
221
            exit(0);
G
groot 已提交
222 223 224
        }
    }

Y
yudong.cai 已提交
225
    /* delete lockfile */
G
groot 已提交
226 227
    if (!pid_filename_.empty()) {
        int ret = unlink(pid_filename_.c_str());
228
        if (ret != 0) {
S
starlord 已提交
229
            std::cerr << "Can't unlink file: " << strerror(errno) << std::endl;
J
jinhai 已提交
230
            exit(0);
G
groot 已提交
231 232 233 234 235
        }
    }

    StopService();

S
starlord 已提交
236
    std::cerr << "Milvus server is closed!" << std::endl;
G
groot 已提交
237 238 239
}


S
starlord 已提交
240
ErrorCode
G
groot 已提交
241
Server::LoadConfig() {
G
groot 已提交
242
    ServerConfig::GetInstance().LoadConfigFile(config_filename_);
S
starlord 已提交
243 244 245
    auto status = ServerConfig::GetInstance().ValidateConfig();
    if (!status.ok()) {
        std::cerr << "Failed to load config file: " << config_filename_ << std::endl;
246 247
        exit(0);
    }
G
groot 已提交
248 249 250 251 252 253

    return SERVER_SUCCESS;
}

void
Server::StartService() {
S
starlord 已提交
254
    engine::KnowhereResource::Initialize();
S
starlord 已提交
255 256
    engine::StartSchedulerService();
    DBWrapper::GetInstance().StartService();
Y
yudong.cai 已提交
257
    grpc::GrpcServer::GetInstance().Start();
G
groot 已提交
258 259 260 261
}

void
Server::StopService() {
Y
yudong.cai 已提交
262
    grpc::GrpcServer::GetInstance().Stop();
S
starlord 已提交
263 264
    DBWrapper::GetInstance().StopService();
    engine::StopSchedulerService();
S
starlord 已提交
265
    engine::KnowhereResource::Finalize();
G
groot 已提交
266 267 268 269 270
}

}
}
}