Skip to content

Commit

Permalink
Add params to configure logging semantics of LogSlowExecution, add Lo…
Browse files Browse the repository at this point in the history
…gSlowExecution for bucket merges longer than 2 minutes
  • Loading branch information
ThomasBrady committed Aug 14, 2024
1 parent 55ec348 commit 0829a25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
13 changes: 4 additions & 9 deletions src/bucket/FutureBucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ FutureBucket::startMerge(Application& app, uint32_t maxProtocolVersion,
std::shared_ptr<task_t> task = std::make_shared<task_t>(
[curr, snap, &bm, shadows, maxProtocolVersion, countMergeEvents, level,
&timer, &ctx, doFsync, availableTime]() mutable {
auto timeScope = timer.TimeScope();
auto timeScope = LogSlowExecution(
"Bucket merge", LogSlowExecution::Mode::AUTOMATIC_RAII, "took",
std::chrono::seconds(120), /*warnOnThreshold=*/true,
/*logEveryExecution=*/true);
CLOG_TRACE(Bucket, "Worker merging curr={} with snap={}",
hexAbbrev(curr->getHash()), hexAbbrev(snap->getHash()));

Expand All @@ -372,14 +375,6 @@ FutureBucket::startMerge(Application& app, uint32_t maxProtocolVersion,
CLOG_TRACE(
Bucket, "Worker finished merging curr={} with snap={}",
hexAbbrev(curr->getHash()), hexAbbrev(snap->getHash()));

std::chrono::duration<double> time(timeScope.Stop());
double timePct = time.count() / availableTime.count() * 100;
CLOG_DEBUG(
Perf,
"Bucket merge on level {} finished in {} seconds "
"({}% of available time)",
level, time.count(), timePct);
}

return res;
Expand Down
15 changes: 10 additions & 5 deletions src/util/LogSlowExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ namespace stellar
{
LogSlowExecution::LogSlowExecution(std::string eventName, Mode mode,
std::string message,
std::chrono::milliseconds threshold)
std::chrono::milliseconds threshold,
bool warnOnThreshold, bool logEveryExecution)
: mStart(std::chrono::system_clock::now())
, mName(std::move(eventName))
, mMode(mode)
, mMessage(std::move(message))
, mThreshold(threshold){};
, mThreshold(threshold)
, mWarnOnThreshold(warnOnThreshold)
, mLogEveryExecution(logEveryExecution){};

LogSlowExecution::~LogSlowExecution()
{
Expand All @@ -44,13 +47,15 @@ LogSlowExecution::checkElapsedTime() const
auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(finish - mStart);

if (!mThreshold.count() || elapsed > mThreshold)
if (!mThreshold.count() || elapsed > mThreshold || mLogEveryExecution)
{
std::lock_guard<std::mutex> guard(gLogSlowExecMutex);

// Check whether we're 10 times over the threshold to decide the log
// level.
LogLevel ll = (elapsed > mThreshold * 10 || !mThreshold.count())
// level. If warnOnThreshold is set, log as warning if we're over the
// threshold.
LogLevel ll = (elapsed > mThreshold * 10 || !mThreshold.count() ||
(elapsed > mThreshold && mWarnOnThreshold))
? LogLevel::LVL_WARNING
: LogLevel::LVL_DEBUG;

Expand Down
5 changes: 4 additions & 1 deletion src/util/LogSlowExecution.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class LogSlowExecution
LogSlowExecution(
std::string eventName, Mode mode = Mode::AUTOMATIC_RAII,
std::string message = "took",
std::chrono::milliseconds threshold = std::chrono::seconds(1));
std::chrono::milliseconds threshold = std::chrono::seconds(1),
bool warnOnThreshold = false, bool logEveryExecution = false);
~LogSlowExecution();
std::chrono::milliseconds checkElapsedTime() const;

Expand All @@ -32,6 +33,8 @@ class LogSlowExecution
Mode mMode;
std::string mMessage;
std::chrono::milliseconds mThreshold;
bool mWarnOnThreshold;
bool mLogEveryExecution;
};

// Helper class to emit rate-limited log messages without any threshold
Expand Down

0 comments on commit 0829a25

Please sign in to comment.