• create replica set
  • connect ot replica set
  • failover
  • read concern
  • write concern
  • Homework: Homework 6.4: sharding

create replica set

//init_replica.js
config = { _id: "m101", members:[
          { _id : 0, host : "jomac.local:27017"},
          { _id : 1, host : "jomac.local:27018"},
          { _id : 2, host : "jomac.local:27019"} ]
};
 
rs.initiate(config);
rs.status();
 
//wait.js
import pymongo
 
read_pref = pymongo.read_preferences.ReadPreference.SECONDARY
 
c = pymongo.MongoClient(host="mongodb://localhost:37017",
                        replicaSet="s0",
                        w=4, j=True, 
                        read_preference=read_pref)
 
db = c.m101
people = db.people
 
print "inserting"
people.insert({"name":"Andrew Erlichson", "favorite_color":"blue"})
print "inserting"
people.insert({"name":"Richard Krueter", "favorite_color":"red"})
print "inserting"
people.insert({"name":"Dwight Merriman", "favorite_color":"green"})

connect ot replica set

//app.js
var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect("mongodb://localhost:30001,localhost:30002,localhost:30003/course", function(err, db) {
    if (err) throw err;
 
    db.collection("repl").insert({ 'x' : 1 }, function(err, doc) {
        if (err) throw err;
 
        db.collection("repl").findOne({ 'x' : 1 }, function(err, doc) {
            if (err) throw err;
 
            console.log(doc);
            db.close();
        });
    });
});

failover

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect("mongodb://localhost:30001,localhost:30002,localhost:30003/course", function(err, db) {
    if (err) throw err;
 
    var documentNumber = 0;
    function insertDocument() {
 
        db.collection("repl").insert({ 'documentNumber' : documentNumber++ }, function(err, doc) {
            if (err) throw err;
            console.log(doc);
        });
 
        console.log("Dispatched insert");
        setTimeout(insertDocument, 1000);
    }
 
    insertDocument();
});

read concern

var MongoClient = require('mongodb').MongoClient;
    ReadPreference = require('mongodb').ReadPreference;
 
MongoClient.connect("mongodb://localhost:30001,localhost:30002,localhost:30003/course?readPreference=secondary", function(err, db) {
    if (err) throw err;
 
    db.collection("repl").insert({ 'x' : 1 }, function(err, doc) {
        if (err) throw err;
        console.log(doc);
    });
 
    function findDocument() {
 
        //db.collection("repl").findOne({ 'x' : 1 }, { 'readPreference' : ReadPreference.PRIMARY }, function(err, doc) {
        db.collection("repl").findOne({ 'x' : 1 }, function(err, doc) {
            if (err) throw err;
            console.log(doc);
        });
 
        console.log("Dispatched find");
        setTimeout(findDocument, 1000);
    }
 
    findDocument();
});

write concern

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect("mongodb://localhost:27017,localhost:27018,localhost:27019/course?w=1", function(err, db) {
    if (err) throw err;
 
    // Write concern of one
    db.collection("repl").insert({ 'x' : 1 }, function(err, doc) {
        if (err) throw err;
        console.log(doc);
 
        // Write concern of two
        db.collection("repl").insert({ 'x' : 2 }, { 'w' : 2 }, function(err, doc) {
            if (err) throw err;
            console.log(doc);
            db.close();
        });
 
        // Write concern of four - callback won't be called indefinitely with a write concern greater
          // than the number of nodes in the replica set.
        db.collection("repl").insert({ 'x' : 2 }, { 'w' : 4 }, function(err, doc) {
            if (err) throw err;
            console.log(doc);
            db.close();
        });
    });
});

Homework: Homework 6.4: sharding

#### You have a sharded system with three shards and have sharded the collections "grades" in the "test" database across those shards. The output of sh.status() when connected to mongos looks like this:

mongos> sh.status()
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
    {  "_id" : "s0",  "host" : "s0/localhost:37017,localhost:37018,localhost:37019" }
    {  "_id" : "s1",  "host" : "s1/localhost:47017,localhost:47018,localhost:47019" }
    {  "_id" : "s2",  "host" : "s2/localhost:57017,localhost:57018,localhost:57019" }
  databases:
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    {  "_id" : "test",  "partitioned" : true,  "primary" : "s0" }
        test.grades chunks:
                s1  4
                s0  4
                s2  4
            { "student_id" : { $minKey : 1 } } -->> { "student_id" : 0 } on : s1 Timestamp(12000, 0) 
            { "student_id" : 0 } -->> { "student_id" : 2640 } on : s0 Timestamp(11000, 1) 
            { "student_id" : 2640 } -->> { "student_id" : 91918 } on : s1 Timestamp(10000, 1) 
            { "student_id" : 91918 } -->> { "student_id" : 176201 } on : s0 Timestamp(4000, 2) 
            { "student_id" : 176201 } -->> { "student_id" : 256639 } on : s2 Timestamp(12000, 1) 
            { "student_id" : 256639 } -->> { "student_id" : 344351 } on : s2 Timestamp(6000, 2) 
            { "student_id" : 344351 } -->> { "student_id" : 424983 } on : s0 Timestamp(7000, 2) 
            { "student_id" : 424983 } -->> { "student_id" : 509266 } on : s1 Timestamp(8000, 2) 
            { "student_id" : 509266 } -->> { "student_id" : 596849 } on : s1 Timestamp(9000, 2) 
            { "student_id" : 596849 } -->> { "student_id" : 772260 } on : s0 Timestamp(10000, 2) 
            { "student_id" : 772260 } -->> { "student_id" : 945802 } on : s2 Timestamp(11000, 2) 
            { "student_id" : 945802 } -->> { "student_id" : { $maxKey : 1 } } on : s2 Timestamp(11000, 3)

If you ran the query use test db.grades.find({'student_id':530289}) Which shards would be involved in answering the query? s1

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.