summaryrefslogtreecommitdiffstats
path: root/configd
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-12-10 00:54:16 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2019-12-16 23:48:46 +0000
commitc2ea426c8d955ff40873db1e7d06c482ca662e75 (patch)
tree3f1ef7843ca4c984048a4d1cf7ef333b07d9ffb5 /configd
parent6a807615c7ee6364362a9a14d725165e948c5585 (diff)
Drop timestamp.h
Diffstat (limited to 'configd')
-rw-r--r--configd/src/apps/sentinel/config-handler.cpp73
-rw-r--r--configd/src/apps/sentinel/metrics.cpp7
-rw-r--r--configd/src/apps/sentinel/metrics.h4
-rw-r--r--configd/src/apps/sentinel/service.cpp19
-rw-r--r--configd/src/apps/sentinel/service.h9
5 files changed, 38 insertions, 74 deletions
diff --git a/configd/src/apps/sentinel/config-handler.cpp b/configd/src/apps/sentinel/config-handler.cpp
index 3e7b9ef0ea0..d4600471904 100644
--- a/configd/src/apps/sentinel/config-handler.cpp
+++ b/configd/src/apps/sentinel/config-handler.cpp
@@ -5,7 +5,6 @@
#include <vespa/vespalib/net/socket_address.h>
#include <vespa/vespalib/util/exceptions.h>
-#include <vespa/fastos/timestamp.h>
#include <string>
#include <fcntl.h>
#include <sys/wait.h>
@@ -44,24 +43,22 @@ ConfigHandler::ConfigHandler()
_startMetrics(),
_stateApi()
{
- _startMetrics.startedTime = fastos::time();
+ _startMetrics.startedTime = vespalib::steady_clock::now();
}
ConfigHandler::~ConfigHandler()
{
terminateServices(false);
- std::list<OutputConnection *>::iterator it;
- for (it = _outputConnections.begin(); it != _outputConnections.end(); ++it)
- {
- delete *it;
+ for (OutputConnection * conn : _outputConnections) {
+ delete conn;
}
}
void
ConfigHandler::terminateServices(bool catchable, bool printDebug)
{
- for (ServiceMap::iterator it(_services.begin()), mt(_services.end()); it != mt; it++) {
- Service *service = it->second.get();
+ for (const auto & entry : _services) {
+ Service *service = entry.second.get();
if (printDebug && service->isRunning()) {
LOG(info, "%s: killing", service->name().c_str());
}
@@ -77,34 +74,10 @@ ConfigHandler::terminate()
// Give them 58 seconds to exit cleanly, then terminate(false) all
// of them.
terminateServices(true);
- struct timeval endTime;
- gettimeofday(&endTime, nullptr);
- endTime.tv_sec += 58;
- struct timeval tv = {0, 0};
-
- while (tv.tv_sec >= 0 && doWork()) {
- gettimeofday(&tv, nullptr);
- tv.tv_sec = endTime.tv_sec - tv.tv_sec;
- tv.tv_usec = endTime.tv_usec - tv.tv_usec;
-
- if (tv.tv_usec >= 1000000) {
- tv.tv_usec -= 1000000;
- tv.tv_sec += 1;
- } else if (tv.tv_usec < 0) {
- tv.tv_usec += 100000;
- tv.tv_sec -= 1;
- }
-
- if (tv.tv_sec < 0) {
- break;
- }
-
- if (tv.tv_sec > 0 || tv.tv_usec > 200000) {
- // Never wait more than 200ms per select regardless
- tv.tv_sec = 0;
- tv.tv_usec = 200000;
- }
+ vespalib::steady_time endTime = vespalib::steady_clock::now() + 58s;
+ while ((vespalib::steady_clock::now() < endTime) && doWork()) {
+ struct timeval tv {0, 200000};
// Any child exiting will send SIGCHLD and break this select so
// we handle the children exiting even quicker..
select(0, nullptr, nullptr, nullptr, &tv);
@@ -112,8 +85,7 @@ ConfigHandler::terminate()
for (int retry = 0; retry < 10 && doWork(); ++retry) {
LOG(warning, "some services refuse to terminate cleanly, sending KILL");
terminateServices(false, true);
- tv.tv_sec = 0;
- tv.tv_usec = 200000;
+ struct timeval tv {0, 200000};
select(0, nullptr, nullptr, nullptr, &tv);
}
return !doWork();
@@ -146,9 +118,9 @@ ConfigHandler::doConfigure()
for (unsigned int i = 0; i < config.service.size(); ++i) {
const SentinelConfig::Service& serviceConfig = config.service[i];
const vespalib::string name(serviceConfig.name);
- ServiceMap::iterator found(_services.find(name));
+ auto found(_services.find(name));
if (found == _services.end()) {
- services[name] = Service::UP(new Service(serviceConfig, config.application, _outputConnections, _startMetrics));
+ services[name] = std::make_unique<Service>(serviceConfig, config.application, _outputConnections, _startMetrics);
} else {
found->second->reconfigure(serviceConfig);
services[name] = std::move(found->second);
@@ -182,8 +154,8 @@ ConfigHandler::doWork()
_startMetrics.maybeLog();
// Check for active services.
- for (ServiceMap::iterator it(_services.begin()), mt(_services.end()); it != mt; it++) {
- if (it->second->isRunning()) {
+ for (const auto & service : _services) {
+ if (service.second->isRunning()) {
return true;
}
}
@@ -227,12 +199,8 @@ ConfigHandler::handleChildDeaths()
void
ConfigHandler::updateActiveFdset(fd_set *fds, int *maxNum)
{
- std::list<OutputConnection *>::const_iterator
- src = _outputConnections.begin();
// ### _Possibly put an assert here if fd is > 1023???
- while (src != _outputConnections.end()) {
- OutputConnection *c = *src;
- ++src;
+ for (OutputConnection *c : _outputConnections) {
int fd = c->fd();
if (fd >= 0) {
FD_SET(fd, fds);
@@ -280,10 +248,9 @@ ConfigHandler::handleCommands()
Service *
ConfigHandler::serviceByPid(pid_t pid)
{
- for (ServiceMap::iterator it(_services.begin()), mt(_services.end()); it != mt; it++) {
- Service *service = it->second.get();
- if (service->pid() == pid) {
- return service;
+ for (const auto & service : _services) {
+ if (service.second->pid() == pid) {
+ return service.second.get();
}
}
for (const auto & it : _orphans) {
@@ -298,7 +265,7 @@ ConfigHandler::serviceByPid(pid_t pid)
Service *
ConfigHandler::serviceByName(const vespalib::string & name)
{
- ServiceMap::iterator found(_services.find(name));
+ auto found(_services.find(name));
if (found != _services.end()) {
return found->second.get();
}
@@ -316,8 +283,8 @@ ConfigHandler::handleCmd(const Cmd& cmd)
size_t left = 65536;
size_t pos = 0;
retbuf[pos] = 0;
- for (ServiceMap::iterator it(_services.begin()), mt(_services.end()); it != mt; it++) {
- Service *service = it->second.get();
+ for (const auto & entry : _services) {
+ const Service *service = entry.second.get();
const SentinelConfig::Service& config = service->serviceConfig();
int sz = snprintf(retbuf + pos, left,
"%s state=%s mode=%s pid=%d exitstatus=%d id=\"%s\"\n",
diff --git a/configd/src/apps/sentinel/metrics.cpp b/configd/src/apps/sentinel/metrics.cpp
index bb10c94ae12..ed31e7a4d80 100644
--- a/configd/src/apps/sentinel/metrics.cpp
+++ b/configd/src/apps/sentinel/metrics.cpp
@@ -2,7 +2,6 @@
#include "metrics.h"
#include <vespa/vespalib/metrics/simple_metrics.h>
-#include <vespa/fastos/timestamp.h>
namespace config::sentinel {
@@ -14,7 +13,7 @@ StartMetrics::StartMetrics()
producer(metrics),
currentlyRunningServices(0),
totalRestartsCounter(0),
- startedTime(fastos::time()),
+ startedTime(vespalib::steady_clock::now()),
sentinel_restarts(metrics->counter("sentinel.restarts",
"how many times sentinel restarted a service")),
sentinel_totalRestarts(metrics->gauge("sentinel.totalRestarts",
@@ -33,10 +32,10 @@ StartMetrics::~StartMetrics() = default;
void
StartMetrics::maybeLog()
{
- uint32_t curTime = fastos::time();
+ vespalib::steady_time curTime = vespalib::steady_clock::now();
sentinel_totalRestarts.sample(totalRestartsCounter);
sentinel_running.sample(currentlyRunningServices);
- sentinel_uptime.sample(curTime - startedTime);
+ sentinel_uptime.sample(vespalib::to_s(curTime - startedTime));
}
}
diff --git a/configd/src/apps/sentinel/metrics.h b/configd/src/apps/sentinel/metrics.h
index 2263d70fb60..365b7cc2ecf 100644
--- a/configd/src/apps/sentinel/metrics.h
+++ b/configd/src/apps/sentinel/metrics.h
@@ -1,8 +1,8 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
-#include <sys/time.h>
#include <vespa/vespalib/metrics/simple_metrics.h>
+#include <vespa/vespalib/util/time.h>
namespace config::sentinel {
@@ -15,7 +15,7 @@ struct StartMetrics {
vespalib::metrics::Producer producer;
unsigned long currentlyRunningServices;
unsigned long totalRestartsCounter;
- long startedTime;
+ vespalib::steady_time startedTime;
Counter sentinel_restarts;
Gauge sentinel_totalRestarts;
Gauge sentinel_running;
diff --git a/configd/src/apps/sentinel/service.cpp b/configd/src/apps/sentinel/service.cpp
index 22f9681a2ff..9f4196a2457 100644
--- a/configd/src/apps/sentinel/service.cpp
+++ b/configd/src/apps/sentinel/service.cpp
@@ -4,7 +4,6 @@
#include "output-connection.h"
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/signalhandler.h>
-#include <vespa/fastos/timestamp.h>
#include <csignal>
#include <unistd.h>
@@ -44,7 +43,7 @@ Service::Service(const SentinelConfig::Service& service, const SentinelConfig::A
_config(new SentinelConfig::Service(service)),
_isAutomatic(true),
_restartPenalty(0),
- _last_start(0),
+ _last_start(vespalib::steady_time::min()),
_application(application),
_outputConnections(ocs),
_metrics(metrics)
@@ -157,8 +156,7 @@ Service::start()
LOG(warning, "tried to start '%s' in REMOVING state", name().c_str());
return;
}
- time_t now = fastos::time();
- _last_start = now;
+ _last_start = vespalib::steady_clock::now();
// make a pipe, close the good ends of it, mark it close-on-exec
// if exec fails, write a complaint on the fd (which will then be read
@@ -323,8 +321,8 @@ Service::youExited(int status)
if (! expectedDeath) {
// make sure the service does not restart in a tight loop:
- time_t now = fastos::time();
- unsigned int diff = now - _last_start;
+ vespalib::steady_time now = vespalib::steady_clock::now();
+ vespalib::duration diff = now - _last_start;
if (diff < MAX_RESTART_PENALTY) {
incrementRestartPenalty();
}
@@ -332,7 +330,7 @@ Service::youExited(int status)
resetRestartPenalty();
}
if (diff < _restartPenalty) {
- LOG(info, "%s: will delay start by %u seconds", name().c_str(), _restartPenalty - diff);
+ LOG(info, "%s: will delay start by %2.3f seconds", name().c_str(), vespalib::to_s(_restartPenalty - diff));
}
}
if (_isAutomatic && !stop()) {
@@ -421,8 +419,7 @@ bool
Service::wantsRestart() const
{
if (_state == RESTARTING) {
- time_t now = fastos::time();
- if (now > _last_start + _restartPenalty) {
+ if (vespalib::steady_clock::now() > _last_start + _restartPenalty) {
return true;
}
}
@@ -441,12 +438,12 @@ Service::setAutomatic(bool autoStatus)
void
Service::incrementRestartPenalty()
{
- _restartPenalty += 1;
+ _restartPenalty += 1s;
_restartPenalty *= 2;
if (_restartPenalty > MAX_RESTART_PENALTY) {
_restartPenalty = MAX_RESTART_PENALTY;
}
- LOG(info, "%s: incremented restart penalty to %u seconds", name().c_str(), _restartPenalty);
+ LOG(info, "%s: incremented restart penalty to %2.3f seconds", name().c_str(), vespalib::to_s(_restartPenalty));
}
diff --git a/configd/src/apps/sentinel/service.h b/configd/src/apps/sentinel/service.h
index 5a188f217ff..7b7321fb5c6 100644
--- a/configd/src/apps/sentinel/service.h
+++ b/configd/src/apps/sentinel/service.h
@@ -3,6 +3,7 @@
#include "metrics.h"
#include <vespa/vespalib/stllike/string.h>
+#include <vespa/vespalib/util/time.h>
#include <vespa/config-sentinel.h>
#include <list>
@@ -27,9 +28,9 @@ private:
SentinelConfig::Service *_config;
bool _isAutomatic;
- static const unsigned int MAX_RESTART_PENALTY = 1800;
- unsigned int _restartPenalty;
- time_t _last_start;
+ static constexpr vespalib::duration MAX_RESTART_PENALTY = 1800s;
+ vespalib::duration _restartPenalty;
+ vespalib::steady_time _last_start;
void runChild(int pipes[2]) __attribute__((noreturn));
void ensureChildRuns(int fd);
@@ -67,7 +68,7 @@ public:
const SentinelConfig::Service& serviceConfig() const { return *_config; }
void setAutomatic(bool autoStatus);
bool isAutomatic() const { return _isAutomatic; }
- void resetRestartPenalty() { _restartPenalty = 0; }
+ void resetRestartPenalty() { _restartPenalty = vespalib::duration::zero(); }
void incrementRestartPenalty();
};