main.cpp 4.6 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 21 22
#include <getopt.h>
#include <libgen.h>
#include <cstring>
#include <string>
#include <signal.h>
Y
yudong.cai 已提交
23
#include <unistd.h>
G
groot 已提交
24

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

G
groot 已提交
32 33

INITIALIZE_EASYLOGGINGPP
G
groot 已提交
34 35 36

void print_help(const std::string &app_name);

J
jinhai 已提交
37
using namespace zilliz::milvus;
G
groot 已提交
38 39 40

int
main(int argc, char *argv[]) {
J
jinhai 已提交
41
    std::cout << std::endl << "Welcome to use Milvus by Zilliz!" << std::endl;
G
groot 已提交
42
    std::cout << "Milvus " << BUILD_TYPE << " version: v" << MILVUS_VERSION << " built at " << BUILD_TIME << std::endl;
G
groot 已提交
43 44

    static struct option long_options[] = {{"conf_file", required_argument, 0, 'c'},
G
groot 已提交
45
                                           {"log_conf_file", required_argument, 0, 'l'},
G
groot 已提交
46 47 48 49 50 51 52 53
                                           {"help", no_argument, 0, 'h'},
                                           {"daemon", no_argument, 0, 'd'},
                                           {"pid_file", required_argument, 0, 'p'},
                                           {NULL, 0, 0, 0}};

    int option_index = 0;
    int64_t start_daemonized = 0;

G
groot 已提交
54
    std::string config_filename, log_config_file;
G
groot 已提交
55
    std::string pid_filename;
Y
yudong.cai 已提交
56
    std::string app_name = argv[0];
G
groot 已提交
57

Y
yudong.cai 已提交
58
    if (argc < 2) {
G
groot 已提交
59
        print_help(app_name);
G
groot 已提交
60
        std::cout << "Milvus server exit..." << std::endl;
G
groot 已提交
61 62
        return EXIT_FAILURE;
    }
G
groot 已提交
63 64

    int value;
G
groot 已提交
65
    while ((value = getopt_long(argc, argv, "c:l:p:dh", long_options, &option_index)) != -1) {
G
groot 已提交
66 67 68 69 70
        switch (value) {
            case 'c': {
                char *config_filename_ptr = strdup(optarg);
                config_filename = config_filename_ptr;
                free(config_filename_ptr);
G
groot 已提交
71
                std::cout << "Loading configuration from: " << config_filename << std::endl;
G
groot 已提交
72 73
                break;
            }
G
groot 已提交
74 75 76 77
            case 'l': {
                char *log_filename_ptr = strdup(optarg);
                log_config_file = log_filename_ptr;
                free(log_filename_ptr);
G
groot 已提交
78
                std::cout << "Initial log config from: " << log_config_file << std::endl;
G
groot 已提交
79 80
                break;
            }
G
groot 已提交
81 82 83 84 85

            case 'p': {
                char *pid_filename_ptr = strdup(optarg);
                pid_filename = pid_filename_ptr;
                free(pid_filename_ptr);
G
groot 已提交
86
                std::cout << pid_filename << std::endl;
G
groot 已提交
87 88 89
                break;
            }

Y
yudong.cai 已提交
90 91
            case 'd':
                start_daemonized = 1;
G
groot 已提交
92
                break;
Y
yudong.cai 已提交
93 94
            case 'h':
                print_help(app_name);
G
groot 已提交
95
                return EXIT_SUCCESS;
Y
yudong.cai 已提交
96 97
            case '?':
                print_help(app_name);
G
groot 已提交
98
                return EXIT_FAILURE;
Y
yudong.cai 已提交
99 100
            default:
                print_help(app_name);
G
groot 已提交
101 102 103 104
                break;
        }
    }

Y
yudong.cai 已提交
105
    server::Server &server = server::Server::Instance();
G
groot 已提交
106
    server.Init(start_daemonized, pid_filename, config_filename, log_config_file);
Y
yudong.cai 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
    server.Start();

    /* Handle Signal */
    signal(SIGHUP, server::SignalUtil::HandleSignal);
    signal(SIGINT, server::SignalUtil::HandleSignal);
    signal(SIGUSR1, server::SignalUtil::HandleSignal);
    signal(SIGSEGV, server::SignalUtil::HandleSignal);
    signal(SIGUSR2, server::SignalUtil::HandleSignal);
    signal(SIGTERM, server::SignalUtil::HandleSignal);

    /* wait signal */
    pause();

    return 0;
G
groot 已提交
121 122 123 124
}

void
print_help(const std::string &app_name) {
Y
yudong.cai 已提交
125
    std::cout << std::endl << "Usage: " << app_name << " [OPTIONS]" << std::endl << std::endl;
G
groot 已提交
126 127 128 129 130 131
    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;
G
groot 已提交
132
}