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
|
// @file d_logic.h
/*
* Copyright (C) 2010 10gen 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/>.
*/
#pragma once
#include "../pch.h"
#include "../db/jsobj.h"
#include "d_chunk_manager.h"
#include "util.h"
namespace mongo {
class Database;
class DiskLoc;
typedef ShardChunkVersion ConfigVersion;
typedef map<string,ConfigVersion> NSVersionMap;
// --------------
// --- global state ---
// --------------
class ShardingState {
public:
ShardingState();
bool enabled() const { return _enabled; }
const string& getConfigServer() const { return _configServer; }
void enable( const string& server );
void gotShardName( const string& name );
void gotShardHost( string host );
/** Reverts back to a state where this mongod is not sharded. */
void resetShardingState();
// versioning support
bool hasVersion( const string& ns );
bool hasVersion( const string& ns , ConfigVersion& version );
const ConfigVersion getVersion( const string& ns ) const;
/**
* Uninstalls the manager for a given collection. This should be used when the collection is dropped.
*
* NOTE:
* An existing collection with no chunks on this shard will have a manager on version 0, which is different than a
* a dropped collection, which will not have a manager.
*
* TODO
* When sharding state is enabled, absolutely all collections should have a manager. (The non-sharded ones are
* a be degenerate case of one-chunk collections).
* For now, a dropped collection and an non-sharded one are indistinguishable (SERVER-1849)
*
* @param ns the collection to be dropped
*/
void resetVersion( const string& ns );
/**
* Requests to access a collection at a certain version. If the collection's manager is not at that version it
* will try to update itself to the newest version. The request is only granted if the version is the current or
* the newest one.
*
* @param ns collection to be accessed
* @param version (IN) the client belive this collection is on and (OUT) the version the manager is actually in
* @return true if the access can be allowed at the provided version
*/
bool trySetVersion( const string& ns , ConfigVersion& version );
void appendInfo( BSONObjBuilder& b );
// querying support
bool needShardChunkManager( const string& ns ) const;
ShardChunkManagerPtr getShardChunkManager( const string& ns );
// chunk migrate and split support
/**
* Creates and installs a new chunk manager for a given collection by "forgetting" about one of its chunks.
* The new manager uses the provided version, which has to be higher than the current manager's.
* One exception: if the forgotten chunk is the last one in this shard for the collection, version has to be 0.
*
* If it runs successfully, clients need to grab the new version to access the collection.
*
* @param ns the collection
* @param min max the chunk to eliminate from the current manager
* @param version at which the new manager should be at
*/
void donateChunk( const string& ns , const BSONObj& min , const BSONObj& max , ShardChunkVersion version );
/**
* Creates and installs a new chunk manager for a given collection by reclaiming a previously donated chunk.
* The previous manager's version has to be provided.
*
* If it runs successfully, clients that became stale by the previous donateChunk will be able to access the
* collection again.
*
* @param ns the collection
* @param min max the chunk to reclaim and add to the current manager
* @param version at which the new manager should be at
*/
void undoDonateChunk( const string& ns , const BSONObj& min , const BSONObj& max , ShardChunkVersion version );
/**
* Creates and installs a new chunk manager for a given collection by splitting one of its chunks in two or more.
* The version for the first split chunk should be provided. The subsequent chunks' version would be the latter with the
* minor portion incremented.
*
* The effect on clients will depend on the version used. If the major portion is the same as the current shards,
* clients shouldn't perceive the split.
*
* @param ns the collection
* @param min max the chunk that should be split
* @param splitKeys point in which to split
* @param version at which the new manager should be at
*/
void splitChunk( const string& ns , const BSONObj& min , const BSONObj& max , const vector<BSONObj>& splitKeys ,
ShardChunkVersion version );
bool inCriticalMigrateSection();
private:
bool _enabled;
string _configServer;
string _shardName;
string _shardHost;
// protects state below
mutable mongo::mutex _mutex;
// protects accessing the config server
// Using a ticket holder so we can have multiple redundant tries at any given time
mutable TicketHolder _configServerTickets;
// map from a namespace into the ensemble of chunk ranges that are stored in this mongod
// a ShardChunkManager carries all state we need for a collection at this shard, including its version information
typedef map<string,ShardChunkManagerPtr> ChunkManagersMap;
ChunkManagersMap _chunks;
};
extern ShardingState shardingState;
/**
* one per connection from mongos
* holds version state for each namesapce
*/
class ShardedConnectionInfo {
public:
ShardedConnectionInfo();
const OID& getID() const { return _id; }
bool hasID() const { return _id.isSet(); }
void setID( const OID& id );
const ConfigVersion getVersion( const string& ns ) const;
void setVersion( const string& ns , const ConfigVersion& version );
static ShardedConnectionInfo* get( bool create );
static void reset();
static void addHook();
bool inForceVersionOkMode() const {
return _forceVersionOk;
}
void enterForceVersionOkMode() { _forceVersionOk = true; }
void leaveForceVersionOkMode() { _forceVersionOk = false; }
private:
OID _id;
NSVersionMap _versions;
bool _forceVersionOk; // if this is true, then chunk version #s aren't check, and all ops are allowed
static boost::thread_specific_ptr<ShardedConnectionInfo> _tl;
};
struct ShardForceVersionOkModeBlock {
ShardForceVersionOkModeBlock() {
info = ShardedConnectionInfo::get( false );
if ( info )
info->enterForceVersionOkMode();
}
~ShardForceVersionOkModeBlock() {
if ( info )
info->leaveForceVersionOkMode();
}
ShardedConnectionInfo * info;
};
// -----------------
// --- core ---
// -----------------
unsigned long long extractVersion( BSONElement e , string& errmsg );
/**
* @return true if we have any shard info for the ns
*/
bool haveLocalShardingInfo( const string& ns );
/**
* @return true if the current threads shard version is ok, or not in sharded version
*/
bool shardVersionOk( const string& ns , string& errmsg );
/**
* @return true if we took care of the message and nothing else should be done
*/
struct DbResponse;
bool _handlePossibleShardedMessage( Message &m, DbResponse * dbresponse );
/** What does this do? document please? */
inline bool handlePossibleShardedMessage( Message &m, DbResponse * dbresponse ) {
if( !shardingState.enabled() )
return false;
return _handlePossibleShardedMessage(m, dbresponse);
}
void logOpForSharding( const char * opstr , const char * ns , const BSONObj& obj , BSONObj * patt );
void aboutToDeleteForSharding( const Database* db , const DiskLoc& dl );
}
|