In this chapter's homework we will create a replica set and add some data to it.
- Download the replication.js handout.
2. We will create a three member replica set. Pick a root working directory to work in. Go to that directory in a console window.
Given we will have three members in the set, and three mongod processes, create three data directories:
mkdir 1 mkdir 2 mkdir 3
3. We will now start a single mongod as a standalone server. Given that we will have three mongod processes on our single test server, we will explicitly specify the port numbers (this wouldn't be necessary if we had three real machines or three virtual machines). We'll also use the --smallfiles parameter and --oplogSize so the files are small given we have a lot of server processes running on our test PC.
# starting as a standalone server for problem 1: mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50
Note: for all mongod startups in the homework this chapter, you can optionally use --logPath, --logappend, and --fork. Or, since this is just an exercise on a local PC, you could simply have a separate terminal window for all and forgo those settings. Run "mongod --help" for more info on those.
4. In a separate terminal window (cmd.exe on Windows), run the mongo shell with the replication.js file:
mongo --port 27001 --shell replication.js
Then run in the shell:
homework.init()
This will load a small amount of test data into the database.
Now run:
homework.a()
and enter the result. This will simply confirm all the above happened ok.
Answer:
5001
// replication.js
homework = { }
var d = db.getSisterDB("replication");
homework.init = function() {
var rsConf; try { rsConf = rs.conf(); } catch (e) { rsConf = null; }
if( rs.Conf != null ) {
print("at this stage of the homework the mongod should NOT be using --replSet");
return;
}
if( d.foo.count() != 0 ) {
print("expected replication.foo collection to be empty to init. can't init. :-(");
return;
}
for( var i = 0; i < 5000; i++ ) d.foo.insert({x:i,y:Math.random()});
var result = db.getLastError();
if( result ) print("Something is wrong? : " + result);
else print("ok");
}
homework.a = function() {
return d.foo.stats().count + d.foo.stats().nindexes;
}
homework.b = function() {
if( !db.isMaster().ismaster ) {
print("something is wrong. try again. initiate?");
return;
}
if( rs.status().members.length != 1 ) {
print("only supposed to be one member of the set at this point. try again?");
return;
}
return rs.status().members.length + d.foo.count() + 1;
}
homework.c = function() {
var s = rs.status();
if( s.members.length != 3 ) {
print("something is wrong i don't see 3 members");
return;
}
var x = 0;
s.members.forEach( function(m){x+=m.state} );
if( x > 7 ) {
print("something isn't right yet. did you wait for the new members to finish synchronizing?");
}
return x;
}
homework.d = function() {
var s = rs.status();
if( s.members.length != 2 ) {
print("something is wrong i don't see 2 members");
return;
}
var x = 0;
s.members.forEach( function(m){x+=m._id+m.state} );
if( x > 9 ) {
print("something isn't right yet. did you wait for a new election to finish? want to see a primary and a secondary");
}
return x;
}
No comments:
Post a Comment