1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/platform/basic.h"
32
33 #include "mongo/executor/connection_pool_stats.h"
34
35 #include "mongo/bson/bsonobjbuilder.h"
36 #include "mongo/util/map_util.h"
37
38 namespace mongo {
39 namespace executor {
40
ConnectionStatsPer(size_t nInUse,size_t nAvailable,size_t nCreated,size_t nRefreshing)41 ConnectionStatsPer::ConnectionStatsPer(size_t nInUse,
42 size_t nAvailable,
43 size_t nCreated,
44 size_t nRefreshing)
45 : inUse(nInUse), available(nAvailable), created(nCreated), refreshing(nRefreshing) {}
46
47 ConnectionStatsPer::ConnectionStatsPer() = default;
48
operator +=(const ConnectionStatsPer & other)49 ConnectionStatsPer& ConnectionStatsPer::operator+=(const ConnectionStatsPer& other) {
50 inUse += other.inUse;
51 available += other.available;
52 created += other.created;
53 refreshing += other.refreshing;
54
55 return *this;
56 }
57
updateStatsForHost(std::string pool,HostAndPort host,ConnectionStatsPer newStats)58 void ConnectionPoolStats::updateStatsForHost(std::string pool,
59 HostAndPort host,
60 ConnectionStatsPer newStats) {
61 // Update stats for this host.
62 statsByPool[pool] += newStats;
63 statsByHost[host] += newStats;
64 statsByPoolHost[pool][host] += newStats;
65
66 // Update total connection stats.
67 totalInUse += newStats.inUse;
68 totalAvailable += newStats.available;
69 totalCreated += newStats.created;
70 totalRefreshing += newStats.refreshing;
71 }
72
appendToBSON(mongo::BSONObjBuilder & result,bool forFTDC)73 void ConnectionPoolStats::appendToBSON(mongo::BSONObjBuilder& result, bool forFTDC) {
74 result.appendNumber("totalInUse", totalInUse);
75 result.appendNumber("totalAvailable", totalAvailable);
76 result.appendNumber("totalCreated", totalCreated);
77 result.appendNumber("totalRefreshing", totalRefreshing);
78
79 if (forFTDC) {
80 BSONObjBuilder poolBuilder(result.subobjStart("connectionsInUsePerPool"));
81 for (const auto& pool : statsByPool) {
82 BSONObjBuilder poolInfo(poolBuilder.subobjStart(pool.first));
83 auto& poolStats = pool.second;
84 poolInfo.appendNumber("poolInUse", poolStats.inUse);
85 for (const auto& host : statsByPoolHost[pool.first]) {
86 auto hostStats = host.second;
87 poolInfo.appendNumber(host.first.toString(), hostStats.inUse);
88 }
89 }
90
91 return;
92 }
93
94 {
95 BSONObjBuilder poolBuilder(result.subobjStart("pools"));
96 for (const auto& pool : statsByPool) {
97 BSONObjBuilder poolInfo(poolBuilder.subobjStart(pool.first));
98 auto& poolStats = pool.second;
99 poolInfo.appendNumber("poolInUse", poolStats.inUse);
100 poolInfo.appendNumber("poolAvailable", poolStats.available);
101 poolInfo.appendNumber("poolCreated", poolStats.created);
102 poolInfo.appendNumber("poolRefreshing", poolStats.refreshing);
103 for (const auto& host : statsByPoolHost[pool.first]) {
104 BSONObjBuilder hostInfo(poolInfo.subobjStart(host.first.toString()));
105 auto& hostStats = host.second;
106 hostInfo.appendNumber("inUse", hostStats.inUse);
107 hostInfo.appendNumber("available", hostStats.available);
108 hostInfo.appendNumber("created", hostStats.created);
109 hostInfo.appendNumber("refreshing", hostStats.refreshing);
110 }
111 }
112 }
113 {
114 BSONObjBuilder hostBuilder(result.subobjStart("hosts"));
115 for (auto&& host : statsByHost) {
116 BSONObjBuilder hostInfo(hostBuilder.subobjStart(host.first.toString()));
117 auto hostStats = host.second;
118 hostInfo.appendNumber("inUse", hostStats.inUse);
119 hostInfo.appendNumber("available", hostStats.available);
120 hostInfo.appendNumber("created", hostStats.created);
121 hostInfo.appendNumber("refreshing", hostStats.refreshing);
122 }
123 }
124 }
125
126 } // namespace executor
127 } // namespace mongo
128