Skip to content

Commit

Permalink
fix error when key provided is not a valid split key
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Lerner committed Oct 23, 2010
1 parent 57507b0 commit 18938d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions jstests/sharding/key_string.js
Expand Up @@ -39,6 +39,11 @@ assert.eq( 6 , db.foo.find().sort( { name : 1 } ).count() , "total count with co
assert.eq( "allan,bob,eliot,joe,mark,sara" , db.foo.find().sort( { name : 1 } ).toArray().map( function(z){ return z.name; } ) , "sort 1" );
assert.eq( "sara,mark,joe,eliot,bob,allan" , db.foo.find().sort( { name : -1 } ).toArray().map( function(z){ return z.name; } ) , "sort 2" );

// make sure we can't foce a split on an extreme key
// [allan->joe)
assert.throws( function(){ s.adminCommand( { split : "test.foo" , middle : { name : "allan" } } ) } );
assert.throws( function(){ s.adminCommand( { split : "test.foo" , middle : { name : "joe" } } ) } );

s.stop();


18 changes: 12 additions & 6 deletions s/commands_admin.cpp
Expand Up @@ -479,19 +479,25 @@ namespace mongo {
}

ChunkManagerPtr info = config->getChunkManager( ns );
ChunkPtr old = info->findChunk( find );

ChunkPtr chunk = info->findChunk( find );
BSONObj middle = cmdObj.getObjectField( "middle" );

assert( old.get() );
log() << "splitting: " << ns << " shard: " << old << endl;
assert( chunk.get() );
log() << "splitting: " << ns << " shard: " << chunk << endl;

if ( middle.isEmpty() )
old->simpleSplit( true /* force a split even if not enough data */ );
chunk->simpleSplit( true /* force a split even if not enough data */ );

else {
// sanity check if the key provided is a valid split point
if ( ( middle == chunk->getMin() ) || ( middle == chunk->getMax() ) ) {
errmsg = "cannot split on initial or final chunk's key";
return false;
}

vector<BSONObj> splitPoints;
splitPoints.push_back( middle );
old->multiSplit( splitPoints );
chunk->multiSplit( splitPoints );
}

return true;
Expand Down

0 comments on commit 18938d6

Please sign in to comment.