summaryrefslogtreecommitdiff
path: root/src/mongo/client/connection_pool.cpp
blob: 17628610135767ffe130bdd4d1ff02e0ddcee431 (plain)
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
/**
 *    Copyright (C) 2015 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    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 GNU Affero General 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.
 */

#include "mongo/platform/basic.h"

#include "mongo/client/connection_pool.h"

#include "mongo/client/connpool.h"
#include "mongo/db/auth/authorization_manager_global.h"
#include "mongo/db/auth/internal_user_auth.h"
#include "mongo/stdx/mutex.h"

namespace mongo {
namespace {

    const Date_t kNeverTooStale = Date_t::max();

    const Minutes kCleanUpInterval(5); // Note: Must be larger than kMaxConnectionAge below)
    const Seconds kMaxConnectionAge(30);

} // namespace

    ConnectionPool::ConnectionPool(int messagingPortTags) : _messagingPortTags(messagingPortTags) {}

    ConnectionPool::~ConnectionPool() {
        cleanUpOlderThan(Date_t::max());

        invariant(_connections.empty());
        invariant(_inUseConnections.empty());
    }

    void ConnectionPool::cleanUpOlderThan(Date_t now) {
        stdx::lock_guard<stdx::mutex> lk(_mutex);
        _cleanUpOlderThan_inlock(now);
    }

    void ConnectionPool::_cleanUpOlderThan_inlock(Date_t now) {
        HostConnectionMap::iterator hostConns = _connections.begin();
        while (hostConns != _connections.end()) {
            _cleanUpOlderThan_inlock(now, &hostConns->second);
            if (hostConns->second.empty()) {
                _connections.erase(hostConns++);
            }
            else {
                ++hostConns;
            }
        }
    }

    void ConnectionPool::_cleanUpOlderThan_inlock(Date_t now, ConnectionList* hostConns) {
        ConnectionList::iterator iter = hostConns->begin();
        while (iter != hostConns->end()) {
            if (_shouldKeepConnection(now, *iter)) {
                ++iter;
            }
            else {
                _destroyConnection_inlock(hostConns, iter++);
            }
        }
    }

    bool ConnectionPool::_shouldKeepConnection(Date_t now, const ConnectionInfo& connInfo) const {
        const Date_t expirationDate = connInfo.creationDate + kMaxConnectionAge;
        if (expirationDate <= now) {
            return false;
        }

        return true;
    }

    void ConnectionPool::closeAllInUseConnections() {
        stdx::lock_guard<stdx::mutex> lk(_mutex);
        for (ConnectionList::iterator iter = _inUseConnections.begin();
             iter != _inUseConnections.end();
             ++iter) {

            iter->conn->port().shutdown();
        }
    }

    void ConnectionPool::_cleanUpStaleHosts_inlock(Date_t now) {
        if (now > _lastCleanUpTime + kCleanUpInterval) {
            for (HostLastUsedMap::iterator itr = _lastUsedHosts.begin();
                 itr != _lastUsedHosts.end();
                 itr++) {

                if (itr->second <= _lastCleanUpTime) {
                    ConnectionList connList = _connections.find(itr->first)->second;
                    _cleanUpOlderThan_inlock(now, &connList);
                    invariant(connList.empty());
                    itr->second = kNeverTooStale;
                }
            }

            _lastCleanUpTime = now;
        }
    }

