summaryrefslogtreecommitdiffstats
path: root/searchcore/src/apps/fdispatch/fdispatch.cpp
blob: 21fa696330a12ce325227e19a4511e749a613d83 (plain) (blame)
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
50
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcore/fdispatch/program/fdispatch.h>
#include <vespa/searchcore/fdispatch/common/perftask.h>
#include <vespa/vespalib/net/state_server.h>
#include <vespa/vespalib/net/simple_health_producer.h>
#include <vespa/vespalib/net/simple_metrics_producer.h>
#include <vespa/searchlib/expression/forcelink.hpp>
#include <vespa/searchlib/aggregation/forcelink.hpp>
#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/fastos/app.h>
#include <thread>
#include <getopt.h>

#include <vespa/log/log.h>
LOG_SETUP("fdispatch");


using fdispatch::Fdispatch;
using vespa::config::search::core::FdispatchrcConfig;
using namespace std::literals;

extern char FastS_VersionTag[];

class FastS_FDispatchApp : public FastOS_Application
{
private:
    FastS_FDispatchApp(const FastS_FDispatchApp &);
    FastS_FDispatchApp& operator=(const FastS_FDispatchApp &);

protected:
    vespalib::string _configId;

    bool CheckShutdownFlags () const {
        return (vespalib::SignalHandler::INT.check() || vespalib::SignalHandler::TERM.check());
    }

    void Usage();
    bool GetOptions(int *exitCode);

public:
    int Main() override;
    FastS_FDispatchApp();
    ~FastS_FDispatchApp();
};


FastS_FDispatchApp::FastS_FDispatchApp()
{
}


/**
 * Main program for a dispatcher node.
 */
int
FastS_FDispatchApp::Main()
{
    int exitCode;

    forcelink_searchlib_expression();
    forcelink_searchlib_aggregation();

    if (!GetOptions(&exitCode)) {
        EV_STOPPING("fdispatch", (exitCode == 0) ? "clean shutdown" : "error");
        return exitCode;
    }

    exitCode = 0;

    EV_STARTED("fdispatch");

    vespalib::SignalHandler::INT.hook();
    vespalib::SignalHandler::TERM.hook();
    vespalib::SignalHandler::PIPE.ignore();

    std::unique_ptr<Fdispatch> myfdispatch;
    try {
        myfdispatch.reset(new Fdispatch(_configId));
    } catch (std::exception &ex) {
        LOG(error, "getting config: %s", (const char *) ex.what());
        exitCode = 1;
        EV_STOPPING("fdispatch", "error getting config");
        return exitCode;
    }

    try {

        if (!myfdispatch->Init()) {
            throw std::runtime_error("myfdispatch->Init(" + _configId + ") failed");
        }
        if (myfdispatch->Failed()) {
            throw std::runtime_error("myfdispatch->Failed()");
        }
        { // main loop scope
            vespalib::SimpleHealthProducer health;
            vespalib::SimpleMetricsProducer metrics;
            vespalib::StateServer stateServer(myfdispatch->getHealthPort(), health, metrics, myfdispatch->getComponentConfig());
            FastS_PerfTask perfTask(*myfdispatch, 300.0);
            while (!CheckShutdownFlags()) {
                if (myfdispatch->Failed()) {
                    throw std::runtime_error("myfdispatch->Failed()");
                }
                std::this_thread::sleep_for(100ms);
                if (!myfdispatch->CheckTempFail())
                    break;
            }
        } // main loop scope
        if (myfdispatch->Failed()) {
            throw std::runtime_error("myfdispatch->Failed()");
        }
    } catch (std::runtime_error &e) {
        LOG(warning, "got std::runtime_error during init: %s", e.what());
        exitCode = 1;
    } catch (std::exception &e) {
        LOG(error, "got exception during init: %s", e.what());
        exitCode = 1;
    } catch (...) {
        LOG(error, "got exception during init");
        exitCode = 1;
    }

    LOG(debug, "Deleting fdispatch");
    myfdispatch.reset();
    LOG(debug, "COMPLETION: Exiting");
    EV_STOPPING("fdispatch", (exitCode == 0) ? "clean shutdown" : "error");
    return exitCode;
}


bool
FastS_FDispatchApp::GetOptions(int *exitCode)
{
    int errflg = 0;
    int c;
    const char *optArgument;
    int longopt_index;	/* Shows which long option was used */
    static struct option longopts[] = {
        { "config-id", 1, NULL, 0 },
        { NULL, 0, NULL, 0 }
    };
    enum longopts_enum {
        LONGOPT_CONFIGID
    };
    int optIndex = 1;    // Start with argument 1
    while ((c = GetOptLong("c:", optArgument, optIndex, longopts, &longopt_index)) != -1) {
        switch (c) {
        case 0:
            switch (longopt_index) {
            case LONGOPT_CONFIGID:
                break;
            default:
                if (optArgument != NULL) {
                    LOG(info, "longopt %s with arg %s", longopts[longopt_index].name, optArgument);
                } else {
                    LOG(info, "longopt %s", longopts[longopt_index].name);
                }
                break;
            }
            break;
        case 'c':
            _configId = optArgument;
            break;
        case '?':
        default:
            errflg++;
        }
    }
    if (errflg) {
        Usage();
        *exitCode = 1;
        return false;
    }
    return true;
}

void
FastS_FDispatchApp::Usage()
{
    printf("FAST Search - fdispatch %s\n", FastS_VersionTag);
    printf("\n"
           "USAGE:\n"
           "\n"
           "fdispatch [-C fsroot] [-c rcFile] [-P preHttPort] [-V] [-w FFF]\n"
           "\n"
           "  -C fsroot      Fast Search's root directory\n"
           "                 (default /usr/fastsearch/fastserver4)\n"
           "  -c rcFile      fdispatchrc file (default FASTSEARCHROOT/etc/fdispatchrc)\n"
           "  -P preHttPort  pre-allocated socket number for http service\n"
           "  -V             show version and exit\n"
           "  -w FFF         hex value (max 32 bit) for the Verbose mask\n"
           "\n");
}


FastS_FDispatchApp::~FastS_FDispatchApp()
{
}


int
main(int argc, char **argv)
{
    FastS_FDispatchApp app;
    int retval;

    // Maybe this should be handeled by FastOS
    setlocale(LC_ALL, "C");

    retval = app.Entry(argc, argv);

    return retval;
}