Skip to content

Commit

Permalink
Get rid of lambdas in logging macros (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
UgnineSirdis authored Jan 8, 2024
1 parent ed6971c commit bb77248
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 53 deletions.
18 changes: 7 additions & 11 deletions ydb/core/tx/columnshard/columnshard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace NKikimr::NColumnShard {
void TColumnShard::CleanupActors(const TActorContext& ctx) {
ctx.Send(ResourceSubscribeActor, new TEvents::TEvPoisonPill);
ctx.Send(BufferizationWriteActorId, new TEvents::TEvPoisonPill);

StoragesManager->Stop();
if (Tiers) {
Tiers->Stop();
Expand Down Expand Up @@ -307,10 +307,10 @@ void TColumnShard::ConfigureStats(const NOlap::TColumnEngineStats& indexStats, :

tabletStats->SetRowCount(activeIndexStats.Rows);
tabletStats->SetDataSize(activeIndexStats.Bytes + TabletCounters->Simple()[COUNTER_COMMITTED_BYTES].Get());

// TODO: we need row/dataSize counters for evicted data (managed by tablet but stored outside)
//tabletStats->SetIndexSize(); // TODO: calc size of internal tables

tabletStats->SetLastAccessTime(LastAccessTime.MilliSeconds());
tabletStats->SetLastUpdateTime(lastIndexUpdate.GetPlanStep());
}
Expand Down Expand Up @@ -361,13 +361,13 @@ void TColumnShard::FillColumnTableStats(const TActorContext& ctx, std::unique_pt
auto* periodicTableStats = ev->Record.AddTables();
periodicTableStats->SetDatashardId(TabletID());
periodicTableStats->SetTableLocalId(tableLocalID);

periodicTableStats->SetShardState(2); // NKikimrTxDataShard.EDatashardState.Ready
periodicTableStats->SetGeneration(Executor()->Generation());
periodicTableStats->SetRound(StatsReportRound++);
periodicTableStats->SetNodeId(ctx.ExecutorThread.ActorSystem->NodeId);
periodicTableStats->SetStartTime(StartTime().MilliSeconds());

if (auto* resourceMetrics = Executor()->GetResourceMetrics()) {
resourceMetrics->Fill(*periodicTableStats->MutableTabletMetrics());
}
Expand All @@ -376,11 +376,7 @@ void TColumnShard::FillColumnTableStats(const TActorContext& ctx, std::unique_pt
FillTxTableStats(tableStats);
ConfigureStats(*columnStats, tableStats);

{
// Workaround "reference to local binding declared in enclousing function" issue for clang14
ui64 id = tableLocalID;
LOG_S_TRACE("Add stats for table, tableLocalID=" << id);
}
LOG_S_TRACE("Add stats for table, tableLocalID=" << tableLocalID);
}
}

Expand Down Expand Up @@ -411,7 +407,7 @@ void TColumnShard::SendPeriodicStats() {

FillOlapStats(ctx, ev);
FillColumnTableStats(ctx, ev);

NTabletPipe::SendData(ctx, StatsReportPipe, ev.release());
}

Expand Down
53 changes: 37 additions & 16 deletions ydb/core/tx/ctor_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,52 @@
#include <ydb/library/actors/core/log.h>
#include <ydb/library/services/services.pb.h>

#include <optional>

// You have to define TLogThis in local namespace to use these macros
#define LOG_S_EMERG(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_EMERG, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_ALERT(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_ALERT, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_CRIT(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_CRIT, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_ERROR(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_ERROR, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_WARN(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_WARN, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_NOTICE(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_NOTICE, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_INFO(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_INFO, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_DEBUG(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_DEBUG, [&](TStringBuilder& ss){ ss << stream; }); }
#define LOG_S_TRACE(stream) { TLogThis(*TlsActivationContext, NActors::NLog::PRI_TRACE, [&](TStringBuilder& ss){ ss << stream; }); }
#define __LOG_S_PRIORITY_IMPL__(stream, priority) \
if (auto logThis = TLogThis(*TlsActivationContext, priority)) { \
logThis.Str() << stream; \
logThis.WriteLog(); \
}

#define LOG_S_EMERG(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_EMERG)
#define LOG_S_ALERT(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_ALERT)
#define LOG_S_CRIT(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_CRIT)
#define LOG_S_ERROR(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_ERROR)
#define LOG_S_WARN(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_WARN)
#define LOG_S_NOTICE(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_NOTICE)
#define LOG_S_INFO(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_INFO)
#define LOG_S_DEBUG(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_DEBUG)
#define LOG_S_TRACE(stream) __LOG_S_PRIORITY_IMPL__(stream, NActors::NLog::PRI_TRACE)

namespace NKikimr {

template <NKikimrServices::EServiceKikimr Component>
class TCtorLogger
{
const NActors::TActivationContext& Ctx;
NActors::NLog::EPriority Priority;
std::optional<TStringBuilder> StrStream;

public:
template <typename TFunc>
TCtorLogger(const NActors::TActivationContext& ctx, NActors::NLog::EPriority priority, TFunc logFunc)
TCtorLogger(const NActors::TActivationContext& ctx, NActors::NLog::EPriority priority)
: Ctx(ctx)
, Priority(priority)
{
if (IS_LOG_PRIORITY_ENABLED(priority, Component)) {
TStringBuilder strStream;
logFunc(strStream);
::NActors::MemLogAdapter(ctx, priority, Component, "%s", strStream.data());
}
}

operator bool() const {
return IS_LOG_PRIORITY_ENABLED(Priority, Component);
}

TStringBuilder& Str() {
StrStream.emplace();
return *StrStream;
}

void WriteLog() {
::NActors::MemLogAdapter(Ctx, Priority, Component, std::move(*StrStream));
}
};

Expand Down
41 changes: 27 additions & 14 deletions ydb/library/actors/core/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
// TODO: limit number of messages per second
// TODO: make TLogComponentLevelRequest/Response network messages

#define IS_LOG_PRIORITY_ENABLED(priority, component) \
[p = static_cast<::NActors::NLog::EPriority>(priority), c = static_cast<::NActors::NLog::EComponent>(component)]() -> bool { \
::NActors::TActivationContext *context = ::NActors::TlsActivationContext; \
return !context || context->LoggerSettings()->Satisfies(p, c, 0ull); \
}()
#define IS_LOG_PRIORITY_ENABLED(priority, component) \
::NActors::IsLogPriorityEnabled(static_cast<::NActors::NLog::EPriority>(priority), \
static_cast<::NActors::NLog::EComponent>(component))

#define IS_CTX_LOG_PRIORITY_ENABLED(actorCtxOrSystem, priority, component, sampleBy) \
::NActors::IsLogPriorityEnabled(static_cast<::NActors::NLog::TSettings*>((actorCtxOrSystem).LoggerSettings()), \
static_cast<::NActors::NLog::EPriority>(priority), \
static_cast<::NActors::NLog::EComponent>(component), \
sampleBy)

#define IS_EMERG_LOG_ENABLED(component) IS_LOG_PRIORITY_ENABLED(NActors::NLog::PRI_EMERG, component)
#define IS_ALERT_LOG_ENABLED(component) IS_LOG_PRIORITY_ENABLED(NActors::NLog::PRI_ALERT, component)
Expand All @@ -43,21 +47,21 @@

#define LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, ...) \
do { \
::NActors::NLog::TSettings* mSettings = static_cast<::NActors::NLog::TSettings*>((actorCtxOrSystem).LoggerSettings()); \
::NActors::NLog::EPriority mPriority = static_cast<::NActors::NLog::EPriority>(priority); \
::NActors::NLog::EComponent mComponent = static_cast<::NActors::NLog::EComponent>(component); \
if (mSettings && mSettings->Satisfies(mPriority, mComponent, sampleBy)) { \
if (IS_CTX_LOG_PRIORITY_ENABLED(actorCtxOrSystem, priority, component, sampleBy)) { \
::NActors::MemLogAdapter( \
actorCtxOrSystem, priority, component, __VA_ARGS__); \
} \
} while (0) /**/

#define LOG_LOG_S_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, stream) \
LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, [&]() -> TString { \
TStringBuilder logStringBuilder; \
logStringBuilder << stream; \
return std::move(logStringBuilder); \
}())
do { \
if (IS_CTX_LOG_PRIORITY_ENABLED(actorCtxOrSystem, priority, component, sampleBy)) { \
TStringBuilder logStringBuilder; \
logStringBuilder << stream; \
::NActors::MemLogAdapter( \
actorCtxOrSystem, priority, component, std::move(logStringBuilder)); \
} \
} while (0) /**/

#define LOG_LOG(actorCtxOrSystem, priority, component, ...) LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, 0ull, __VA_ARGS__)
#define LOG_LOG_S(actorCtxOrSystem, priority, component, stream) LOG_LOG_S_SAMPLED_BY(actorCtxOrSystem, priority, component, 0ull, stream)
Expand Down Expand Up @@ -327,6 +331,15 @@ namespace NActors {
}
} // namespace NDetail

inline bool IsLogPriorityEnabled(NLog::TSettings* settings, NLog::EPriority proirity, NLog::EComponent component, ui64 sampleBy = 0) {
return settings && settings->Satisfies(proirity, component, sampleBy);
}

inline bool IsLogPriorityEnabled(NLog::EPriority priority, NLog::EComponent component, ui64 sampleBy = 0) {
TActivationContext* context = TlsActivationContext;
return !context || IsLogPriorityEnabled(context->LoggerSettings(), priority, component, sampleBy);
}

template <typename TCtx>
inline void DeliverLogMessage(TCtx& ctx, NLog::EPriority mPriority, NLog::EComponent mComponent, TString &&str)
{
Expand Down
18 changes: 6 additions & 12 deletions ydb/library/yql/dq/actors/compute/dq_compute_actor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1572,10 +1572,9 @@ class TDqComputeActorBase : public NActors::TActorBootstrapped<TDerived>
const auto& inputDesc = Task.GetInputs(inputIndex);
Y_ABORT_UNLESS(inputDesc.HasSource());
source.Type = inputDesc.GetSource().GetType();
const ui64 i = inputIndex; // Crutch for clang
const auto& settings = Task.GetSourceSettings();
Y_ABORT_UNLESS(settings.empty() || inputIndex < settings.size());
CA_LOG_D("Create source for input " << i << " " << inputDesc);
CA_LOG_D("Create source for input " << inputIndex << " " << inputDesc);
try {
std::tie(source.AsyncInput, source.Actor) = AsyncIoFactory->CreateDqSource(
IDqAsyncIoFactory::TSourceArguments {
Expand Down Expand Up @@ -1607,8 +1606,7 @@ class TDqComputeActorBase : public NActors::TActorBootstrapped<TDerived>
std::tie(transform.InputBuffer, transform.Buffer) = TaskRunner->GetInputTransform(inputIndex);
Y_ABORT_UNLESS(AsyncIoFactory);
const auto& inputDesc = Task.GetInputs(inputIndex);
const ui64 i = inputIndex; // Crutch for clang
CA_LOG_D("Create transform for input " << i << " " << inputDesc.ShortDebugString());
CA_LOG_D("Create transform for input " << inputIndex << " " << inputDesc.ShortDebugString());
try {
std::tie(transform.AsyncInput, transform.Actor) = AsyncIoFactory->CreateDqInputTransform(
IDqAsyncIoFactory::TInputTransformArguments {
Expand Down Expand Up @@ -1644,8 +1642,7 @@ class TDqComputeActorBase : public NActors::TActorBootstrapped<TDerived>
std::tie(transform.Buffer, transform.OutputBuffer) = TaskRunner->GetOutputTransform(outputIndex);
Y_ABORT_UNLESS(AsyncIoFactory);
const auto& outputDesc = Task.GetOutputs(outputIndex);
const ui64 i = outputIndex; // Crutch for clang
CA_LOG_D("Create transform for output " << i << " " << outputDesc.ShortDebugString());
CA_LOG_D("Create transform for output " << outputIndex << " " << outputDesc.ShortDebugString());
try {
std::tie(transform.AsyncOutput, transform.Actor) = AsyncIoFactory->CreateDqOutputTransform(
IDqAsyncIoFactory::TOutputTransformArguments {
Expand Down Expand Up @@ -1673,8 +1670,7 @@ class TDqComputeActorBase : public NActors::TActorBootstrapped<TDerived>
const auto& outputDesc = Task.GetOutputs(outputIndex);
Y_ABORT_UNLESS(outputDesc.HasSink());
sink.Type = outputDesc.GetSink().GetType();
const ui64 i = outputIndex; // Crutch for clang
CA_LOG_D("Create sink for output " << i << " " << outputDesc);
CA_LOG_D("Create sink for output " << outputIndex << " " << outputDesc);
try {
std::tie(sink.AsyncOutput, sink.Actor) = AsyncIoFactory->CreateDqSink(
IDqAsyncIoFactory::TSinkArguments {
Expand Down Expand Up @@ -1829,15 +1825,13 @@ class TDqComputeActorBase : public NActors::TActorBootstrapped<TDerived>
bool AllAsyncOutputsFinished() const {
for (const auto& [outputIndex, sinkInfo] : SinksMap) {
if (!sinkInfo.FinishIsAcknowledged) {
ui64 index = outputIndex; // Crutch for logging through lambda.
CA_LOG_D("Waiting finish of sink[" << index << "]");
CA_LOG_D("Waiting finish of sink[" << outputIndex << "]");
return false;
}
}
for (const auto& [outputIndex, transformInfo] : OutputTransformsMap) {
if (!transformInfo.FinishIsAcknowledged) {
ui64 index = outputIndex; // Crutch for logging through lambda.
CA_LOG_D("Waiting finish of transform[" << index << "]");
CA_LOG_D("Waiting finish of transform[" << outputIndex << "]");
return false;
}
}
Expand Down

0 comments on commit bb77248

Please sign in to comment.