    ConnectionPool::ConnectionList::iterator ConnectionPool::acquireConnection(
                                                                const HostAndPort& target,
                                                                Date_t now,
                                                                Milliseconds timeout) {
        stdx::unique_lock<stdx::mutex> lk(_mutex);

        // Clean up connections on stale/unused hosts
        _cleanUpStaleHosts_inlock(now);

        for (HostConnectionMap::iterator hostConns;
             ((hostConns = _connections.find(target)) != _connections.end());) {

            // Clean up the requested host to remove stale/unused connections
            _cleanUpOlderThan_inlock(now, &hostConns->second);
            if (hostConns->second.empty()) {
                // prevent host from causing unnecessary cleanups
                _lastUsedHosts[hostConns->first] = kNeverTooStale;
                break;
            }

            _inUseConnections.splice(_inUseConnections.begin(),
                                     hostConns->second,
                                     hostConns->second.begin());

            const ConnectionList::iterator candidate = _inUseConnections.begin();
            lk.unlock();
            try {
                if (candidate->conn->isStillConnected()) {
                    // setSoTimeout takes a double representing the number of seconds for send and
                    // receive timeouts.  Thus, we must take count() and divide by
                    // 1000.0 to get the number of seconds with a fractional part.
                    candidate->conn->setSoTimeout(timeout.count() / 1000.0);
                    return candidate;
                }
            }
            catch (...) {
                lk.lock();
                _destroyConnection_inlock(&_inUseConnections, candidate);
                throw;
            }

            lk.lock();
            _destroyConnection_inlock(&_inUseConnections, candidate);
        }

        // No idle connection in the pool; make a new one.
        lk.unlock();
        std::unique_ptr<DBClientConnection> conn(new DBClientConnection);

        // setSoTimeout takes a double representing the number of seconds for send and receive
        // timeouts.  Thus, we must take count() and divide by 1000.0 to get the number
        // of seconds with a fractional part.
        conn->setSoTimeout(timeout.count() / 1000.0);
        std::string errmsg;
        uassert(28640,
                str::stream() << "Failed attempt to connect to "
                              << target.toString() << "; " << errmsg,
                conn->connect(target, errmsg));

        conn->port().tag |= _messagingPortTags;

        if (getGlobalAuthorizationManager()->isAuthEnabled()) {
            uassert(ErrorCodes::AuthenticationFailed,
                    "Missing credentials for authenticating as internal user",
                    isInternalAuthSet());
            conn->auth(getInternalUserAuthParamsWithFallback());
        }

        lk.lock();
        return _inUseConnections.insert(_inUseConnections.begin(),
                                        ConnectionInfo(conn.release(), now));
    }

    void ConnectionPool::releaseConnection(ConnectionList::iterator iter, const Date_t now) {
        stdx::lock_guard<stdx::mutex> lk(_mutex);
        if (!_shouldKeepConnection(now, *iter)) {
            _destroyConnection_inlock(&_inUseConnections, iter);
            return;
        }

        ConnectionList& hostConns = _connections[iter->conn->getServerHostAndPort()];
        _cleanUpOlderThan_inlock(now, &hostConns);
        hostConns.splice(hostConns.begin(), _inUseConnections, iter);
        _lastUsedHosts[iter->conn->getServerHostAndPort()] = now;
    }

    void ConnectionPool::destroyConnection(ConnectionList::iterator iter) {
        stdx::lock_guard<stdx::mutex> lk(_mutex);
        _destroyConnection_inlock(&_inUseConnections, iter);
    }

    void ConnectionPool::_destroyConnection_inlock(ConnectionList* connList,
                                                  ConnectionList::iterator iter) {
        delete iter->conn;
        connList->erase(iter);
    }


    //
    // ConnectionPool::ConnectionPtr
    //

    ConnectionPool::ConnectionPtr::ConnectionPtr(ConnectionPool* pool,
                                                 const HostAndPort& target,
                                                 Date_t now,
                                                 Milliseconds timeout)
        : _pool(pool),
          _connInfo(pool->acquireConnection(target, now, timeout)) {

    }

    ConnectionPool::ConnectionPtr::~ConnectionPtr() {
        if (_pool) {
            _pool->destroyConnection(_connInfo);
        }
    }

    void ConnectionPool::ConnectionPtr::done(Date_t now) {
        _pool->releaseConnection(_connInfo, now);
        _pool = NULL;
    }

} // namespace mongo