forked from mongodb/mongo-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// assumption is we are primary | ||
|
||
assert( rs.isMaster().setName, "not a repl set" ); // assuming this is a repl set | ||
assert( rs.isMaster().ismaster, "we are not primary and we were assuming that"); | ||
|
||
// we want to compact, but in the background | ||
|
||
// so step down... | ||
try { | ||
rs.stepDown(); | ||
} catch(e) { | ||
print("exception:" + e); | ||
} | ||
|
||
// after stepdown connections are dropped. do an operation to cause reconnect: | ||
rs.isMaster(); | ||
|
||
// now ready to go. | ||
|
||
// wait for another node to become primary -- it may need data from us for the last | ||
// small sliver of time, and if we are already compacting it cannot get it while the | ||
// compaction is running. | ||
|
||
while( 1 ) { | ||
var m = rs.isMaster(); | ||
if( m.ismaster ) { | ||
print("ERROR: no one took over during our stepDown duration. we are primary again!"); | ||
assert(false); | ||
} | ||
if( m.primary ) | ||
break; // someone else is, great | ||
print("waiting"); | ||
sleep(1000); | ||
} | ||
|
||
// someone else is primary, so we are ready to proceed with a compaction | ||
print("Compacting..."); | ||
printjson( db.runCommand({compact:"mycollection",dev:true}) ); |