Server.cpp 5.1 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "Server.h"
#include "utils/CommonUtil.h"
#include "utils/SignalUtil.h"
#include "utils/TimeRecorder.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <csignal>
#include <numaif.h>
#include <unistd.h>
#include <string.h>

namespace zilliz {
namespace vecwise {
namespace server {

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

Server::Server()
    : opt_config_ptr_(nullptr){

}
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;
    }

G
groot 已提交
50
    CommonUtil::PrintInfo("Vecwise server run in daemonize mode");
G
groot 已提交
51 52 53 54 55 56 57 58 59 60 61 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

//    std::string log_path(GetLogDirFullPath());
//    log_path += "zdb_server.(INFO/WARNNING/ERROR/CRITICAL)";
//    CommonUtil::PrintInfo("Log will be exported to: " + log_path);

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

    CommonUtil::PrintInfo("Redirect stdin/stdout/stderr to /dev/null");

    // 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) {
            CommonUtil::PrintInfo("Can't open filename: " + pid_filename_ + ", Error: " + strerror(errno));
            exit(EXIT_FAILURE);
        }
        if (lockf(pid_fd, F_TLOCK, 0) < 0) {
            CommonUtil::PrintInfo("Can't lock filename: " + pid_filename_ + ", Error: " + strerror(errno));
            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() {
    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
            ServerConfig *config = ServerConfig::GetInstance();
            ConfigNode server_config = config->GetServerConfig();

            //print config into console and log
            opt_config_ptr_->PrintAll();

            // Handle Signal
            signal(SIGINT, SignalUtil::HandleSignal);
            signal(SIGHUP, SignalUtil::HandleSignal);
            signal(SIGTERM, SignalUtil::HandleSignal);

            StartService();

G
groot 已提交
159
            CommonUtil::PrintInfo("Vecwise server is running...");
G
groot 已提交
160 161

        } catch(std::exception& ex){
G
groot 已提交
162
            std::string info = "Vecwise server encounter exception: " + std::string(ex.what());
G
groot 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175
            CommonUtil::PrintError(info);

            CommonUtil::PrintInfo("Is another server instance running?");
            break;
        }
    } while(false);

    Stop();
    return 0;
}

void
Server::Stop() {
G
groot 已提交
176
    CommonUtil::PrintInfo("Vecwise server will be closed");
G
groot 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    // Unlock and close lockfile
    if (pid_fd != -1) {
        int ret = lockf(pid_fd, F_ULOCK, 0);
        if(ret != 0){

        }
        ret = close(pid_fd);
        if(ret != 0){

        }
    }

    // Try to delete lockfile
    if (!pid_filename_.empty()) {
        int ret = unlink(pid_filename_.c_str());
        if(ret != 0){

        }
    }

    running_ = 0;

    StopService();


G
groot 已提交
203
    CommonUtil::PrintInfo("Vecwise server closed");
G
groot 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
}


ServerError
Server::LoadConfig() {
    opt_config_ptr_ = ServerConfig::GetInstance();
    opt_config_ptr_->LoadConfigFile(config_filename_);

    return SERVER_SUCCESS;
}

void
Server::StartService() {

}

void
Server::StopService() {

}

}
}
}