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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/**
* Copyright (C) 2019-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include "mongo/db/client.h"
#include "mongo/rpc/metadata/client_metadata_ismaster.h"
#include "mongo/util/tick_source.h"
#include "mongo/util/time_support.h"
namespace mongo {
/**
* Tracks metrics for a single transaction coordinator.
*
* For all timing related stats, a TickSource with at least microsecond resolution must be used.
*/
class SingleTransactionCoordinatorStats {
public:
SingleTransactionCoordinatorStats() = default;
struct LastClientInfo {
std::string clientHostAndPort;
long long connectionId;
BSONObj clientMetadata;
std::string appName;
void update(Client* client) {
if (client->hasRemote()) {
clientHostAndPort = client->getRemote().toString();
}
connectionId = client->getConnectionId();
if (const auto& metadata =
ClientMetadataIsMasterState::get(client).getClientMetadata()) {
clientMetadata = metadata.get().getDocument();
appName = metadata.get().getApplicationName().toString();
}
}
};
//
// Setters
//
/**
* Sets the time the transaction coordinator was created.
*
* Can only be called once.
*/
void setCreateTime(TickSource::Tick curTick, Date_t curWallClockTime);
/**
* Sets the time the transaction coordinator was destroyed.
*
* Can only be called once, and must be called after setCreateTime.
*/
void setEndTime(TickSource::Tick curTick, Date_t curWallClockTime);
/**
* Sets the time the transaction coordinator wrote the participant list and started waiting for
* the participant list to become majority-committed.
*
* Can only be called once, and must be called after setCreateTime.
*/
void setWritingParticipantListStartTime(TickSource::Tick curTick, Date_t curWallClockTime);
/**
* Sets the time the transaction coordinator sent 'prepare' to the participants and started
* waiting for votes (i.e., 'prepare' responses).
*
* Can only be called once, and must be called after setWritingParticipantListStartTime.
*/
void setWaitingForVotesStartTime(TickSource::Tick curTick, Date_t curWallClockTime);
/**
* Sets the time the transaction coordinator wrote the decision and started waiting for the
* decision to become majority-committed.
*
* Can only be called once, and must be called after setWaitingForVotesStartTime.
*/
void setWritingDecisionStartTime(TickSource::Tick curTick, Date_t curWallClockTime);
/**
* Sets the time the transaction coordinator sent the decision to the participants and started
* waiting for acknowledgments.
*
* Can only be called once, and must be called after setWritingDecisionStartTime.
*/
void setWaitingForDecisionAcksStartTime(TickSource::Tick curTick, Date_t curWallClockTime);
/**
* Sets the time the transaction coordinator deleted its durable state.
*
* Can only be called once, and must be called after setWaitingForDecisionAcksStartTime.
*/
void setDeletingCoordinatorDocStartTime(TickSource::Tick curTick, Date_t curWallClockTime);
//
// Getters
//
/**
* Returns the time the coordinator was created.
*
* Must be called after setCreateTime.
*/
Date_t getCreateTime() const {
return _createWallClockTime;
}
/**
* Returns the time the coordinator was destroyed.
*
* Must be called after setCreateTime.
*/
Date_t getEndTime() const {
return _endWallClockTime;
}
/**
* Returns the time the coordinator started writing the participant list. Note, this is also the
* two-phase commit start time.
*
* Must be called after setWritingParticipantListStartTime.
*/
Date_t getWritingParticipantListStartTime() const {
return _writingParticipantListStartWallClockTime;
}
/**
* Returns the time the coordinator started sending 'prepare' and collecting votes.
*
* Must be called after setWaitingForVotesStartTime.
*/
Date_t getWaitingForVotesStartTime() const {
return _waitingForVotesStartWallClockTime;
}
/**
* Returns the time the coordinator started making the decision durable.
*
* Must be called after setWritingDecisionStartTime.
*/
Date_t getWritingDecisionStartTime() const {
return _writingDecisionStartWallClockTime;
}
/**
* Returns the time the coordinator started sending the decision and waiting for
* acknowledgments.
*
* Must be called after setWaitingForDecisionAcksStartTime.
*/
Date_t getWaitingForDecisionAcksStartTime() const {
return _waitingForDecisionAcksStartWallClockTime;
}
/**
* Returns the time the coordinator started deleting its durable state.
*
* Must be called after setDeletingCoordinatorDocStartTime.
*/
Date_t getDeletingCoordinatorDocStartTime() const {
return _deletingCoordinatorDocStartWallClockTime;
}
/**
* If the end time has been set, returns the duration between the create time and end time, else
* returns the duration between the create time and curTick.
*
* Must be called after setCreateTime, but can be called any number of times.
*/
Microseconds getDurationSinceCreation(TickSource* tickSource, TickSource::Tick curTick) const;
/**
* If the end time has been set, returns the duration between the writing participant list start
* time and end time, else returns the duration between the writing participant list start time
* and curTick.
*
* Must be called after setWritingParticipantListStartTime, but can be called any number of
* times.
*/
Microseconds getTwoPhaseCommitDuration(TickSource* tickSource, TickSource::Tick curTick) const;
/**
* If the waiting for votes start time has been set, returns the duration between the writing
* participant list start time and the waiting for votes start time, else returns the duration
* between the writing for participant list start time and curTick.
*
* Must be called after setWritingParticipantListStartTime, but can be called any number of
* times.
*/
Microseconds getWritingParticipantListDuration(TickSource* tickSource,
TickSource::Tick curTick) const;
/**
* If the writing decision start time has been set, returns the duration between the waiting for
* votes start time and the writing decision start time, else returns the duration between
* the waiting for votes start time and curTick.
*
* Must be called after setWaitingForVotesStartTime, but can be called any number of times.
*/
Microseconds getWaitingForVotesDuration(TickSource* tickSource, TickSource::Tick curTick) const;
/**
* If the waiting for decision acks start time has been set, returns the duration between the
* writing decision start time and the waiting for decision acks start time, else returns the
* duration between the writing decision start time and curTick.
*
* Must be called after setWritingDecisionStartTime, but can be called any number of times.
*/
Microseconds getWritingDecisionDuration(TickSource* tickSource, TickSource::Tick curTick) const;
/**
* If the deleting coordinator doc start time has been set, returns the duration between the
* waiting for decision acks start time and the deleting coordinator doc start time, else
* returns the duration between the waiting for decision acks start time and curTick.
*
* Must be called after setWaitingForDecisionAcksStartTime, but can be called any number of
* times.
*/
Microseconds getWaitingForDecisionAcksDuration(TickSource* tickSource,
TickSource::Tick curTick) const;
/**
* If the end time has been set, returns the duration between the deleting coordinator doc start
* and the end time, else returns the duration between the deleting coordinator doc start time
* and curTick.
*
* Must be called after setDeletingCoordinatorDocStartTime, but can be called any number of
* times.
*/
Microseconds getDeletingCoordinatorDocDuration(TickSource* tickSource,
TickSource::Tick curTick) const;
/**
* Reports the time duration for each step in the two-phase commit and stores them as a
* sub-document of the provided parent BSONObjBuilder. The metrics are stored under key
* "stepDurations" in the parent document.
*/
void reportMetrics(BSONObjBuilder& parent,
TickSource* tickSource,
TickSource::Tick curTick) const;
/**
* Reports information about the last client to interact with this transaction.
*/
void reportLastClient(BSONObjBuilder& parent) const;
/**
* Updates the LastClientInfo object stored in this SingleTransactionStats instance with the
* given Client's information.
*/
void updateLastClientInfo(Client* client) {
invariant(client);
_lastClientInfo.update(client);
}
private:
Date_t _createWallClockTime;
TickSource::Tick _createTime{0};
// The writing participant list start time doubles as the two-phase commit start time, since
// writing the participant list is the first step of the two-phase commit.
Date_t _writingParticipantListStartWallClockTime;
TickSource::Tick _writingParticipantListStartTime{0};
Date_t _waitingForVotesStartWallClockTime;
TickSource::Tick _waitingForVotesStartTime{0};
Date_t _writingDecisionStartWallClockTime;
TickSource::Tick _writingDecisionStartTime{0};
Date_t _waitingForDecisionAcksStartWallClockTime;
TickSource::Tick _waitingForDecisionAcksStartTime{0};
Date_t _deletingCoordinatorDocStartWallClockTime;
TickSource::Tick _deletingCoordinatorDocStartTime{0};
Date_t _endWallClockTime;
TickSource::Tick _endTime{0};
LastClientInfo _lastClientInfo;
};
} // namespace mongo
|