main.cpp 5.2 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.

G
groot 已提交
18 19 20
#include <getopt.h>
#include <libgen.h>
#include <signal.h>
Y
yudong.cai 已提交
21
#include <unistd.h>
S
starlord 已提交
22 23
#include <cstring>
#include <string>
G
groot 已提交
24

S
starlord 已提交
25
#include "../version.h"
W
wxyu 已提交
26
#include "external/easyloggingpp/easylogging++.h"
Y
yudong.cai 已提交
27 28
#include "metrics/Metrics.h"
#include "server/Server.h"
S
starlord 已提交
29 30
#include "utils/CommonUtil.h"
#include "utils/SignalUtil.h"
G
groot 已提交
31 32

INITIALIZE_EASYLOGGINGPP
G
groot 已提交
33

S
starlord 已提交
34
void
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
print_help(const std::string& app_name) {
    std::cout << std::endl << "Usage: " << app_name << " [OPTIONS]" << std::endl << std::endl;
    std::cout << "  Options:" << std::endl;
    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;
    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;
S
starlord 已提交
53
    std::cout << "Welcome to Milvus!" << std::endl;
54 55 56
    std::cout << "Milvus " << BUILD_TYPE << " version: v" << MILVUS_VERSION << ", built at " << BUILD_TIME << std::endl;
    std::cout << std::endl;
}
G
groot 已提交
57 58

int
S
starlord 已提交
59
main(int argc, char* argv[]) {
60
    print_banner();
G
groot 已提交
61

S
starlord 已提交
62 63 64 65 66 67
    static struct option long_options[] = {{"conf_file", required_argument, nullptr, 'c'},
                                           {"log_conf_file", required_argument, nullptr, 'l'},
                                           {"help", no_argument, nullptr, 'h'},
                                           {"daemon", no_argument, nullptr, 'd'},
                                           {"pid_file", required_argument, nullptr, 'p'},
                                           {nullptr, 0, nullptr, 0}};
G
groot 已提交
68 69 70 71

    int option_index = 0;
    int64_t start_daemonized = 0;

G
groot 已提交
72
    std::string config_filename, log_config_file;
G
groot 已提交
73
    std::string pid_filename;
Y
yudong.cai 已提交
74
    std::string app_name = argv[0];
G
groot 已提交
75

76 77 78
    milvus::server::Server& server = milvus::server::Server::GetInstance();
    milvus::Status s;

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

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

Y
yudong.cai 已提交
123
    /* Handle Signal */
S
starlord 已提交
124 125 126 127 128 129
    signal(SIGHUP, milvus::server::SignalUtil::HandleSignal);
    signal(SIGINT, milvus::server::SignalUtil::HandleSignal);
    signal(SIGUSR1, milvus::server::SignalUtil::HandleSignal);
    signal(SIGSEGV, milvus::server::SignalUtil::HandleSignal);
    signal(SIGUSR2, milvus::server::SignalUtil::HandleSignal);
    signal(SIGTERM, milvus::server::SignalUtil::HandleSignal);
S
starlord 已提交
130

131
    server.Init(start_daemonized, pid_filename, config_filename, log_config_file);
132 133 134 135 136 137 138

    s = server.Start();
    if (s.ok()) {
        std::cout << "Milvus server start successfully." << std::endl;
    } else {
        goto FAIL;
    }
139

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

143
    return EXIT_SUCCESS;
G
groot 已提交
144

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