Monday, November 4, 2013

MongoDB replica set hidden member and application configuration


Replica set usage

It seems the driver made 3 connection when we specify the three mongod servers in connection URL.
            MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),new ServerAddress("localhost", 37017), new ServerAddress("localhost", 47017)));

Tue Nov 05 12:04:53.690 [conn56] end connection 127.0.0.1:64511 (3 connections now open)
Tue Nov 05 12:04:53.693 [conn57] end connection 127.0.0.1:64516 (2 connections now open)
Tue Nov 05 12:04:53.909 [conn52] end connection 127.0.0.1:64486 (1 connection now open)
Tue Nov 05 12:04:53.911 [initandlisten] connection accepted from 127.0.0.1:64520 #58 (2 connections now open)


            MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),new ServerAddress("localhost", 37017)));

Tue Nov 05 12:05:23.918 [conn58] end connection 127.0.0.1:64520 (1 connection now open)
Tue Nov 05 12:05:23.920 [initandlisten] connection accepted from 127.0.0.1:64555 #60 (2 connections now open)

Tue Nov 05 12:05:27.360 [conn59] end connection 127.0.0.1:64521 (1 connection now open)
Tue Nov 05 12:05:27.361 [initandlisten] connection accepted from 127.0.0.1:64556 #61 (2 connections now open)


When the PRIMARY is down can clearly see it first try with previous PRIMARY but then move to the new PRIMARY. After sometime it will make sure it will route to the correct server on next attempt.

Nov 05, 2013 12:17:08 PM com.mongodb.ConnectionStatus$UpdatableNode update
WARNING: Server seen down: localhost/127.0.0.1:37017 - java.io.IOException - message: Connection refused: connect
Nov 05, 2013 12:17:08 PM com.mongodb.ConnectionStatus$UpdatableNode update
WARNING: Server seen down: localhost/127.0.0.1:37017 - java.io.IOException - message: Connection refused: connect
Nov 05, 2013 12:17:09 PM com.mongodb.ConnectionStatus$UpdatableNode update
WARNING: Server seen down: MA02ENLKASUN/127.0.0.1:37017 - java.io.IOException - message: Connection refused: connect

But on next attempt it is getting without connection exceptions.


Hidden member
You can make hidden member, which will never be a primary rather synch with data and can be used for backup or reporting. But whenever new primary need to elected it will be join in. Priority is 0 and must enable the hidden.

{
"_id" : <num>
  "host" : <hostname:port>,
  "priority" : 0,
  "hidden" : true
}

cfg = rs.conf()
cfg.members[0].priority = 0
cfg.members[0].hidden = true
rs.reconfig(cfg)
After the hidden member add in. Can’t be view from db.isMaster() 

rs0:PRIMARY> db.isMaster()
{
        "setName" : "rs0",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "MA02ENLKASUN:37017",
                "MA02ENLKASUN:57017",
                "MA02ENLKASUN:27017"
        ],
        "arbiters" : [
                "MA02ENLKASUN:47017"
        ],
        "primary" : "MA02ENLKASUN:37017",
        "me" : "MA02ENLKASUN:37017",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-11-05T07:25:25.248Z"),
        "ok" : 1
}
rs0:PRIMARY> cfg = rs.conf();
rs0:PRIMARY> cfg.members[3].priority=0;
0
rs0:PRIMARY> cfg.members[3].hidden=true;

rs0:PRIMARY> rs.reconfig(cfg);
Tue Nov 05 15:26:37.998 DBClientCursor::init call() failed
Tue Nov 05 15:26:38.005 trying reconnect to 127.0.0.1:37017
Tue Nov 05 15:26:38.009 reconnect 127.0.0.1:37017 ok
reconnected to server after rs command (which is normal)

rs0:SECONDARY> db.isMaster()
{
        "setName" : "rs0",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "MA02ENLKASUN:37017",
                "MA02ENLKASUN:27017"
        ],
        "arbiters" : [
                "MA02ENLKASUN:47017"
        ],
        "primary" : "MA02ENLKASUN:37017",
        "me" : "MA02ENLKASUN:37017",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2013-11-05T07:26:45.595Z"),
        "ok" : 1
}

No comments:

Post a Comment