Skip to content

Commit

Permalink
SERVER-13643 Use TransactionExperiment in dropDatabase()
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeard0531 committed May 1, 2014
1 parent 4ac7bff commit e3a0652
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/mongo/db/dbcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ namespace mongo {
log() << "dropDatabase " << dbname << " starting" << endl;

stopIndexBuilds(context.db(), cmdObj);
dropDatabase(context.db());
dropDatabase(&txn, context.db());

log() << "dropDatabase " << dbname << " finished";

Expand Down
8 changes: 5 additions & 3 deletions src/mongo/db/pdfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ _ disallow system* manipulations from the database.
#include "mongo/util/processinfo.h"
#include "mongo/db/stats/timer_stats.h"
#include "mongo/db/stats/counters.h"
#include "mongo/db/storage/mmap_v1/dur_transaction.h"

namespace mongo {

Expand Down Expand Up @@ -141,6 +142,7 @@ namespace mongo {

void dropAllDatabasesExceptLocal() {
Lock::GlobalWrite lk;
DurTransaction txn;

vector<string> n;
getDatabaseNames(n);
Expand All @@ -149,12 +151,12 @@ namespace mongo {
for( vector<string>::iterator i = n.begin(); i != n.end(); i++ ) {
if( *i != "local" ) {
Client::Context ctx(*i);
dropDatabase(ctx.db());
dropDatabase(&txn, ctx.db());
}
}
}

void dropDatabase(Database* db ) {
void dropDatabase(TransactionExperiment* txn, Database* db ) {
invariant( db );

string name = db->name(); // just to have safe
Expand All @@ -173,7 +175,7 @@ namespace mongo {
//
// RWLockRecursive::Exclusive lk(MongoFile::mmmutex);

getDur().syncDataAndTruncateJournal();
txn->syncDataAndTruncateJournal();

Database::closeDatabase( name, db->path() );
db = 0; // d is now deleted
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/db/pdfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace mongo {
class Database;
class TransactionExperiment;

void dropDatabase(Database* db);
void dropDatabase(TransactionExperiment* txn, Database* db);

Status userCreateNS( TransactionExperiment* txn,
Database* db,
Expand Down
34 changes: 19 additions & 15 deletions src/mongo/db/repl/master_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,28 +303,28 @@ namespace mongo {
uassert(17066, "Internal error reading from local.sources", Runner::RUNNER_EOF == state);
}

bool ReplSource::throttledForceResyncDead( const char *requester ) {
bool ReplSource::throttledForceResyncDead( TransactionExperiment* txn, const char *requester ) {
if ( time( 0 ) - lastForcedResync > 600 ) {
forceResyncDead( requester );
forceResyncDead( txn, requester );
lastForcedResync = time( 0 );
return true;
}
return false;
}

void ReplSource::forceResyncDead( const char *requester ) {
void ReplSource::forceResyncDead( TransactionExperiment* txn, const char *requester ) {
if ( !replAllDead )
return;
SourceVector sources;
ReplSource::loadAll(sources);
for( SourceVector::iterator i = sources.begin(); i != sources.end(); ++i ) {
log() << requester << " forcing resync from " << (*i)->hostName << endl;
(*i)->forceResync( requester );
(*i)->forceResync( txn, requester );
}
replAllDead = 0;
}

void ReplSource::forceResync( const char *requester ) {
void ReplSource::forceResync( TransactionExperiment* txn, const char *requester ) {
BSONObj info;
{
dbtemprelease t;
Expand All @@ -344,7 +344,7 @@ namespace mongo {
if ( !e.embeddedObject().getBoolField( "empty" ) ) {
if ( name != "local" ) {
if ( only.empty() || only == name ) {
resyncDrop( name );
resyncDrop( txn, name );
}
}
}
Expand All @@ -354,16 +354,16 @@ namespace mongo {
save();
}

void ReplSource::resyncDrop( const string& db ) {
void ReplSource::resyncDrop( TransactionExperiment* txn, const string& db ) {
log() << "resync: dropping database " << db;
Client::Context ctx(db);
dropDatabase(ctx.db());
dropDatabase(txn, ctx.db());
}

/* grab initial copy of a database from the master */
void ReplSource::resync(TransactionExperiment* txn, const std::string& dbName) {
const std::string db(dbName); // need local copy of the name, we're dropping the original
resyncDrop( db );
resyncDrop( txn, db );
Client::Context ctx( db );
{
log() << "resync: cloning database " << db << " to get an initial copy" << endl;
Expand All @@ -382,7 +382,7 @@ namespace mongo {

if ( !ok ) {
if ( errCode == DatabaseDifferCaseCode ) {
resyncDrop( db );
resyncDrop( txn, db );
log() << "resync: database " << db << " not valid on the master due to a name conflict, dropping." << endl;
return;
}
Expand Down Expand Up @@ -419,7 +419,10 @@ namespace mongo {
}
}

bool ReplSource::handleDuplicateDbName( const BSONObj &op, const char *ns, const char *db ) {
bool ReplSource::handleDuplicateDbName( TransactionExperiment* txn,
const BSONObj &op,
const char* ns,
const char* db ) {
if (dbHolder()._isLoaded(ns, storageGlobalParams.dbpath)) {
// Database is already present.
return true;
Expand Down Expand Up @@ -492,7 +495,7 @@ namespace mongo {
incompleteCloneDbs.erase(*i);
addDbNextPass.erase(*i);
Client::Context ctx(*i);
dropDatabase(ctx.db());
dropDatabase(txn, ctx.db());
}

massert( 14034, "Duplicate database names present after attempting to delete duplicates",
Expand Down Expand Up @@ -594,19 +597,19 @@ namespace mongo {
}

scoped_ptr<Lock::GlobalWrite> lk( alreadyLocked ? 0 : new Lock::GlobalWrite() );
DurTransaction txn; // XXX?

if ( replAllDead ) {
// hmmm why is this check here and not at top of this function? does it get set between top and here?
log() << "replAllDead, throwing SyncException: " << replAllDead << endl;
throw SyncException();
}

if ( !handleDuplicateDbName( op, ns, clientName ) ) {
if ( !handleDuplicateDbName( &txn, op, ns, clientName ) ) {
return;
}

Client::Context ctx( ns );
DurTransaction txn;
ctx.getClient()->curop()->reset();

bool empty = ctx.db()->isEmpty();
Expand Down Expand Up @@ -1086,9 +1089,10 @@ namespace mongo {
int s = 0;
{
Lock::GlobalWrite lk;
DurTransaction txn;
if ( replAllDead ) {
// throttledForceResyncDead can throw
if ( !replSettings.autoresync || !ReplSource::throttledForceResyncDead( "auto" ) ) {
if ( !replSettings.autoresync || !ReplSource::throttledForceResyncDead( &txn, "auto" ) ) {
log() << "all sources dead: " << replAllDead << ", sleeping for 5 seconds" << endl;
break;
}
Expand Down
13 changes: 8 additions & 5 deletions src/mongo/db/repl/master_slave.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace mongo {

ReplSource();

void resyncDrop( const string& db );
void resyncDrop( TransactionExperiment* txn, const string& db );
// call without the db mutex
void syncToTailOfRemoteLog();
string ns() const { return string( "local.oplog.$" ) + sourceName(); }
Expand All @@ -112,7 +112,10 @@ namespace mongo {
* master.
* @return true iff an op with the specified ns may be applied.
*/
bool handleDuplicateDbName( const BSONObj &op, const char *ns, const char *db );
bool handleDuplicateDbName( TransactionExperiment* txn,
const BSONObj &op,
const char* ns,
const char* db );

// populates _me so that it can be passed to oplogreader for handshakes
void ensureMe();
Expand Down Expand Up @@ -157,9 +160,9 @@ namespace mongo {
return wait > 0 ? wait : 0;
}

static bool throttledForceResyncDead( const char *requester );
static void forceResyncDead( const char *requester );
void forceResync( const char *requester );
static bool throttledForceResyncDead( TransactionExperiment* txn, const char *requester );
static void forceResyncDead( TransactionExperiment* txn, const char *requester );
void forceResync( TransactionExperiment* txn, const char *requester );
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/mongo/db/repl/resync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "mongo/db/repl/master_slave.h" // replSettings
#include "mongo/db/repl/repl_settings.h" // replSettings
#include "mongo/db/repl/rs.h" // replLocalAuth()
#include "mongo/db/storage/mmap_v1/dur_transaction.h"

namespace mongo {

Expand Down Expand Up @@ -66,6 +67,7 @@ namespace mongo {
const std::string ns = parseNs(dbname, cmdObj);
Lock::GlobalWrite globalWriteLock;
Client::Context ctx(ns);
DurTransaction txn;

if (replSettings.usingReplSets()) {
if (theReplSet->isPrimary()) {
Expand All @@ -88,7 +90,7 @@ namespace mongo {
if ( !waitForSyncToFinish( errmsg ) )
return false;

ReplSource::forceResyncDead( "client" );
ReplSource::forceResyncDead( &txn, "client" );
result.append( "info", "triggered resync for all sources" );
return true;
}
Expand Down

0 comments on commit e3a0652

Please sign in to comment.