Skip to content

Commit 7e477bd

Browse files
committed
SERVER-22359 Introduce per collection sharding state
1 parent 56b626f commit 7e477bd

File tree

5 files changed

+236
-65
lines changed

5 files changed

+236
-65
lines changed

src/mongo/db/s/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ env.Library(
3636
target='sharding',
3737
source=[
3838
'chunk_move_write_concern_options.cpp',
39+
'collection_sharding_state.cpp',
3940
'migration_destination_manager.cpp',
4041
'migration_impl.cpp',
4142
'migration_source_manager.cpp',
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright (C) 2015 MongoDB Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
* As a special exception, the copyright holders give permission to link the
17+
* code of portions of this program with the OpenSSL library under certain
18+
* conditions as described in each individual source file and distribute
19+
* linked combinations including the program with the OpenSSL library. You
20+
* must comply with the GNU Affero General Public License in all respects
21+
* for all of the code used other than as permitted herein. If you modify
22+
* file(s) with this exception, you may extend this exception to your
23+
* version of the file(s), but you are not obligated to do so. If you do not
24+
* wish to do so, delete this exception statement from your version. If you
25+
* delete this exception statement from all source files in the program,
26+
* then also delete it in the license file.
27+
*/
28+
29+
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
30+
31+
#include "mongo/platform/basic.h"
32+
33+
#include "mongo/db/s/collection_sharding_state.h"
34+
35+
#include "mongo/db/concurrency/lock_state.h"
36+
#include "mongo/db/operation_context.h"
37+
#include "mongo/db/s/collection_metadata.h"
38+
#include "mongo/db/s/sharding_state.h"
39+
#include "mongo/s/chunk_version.h"
40+
41+
namespace mongo {
42+
43+
CollectionShardingState::CollectionShardingState(
44+
NamespaceString nss, std::unique_ptr<CollectionMetadata> initialMetadata)
45+
: _nss(std::move(nss)), _metadata(std::move(initialMetadata)) {}
46+
47+
CollectionShardingState::~CollectionShardingState() = default;
48+
49+
CollectionShardingState* CollectionShardingState::get(OperationContext* txn,
50+
const std::string& ns) {
51+
// Collection lock must be held to have a reference to the collection's sharding state
52+
dassert(txn->lockState()->isCollectionLockedForMode(ns, MODE_IS));
53+
54+
ShardingState* const shardingState = ShardingState::get(txn);
55+
return shardingState->getNS(ns);
56+
}
57+
58+
void CollectionShardingState::setMetadata(std::shared_ptr<CollectionMetadata> newMetadata) {
59+
invariant(newMetadata);
60+
_metadata = std::move(newMetadata);
61+
}
62+
63+
} // namespace mongo
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (C) 2015 MongoDB Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
* As a special exception, the copyright holders give permission to link the
17+
* code of portions of this program with the OpenSSL library under certain
18+
* conditions as described in each individual source file and distribute
19+
* linked combinations including the program with the OpenSSL library. You
20+
* must comply with the GNU Affero General Public License in all respects
21+
* for all of the code used other than as permitted herein. If you modify
22+
* file(s) with this exception, you may extend this exception to your
23+
* version of the file(s), but you are not obligated to do so. If you do not
24+
* wish to do so, delete this exception statement from your version. If you
25+
* delete this exception statement from all source files in the program,
26+
* then also delete it in the license file.
27+
*/
28+
29+
#pragma once
30+
31+
#include <string>
32+
33+
#include "mongo/base/disallow_copying.h"
34+
#include "mongo/base/string_data.h"
35+
#include "mongo/db/namespace_string.h"
36+
37+
namespace mongo {
38+
39+
class BSONObj;
40+
class CollectionMetadata;
41+
class OperationContext;
42+
43+
/**
44+
* Contains all sharding-related runtime state for a given collection. One such object is assigned
45+
* to each sharded collection known on a mongod instance. A set of these objects is linked off the
46+
* instance's sharding state.
47+
*
48+
* Synchronization rules: In order to look-up this object in the instance's sharding map, one must
49+
* have some lock on the respective collection.
50+
*/
51+
class CollectionShardingState {
52+
MONGO_DISALLOW_COPYING(CollectionShardingState);
53+
54+
public:
55+
/**
56+
* Instantiates a new per-collection sharding state with some initial metadata.
57+
*/
58+
CollectionShardingState(NamespaceString nss,
59+
std::unique_ptr<CollectionMetadata> initialMetadata);
60+
~CollectionShardingState();
61+
62+
/**
63+
* Obtains the sharding state for the specified collection. If it does not exist, it will be
64+
* created and will remain active until the collection is dropped or unsharded.
65+
*
66+
* Must be called with some lock held on the specific collection being looked up and the
67+
* returned pointer should never be stored.
68+
*/
69+
static CollectionShardingState* get(OperationContext* txn, const std::string& ns);
70+
71+
/**
72+
* Returns the chunk metadata for the collection.
73+
*/
74+
std::shared_ptr<CollectionMetadata> getMetadata() const {
75+
return _metadata;
76+
}
77+
78+
/**
79+
* Set a new metadata to be used for this collection.
80+
*/
81+
void setMetadata(std::shared_ptr<CollectionMetadata> newMetadata);
82+
83+
private:
84+
// Namespace to which this state belongs.
85+
const NamespaceString _nss;
86+
87+
// Contains all the chunks associated with this collection. This value is always non-null.
88+
std::shared_ptr<CollectionMetadata> _metadata;
89+
};
90+
91+
} // namespace mongo

0 commit comments

Comments
 (0)