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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "manager.h"
#include <vespa/config/common/exceptions.h>
#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/defaults.h>
#include <chrono>
#include <string>
#include <unistd.h>
#include <sys/time.h>
#include <vespa/log/log.h>
LOG_SETUP("config-sentinel");
using namespace config;
constexpr std::chrono::milliseconds CONFIG_TIMEOUT_MS(3 * 60 * 1000);
static bool stop()
{
return (vespalib::SignalHandler::INT.check() ||
vespalib::SignalHandler::TERM.check());
}
int
main(int argc, char **argv)
{
int c = getopt(argc, argv, "c:");
if (c != 'c') {
LOG(error, "Usage: %s -c <config-id>", argv[0]);
EV_STOPPING("config-sentinel", "Bad arguments on command line");
return EXIT_FAILURE;
}
std::string configId(optarg);
const char *rootDir = getenv("ROOT");
if (!rootDir) {
rootDir = vespa::Defaults::vespaHome();
LOG(warning, "ROOT is not set, using %s", rootDir);
setenv("ROOT", rootDir, 1);
}
if (chdir(rootDir) == -1) {
LOG(error, "Fatal: Cannot cd to $ROOT (%s)", rootDir);
EV_STOPPING("config-sentinel", "Cannot cd to $ROOT");
return EXIT_FAILURE;
}
EV_STARTED("config-sentinel");
vespalib::SignalHandler::PIPE.ignore();
vespalib::SignalHandler::TERM.hook();
vespalib::SignalHandler::INT.hook();
vespalib::SignalHandler::CHLD.hook();
if (setenv("LC_ALL", "C", 1) != 0) {
LOG(error, "Unable to set locale");
return EXIT_FAILURE;
}
setlocale(LC_ALL, "C");
sentinel::Env environment;
LOG(debug, "Reading configuration");
try {
environment.boot(configId);
} catch (ConfigTimeoutException & ex) {
LOG(warning, "Timeout getting config, please check your setup. Will exit and restart: %s", ex.getMessage().c_str());
EV_STOPPING("config-sentinel", ex.what());
return EXIT_FAILURE;
} catch (InvalidConfigException& ex) {
LOG(error, "Fatal: Invalid configuration, please check your setup: %s", ex.getMessage().c_str());
EV_STOPPING("config-sentinel", ex.what());
return EXIT_FAILURE;
} catch (ConfigRuntimeException& ex) {
LOG(error, "Fatal: Could not get config, please check your setup: %s", ex.getMessage().c_str());
EV_STOPPING("config-sentinel", ex.what());
return EXIT_FAILURE;
}
sentinel::Manager manager(environment);
vespalib::steady_time lastTime = vespalib::steady_clock::now();
while (!stop()) {
try {
vespalib::SignalHandler::CHLD.clear();
manager.doWork(); // Check for child procs & commands
} catch (InvalidConfigException& ex) {
LOG(warning, "Configuration problem: (ignoring): %s", ex.what());
} catch (vespalib::PortListenException& ex) {
LOG(error, "Fatal: %s", ex.getMessage().c_str());
EV_STOPPING("config-sentinel", ex.what());
return EXIT_FAILURE;
} catch (vespalib::FatalException& ex) {
LOG(error, "Fatal: %s", ex.getMessage().c_str());
EV_STOPPING("config-sentinel", ex.what());
return EXIT_FAILURE;
}
if (vespalib::SignalHandler::CHLD.check()) {
continue;
}
int maxNum = 0;
fd_set fds;
FD_ZERO(&fds);
manager.updateActiveFdset(&fds, &maxNum);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000; //0.1s
select(maxNum, &fds, nullptr, nullptr, &tv);
vespalib::steady_time now = vespalib::steady_clock::now();
if ((now - lastTime) < 10ms) {
std::this_thread::sleep_for(12ms); // Avoid busy looping;
}
lastTime = now;
}
EV_STOPPING("config-sentinel", "normal exit");
int rv = manager.terminate();
return rv;
}
|