aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/providers/dq/counters/task_counters.cpp
blob: cd569130424415a2311af4ffc773b3e2b7edd63d (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "task_counters.h"
#include <ydb/library/yql/providers/dq/common/yql_dq_common.h>

namespace NYql {

void AddHistogram(THashMap<i64, ui64>& aggregatedHist, const THashMap<i64, ui64>& hist) {
    for (const auto& [k, v] : hist) {
        aggregatedHist[k] += v;
    }
}

TTaskCounters AggregateQueryStatsByStage(TTaskCounters& queryStat, const THashMap<ui64, ui64>& task2Stage, bool collectFull) {
    TTaskCounters aggregatedQueryStat;
    THashMap<TString, THashSet<ui64>> stage2Tasks;
    THashMap<TString, THashSet<ui64>> stage2Output;
    THashMap<TString, THashSet<ui64>> stage2Input;

    /* Depends on missing TotalTime statistics
    THashMap<ui64, i64> BusyTime;
    for (const auto& [k, v] : queryStat.Get()) {
        std::map<TString, TString> labels;
        TString prefix, name;
        if (NCommon::ParseCounterName(&prefix, &labels, &name, k)) {
            if (prefix == "TaskRunner") {
                auto maybeTask = labels.find("Task");
                if (maybeTask == labels.end()) {
                    continue;
                }
                ui64 taskId;
                if (!TryFromString(maybeTask->second, taskId)) {
                    continue;
                }
                if (name == "TotalTime") {
                    BusyTime[taskId] += v.Sum;
                } else if (name == "WaitTime") {
                    BusyTime[taskId] -= v.Sum;
                }
            }
        }
    }
    */
    /*
    for (const auto& [taskId, value] : BusyTime) {
        TCounters::TEntry entry = {value, value, value, value, 1};
        queryStat.AddCounter(queryStat.GetCounterName("TaskRunner", {{"Task", ToString(taskId)}}, "BusyTime"), entry);
    }
    */
    for (const auto& [k, v] : queryStat.Get()) {
        std::map<TString, TString> labels;
        TString prefix, name;
        if (k.StartsWith("TaskRunner") && NCommon::ParseCounterName(&prefix, &labels, &name, k)) {
            auto maybeTask = labels.find("Task");
            if (maybeTask == labels.end()) {
                aggregatedQueryStat.AddCounter(k, v);
                continue;
            }
            ui64 taskId;
            if (!TryFromString(maybeTask->second, taskId)) {
                continue;
            }
            auto maybeStage = task2Stage.find(taskId);
            TString stageId = maybeStage == task2Stage.end()
                ? "0"
                : ToString(maybeStage->second);
            ui64 channelId;
            bool input = false;
            bool output = false;
            auto maybeInputChannel = labels.find("InputChannel");
            if (maybeInputChannel != labels.end()) {
                if (!TryFromString(maybeInputChannel->second, channelId)) {
                    continue;
                }
                ui32 stage = 0;
                auto maybeSrcStageId = labels.find("SrcStageId");
                if (maybeSrcStageId != labels.end()) {
                    TryFromString(maybeSrcStageId->second, stage);
                    labels.erase(maybeSrcStageId);
                }
                stage2Input[stageId].insert(channelId);
                stage2Input["Total"].insert(channelId);
                labels.erase(maybeInputChannel);
                labels["Input"] = ToString(stage);
                input = true;
            }
            auto maybeOutputChannel = labels.find("OutputChannel");
            if (maybeOutputChannel != labels.end()) {
                if (!TryFromString(maybeOutputChannel->second, channelId)) {
                    continue;
                }
                ui32 stage = 0;
                auto maybeDstStageId = labels.find("DstStageId");
                if (maybeDstStageId != labels.end()) {
                    TryFromString(maybeDstStageId->second, stage);
                    labels.erase(maybeDstStageId);
                }
                stage2Output[stageId].insert(channelId);
                stage2Output["Total"].insert(channelId);
                labels.erase(maybeOutputChannel);
                labels["Output"] = ToString(stage);
                output = true;
            }
            labels.erase(maybeTask);
            labels["Stage"] = ToString(stageId);
            stage2Tasks[stageId].insert(taskId);
            stage2Tasks["Total"].insert(taskId);
            if (collectFull) {
                aggregatedQueryStat.AddCounter(queryStat.GetCounterName("TaskRunner", labels, name), v);
            }
            if (input || output) {
                if (input) {
                    labels["Input"] = "Total";
                }
                if (output) {
                    labels["Output"] = "Total";
                }
                if (collectFull) {
                    aggregatedQueryStat.AddCounter(queryStat.GetCounterName("TaskRunner", labels, name), v);
                }
            }
            labels["Stage"] = "Total";
            aggregatedQueryStat.AddCounter(queryStat.GetCounterName("TaskRunner", labels, name), v);
        } else {
            aggregatedQueryStat.AddCounter(k, v);
        }
    }
    for (const auto& [k, v] : queryStat.GetHistograms()) {
        std::map<TString, TString> labels;
        TString prefix, name;
        if (k.StartsWith("TaskRunner") && NCommon::ParseCounterName(&prefix, &labels, &name, k)) {
            auto maybeTask = labels.find("Task");
            if (maybeTask == labels.end()) {
                AddHistogram(aggregatedQueryStat.GetHistogram(k), v);
                continue;
            }
            ui64 taskId;
            if (!TryFromString(maybeTask->second, taskId)) {
                continue;
            }
            auto maybeStage = task2Stage.find(taskId);
            TString stageId = maybeStage == task2Stage.end()
                ? "0"
                : ToString(maybeStage->second);
            labels.erase(maybeTask);
            labels["Stage"] = ToString(stageId);
            AddHistogram(aggregatedQueryStat.GetHistogram(queryStat.GetCounterName("TaskRunner", labels, name)), v);
        } else {
            AddHistogram(aggregatedQueryStat.GetHistogram(k), v);
        }
    }
    
    for (const auto& [stageId, v] : stage2Tasks) {
        if (collectFull || stageId == "Total") {
            aggregatedQueryStat.AddCounter(queryStat.GetCounterName("TaskRunner",
                {{"Stage", stageId}}, "TasksCount"), static_cast<ui64>(v.size()));
        }
    }
    for (const auto& [stageId, v] : stage2Input) {
        if (collectFull || stageId == "Total") {
            aggregatedQueryStat.AddCounter(queryStat.GetCounterName("TaskRunner",
                {{"Stage", stageId},{"Input", "Total"}}, "ChannelsCount"), static_cast<ui64>(v.size()));
        }
    }
    for (const auto& [stageId, v] : stage2Output) {
        if (collectFull || stageId == "Total") {
            aggregatedQueryStat.AddCounter(queryStat.GetCounterName("TaskRunner",
                {{"Stage", stageId},{"Output", "Total"}}, "ChannelsCount"), static_cast<ui64>(v.size()));
        }
    }

    return aggregatedQueryStat;
}

} // namespace NYql