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
|
/**
* Copyright (C) 2008-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.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
#include "mongo/platform/basic.h"
#include "mongo/s/config.h"
#include <vector>
#include "mongo/db/lasterror.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/query/collation/collator_factory_interface.h"
#include "mongo/s/catalog/sharding_catalog_client.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/catalog/type_collection.h"
#include "mongo/s/catalog/type_database.h"
#include "mongo/s/catalog_cache.h"
#include "mongo/s/chunk_manager.h"
#include "mongo/s/chunk_version.h"
#include "mongo/s/grid.h"
#include "mongo/stdx/memory.h"
#include "mongo/util/log.h"
namespace mongo {
struct CollectionInfo {
// The config server opTime at which the chunk manager below was loaded
const repl::OpTime configOpTime;
// The chunk manager
const std::shared_ptr<ChunkManager> cm;
};
DBConfig::DBConfig(const DatabaseType& dbt, repl::OpTime configOpTime)
: _name(dbt.getName()),
_shardingEnabled(dbt.getSharded()),
_primaryId(dbt.getPrimary()),
_configOpTime(std::move(configOpTime)) {}
DBConfig::~DBConfig() = default;
bool DBConfig::isSharded(const std::string& ns) {
stdx::lock_guard<stdx::mutex> lk(_lock);
return _collections.count(ns) > 0;
}
void DBConfig::markNSNotSharded(const std::string& ns) {
stdx::lock_guard<stdx::mutex> lk(_lock);
CollectionInfoMap::iterator it = _collections.find(ns);
if (it != _collections.end()) {
_collections.erase(it);
}
}
std::shared_ptr<ChunkManager> DBConfig::getChunkManagerIfExists(OperationContext* txn,
const std::string& ns,
bool shouldReload,
bool forceReload) {
// Don't report exceptions here as errors in GetLastError
LastError::Disabled ignoreForGLE(&LastError::get(cc()));
try {
return getChunkManager(txn, ns, shouldReload, forceReload);
} catch (const DBException&) {
return nullptr;
}
}
std::shared_ptr<ChunkManager> DBConfig::getChunkManager(OperationContext* txn,
const std::string& ns,
bool shouldReload,
bool forceReload) {
ChunkVersion oldVersion;
std::shared_ptr<ChunkManager> oldManager;
{
stdx::lock_guard<stdx::mutex> lk(_lock);
auto it = _collections.find(ns);
const bool earlyReload = (it == _collections.end()) && (shouldReload || forceReload);
if (earlyReload) {
// This is to catch cases where there this is a new sharded collection.
// Note: read the _reloadCount inside the _lock mutex, so _loadIfNeeded will always
// be forced to perform a reload.
const auto currentReloadIteration = _reloadCount.load();
_loadIfNeeded(txn, currentReloadIteration);
it = _collections.find(ns);
}
uassert(ErrorCodes::NamespaceNotSharded,
str::stream() << "Collection is not sharded: " << ns,
it != _collections.end());
const auto& ci = it->second;
if (!(shouldReload || forceReload) || earlyReload) {
return ci.cm;
}
if (ci.cm) {
oldManager = ci.cm;
oldVersion = ci.cm->getVersion();
}
}
// TODO: We need to keep this first one-chunk check in until we have a more efficient way of
// creating/reusing a chunk manager, as doing so requires copying the full set of chunks
// currently
std::vector<ChunkType> newestChunk;
if (oldVersion.isSet() && !forceReload) {
uassertStatusOK(Grid::get(txn)->catalogClient(txn)->getChunks(
txn,
BSON(ChunkType::ns(ns)),
BSON(ChunkType::DEPRECATED_lastmod() << -1),
1,
&newestChunk,
nullptr,
repl::ReadConcernLevel::kMajorityReadConcern));
if (!newestChunk.empty()) {
invariant(newestChunk.size() == 1);
ChunkVersion v = newestChunk[0].getVersion();
if (v.equals(oldVersion)) {
stdx::lock_guard<stdx::mutex> lk(_lock);
auto it = _collections.find(ns);
uassert(15885,
str::stream() << "not sharded after reloading from chunks : " << ns,
it != _collections.end());
const auto& ci = it->second;
return ci.cm;
}
}
} else if (!oldVersion.isSet()) {
warning() << "version 0 found when " << (forceReload ? "reloading" : "checking")
<< " chunk manager; collection '" << ns << "' initially detected as sharded";
}
std::unique_ptr<ChunkManager> tempChunkManager;
{
stdx::lock_guard<stdx::mutex> lll(_hitConfigServerLock);
if (!newestChunk.empty() && !forceReload) {
// If we have a target we're going for see if we've hit already
stdx::lock_guard<stdx::mutex> lk(_lock);
auto it = _collections.find(ns);
if (it != _collections.end()) {
const auto& ci = it->second;
ChunkVersion currentVersion = newestChunk[0].getVersion();
// Only reload if the version we found is newer than our own in the same epoch
if (currentVersion <= ci.cm->getVersion() &&
ci.cm->getVersion().hasEqualEpoch(currentVersion)) {
return ci.cm;
}
}
}
// Reload the chunk manager outside of the DBConfig's mutex so as to not block operations
// for different collections on the same database
tempChunkManager.reset(new ChunkManager(
NamespaceString(oldManager->getns()),
oldManager->getVersion().epoch(),
oldManager->getShardKeyPattern(),
oldManager->getDefaultCollator() ? oldManager->getDefaultCollator()->clone() : nullptr,
oldManager->isUnique()));
tempChunkManager->loadExistingRanges(txn, oldManager.get());
if (!tempChunkManager->numChunks()) {
// Maybe we're not sharded any more, so do a full reload
const auto currentReloadIteration = _reloadCount.load();
const bool successful = [&]() {
stdx::lock_guard<stdx::mutex> lk(_lock);
return _loadIfNeeded(txn, currentReloadIteration);
}();
// If we aren't successful loading the database entry, we don't want to keep the stale
// object around which has invalid data.
if (!successful) {
Grid::get(txn)->catalogCache()->invalidate(_name);
}
return getChunkManager(txn, ns);
}
}
stdx::lock_guard<stdx::mutex> lk(_lock);
auto it = _collections.find(ns);
uassert(14822,
str::stream() << "Collection " << ns << " became unsharded in the middle.",
it != _collections.end());
const auto& ci = it->second;
// Reset if our versions aren't the same
bool shouldReset = !tempChunkManager->getVersion().equals(ci.cm->getVersion());
// Also reset if we're forced to do so
if (!shouldReset && forceReload) {
shouldReset = true;
warning() << "chunk manager reload forced for collection '" << ns << "', config version is "
<< tempChunkManager->getVersion();
}
//
// LEGACY BEHAVIOR
//
// It's possible to get into a state when dropping collections when our new version is
// less than our prev version. Behave identically to legacy mongos, for now, and warn to
// draw attention to the problem.
//
// TODO: Assert in next version, to allow smooth upgrades
//
if (shouldReset && tempChunkManager->getVersion() < ci.cm->getVersion()) {
shouldReset = false;
warning() << "not resetting chunk manager for collection '" << ns << "', config version is "
<< tempChunkManager->getVersion() << " and "
<< "old version is " << ci.cm->getVersion();
}
// end legacy behavior
if (shouldReset) {
const auto cmOpTime = tempChunkManager->getConfigOpTime();
// The existing ChunkManager could have been updated since we last checked, so replace the
// existing chunk manager only if it is strictly newer.
if (cmOpTime > ci.cm->getConfigOpTime()) {
_collections.erase(ns);
auto emplacedEntryIt =
_collections.emplace(ns, CollectionInfo{cmOpTime, std::move(tempChunkManager)})
.first;
return emplacedEntryIt->second.cm;
}
}
return ci.cm;
}
bool DBConfig::load(OperationContext* txn) {
const auto currentReloadIteration = _reloadCount.load();
stdx::lock_guard<stdx::mutex> lk(_lock);
return _loadIfNeeded(txn, currentReloadIteration);
}
bool DBConfig::_loadIfNeeded(OperationContext* txn, Counter reloadIteration) {
if (reloadIteration != _reloadCount.load()) {
return true;
}
const auto catalogClient = Grid::get(txn)->catalogClient(txn);
auto status = catalogClient->getDatabase(txn, _name);
if (status == ErrorCodes::NamespaceNotFound) {
return false;
}
// All other errors are connectivity, etc so throw an exception.
uassertStatusOK(status.getStatus());
const auto& dbOpTimePair = status.getValue();
const auto& dbt = dbOpTimePair.value;
invariant(_name == dbt.getName());
_primaryId = dbt.getPrimary();
invariant(dbOpTimePair.opTime >= _configOpTime);
_configOpTime = dbOpTimePair.opTime;
// Load all collections
std::vector<CollectionType> collections;
repl::OpTime configOpTimeWhenLoadingColl;
uassertStatusOK(
catalogClient->getCollections(txn, &_name, &collections, &configOpTimeWhenLoadingColl));
invariant(configOpTimeWhenLoadingColl >= _configOpTime);
for (const auto& coll : collections) {
auto collIter = _collections.find(coll.getNs().ns());
if (collIter != _collections.end()) {
invariant(configOpTimeWhenLoadingColl >= collIter->second.configOpTime);
}
_collections.erase(coll.getNs().ns());
if (!coll.getDropped()) {
std::unique_ptr<CollatorInterface> defaultCollator;
if (!coll.getDefaultCollation().isEmpty()) {
auto statusWithCollator = CollatorFactoryInterface::get(txn->getServiceContext())
->makeFromBSON(coll.getDefaultCollation());
// The collation was validated upon collection creation.
invariantOK(statusWithCollator.getStatus());
defaultCollator = std::move(statusWithCollator.getValue());
}
std::unique_ptr<ChunkManager> manager(
stdx::make_unique<ChunkManager>(coll.getNs(),
coll.getEpoch(),
ShardKeyPattern(coll.getKeyPattern()),
std::move(defaultCollator),
coll.getUnique()));
// Do the blocking collection load
manager->loadExistingRanges(txn, nullptr);
// Collections with no chunks are unsharded, no matter what the collections entry says
if (manager->numChunks()) {
_collections.emplace(
coll.getNs().ns(),
CollectionInfo{configOpTimeWhenLoadingColl, std::move(manager)});
}
}
}
_reloadCount.fetchAndAdd(1);
return true;
}
ShardId DBConfig::getPrimaryId() {
stdx::lock_guard<stdx::mutex> lk(_lock);
return _primaryId;
}
} // namespace mongo
|