Skip to content

Instantly share code, notes, and snippets.

@shussekaido
Created December 5, 2012 14:56
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save shussekaido/4216123 to your computer and use it in GitHub Desktop.
Save shussekaido/4216123 to your computer and use it in GitHub Desktop.
MongoDB M102 week6 file
db = db.getSisterDB("config");
var mongosConn = db; // assume we connected to a mongos to get going
var res = null;
function check() {
printjson(res);
if( !res || !res.ok ) {
throw "check(): not ok, stopping";
}
}
function waitForSetHealthy() {
print("waiting for repl set initiate to complete...");
while( 1 ) {
sleep(250);
var res = rs.status();
if( !res.ok )
continue;
var bad = false;
for (var i = 0; i < res.members.length; i++ ) {
var state = res.members[i].state;
if( state != 1 && state != 2 ) // primary or secondary?
bad = true;
}
if( !bad )
break;
}
}
function ourAddShard(setname,port) {
db = connect("localhost:"+port+"/test");
print(db.isMaster().me);
res = rs.initiate(
{
"_id" : setname,
"members" : [
{ _id: 0, host: "10gen.local:" + port },
{ _id: 1, host: "10gen.local:" + (port+1) },
{ _id: 2, host: "10gen.local:" + (port+2) }
]
}
);
check();
waitForSetHealthy();
print("adding shard...");
db = mongosConn;
res = sh.addShard(setname+"/10gen.local:"+port);
check();
print("done; run sh.status()");
}
print("setup.js loaded.");
print("list of existing shards before doing anything:");
printjson( db.shards.find().toArray() );
print()
print("You can invoke:");
print("ourAddShard(setname,port)");
print()
@wavesailor
Copy link

Created an updated version with an additional parameter namley "hostname". Here is the updated version:

db = db.getSisterDB("config");

var mongosConn = db; // assume we connected to a mongos to get going

var res = null;

function check() {
    printjson(res);
    if( !res || !res.ok ) {
        throw "check(): not ok, stopping";
    }
}

function waitForSetHealthy() {
    print("waiting for repl set initiate to complete...");
    while( 1 ) {
        sleep(250);
        var res = rs.status();
        if( !res.ok )
            continue;
        var bad = false;
        for (var i = 0; i &lt; res.members.length; i++ ) {
            var state = res.members[i].state;
            if( state != 1 &amp;&amp; state != 2 ) // primary or secondary?
                bad = true;
        }
        if( !bad )
            break;
    } 
}

function ourAddShard(setname,hostname,port) {
    var cfgset;
    cfgset =         {
            "_id" : setname,
            "members" : [
                { _id: 0, host: hostname + ":" + port },
                { _id: 1, host: hostname + ":" + (port+1) },
                { _id: 2, host: hostname + ":" + (port+2) }
            ]
        };
    // printjson(cfgset);
    db = connect("localhost:"+port+"/test");
    print(db.isMaster().me);
    res = rs.initiate(cfgset);
    check();
    waitForSetHealthy();

    print("adding shard...");
    db = mongosConn;
    res = sh.addShard(setname+"/"+hostname+":"+port);
    check();
    print("done; run sh.status()");
}

print("setup.js loaded.");
print("list of existing shards before doing anything:");
printjson( db.shards.find().toArray() );
print()
print("You can invoke:");
print("ourAddShard(setname,hostname,port)");
print()

@AndrewStarlike
Copy link

@wavesailor
you know what it would be really nice??? the code you posted to be written as CODE! all those "(var i = 0; i <" are creating enough errors so i can't use it :)

@AndrewStarlike
Copy link

db = db.getSisterDB("config");

var mongosConn = db; // assume we connected to a mongos to get going

var res = null;

function check() {
printjson(res);
if( !res || !res.ok ) {
throw "check(): not ok, stopping";
}
}

function waitForSetHealthy() {
print("waiting for repl set initiate to complete...");
while( 1 ) {
sleep(250);
var res = rs.status();
if( !res.ok )
continue;
var bad = false;
for (var i = 0; i < res.members.length; i++ ) {
var state = res.members[i].state;
if( state != 1 && state != 2 ) // primary or secondary?
bad = true;
}
if( !bad )
break;
}
}

function ourAddShard(setname,hostname,port) {
var cfgset;
cfgset = {
"_id" : setname,
"members" : [
{ _id: 0, host: hostname + ":" + port },
{ _id: 1, host: hostname + ":" + (port+1) },
{ _id: 2, host: hostname + ":" + (port+2) }
]
};
// printjson(cfgset);
db = connect("localhost:"+port+"/test");
print(db.isMaster().me);
res = rs.initiate(cfgset);
check();
waitForSetHealthy();

print("adding shard...");
db = mongosConn;
res = sh.addShard(setname+"/"+hostname+":"+port);
check();
print("done; run sh.status()");

}

print("setup.js loaded.");
print("list of existing shards before doing anything:");
printjson( db.shards.find().toArray() );
print()
print("You can invoke:");
print("ourAddShard(setname,hostname,port)");
print()

@AndrewStarlike
Copy link

db = db.getSisterDB("config");

var mongosConn = db; // assume we connected to a mongos to get going

var res = null;

function check() {
printjson(res);
if (!res || !res.ok) {
throw "check(): not ok, stopping";
}
}

function waitForSetHealthy() {
print("waiting for repl set initiate to complete...");
while (1) {
sleep(250);
var res = rs.status();
if (!res.ok)
continue;
var bad = false;
for (var i = 0; i < res.members.length; i++) {
var state = res.members[i].state;
if (state != 1 && state != 2) // primary or secondary?
bad = true;
}
if (!bad)
break;
}
}

function ourAddShard(setname, hostname, port) {
var cfgset;
cfgset = {
"_id": setname,
"members": [
{_id: 0, host: hostname + ":" + port},
{_id: 1, host: hostname + ":" + (port + 1)},
{_id: 2, host: hostname + ":" + (port + 2)}
]
};
// printjson(cfgset);
db = connect("localhost:" + port + "/test");
print(db.isMaster().me);
res = rs.initiate(cfgset);
check();
waitForSetHealthy();

print("adding shard...");
db = mongosConn;
res = sh.addShard(setname + "/" + hostname + ":" + port);
check();
print("done; run sh.status()");

}

print("setup.js loaded.");
print("list of existing shards before doing anything:");
printjson(db.shards.find().toArray());
print()
print("You can invoke:");
print("ourAddShard(setname,hostname,port)");
print()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment