aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/remote_connection_status.cpp
blob: 920ff802cd3a0c27c440cc3e06c52e2aabc5d399 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "remote_connection_status.h"

#include "key_value_printer.h"

#include <library/cpp/messagebus/monitoring/mon_proto.pb.h>
 
#include <util/stream/format.h>
#include <util/stream/output.h>
#include <util/system/yassert.h>

using namespace NBus;
using namespace NBus::NPrivate;

template <typename T>
static void Add(T& thiz, const T& that) {
    thiz += that;
}

template <typename T>
static void Max(T& thiz, const T& that) {
    if (that > thiz) {
        thiz = that;
    }
}

template <typename T>
static void AssertZero(T& thiz, const T& that) {
    Y_ASSERT(thiz == T());
    Y_UNUSED(that);
}

TDurationCounter::TDurationCounter()
    : DURATION_COUNTER_MAP(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TDuration TDurationCounter::AvgDuration() const {
    if (Count == 0) {
        return TDuration::Zero();
    } else {
        return SumDuration / Count;
    }
}

TDurationCounter& TDurationCounter::operator+=(const TDurationCounter& that) {
    DURATION_COUNTER_MAP(STRUCT_FIELD_ADD, )
    return *this;
}

TString TDurationCounter::ToString() const {
    if (Count == 0) {
        return "0";
    } else {
        TStringStream ss;
        ss << "avg: " << AvgDuration() << ", max: " << MaxDuration << ", count: " << Count;
        return ss.Str();
    }
}

TRemoteConnectionStatusBase::TRemoteConnectionStatusBase()
    : REMOTE_CONNECTION_STATUS_BASE_MAP(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TRemoteConnectionStatusBase& TRemoteConnectionStatusBase ::operator+=(const TRemoteConnectionStatusBase& that) {
    REMOTE_CONNECTION_STATUS_BASE_MAP(STRUCT_FIELD_ADD, )
    return *this;
}

TRemoteConnectionIncrementalStatusBase::TRemoteConnectionIncrementalStatusBase()
    : REMOTE_CONNECTION_INCREMENTAL_STATUS_BASE_MAP(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TRemoteConnectionIncrementalStatusBase& TRemoteConnectionIncrementalStatusBase::operator+=(
    const TRemoteConnectionIncrementalStatusBase& that) {
    REMOTE_CONNECTION_INCREMENTAL_STATUS_BASE_MAP(STRUCT_FIELD_ADD, )
    return *this;
}

TRemoteConnectionReaderIncrementalStatus::TRemoteConnectionReaderIncrementalStatus()
    : REMOTE_CONNECTION_READER_INCREMENTAL_STATUS_MAP(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TRemoteConnectionReaderIncrementalStatus& TRemoteConnectionReaderIncrementalStatus::operator+=(
    const TRemoteConnectionReaderIncrementalStatus& that) {
    TRemoteConnectionIncrementalStatusBase::operator+=(that);
    REMOTE_CONNECTION_READER_INCREMENTAL_STATUS_MAP(STRUCT_FIELD_ADD, )
    return *this;
}

TRemoteConnectionReaderStatus::TRemoteConnectionReaderStatus()
    : REMOTE_CONNECTION_READER_STATUS_MAP(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TRemoteConnectionReaderStatus& TRemoteConnectionReaderStatus::operator+=(const TRemoteConnectionReaderStatus& that) {
    TRemoteConnectionStatusBase::operator+=(that);
    REMOTE_CONNECTION_READER_STATUS_MAP(STRUCT_FIELD_ADD, )
    return *this;
}

TRemoteConnectionWriterIncrementalStatus::TRemoteConnectionWriterIncrementalStatus()
    : REMOTE_CONNECTION_WRITER_INCREMENTAL_STATUS(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TRemoteConnectionWriterIncrementalStatus& TRemoteConnectionWriterIncrementalStatus::operator+=(
    const TRemoteConnectionWriterIncrementalStatus& that) {
    TRemoteConnectionIncrementalStatusBase::operator+=(that);
    REMOTE_CONNECTION_WRITER_INCREMENTAL_STATUS(STRUCT_FIELD_ADD, )
    return *this;
}

TRemoteConnectionWriterStatus::TRemoteConnectionWriterStatus()
    : REMOTE_CONNECTION_WRITER_STATUS(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TRemoteConnectionWriterStatus& TRemoteConnectionWriterStatus::operator+=(const TRemoteConnectionWriterStatus& that) {
    TRemoteConnectionStatusBase::operator+=(that);
    REMOTE_CONNECTION_WRITER_STATUS(STRUCT_FIELD_ADD, )
    return *this;
}

size_t TRemoteConnectionWriterStatus::GetInFlight() const {
    return SendQueueSize + AckMessagesSize;
}

TConnectionStatusMonRecord TRemoteConnectionStatus::GetStatusProtobuf() const {
    TConnectionStatusMonRecord status;

    // TODO: fill unfilled fields
    status.SetSendQueueSize(WriterStatus.SendQueueSize);
    status.SetAckMessagesSize(WriterStatus.AckMessagesSize);
    // status.SetErrorCount();
    // status.SetWriteBytes();
    // status.SetWriteBytesCompressed();
    // status.SetWriteMessages();
    status.SetWriteSyscalls(WriterStatus.Incremental.NetworkOps);
    status.SetWriteActs(WriterStatus.Acts);
    // status.SetReadBytes();
    // status.SetReadBytesCompressed();
    // status.SetReadMessages();
    status.SetReadSyscalls(ReaderStatus.Incremental.NetworkOps);
    status.SetReadActs(ReaderStatus.Acts);

    TMessageStatusCounter sumStatusCounter;
    sumStatusCounter += WriterStatus.Incremental.StatusCounter;
    sumStatusCounter += ReaderStatus.Incremental.StatusCounter;
    sumStatusCounter.FillErrorsProtobuf(&status);

    return status;
}

TString TRemoteConnectionStatus::PrintToString() const {
    TStringStream ss;

    TKeyValuePrinter p;

    if (!Summary) {
        // TODO: print MyAddr too, but only if it is set
        ss << WriterStatus.PeerAddr << " (" << WriterStatus.ConnectionId << ")"
           << ", writefd=" << WriterStatus.Fd
           << ", readfd=" << ReaderStatus.Fd
           << Endl;
        if (WriterStatus.Connected) {
            p.AddRow("connect time", WriterStatus.ConnectTime.ToString());
            p.AddRow("writer state", ToCString(WriterStatus.State));
        } else {
            ss << "not connected";
            if (WriterStatus.ConnectError != 0) {
                ss << ", last connect error: " << LastSystemErrorText(WriterStatus.ConnectError);
            }
            ss << Endl;
        }
    }
    if (!Server) {
        p.AddRow("connect syscalls", WriterStatus.ConnectSyscalls);
    }

    p.AddRow("send queue", LeftPad(WriterStatus.SendQueueSize, 6));

    if (Server) {
        p.AddRow("quota msg", LeftPad(ReaderStatus.QuotaMsg, 6));
        p.AddRow("quota bytes", LeftPad(ReaderStatus.QuotaBytes, 6));
        p.AddRow("quota exhausted", LeftPad(ReaderStatus.QuotaExhausted, 6));
        p.AddRow("reader wakeups", LeftPad(WriterStatus.ReaderWakeups, 6));
    } else {
        p.AddRow("ack messages", LeftPad(WriterStatus.AckMessagesSize, 6));
    }

    p.AddRow("written", WriterStatus.Incremental.MessageCounter.ToString(false));
    p.AddRow("read", ReaderStatus.Incremental.MessageCounter.ToString(true));

    p.AddRow("write syscalls", LeftPad(WriterStatus.Incremental.NetworkOps, 12));
    p.AddRow("read syscalls", LeftPad(ReaderStatus.Incremental.NetworkOps, 12));

    p.AddRow("write acts", LeftPad(WriterStatus.Acts, 12));
    p.AddRow("read acts", LeftPad(ReaderStatus.Acts, 12));

    p.AddRow("write buffer cap", LeftPad(WriterStatus.BufferSize, 12));
    p.AddRow("read buffer cap", LeftPad(ReaderStatus.BufferSize, 12));

    p.AddRow("write buffer drops", LeftPad(WriterStatus.Incremental.BufferDrops, 10));
    p.AddRow("read buffer drops", LeftPad(ReaderStatus.Incremental.BufferDrops, 10));

    if (Server) {
        p.AddRow("process dur", WriterStatus.DurationCounterPrev.ToString());
    }

    ss << p.PrintToString();

    if (false && Server) {
        ss << "time histogram:\n";
        ss << WriterStatus.Incremental.ProcessDurationHistogram.PrintToString();
    }

    TMessageStatusCounter sumStatusCounter;
    sumStatusCounter += WriterStatus.Incremental.StatusCounter;
    sumStatusCounter += ReaderStatus.Incremental.StatusCounter;

    ss << sumStatusCounter.PrintToString();

    return ss.Str();
}

TRemoteConnectionStatus::TRemoteConnectionStatus()
    : REMOTE_CONNECTION_STATUS_MAP(STRUCT_FIELD_INIT_DEFAULT, COMMA)
{
}

TString TSessionDumpStatus::PrintToString() const {
    if (Shutdown) {
        return "shutdown";
    }

    TStringStream ss;
    ss << Head;
    if (ConnectionStatusSummary.Server) {
        ss << "\n";
        ss << Acceptors;
    }
    ss << "\n";
    ss << "connections summary:" << Endl;
    ss << ConnectionsSummary;
    if (!!Connections) {
        ss << "\n";
        ss << Connections;
    }
    ss << "\n";
    ss << Config.PrintToString();
    return ss.Str();
}

TString TBusMessageQueueStatus::PrintToString() const {
    TStringStream ss;
    ss << "work queue:\n";
    ss << ExecutorStatus.Status;
    ss << "\n";
    ss << "queue config:\n";
    ss << Config.PrintToString();
    return ss.Str();
}