MongoDB replication not working

588 views
Skip to first unread message

Astro

unread,
Jul 17, 2016, 11:54:19 AM7/17/16
to mongodb-user


I am exploring the replication functionality in MongoDB, just wanted to clarify a simple scenario.

I have a java program which is inserting values in the db constantly in a three instance replica set, now if i shutdown the primary the java program just throws an ioexception and exits. Shouldn't it continue inserting in after the election when a new primary is selected whereas i can see using mongo shell that a new primary is selected and everything is fine.

Following is the code snippet:


MongoClient client = new MongoClient(Arrays.asList( new ServerAddress("myhost", 27017), 
                                  new ServerAddress("myhost", 27018), 
                                  new ServerAddress("myhost", 27019)));

MongoDatabase database = client.getDatabase("test"); 

MongoCollection<Document> collection = database.getCollection("recharge");

collection.drop(); for(int i=0;i<100000;i++){ Document doc = new Document("reference",""+i).append("owner", "a"+i); collection.insertOne(doc); 

Thread.sleep(5000); 

} }

at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ... 3 more May 06, 2016 10:13:27 AM com.mongodb.diagnostics.logging.JULLogger log INFO: Closed connection [connectionId{localValue:6}] to akhurraml2:27017 because there was a socket exception raised by this connection. Exception in thread "main" com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) at com.mongodb.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:46) at com.mongodb.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:381) at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:96) at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:82) at com.mongodb.connection.DefaultServer.getConnection(DefaultServer.java:72) at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:86) at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:228) at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221) at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168) at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74) at com.mongodb.Mongo.execute(Mongo.java:781) at com.mongodb.Mongo$2.execute(Mongo.java:764) at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:306) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:297) at com.nextcontrols.App.main(App.java:33) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ... 17 more



Astro

unread,
Jul 17, 2016, 12:01:37 PM7/17/16
to mongodb-user
Update: 
mongodb version: 3.2.3
    mongodb java driver: 3.2.2 

Astro

unread,
Jul 18, 2016, 10:45:44 PM7/18/16
to mongodb-user
Is there anything I am missing in the code to make it auto-reconnect when fail over happes?

Any help on this?

William Hagan

unread,
Jul 19, 2016, 7:07:17 AM7/19/16
to mongodb-user
Hello Astro,

Can you confirm that the mongod server(s) and its ports are reachable from the application server?

Also ensure that "myhost" resolves to the server host name and not say "localhost" for example.

I tested your code using ReplSetTest to create a 3-node replica set and all is working fine. Of course, there is the period of the election where I get the ConnectException but it quickly resolves itself.

PS: mongod version is: 3.2.7

Cheers

Astro

unread,
Jul 19, 2016, 10:44:04 AM7/19/16
to mongodb-user
The replica set is on the same machine.

All members are reachable. The app throws an exception app stops working when the primary goes down.

Thanks.

William Hagan

unread,
Jul 19, 2016, 2:17:41 PM7/19/16
to mongod...@googlegroups.com
Hi Astro,

I couldn't replicate my successful test anymore. I am now experiencing a similar issue.

It will appear there is an issue with the Java driver.

Hopefully someone will help with this anomaly while I'll continue to see if I can identify a specific reason.

Cheers

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/OSzgMAInFfo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8494d8fe-a9b7-4e10-9fb9-f0a15633208a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Never be afraid to try something new. Remember that a lone amateur built the Ark. A large group of professionals built the Titanic.

Jeff Yemin

unread,
Jul 19, 2016, 2:58:41 PM7/19/16
to mongodb-user
Hi Astro,

The driver will automatically reconnect, but you still have to catch the exception thrown from the insert method.  Otherwise, your application will exit.

Have a look at this excellent blog post about retrying write operations with MongoDB.  While the examples are in Python, the lessons can reliably be applied to your Java application.


Regards,
Jeff

William Hagan

unread,
Jul 20, 2016, 10:17:02 AM7/20/16
to mongod...@googlegroups.com
Thanks Jeff.

The explanation really does make things clear. My tests work as expected.

Cheers

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/OSzgMAInFfo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages