Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

SERVER-2054 don't uassert due to unique index constraint violation in…

… dropdups mode
  • Loading branch information...
commit 23c7fefdfe2a798027e47bb684424feeb130ad91 1 parent a30a6a0
@astaple astaple authored
Showing with 81 additions and 6 deletions.
  1. +22 −6 db/pdfile.cpp
  2. +46 −0 jstests/unique2.js
  3. +13 −0 jstests/uniqueness.js
View
28 db/pdfile.cpp
@@ -1193,7 +1193,13 @@ namespace mongo {
BSONObjExternalSorter::Data d = i->next();
try {
- btBuilder.addKey(d.first, d.second);
+ if ( !dupsAllowed && dropDups ) {
+ LastError::Disabled led( lastError.get() );
+ btBuilder.addKey(d.first, d.second);
+ }
+ else {
+ btBuilder.addKey(d.first, d.second);
+ }
}
catch( AssertionException& e ) {
if ( dupsAllowed ) {
@@ -1201,8 +1207,9 @@ namespace mongo {
throw;
}
- if( e.interrupted() )
- throw;
+ if( e.interrupted() ) {
+ killCurrentOp.checkForInterrupt();
+ }
if ( ! dropDups )
throw;
@@ -1314,12 +1321,21 @@ namespace mongo {
while ( cc->ok() ) {
BSONObj js = cc->current();
try {
- _indexRecord(d, idxNo, js, cc->currLoc(), dupsAllowed);
+ {
+ if ( !dupsAllowed && dropDups ) {
+ LastError::Disabled led( lastError.get() );
+ _indexRecord(d, idxNo, js, cc->currLoc(), dupsAllowed);
+ }
+ else {
+ _indexRecord(d, idxNo, js, cc->currLoc(), dupsAllowed);
+ }
+ }
cc->advance();
}
catch( AssertionException& e ) {
- if( e.interrupted() )
- throw;
+ if( e.interrupted() ) {
+ killCurrentOp.checkForInterrupt();
+ }
if ( dropDups ) {
DiskLoc toDelete = cc->currLoc();
View
46 jstests/unique2.js
@@ -1,3 +1,4 @@
+// Test unique and dropDups index options.
t = db.jstests_unique2;
@@ -21,7 +22,9 @@ t.ensureIndex({k:1}, {unique:true});
t.insert({k:3});
t.insert({k:[2,3]});
+assert( db.getLastError() );
t.insert({k:[4,3]});
+assert( db.getLastError() );
assert( t.count() == 1 ) ;
assert( t.find().sort({k:1}).toArray().length == 1 ) ;
@@ -33,9 +36,52 @@ t.insert({k:[2,3]});
t.insert({k:[4,3]});
assert( t.count() == 3 ) ;
+// Trigger an error, so we can test n of getPrevError() later.
+assert.throws( function() { t.find( {$where:'aaa'} ).itcount(); } );
+assert( db.getLastError() );
+assert.eq( 1, db.getPrevError().nPrev );
+
t.ensureIndex({k:1}, {unique:true, dropDups:true});
+// Check error flag was not set SERVER-2054.
+assert( !db.getLastError() );
+// Check that offset of pervious error is correct.
+assert.eq( 2, db.getPrevError().nPrev );
+// Check the dups were dropped.
assert( t.count() == 1 ) ;
assert( t.find().sort({k:1}).toArray().length == 1 ) ;
assert( t.find().sort({k:1}).count() == 1 ) ;
+// Check that a new conflicting insert will cause an error.
+t.insert({k:[2,3]});
+assert( db.getLastError() );
+
+t.drop();
+
+t.insert({k:3});
+t.insert({k:[2,3]});
+t.insert({k:[4,3]});
+assert( t.count() == 3 ) ;
+
+
+// Now try with a background index op.
+
+// Trigger an error, so we can test n of getPrevError() later.
+assert.throws( function() { t.find( {$where:'aaa'} ).itcount(); } );
+assert( db.getLastError() );
+assert.eq( 1, db.getPrevError().nPrev );
+
+t.ensureIndex({k:1}, {background:true, unique:true, dropDups:true});
+// Check error flag was not set SERVER-2054.
+assert( !db.getLastError() );
+// Check that offset of pervious error is correct.
+assert.eq( 2, db.getPrevError().nPrev );
+
+// Check the dups were dropped.
+assert( t.count() == 1 ) ;
+assert( t.find().sort({k:1}).toArray().length == 1 ) ;
+assert( t.find().sort({k:1}).count() == 1 ) ;
+
+// Check that a new conflicting insert will cause an error.
+t.insert({k:[2,3]});
+assert( db.getLastError() );
View
13 jstests/uniqueness.js
@@ -26,8 +26,21 @@ db.jstests_uniqueness2.drop();
db.jstests_uniqueness2.insert({a:3});
db.jstests_uniqueness2.insert({a:3});
assert( db.jstests_uniqueness2.count() == 2 , 6) ;
+db.resetError();
db.jstests_uniqueness2.ensureIndex({a:1}, true);
assert( db.getLastError() , 7);
+assert( db.getLastError().match( /E11000/ ) );
+
+// Check for an error message when we index in the background and there are dups
+db.jstests_uniqueness2.drop();
+db.jstests_uniqueness2.insert({a:3});
+db.jstests_uniqueness2.insert({a:3});
+assert( db.jstests_uniqueness2.count() == 2 , 6) ;
+assert( !db.getLastError() );
+db.resetError();
+db.jstests_uniqueness2.ensureIndex({a:1}, {unique:true,background:true});
+assert( db.getLastError() , 7);
+assert( db.getLastError().match( /E11000/ ) );
/* Check that if we update and remove _id, it gets added back by the DB */
Please sign in to comment.
Something went wrong with that request. Please try again.