mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
unique shard key support SHARDING-31
This commit is contained in:
parent
66072780c0
commit
08489a2b42
@ -62,5 +62,21 @@ printjson( db.system.indexes.find( { ns : "test.foo3" } ).toArray() );
|
||||
assert( ! s.admin.runCommand( { shardcollection : "test.foo3" , key : { num : 1 } } ).ok , "shard with unique index" );
|
||||
|
||||
|
||||
// ---- unique shard key ----
|
||||
|
||||
assert( s.admin.runCommand( { shardcollection : "test.foo4" , key : { num : 1 } , unique : true } ).ok , "shard with index and unique" );
|
||||
s.adminCommand( { split : "test.foo4" , middle : { num : 10 } } );
|
||||
s.adminCommand( { movechunk : "test.foo4" , find : { num : 20 } , to : s.getOther( s.getServer( "test" ) ).name } );
|
||||
db.foo4.save( { num : 5 } );
|
||||
db.foo4.save( { num : 15 } );
|
||||
s.sync();
|
||||
assert.eq( 1 , a.foo4.count() , "ua1" );
|
||||
assert.eq( 1 , b.foo4.count() , "ub1" );
|
||||
|
||||
assert.eq( 2 , a.foo4.getIndexes().length , "ua2" );
|
||||
assert.eq( 2 , b.foo4.getIndexes().length , "ub2" );
|
||||
|
||||
assert( a.foo4.getIndexes()[1].unique , "ua3" );
|
||||
assert( b.foo4.getIndexes()[1].unique , "ub3" );
|
||||
|
||||
s.stop()
|
||||
|
@ -19,7 +19,7 @@ assert.eq( 3 , db.foo.find().length() , "after partitioning count failed" );
|
||||
|
||||
s.adminCommand( shardCommand );
|
||||
dbconfig = s.config.databases.findOne( { name : "test" } );
|
||||
assert.eq( dbconfig.sharded["test.foo"] , { num : 1 } , "Sharded content" );
|
||||
assert.eq( dbconfig.sharded["test.foo"] , { key : { num : 1 } , unique : false } , "Sharded content" );
|
||||
|
||||
assert.eq( 1 , s.config.chunks.count() );
|
||||
si = s.config.chunks.findOne();
|
||||
|
@ -348,7 +348,7 @@ namespace mongo {
|
||||
|
||||
void Chunk::ensureIndex(){
|
||||
ScopedDbConnection conn( getShard() );
|
||||
conn->ensureIndex( _ns , _manager->getShardKey().key() );
|
||||
conn->ensureIndex( _ns , _manager->getShardKey().key() , _manager->_unique );
|
||||
conn.done();
|
||||
}
|
||||
|
||||
@ -367,7 +367,8 @@ namespace mongo {
|
||||
|
||||
unsigned long long ChunkManager::NextSequenceNumber = 1;
|
||||
|
||||
ChunkManager::ChunkManager( DBConfig * config , string ns , ShardKeyPattern pattern ) : _config( config ) , _ns( ns ) , _key( pattern ){
|
||||
ChunkManager::ChunkManager( DBConfig * config , string ns , ShardKeyPattern pattern , bool unique ) :
|
||||
_config( config ) , _ns( ns ) , _key( pattern ) , _unique( unique ){
|
||||
Chunk temp(0);
|
||||
|
||||
ScopedDbConnection conn( temp.modelServer() );
|
||||
|
@ -154,7 +154,7 @@ namespace mongo {
|
||||
class ChunkManager {
|
||||
public:
|
||||
|
||||
ChunkManager( DBConfig * config , string ns ,ShardKeyPattern pattern );
|
||||
ChunkManager( DBConfig * config , string ns , ShardKeyPattern pattern , bool unique );
|
||||
virtual ~ChunkManager();
|
||||
|
||||
string getns(){
|
||||
@ -169,6 +169,7 @@ namespace mongo {
|
||||
Chunk* findChunkOnServer( const string& server ) const;
|
||||
|
||||
ShardKeyPattern& getShardKey(){ return _key; }
|
||||
bool isUnique(){ return _unique; }
|
||||
|
||||
/**
|
||||
* makes sure the shard index is on all servers
|
||||
@ -199,6 +200,7 @@ namespace mongo {
|
||||
DBConfig * _config;
|
||||
string _ns;
|
||||
ShardKeyPattern _key;
|
||||
bool _unique;
|
||||
|
||||
vector<Chunk*> _chunks;
|
||||
|
||||
|
@ -231,7 +231,7 @@ namespace mongo {
|
||||
ShardCollectionCmd() : GridAdminCmd( "shardcollection" ){}
|
||||
virtual void help( stringstream& help ) const {
|
||||
help
|
||||
<< "Shard a collection. Sharding must already be enabled for the database.\n"
|
||||
<< "Shard a collection. Requires key. Optional unique. Sharding must already be enabled for the database.\n"
|
||||
<< " { enablesharding : \"<dbname>\" }\n";
|
||||
}
|
||||
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
|
||||
@ -274,7 +274,7 @@ namespace mongo {
|
||||
}
|
||||
}
|
||||
|
||||
config->shardCollection( ns , key );
|
||||
config->shardCollection( ns , key , cmdObj["unique"].trueValue() );
|
||||
config->save( true );
|
||||
|
||||
result << "collectionsharded" << ns;
|
||||
|
24
s/config.cpp
24
s/config.cpp
@ -53,7 +53,7 @@ namespace mongo {
|
||||
_shardingEnabled = true;
|
||||
}
|
||||
|
||||
ChunkManager* DBConfig::shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder ){
|
||||
ChunkManager* DBConfig::shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder , bool unique ){
|
||||
if ( ! _shardingEnabled )
|
||||
throw UserException( "db doesn't have sharding enabled" );
|
||||
|
||||
@ -64,9 +64,9 @@ namespace mongo {
|
||||
if ( isSharded( ns ) )
|
||||
throw UserException( "already sharded" );
|
||||
|
||||
_sharded[ns] = fieldsAndOrder;
|
||||
_sharded[ns] = CollectionInfo( fieldsAndOrder , unique );
|
||||
|
||||
info = new ChunkManager( this , ns , fieldsAndOrder );
|
||||
info = new ChunkManager( this , ns , fieldsAndOrder , unique );
|
||||
_shards[ns] = info;
|
||||
return info;
|
||||
|
||||
@ -80,7 +80,7 @@ namespace mongo {
|
||||
uassert( (string)"not sharded:" + ns , isSharded( ns ) );
|
||||
if ( m && reload )
|
||||
log() << "reloading shard info for: " << ns << endl;
|
||||
m = new ChunkManager( this , ns , _sharded[ ns ] );
|
||||
m = new ChunkManager( this , ns , _sharded[ ns ].key , _sharded[ns].unique );
|
||||
_shards[ns] = m;
|
||||
return m;
|
||||
}
|
||||
@ -92,8 +92,11 @@ namespace mongo {
|
||||
|
||||
if ( _sharded.size() > 0 ){
|
||||
BSONObjBuilder a;
|
||||
for ( map<string,ShardKeyPattern>::reverse_iterator i=_sharded.rbegin(); i != _sharded.rend(); i++){
|
||||
a.append( i->first.c_str() , i->second.key() );
|
||||
for ( map<string,CollectionInfo>::reverse_iterator i=_sharded.rbegin(); i != _sharded.rend(); i++){
|
||||
BSONObjBuilder temp;
|
||||
temp.append( "key" , i->second.key.key() );
|
||||
temp.appendBool( "unique" , i->second.unique );
|
||||
a.append( i->first.c_str() , temp.obj() );
|
||||
}
|
||||
to.append( "sharded" , a.obj() );
|
||||
}
|
||||
@ -111,7 +114,10 @@ namespace mongo {
|
||||
while ( i.more() ){
|
||||
BSONElement e = i.next();
|
||||
uassert( "sharded things have to be objects" , e.type() == Object );
|
||||
_sharded[e.fieldName()] = e.embeddedObject();
|
||||
BSONObj c = e.embeddedObject();
|
||||
uassert( "key has to be an object" , c["key"].type() == Object );
|
||||
_sharded[e.fieldName()] = CollectionInfo( c["key"].embeddedObject() ,
|
||||
c["unique"].trueValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -365,8 +371,8 @@ namespace mongo {
|
||||
b << "primary" << "myserver";
|
||||
|
||||
BSONObjBuilder a;
|
||||
a << "abc.foo" << BSON( "a" << 1 );
|
||||
a << "abc.bar" << BSON( "b" << -1 );
|
||||
a << "abc.foo" << fromjson( "{ 'key' : { 'a' : 1 } , 'unique' : false }" );
|
||||
a << "abc.bar" << fromjson( "{ 'key' : { 'kb' : -1 } , 'unique' : true }" );
|
||||
|
||||
b.appendArray( "sharded" , a.obj() );
|
||||
|
||||
|
13
s/config.h
13
s/config.h
@ -37,6 +37,15 @@ namespace mongo {
|
||||
extern Grid grid;
|
||||
|
||||
class ChunkManager;
|
||||
|
||||
class CollectionInfo {
|
||||
public:
|
||||
CollectionInfo( ShardKeyPattern _key = BSONObj() , bool _unique = false ) :
|
||||
key( _key ) , unique( _unique ){}
|
||||
|
||||
ShardKeyPattern key;
|
||||
bool unique;
|
||||
};
|
||||
|
||||
/**
|
||||
top level grid configuration for an entire database
|
||||
@ -55,7 +64,7 @@ namespace mongo {
|
||||
}
|
||||
|
||||
void enableSharding();
|
||||
ChunkManager* shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder );
|
||||
ChunkManager* shardCollection( const string& ns , ShardKeyPattern fieldsAndOrder , bool unique );
|
||||
|
||||
/**
|
||||
* @return whether or not this partition is partitioned
|
||||
@ -96,7 +105,7 @@ namespace mongo {
|
||||
string _primary; // e.g. localhost , mongo.foo.com:9999
|
||||
bool _shardingEnabled;
|
||||
|
||||
map<string,ShardKeyPattern> _sharded; // { "alleyinsider.blog.posts" : { ts : 1 } , ... ] - all ns that are sharded
|
||||
map<string,CollectionInfo> _sharded; // { "alleyinsider.blog.posts" : { ts : 1 } , ... ] - all ns that are sharded
|
||||
map<string,ChunkManager*> _shards; // this will only have entries for things that have been looked at
|
||||
|
||||
friend class Grid;
|
||||
|
Loading…
Reference in New Issue
Block a user