Allow to enable shard/cdc debugging at runtime using USR2

This commit is contained in:
Francesco Mazzoli
2023-05-26 10:03:59 +00:00
parent 1b83a50419
commit 1458759534
5 changed files with 77 additions and 4 deletions

View File

@@ -614,7 +614,7 @@ void runCDC(const std::string& dbDir, const CDCOptions& options) {
}
logOut = &fileOut;
}
Logger logger(options.logLevel, *logOut);
Logger logger(options.logLevel, *logOut, true);
{
Env env(logger, "startup");

View File

@@ -1,5 +1,9 @@
#include <atomic>
#include <signal.h>
#include "Env.hpp"
#include "Assert.hpp"
#include "Exception.hpp"
std::ostream& operator<<(std::ostream& out, LogLevel ll) {
switch (ll) {
@@ -20,3 +24,56 @@ std::ostream& operator<<(std::ostream& out, LogLevel ll) {
}
return out;
}
void Logger::toggleDebug() {
if (_logLevel == LogLevel::LOG_DEBUG) {
_logLevel = _savedLogLevel;
} else {
_logLevel = LogLevel::LOG_DEBUG;
}
}
static std::atomic<Logger*> debuggableLogger(nullptr);
extern "C" {
static void loggerSignalHandler(int signal_number) {
(*debuggableLogger).toggleDebug();
}
}
void installLoggerSignalHandler(void* logger) {
Logger* old = nullptr;
if (!debuggableLogger.compare_exchange_strong(old, (Logger*)logger)) {
throw EGGS_EXCEPTION("Could not install logger signal handler, some other logger is already here.");
}
struct sigaction act;
act.sa_handler = loggerSignalHandler;
if (sigemptyset(&act.sa_mask) != 0) {
SYSCALL_EXCEPTION("sigemptyset");
}
act.sa_flags = 0;
// USR1 is used by undertaker
if (sigaction(SIGUSR2, &act, NULL) < 0) {
throw SYSCALL_EXCEPTION("sigaction");
}
}
void tearDownLoggerSignalHandler(void* logger) {
Logger* old = (Logger*)logger;
if (!debuggableLogger.compare_exchange_strong(old, nullptr)) {
throw EGGS_EXCEPTION("Could not tear down logger signal handler, bad preexisting logger");
}
struct sigaction act;
act.sa_handler = SIG_DFL;
if (sigemptyset(&act.sa_mask) != 0) {
SYSCALL_EXCEPTION("sigemptyset");
}
act.sa_flags = 0;
if (sigaction(SIGUSR2, &act, NULL) < 0) {
throw SYSCALL_EXCEPTION("sigaction");
}
}

View File

@@ -4,8 +4,10 @@
#include <mutex>
#include <sstream>
#include <string>
#include <signal.h>
#include "Common.hpp"
#include "Exception.hpp"
#include "Time.hpp"
enum class LogLevel : uint32_t {
@@ -17,13 +19,25 @@ enum class LogLevel : uint32_t {
std::ostream& operator<<(std::ostream& out, LogLevel ll);
void installLoggerSignalHandler(void* logger);
void tearDownLoggerSignalHandler(void* logger);
struct Logger {
private:
LogLevel _logLevel;
LogLevel _savedLogLevel;
std::ostream& _out;
std::mutex _mutex;
public:
Logger(LogLevel logLevel, std::ostream& out): _logLevel(logLevel), _out(out) {}
Logger(LogLevel logLevel, std::ostream& out, bool usr1ToDebug): _logLevel(logLevel), _savedLogLevel(logLevel), _out(out) {
if (usr1ToDebug) {
installLoggerSignalHandler(this);
}
}
~Logger() {
tearDownLoggerSignalHandler(this);
}
template<typename ...Args>
void _log(LogLevel level, const std::string& prefix, const char* fmt, Args&&... args) {
@@ -45,6 +59,8 @@ public:
std::scoped_lock lock(_mutex);
_out.flush();
}
void toggleDebug();
};
struct Env {

View File

@@ -410,7 +410,7 @@ void runShard(ShardId shid, const std::string& dbDir, const ShardOptions& option
}
logOut = &fileOut;
}
Logger logger(options.logLevel, *logOut);
Logger logger(options.logLevel, *logOut, true);
{
Env env(logger, "startup");

View File

@@ -399,7 +399,7 @@ struct TempShardDB {
ShardId shid;
std::unique_ptr<ShardDB> db;
TempShardDB(LogLevel level, ShardId shid_): logger(level, std::cerr), shid(shid_) {
TempShardDB(LogLevel level, ShardId shid_): logger(level, std::cerr, false), shid(shid_) {
dbDir = std::string("temp-shard-db.XXXXXX");
if (mkdtemp(dbDir.data()) == nullptr) {
throw SYSCALL_EXCEPTION("mkdtemp");