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
|
/**
* 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/db/service_context.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/db/client.h"
#include "mongo/db/operation_context.h"
#include "mongo/stdx/memory.h"
#include "mongo/transport/service_entry_point.h"
#include "mongo/transport/session.h"
#include "mongo/transport/transport_layer.h"
#include "mongo/transport/transport_layer_manager.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/system_clock_source.h"
#include "mongo/util/system_tick_source.h"
namespace mongo {
namespace {
ServiceContext* globalServiceContext = NULL;
} // namespace
bool hasGlobalServiceContext() {
return globalServiceContext;
}
ServiceContext* getGlobalServiceContext() {
fassert(17508, globalServiceContext);
return globalServiceContext;
}
void setGlobalServiceContext(std::unique_ptr<ServiceContext>&& serviceContext) {
fassert(17509, serviceContext.get());
delete globalServiceContext;
globalServiceContext = serviceContext.release();
}
bool _supportsDocLocking = false;
bool supportsDocLocking() {
return _supportsDocLocking;
}
bool isMMAPV1() {
StorageEngine* globalStorageEngine = getGlobalServiceContext()->getGlobalStorageEngine();
invariant(globalStorageEngine);
return globalStorageEngine->isMmapV1();
}
Status validateStorageOptions(
const BSONObj& storageEngineOptions,
stdx::function<Status(const StorageEngine::Factory* const, const BSONObj&)> validateFunc) {
BSONObjIterator storageIt(storageEngineOptions);
while (storageIt.more()) {
BSONElement storageElement = storageIt.next();
StringData storageEngineName = storageElement.fieldNameStringData();
if (storageElement.type() != mongo::Object) {
return Status(ErrorCodes::BadValue,
str::stream() << "'storageEngine." << storageElement.fieldNameStringData()
<< "' has to be an embedded document.");
}
std::unique_ptr<StorageFactoriesIterator> sfi(
getGlobalServiceContext()->makeStorageFactoriesIterator());
invariant(sfi);
bool found = false;
while (sfi->more()) {
const StorageEngine::Factory* const& factory = sfi->next();
if (storageEngineName != factory->getCanonicalName()) {
continue;
}
Status status = validateFunc(factory, storageElement.Obj());
if (!status.isOK()) {
return status;
}
found = true;
}
if (!found) {
return Status(ErrorCodes::InvalidOptions,
str::stream() << storageEngineName
<< " is not a registered storage engine for this server");
}
}
return Status::OK();
}
ServiceContext::ServiceContext()
: _transportLayerManager(stdx::make_unique<transport::TransportLayerManager>()),
_tickSource(stdx::make_unique<SystemTickSource>()),
_fastClockSource(stdx::make_unique<SystemClockSource>()),
_preciseClockSource(stdx::make_unique<SystemClockSource>()) {}
ServiceContext::~ServiceContext() {
stdx::lock_guard<stdx::mutex> lk(_mutex);
invariant(_clients.empty());
}
ServiceContext::UniqueClient ServiceContext::makeClient(std::string desc,
transport::SessionHandle session) {
std::unique_ptr<Client> client(new Client(std::move(desc), this, std::move(session)));
auto observer = _clientObservers.cbegin();
try {
for (; observer != _clientObservers.cend(); ++observer) {
observer->get()->onCreateClient(client.get());
}
} catch (...) {
try {
while (observer != _clientObservers.cbegin()) {
--observer;
observer->get()->onDestroyClient(client.get());
}
} catch (...) {
std::terminate();
}
throw;
}
{
stdx::lock_guard<stdx::mutex> lk(_mutex);
invariant(_clients.insert(client.get()).second);
}
return UniqueClient(client.release());
}
transport::TransportLayer* ServiceContext::getTransportLayer() const {
return _transportLayerManager.get();
}
ServiceEntryPoint* ServiceContext::getServiceEntryPoint() const {
return _serviceEntryPoint.get();
}
Status ServiceContext::addAndStartTransportLayer(std::unique_ptr<transport::TransportLayer> tl) {
return _transportLayerManager->addAndStartTransportLayer(std::move(tl));
}
TickSource* ServiceContext::getTickSource() const {
return _tickSource.get();
}
ClockSource* ServiceContext::getFastClockSource() const {
return _fastClockSource.get();
}
ClockSource* ServiceContext::getPreciseClockSource() const {
return _preciseClockSource.get();
}
void ServiceContext::setTickSource(std::unique_ptr<TickSource> newSource) {
_tickSource = std::move(newSource);
}
void ServiceContext::setFastClockSource(std::unique_ptr<ClockSource> newSource) {
_fastClockSource = std::move(newSource);
}
void ServiceContext::setPreciseClockSource(std::unique_ptr<ClockSource> newSource) {
_preciseClockSource = std::move(newSource);
}
void ServiceContext::setServiceEntryPoint(std::unique_ptr<ServiceEntryPoint> sep) {
_serviceEntryPoint = std::move(sep);
}
void ServiceContext::ClientDeleter::operator()(Client* client) const {
ServiceContext* const service = client->getServiceContext();
{
stdx::lock_guard<stdx::mutex> lk(service->_mutex);
invariant(service->_clients.erase(client));
}
try {
for (const auto& observer : service->_clientObservers) {
observer->onDestroyClient(client);
}
} catch (...) {
std::terminate();
}
delete client;
}
ServiceContext::UniqueOperationContext ServiceContext::makeOperationContext(Client* client) {
auto opCtx = _newOpCtx(client, _nextOpId.fetchAndAdd(1));
auto observer = _clientObservers.begin();
try {
for (; observer != _clientObservers.cend(); ++observer) {
observer->get()->onCreateOperationContext(opCtx.get());
}
} catch (...) {
try {
while (observer != _clientObservers.cbegin()) {
--observer;
observer->get()->onDestroyOperationContext(opCtx.get());
}
} catch (...) {
std::terminate();
}
throw;
}
{
stdx::lock_guard<Client> lk(*client);
client->setOperationContext(opCtx.get());
}
return UniqueOperationContext(opCtx.release());
};
void ServiceContext::OperationContextDeleter::operator()(OperationContext* opCtx) const {
auto client = opCtx->getClient();
auto service = client->getServiceContext();
{
stdx::lock_guard<Client> lk(*client);
client->resetOperationContext();
}
try {
for (const auto& observer : service->_clientObservers) {
observer->onDestroyOperationContext(opCtx);
}
} catch (...) {
std::terminate();
}
delete opCtx;
}
void ServiceContext::registerClientObserver(std::unique_ptr<ClientObserver> observer) {
_clientObservers.push_back(std::move(observer));
}
ServiceContext::LockedClientsCursor::LockedClientsCursor(ServiceContext* service)
: _lock(service->_mutex), _curr(service->_clients.cbegin()), _end(service->_clients.cend()) {}
Client* ServiceContext::LockedClientsCursor::next() {
if (_curr == _end)
return nullptr;
Client* result = *_curr;
++_curr;
return result;
}
BSONArray storageEngineList() {
if (!hasGlobalServiceContext())
return BSONArray();
std::unique_ptr<StorageFactoriesIterator> sfi(
getGlobalServiceContext()->makeStorageFactoriesIterator());
if (!sfi)
return BSONArray();
BSONArrayBuilder engineArrayBuilder;
while (sfi->more()) {
engineArrayBuilder.append(sfi->next()->getCanonicalName());
}
return engineArrayBuilder.arr();
}
void appendStorageEngineList(BSONObjBuilder* result) {
result->append("storageEngines", storageEngineList());
}
void ServiceContext::setKillAllOperations() {
stdx::lock_guard<stdx::mutex> clientLock(_mutex);
_globalKill.store(true);
for (const auto listener : _killOpListeners) {
try {
listener->interruptAll();
} catch (...) {
std::terminate();
}
}
}
void ServiceContext::killOperation(OperationContext* opCtx, ErrorCodes::Error killCode) {
opCtx->markKilled(killCode);
for (const auto listener : _killOpListeners) {
try {
listener->interrupt(opCtx->getOpID());
} catch (...) {
std::terminate();
}
}
}
void ServiceContext::killAllUserOperations(const OperationContext* txn,
ErrorCodes::Error killCode) {
for (LockedClientsCursor cursor(this); Client* client = cursor.next();) {
if (!client->isFromUserConnection()) {
// Don't kill system operations.
continue;
}
stdx::lock_guard<Client> lk(*client);
OperationContext* toKill = client->getOperationContext();
// Don't kill ourself.
if (toKill && toKill->getOpID() != txn->getOpID()) {
killOperation(toKill, killCode);
}
}
}
void ServiceContext::unsetKillAllOperations() {
_globalKill.store(false);
}
void ServiceContext::registerKillOpListener(KillOpListenerInterface* listener) {
stdx::lock_guard<stdx::mutex> clientLock(_mutex);
_killOpListeners.push_back(listener);
}
} // namespace mongo
|