From 18938d62ec3e04470de9d555056534e92ef4069d Mon Sep 17 00:00:00 2001 From: Alberto Lerner Date: Sat, 23 Oct 2010 10:40:10 -0400 Subject: [PATCH] fix error when key provided is not a valid split key --- jstests/sharding/key_string.js | 5 +++++ s/commands_admin.cpp | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/jstests/sharding/key_string.js b/jstests/sharding/key_string.js index 48948b7ddbd..bbc5dfb49ec 100644 --- a/jstests/sharding/key_string.js +++ b/jstests/sharding/key_string.js @@ -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(); diff --git a/s/commands_admin.cpp b/s/commands_admin.cpp index a10f2bebd9d..6a1da3b83aa 100644 --- a/s/commands_admin.cpp +++ b/s/commands_admin.cpp @@ -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 splitPoints; splitPoints.push_back( middle ); - old->multiSplit( splitPoints ); + chunk->multiSplit( splitPoints ); } return true;