[JAVA-1252] Should invalidate connection pool when ServerMonitor detects a disconnect Created: 21/May/14  Updated: 30/Jan/15  Resolved: 24/Jun/14

Status: Closed
Project: Java Driver
Component/s: Cluster Management, Connection Management
Affects Version/s: 2.12.2
Fix Version/s: 2.12.3, 3.0.0

Type: Improvement Priority: Minor - P4
Reporter: David CHAU Assignee: Jeffrey Yemin
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends

 Description   

When restarting mongodb, the first request after mongodb has restarted fails.

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import org.junit.Before;
import org.junit.Test;
 
public class TestMongoReconnection {
 
    public static final int RESTART_DELAY = 20000;
    private MongoClient mongoClient;
 
    @Before
    public void setUp() throws Exception {
 
        MongoClientOptions mongoClientOptions = MongoClientOptions.builder()
//                .heartbeatFrequency(10) //check every 10ms
                .build();
 
        mongoClient = new MongoClient("127.0.0.1", mongoClientOptions);
    }
 
    @Test
    public void first_request_after_mongo_restart_should_success() throws Exception {
 
        DB db = mongoClient.getDB("test");
        db.getCollectionNames();
 
        System.out.println(String.format("you have %sms to restart your mongodb server before the test resumes", RESTART_DELAY));
        Thread.sleep(RESTART_DELAY);
 
        db.getCollectionNames();
    }
}

The DefaultServer$DefaultServerStateListener class should call connectionProvider.invalidate(); if the state changed from connected to connecting



 Comments   
Comment by Githook User [ 30/Jan/15 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-1252: Invalidate connection pool if server monitor encounters a connection error, or server is invalidated
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/c0c90b3ce43c21bc24fef9837065dd37263817f1

Comment by Githook User [ 01/Aug/14 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-1252: Invalidate connection pool if server monitor encounters a connection error
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/d7d2c901db6043906bcc2c0ad9e5ef5ba5ad8761

Comment by Githook User [ 24/Jun/14 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-1252: Invalidate connection pool if server monitor encounters a connection error, or server is invalidated
Branch: 3.0.x
https://github.com/mongodb/mongo-java-driver/commit/c0c90b3ce43c21bc24fef9837065dd37263817f1

Comment by Jeffrey Yemin [ 20/Jun/14 ]

Pushed to 2.12.x, still need to port to 3.0.x.

Comment by Githook User [ 16/Jun/14 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-1252: Invalidate connection pool if server monitor encounters a connection error
Branch: 2.12.x
https://github.com/mongodb/mongo-java-driver/commit/d7d2c901db6043906bcc2c0ad9e5ef5ba5ad8761

Comment by David CHAU [ 21/May/14 ]

In jdbc drivers there is usually a "testOnBorrow" flag.
Maybe implementing this feature can be a improvement to "fail fast" and recreate a connection if needed

Comment by Jeffrey Yemin [ 21/May/14 ]

Thanks for sharing the use case. I definitely see where you are coming from.

Comment by David CHAU [ 21/May/14 ]

Not necessary. We had a maintenance in order to upgrade mongo server from 2.4. to 2.6. That took more than 5s. During this period, we managed to prevent any mongo operation on our systems.
after the migration, we would expect everything to work fine..
I understand that what I propose will not always fix the problem but it will increase the robustness of the applications using the driver

Comment by Jeffrey Yemin [ 21/May/14 ]

Understood. My point is that the default heartbeat frequency is 5 seconds, and a typical application will execute operations much more frequently than that.

Comment by David CHAU [ 21/May/14 ]

This is actually to prevent the first operation to fail

Comment by Jeffrey Yemin [ 21/May/14 ]

I'm marking this as a minor improvement because with a more realistic heartbeat frequency, it's more likely that the problem will be first detected via a failed operation than via the monitor.

Generated at Thu Feb 08 08:54:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.