@@ -76,7 +76,7 @@ namespace mongo {
7676 Shard Shard::EMPTY;
7777
7878
79- DBConfig:: CollectionInfo::CollectionInfo ( const BSONObj& in ) {
79+ CollectionInfo::CollectionInfo (const BSONObj& in) {
8080 _dirty = false ;
8181 _dropped = in[CollectionType::dropped ()].trueValue ();
8282
@@ -87,18 +87,18 @@ namespace mongo {
8787 _dirty = false ;
8888 }
8989
90- DBConfig:: CollectionInfo::~CollectionInfo () {
90+ CollectionInfo::~CollectionInfo () {
9191
9292 }
9393
94- void DBConfig:: CollectionInfo::resetCM (ChunkManager* cm) {
94+ void CollectionInfo::resetCM (ChunkManager* cm) {
9595 invariant (cm);
9696 invariant (_cm);
9797
9898 _cm.reset (cm);
9999 }
100100
101- void DBConfig:: CollectionInfo::shard (ChunkManager* manager){
101+ void CollectionInfo::shard (ChunkManager* manager) {
102102 // Do this *first* so we're invisible to everyone else
103103 manager->loadExistingRanges (configServer.getPrimary ().getConnString (), NULL );
104104
@@ -107,12 +107,8 @@ namespace mongo {
107107 // This helps prevent errors when dropping in a different process
108108 //
109109
110- if (manager->numChunks () != 0 ){
111- _cm = ChunkManagerPtr (manager);
112- _key = manager->getShardKeyPattern ().toBSON ().getOwned ();
113- _unqiue = manager->isUnique ();
114- _dirty = true ;
115- _dropped = false ;
110+ if (manager->numChunks () != 0 ) {
111+ useChunkManager (ChunkManagerPtr (manager));
116112 }
117113 else {
118114 warning () << " no chunks found for collection " << manager->getns ()
@@ -121,14 +117,22 @@ namespace mongo {
121117 }
122118 }
123119
124- void DBConfig:: CollectionInfo::unshard () {
120+ void CollectionInfo::unshard () {
125121 _cm.reset ();
126122 _dropped = true ;
127123 _dirty = true ;
128124 _key = BSONObj ();
129125 }
130126
131- void DBConfig::CollectionInfo::save ( const string& ns ) {
127+ void CollectionInfo::useChunkManager (ChunkManagerPtr manager) {
128+ _cm = manager;
129+ _key = manager->getShardKeyPattern ().toBSON ().getOwned ();
130+ _unique = manager->isUnique ();
131+ _dirty = true ;
132+ _dropped = false ;
133+ }
134+
135+ void CollectionInfo::save (const string& ns) {
132136 BSONObj key = BSON ( " _id" << ns );
133137
134138 BSONObjBuilder val;
@@ -218,97 +222,6 @@ namespace mongo {
218222 if ( save ) _save ();
219223 }
220224
221- boost::shared_ptr<ChunkManager> DBConfig::shardCollection (
222- const string& ns,
223- const ShardKeyPattern& fieldsAndOrder,
224- bool unique,
225- vector<BSONObj>* initPoints,
226- vector<Shard>* initShards) {
227-
228- uassert (8042 , " db doesn't have sharding enabled" , _shardingEnabled);
229-
230- ChunkManagerPtr manager;
231-
232- {
233- boost::lock_guard<boost::mutex> lk ( _lock );
234-
235- CollectionInfo& ci = _collections[ns];
236- uassert ( 8043 , " collection already sharded" , ! ci.isSharded () );
237-
238- log () << " enable sharding on: " << ns << " with shard key: " << fieldsAndOrder << endl;
239-
240- // Record start in changelog
241- BSONObjBuilder collectionDetail;
242- collectionDetail.append (" shardKey" , fieldsAndOrder.toBSON ());
243- collectionDetail.append (" collection" , ns);
244- collectionDetail.append (" primary" , getPrimary ().toString ());
245-
246- BSONArray initialShards;
247- if (initShards == NULL )
248- initialShards = BSONArray ();
249- else {
250- BSONArrayBuilder b;
251- for (unsigned i = 0 ; i < initShards->size (); i++) {
252- b.append ((*initShards)[i].getName ());
253- }
254- initialShards = b.arr ();
255- }
256-
257- collectionDetail.append (" initShards" , initialShards);
258- collectionDetail.append (" numChunks" , (int )(initPoints->size () + 1 ));
259-
260- grid.catalogManager ()->logChange (NULL ,
261- " shardCollection.start" ,
262- ns,
263- collectionDetail.obj ());
264-
265- ChunkManager* cm = new ChunkManager ( ns, fieldsAndOrder, unique );
266- cm->createFirstChunks (configServer.getPrimary ().getConnString (),
267- getPrimary (),
268- initPoints,
269- initShards);
270- ci.shard (cm);
271-
272- _save ();
273-
274- // Save the initial chunk manager for later, no need to reload if we're in this lock
275- manager = ci.getCM ();
276- verify ( manager.get () );
277- }
278-
279- // Tell the primary mongod to refresh it's data
280- // TODO: Think the real fix here is for mongos to just assume all collections sharded, when we get there
281- for ( int i = 0 ; i < 4 ; i++ ){
282- if ( i == 3 ){
283- warning () << " too many tries updating initial version of " << ns << " on shard primary " << getPrimary () <<
284- " , other mongoses may not see the collection as sharded immediately" << endl;
285- break ;
286- }
287- try {
288- ShardConnection conn ( getPrimary (), ns );
289- if (!conn.setVersion ()) {
290- warning () << " could not update initial version of "
291- << ns << " on shard primary " << getPrimary ();
292- }
293- conn.done ();
294- break ;
295- }
296- catch ( DBException& e ){
297- warning () << " could not update initial version of " << ns << " on shard primary " << getPrimary () <<
298- causedBy ( e ) << endl;
299- }
300- sleepsecs ( i );
301- }
302-
303- // Record finish in changelog
304- BSONObjBuilder finishDetail;
305- finishDetail.append (" version" , manager->getVersion ().toString ());
306-
307- grid.catalogManager ()->logChange (NULL , " shardCollection" , ns, finishDetail.obj ());
308-
309- return manager;
310- }
311-
312225 bool DBConfig::removeSharding ( const string& ns ) {
313226 if ( ! _shardingEnabled ) {
314227
0 commit comments