[SERVER-16264] Allow unsharding a collection when all chunks are on the primary shard Created: 21/Nov/14  Updated: 06/Dec/22  Resolved: 05/Nov/21

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Vincent Assignee: [DO NOT USE] Backlog - Sharding Team
Resolution: Done Votes: 14
Labels: lamont-triage, pm-1051-legacy-tickets
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-1037 Sharding - command to remove or "unsh... Closed
Related
related to SERVER-4000 command to change shard key of a coll... Closed
related to SERVER-9845 Command to unshard a collection Closed
Assigned Teams:
Sharding
Participants:

 Description   

Changing the shard key "on the fly" is pretty much impossible (see https://jira.mongodb.org/browse/SERVER-4000 ).
A possible workaround could be to move all the chunks to the same shard (database's primary), unsharding the collection and then shard it again.
But it's not currently possible to do so, we have to copy the source collection to another collection (and then drop the source/rename the target), but no changes must be happening to the source collection meanwhile or you would lose those changes.

While not ideal, this could work for some use cases.



 Comments   
Comment by Lamont Nelson [ 05/Nov/21 ]

Fixed with support for resharding.

Comment by Nikolai Zujev [ 05/Jun/15 ]

+1

Comment by Anne Moroney [ 21/Nov/14 ]

+1

Comment by Vincent [ 21/Nov/14 ]

Absolutely, but with a command to properly unshard, instead of the dirt metadata removal requiring "shutdown all shards and all but one mongos which you are going to use to edit the metadata".
What I mean is basically a more specific https://jira.mongodb.org/browse/SERVER-4000

Something like:

database = 'test';
collection = database + '.collection';
 
sh.stopBalancer();
use config;
primary = db.databases.findOne({_id: database}).primary;
 
// move all chunks to primary
db.chunks.find({ns: collection, shard: {$ne: primary}}).forEach(function(chunk){
    print('moving chunk from', chunk.shard, 'to', primary, '::', tojson(chunk.min), '-->', tojson(chunk.max));
    sh.moveChunk(collection, chunk.min, primary);
});
 
sh.unshardCollection(collection);
 
sh.startBalancer();

Comment by Roman Skvazh [ 21/Nov/14 ]

Did you mean https://jira.mongodb.org/browse/SERVER-9845 ?

Generated at Thu Feb 08 03:40:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.