main.cpp 5.1 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

G
groot 已提交
12
#include <getopt.h>
Y
yudong.cai 已提交
13
#include <unistd.h>
J
Jin Hai 已提交
14
#include <csignal>
S
starlord 已提交
15 16
#include <cstring>
#include <string>
G
groot 已提交
17

W
wxyu 已提交
18
#include "easyloggingpp/easylogging++.h"
Y
yudong.cai 已提交
19
#include "server/Server.h"
G
groot 已提交
20
#include "src/version.h"
C
Cai Yudong 已提交
21
#include "utils/SignalHandler.h"
B
BossZou 已提交
22
#include "utils/Status.h"
G
groot 已提交
23 24

INITIALIZE_EASYLOGGINGPP
G
groot 已提交
25

S
starlord 已提交
26
void
Y
youny626 已提交
27
print_help(const std::string& app_name) {
28 29
    std::cout << std::endl << "Usage: " << app_name << " [OPTIONS]" << std::endl << std::endl;
    std::cout << "  Options:" << std::endl;
J
Jin Hai 已提交
30 31 32 33
    std::cout << "   -h --help                 Print this help." << std::endl;
    std::cout << "   -c --conf_file filename   Read configuration from the file." << std::endl;
    std::cout << "   -d --daemon               Daemonize this application." << std::endl;
    std::cout << "   -p --pid_file  filename   PID file used by daemonized app." << std::endl;
34 35 36 37 38 39 40 41 42 43 44
    std::cout << std::endl;
}

void
print_banner() {
    std::cout << std::endl;
    std::cout << "    __  _________ _   ____  ______    " << std::endl;
    std::cout << "   /  |/  /  _/ /| | / / / / / __/    " << std::endl;
    std::cout << "  / /|_/ // // /_| |/ / /_/ /\\ \\    " << std::endl;
    std::cout << " /_/  /_/___/____/___/\\____/___/     " << std::endl;
    std::cout << std::endl;
J
Jin Hai 已提交
45
    std::cout << "Welcome to use Milvus!" << std::endl;
Z
Zhiru Zhu 已提交
46 47 48 49 50 51 52
    std::cout << "Milvus " << BUILD_TYPE << " version: v" << MILVUS_VERSION << ", built at " << BUILD_TIME << ", with "
#ifdef WITH_MKL
              << "MKL"
#else
              << "OpenBLAS"
#endif
              << " library." << std::endl;
Y
yudong.cai 已提交
53
#ifdef MILVUS_GPU_VERSION
G
groot 已提交
54
    std::cout << "You are using Milvus GPU edition" << std::endl;
Y
yudong.cai 已提交
55 56
#else
    std::cout << "You are using Milvus CPU edition" << std::endl;
Y
youny626 已提交
57
#endif
58
    std::cout << "Last commit id: " << LAST_COMMIT_ID << std::endl;
59 60
    std::cout << std::endl;
}
G
groot 已提交
61 62

int
Y
youny626 已提交
63
main(int argc, char* argv[]) {
64
    print_banner();
G
groot 已提交
65

Y
youny626 已提交
66 67 68 69 70
    static struct option long_options[] = {{"conf_file", required_argument, nullptr, 'c'},
                                           {"help", no_argument, nullptr, 'h'},
                                           {"daemon", no_argument, nullptr, 'd'},
                                           {"pid_file", required_argument, nullptr, 'p'},
                                           {nullptr, 0, nullptr, 0}};
G
groot 已提交
71 72 73 74

    int option_index = 0;
    int64_t start_daemonized = 0;

75
    std::string config_filename;
G
groot 已提交
76
    std::string pid_filename;
Y
yudong.cai 已提交
77
    std::string app_name = argv[0];
W
Wang XiangYu 已提交
78
    milvus::Status s;
G
groot 已提交
79

Y
youny626 已提交
80
    milvus::server::Server& server = milvus::server::Server::GetInstance();
81

Y
yudong.cai 已提交
82
    if (argc < 2) {
G
groot 已提交
83
        print_help(app_name);
84
        goto FAIL;
G
groot 已提交
85
    }
G
groot 已提交
86 87

    int value;
88
    while ((value = getopt_long(argc, argv, "c:p:dh", long_options, &option_index)) != -1) {
G
groot 已提交
89 90
        switch (value) {
            case 'c': {
Y
youny626 已提交
91
                char* config_filename_ptr = strdup(optarg);
G
groot 已提交
92 93
                config_filename = config_filename_ptr;
                free(config_filename_ptr);
G
groot 已提交
94
                std::cout << "Loading configuration from: " << config_filename << std::endl;
G
groot 已提交
95 96 97
                break;
            }
            case 'p': {
Y
youny626 已提交
98
                char* pid_filename_ptr = strdup(optarg);
G
groot 已提交
99 100
                pid_filename = pid_filename_ptr;
                free(pid_filename_ptr);
G
groot 已提交
101
                std::cout << pid_filename << std::endl;
G
groot 已提交
102 103
                break;
            }
Y
yudong.cai 已提交
104 105
            case 'd':
                start_daemonized = 1;
G
groot 已提交
106
                break;
Y
yudong.cai 已提交
107 108
            case 'h':
                print_help(app_name);
G
groot 已提交
109
                return EXIT_SUCCESS;
Y
yudong.cai 已提交
110 111
            case '?':
                print_help(app_name);
G
groot 已提交
112
                return EXIT_FAILURE;
Y
yudong.cai 已提交
113 114
            default:
                print_help(app_name);
G
groot 已提交
115 116 117 118
                break;
        }
    }

Y
yudong.cai 已提交
119
    /* Handle Signal */
C
Cai Yudong 已提交
120 121 122 123 124 125 126 127 128 129
    milvus::server::signal_routine_func = [](int32_t exit_code) {
        milvus::server::Server::GetInstance().Stop();
        exit(exit_code);
    };
    signal(SIGHUP, milvus::server::HandleSignal);
    signal(SIGINT, milvus::server::HandleSignal);
    signal(SIGUSR1, milvus::server::HandleSignal);
    signal(SIGSEGV, milvus::server::HandleSignal);
    signal(SIGUSR2, milvus::server::HandleSignal);
    signal(SIGTERM, milvus::server::HandleSignal);
S
starlord 已提交
130

131
    server.Init(start_daemonized, pid_filename, config_filename);
132

W
Wang XiangYu 已提交
133 134
    s = server.Start();
    if (s.ok()) {
Z
Zhiru Zhu 已提交
135
        std::cout << "Milvus server started successfully!" << std::endl;
136
    } else {
W
Wang XiangYu 已提交
137
        std::cout << s.message() << std::endl;
138 139
        goto FAIL;
    }
140

Y
yudong.cai 已提交
141 142 143
    /* wait signal */
    pause();

144
    return EXIT_SUCCESS;
G
groot 已提交
145

Y
youny626 已提交
146
FAIL:
147 148
    std::cout << "Milvus server exit..." << std::endl;
    return EXIT_FAILURE;
G
groot 已提交
149
}