summaryrefslogtreecommitdiff
path: root/src/mongo/s/client/multi_host_query.cpp
blob: bfe9d36f55d76e73dc22d019c40b45349ef21856 (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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
 *    Copyright (C) 2014 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/s/client/multi_host_query.h"

#include "mongo/bson/util/builder.h"

namespace mongo {

using std::shared_ptr;
using std::make_pair;
using std::string;
using std::vector;

typedef stdx::unique_lock<stdx::mutex> boost_unique_lock;

HostThreadPool::HostThreadPool(int poolSize, bool scopeAllWork)
    : _scopeAllWork(scopeAllWork), _context(new PoolContext) {
    // All threads start as active to avoid races detecting idleness on thread startup -
    // the pool isn't idle until all threads have started waiting.
    _context->numActiveWorkers = poolSize;

    for (int i = 0; i < poolSize; ++i) {
        //
        // Each thread keeps a shared context allowing them to synchronize even if this
        // dispatching pool has already been disposed.
        //

        _threads.push_back(new stdx::thread(stdx::bind(&HostThreadPool::doWork, _context)));
    }
}

void HostThreadPool::schedule(Callback callback) {
    boost_unique_lock lk(_context->mutex);
    _context->scheduled.push_back(callback);
    _context->workScheduledCV.notify_one();
}

void HostThreadPool::doWork(std::shared_ptr<PoolContext> context) {
    while (true) {
        Callback callback;

        {
            boost_unique_lock lk(context->mutex);

            --context->numActiveWorkers;
            if (context->numActiveWorkers == 0)
                context->isIdleCV.notify_all();

            // Wait for work or until we're finished
            while (context->isPoolActive && context->scheduled.empty()) {
                context->workScheduledCV.wait(lk);
            }

            //
            // Either the pool is no longer active, or the queue has some work we should do
            //

            if (!context->isPoolActive)
                return;

            invariant(!context->scheduled.empty());
            callback = context->scheduled.front();
            context->scheduled.pop_front();

            ++context->numActiveWorkers;
        }

        callback();
    }
}

void HostThreadPool::waitUntilIdle() {
    boost_unique_lock lk(_context->mutex);
    while (_context->numActiveWorkers > 0) {
        _context->isIdleCV.wait(lk);
    }
}

HostThreadPool::~HostThreadPool() {
    // Boost can throw on notify(), join(), detach()

    {
        boost_unique_lock lk(_context->mutex);
        _context->isPoolActive = false;
        _context->scheduled.clear();
    }

    DESTRUCTOR_GUARD(_context->workScheduledCV.notify_all();)

    for (vector<stdx::thread*>::iterator it = _threads.begin(); it != _threads.end(); ++it) {
        if (_scopeAllWork) {
            DESTRUCTOR_GUARD((*it)->join();)
        } else {
            DESTRUCTOR_GUARD((*it)->detach();)
        }

        delete *it;
    }
}

HostThreadPools::HostThreadPools(int poolSize, bool scopeAllWork)
    : _poolSize(poolSize), _scopeAllWork(scopeAllWork) {}

void HostThreadPools::schedule(const ConnectionString& host, HostThreadPool::Callback callback) {
    boost_unique_lock lk(_mutex);

    HostPoolMap::iterator seenIt = _pools.find(host);
    if (seenIt == _pools.end()) {
        seenIt = _pools.insert(make_pair(host, new HostThreadPool(_poolSize, _scopeAllWork))).first;
    }

    seenIt->second->schedule(callback);
}

void HostThreadPools::waitUntilIdle(const ConnectionString& host) {
    // Note that this prevents the creation of any new pools - it is only intended to be used
    // for testing.

    boost_unique_lock lk(_mutex);

    HostPoolMap::iterator seenIt = _pools.find(host);
    if (seenIt == _pools.end())
        return;

    seenIt->second->waitUntilIdle();
}

HostThreadPools::~HostThreadPools() {
    boost_unique_lock lk(_mutex);
    for (HostPoolMap::iterator it = _pools.begin(); it != _pools.end(); ++it) {
        delete it->second;
    }
}

MultiHostQueryOp::MultiHostQueryOp(SystemEnv* systemEnv, HostThreadPools* hostThreads)
    : _systemEnv(systemEnv), _hostThreads(hostThreads) {}

StatusWith<DBClientCursor*> MultiHostQueryOp::queryAny(const vector<ConnectionString>& hosts,
                                                       const QuerySpec& query,
                                                       int timeoutMillis) {
    Date_t nowMillis = _systemEnv->currentTimeMillis();
    Date_t timeoutAtMillis = nowMillis + Milliseconds(timeoutMillis);

    // Send out all queries
    scheduleQuery(hosts, query, timeoutAtMillis);

    // Wait for them to come back
    return waitForNextResult(timeoutAtMillis);
}

void MultiHostQueryOp::scheduleQuery(const vector<ConnectionString>& hosts,
                                     const QuerySpec& query,
                                     Date_t timeoutAtMillis) {
    invariant(_pending.empty());

    for (vector<ConnectionString>::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
        const ConnectionString& host = *it;

        shared_ptr<PendingQueryContext> pendingOp(
            new PendingQueryContext(host, query, timeoutAtMillis, this));

        _pending.insert(make_pair(host, pendingOp));

        HostThreadPool::Callback callback =
            stdx::bind(&MultiHostQueryOp::PendingQueryContext::doBlockingQuery, pendingOp);

        _hostThreads->schedule(host, callback);
    }
}

StatusWith<DBClientCursor*> MultiHostQueryOp::waitForNextResult(Date_t timeoutAtMillis) {
    StatusWith<DBClientCursor*> nextResult(NULL);

    boost_unique_lock lk(_resultsMutex);
    while (!releaseResult_inlock(&nextResult)) {
        Date_t nowMillis = _systemEnv->currentTimeMillis();

        if (nowMillis >= timeoutAtMillis) {
            nextResult = StatusWith<DBClientCursor*>(combineErrorResults_inlock());
            break;
        }

        _nextResultCV.wait_for(lk, timeoutAtMillis - nowMillis);
    }

    dassert(!nextResult.isOK() || nextResult.getValue());
    return nextResult;
}

void MultiHostQueryOp::noteResult(const ConnectionString& host,
                                  StatusWith<DBClientCursor*> result) {
    boost_unique_lock lk(_resultsMutex);
    dassert(_results.find(host) == _results.end());
    _results.insert(make_pair(host, result));

    _nextResultCV.notify_one();
}

/**
 * The results in the result map have four states:
 * Nonexistent - query result still pending
 * Status::OK w/ pointer - successful query result, not yet released to user
 * Status::OK w/ NULL pointer - successful query result, user consumed the result
 * Status::Not OK - error during query
 *
 * This function returns true and the next result to release to the user (or an error
 * if there can be no successful results to release) or false to indicate the user
 * should keep waiting.
 */
bool MultiHostQueryOp::releaseResult_inlock(StatusWith<DBClientCursor*>* nextResult) {
    int numErrors = 0;
    int numReleased = 0;
    for (ResultMap::iterator it = _results.begin(); it != _results.end(); ++it) {
        StatusWith<DBClientCursor*>& result = it->second;

        if (result.isOK() && result.getValue() != NULL) {
            *nextResult = result;
            it->second = StatusWith<DBClientCursor*>(NULL);
            return true;
        } else if (result.isOK()) {
            ++numReleased;
        } else {
            ++numErrors;
        }
    }

    if (numErrors + numReleased == static_cast<int>(_pending.size())) {
        *nextResult = StatusWith<DBClientCursor*>(combineErrorResults_inlock());
        return true;
    }

    return false;
}

/**
 * Goes through the set of results and combines all non-OK results into a single Status.
 * If a single error is found, just returns that error.
 * If no non-OK results are found, assumes the cause is a timeout.
 */
Status MultiHostQueryOp::combineErrorResults_inlock() {
    ErrorCodes::Error code = ErrorCodes::OK;
    StringBuilder errMsg;
    // Whether we should include human-readable codes in the msg - we don't need them if we're
    // not aggregating multiple statuses together
    bool includeHRCodes = false;

    for (ResultMap::const_iterator it = _results.begin(); it != _results.end(); ++it) {
        const StatusWith<DBClientCursor*>& result = it->second;

        if (!result.isOK()) {
            if (code == ErrorCodes::OK) {
                code = result.getStatus().code();
            } else {
                if (!includeHRCodes) {
                    includeHRCodes = true;
                    // Fixup the single error message to include a code
                    errMsg.reset();
                    errMsg.append(Status(code, errMsg.str()).toString());
                }

                code = ErrorCodes::MultipleErrorsOccurred;
                errMsg.append(" :: and :: ");
            }

            errMsg.append(includeHRCodes ? result.getStatus().toString()
                                         : result.getStatus().reason());
            errMsg.append(string(", host ") + it->first.toString());
        }
    }

    if (code == ErrorCodes::OK) {
        return Status(ErrorCodes::NetworkTimeout, "no results were returned in time");
    }

    return Status(code, errMsg.str());
}

MultiHostQueryOp::PendingQueryContext::PendingQueryContext(const ConnectionString& host,
                                                           const QuerySpec& query,
                                                           const Date_t timeoutAtMillis,
                                                           MultiHostQueryOp* parentOp)
    : host(host), query(query), timeoutAtMillis(timeoutAtMillis), parentOp(parentOp) {}

void MultiHostQueryOp::PendingQueryContext::doBlockingQuery() {
    // This *NEEDS* to be around for as long as we're doing queries - i.e. as long as the
    // HostThreadPools is.
    MultiHostQueryOp::SystemEnv* systemEnv;

    // Extract means of doing query from the parent op
    {
        boost_unique_lock lk(parentMutex);

        if (!parentOp)
            return;

        systemEnv = parentOp->_systemEnv;
    }

    // Make sure we're not timed out
    Date_t nowMillis = systemEnv->currentTimeMillis();
    if (nowMillis >= timeoutAtMillis)
        return;

    // Do query
    StatusWith<DBClientCursor*> result = systemEnv->doBlockingQuery(host, query);

    // Push results back to parent op if it's still around
    {
        boost_unique_lock lk(parentMutex);

        if (parentOp)
            parentOp->noteResult(host, result);
        else if (result.isOK())
            delete result.getValue();
    }
}

MultiHostQueryOp::~MultiHostQueryOp() {
    //
    // Orphan all outstanding query contexts that haven't reported back - these will be gc'd
    // once all scheduled query callbacks are finished.
    //

    for (PendingMap::iterator it = _pending.begin(); it != _pending.end(); ++it) {
        shared_ptr<PendingQueryContext>& pendingContext = it->second;

        boost_unique_lock lk(pendingContext->parentMutex);
        pendingContext->parentOp = NULL;
    }

    //
    // Nobody else should be modifying _results now - callbacks don't have access to this op,
    // and other clients should know the op is going out of scope
    //

    for (ResultMap::iterator it = _results.begin(); it != _results.end(); ++it) {
        StatusWith<DBClientCursor*>& result = it->second;

        if (result.isOK()) {
            delete result.getValue();
        }
    }
}
}