[JAVA-1786] Error running command when rs is not fully setup Created: 30/Apr/15  Updated: 05/May/15  Resolved: 05/May/15

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Dharshan Rangegowda Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Server 3.0.2
Java driver 3.0.0



 Description   

Here is the situation

The servers are setup for a replica set with a keyfile and replica name configured in the conf file

I am trying to run a command on 127.0.0.1 to initialize the replica set. It fails with the following error

29 Apr 2015 22:20:58,981 ERROR ~ MongDBInstance-configureReplicaSet: Timed out after 10000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=secondaryPreferred}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=REPLICA_SET_GHOST, roundTrip Time=1.5 ms, state=CONNECTED}]

If I remove the replica set parameter from the conf file the commands work fine. I need to run commands in this mode because that is now I initialize the replica set.



 Comments   
Comment by Ross Lawley [ 05/May/15 ]

Hi dharshanr@scalegrid.net,

For a successful replicaSet connection you must connect to an initialised replicaSet otherwise the driver will not be able to direct and reads or writes. This follows the Server Selection Specification. As the driver cannot find an authoritative server to direct the read or write command to the serverSelectionTimeout will trigger.

You can run read commands when establishing a direct connection to a node and set the readPreference to allow either Primary or Secondary nodes. In this scenario you are explicitly providing the authoritative server by using a direct connection.

As this is the intended behaviour and is accordance with the Server Selection Specification I am closing this ticket as "Works as Designed".

For future, the best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a boarder audience there.

Comment by Dharshan Rangegowda [ 05/May/15 ]

Hi Jeff,

I am not sure I understand. Even if there are multiple servers you should be able to execute the command.

Comment by Jeffrey Yemin [ 05/May/15 ]

Hi Darshan,

This is telling:

Cluster created with settings
{hosts=[127.0.0.1:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='10000 ms', maxWaitQueueSize=500}

The fact that mode=MULTIPLE means that your application is passing a List of ServerAddress instances to the MongoClient constructor, instead of a single ServerAddress instance. Even if the List contains just a single element, the fact that it's a list will set mode=MULTIPLE and the command will fail, as Ross indicated earlier.

Comment by Dharshan Rangegowda [ 05/May/15 ]

Here you go. I am just trying to run the command now - BasicDBObject cmd = new BasicDBObject("buildInfo", "1"); Another data point I forgot to mention is that I have SSL enabled.

May 5, 2015 12:10:42 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='10000 ms', maxWaitQueueSize=500}
May 5, 2015 12:10:42 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server 127.0.0.1:27017 to client view of cluster
May 5, 2015 12:10:42 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=secondaryPreferred} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, all=[
rverDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 10000 ms before timing out
May 5, 2015 12:10:43 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:4}] to 127.0.0.1:27017
May 5, 2015 12:10:43 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=REPLICA_SET_GHOST, state=CONNECTED, ok=true, version=Server
rsion{versionList=[3, 0, 2]}, minWireVersion=0, maxWireVersion=3, maxDocumentSize=16777216, roundTripTimeNanos=1531947, setName='null', hosts=[], passives=[], arbiters=[], primar
'null', tagSet=TagSet{[]}}
Got exception at 2015-05-05T00:10:52.388Zcom.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPref
ence=secondaryPreferred}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=REPLICA_SET_GHOST, roundTripTime=1.8 ms, state=CONNECTED}]

Comment by Ross Lawley [ 30/Apr/15 ]

Can you provide more logging output to help diagnose?

I was successfully able to connect to run the replSetInitiate command and have it initialize the replicaSet for me.

Here is my sample code:

MongoClient client = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
DB db = client.getDB("admin");
db.command(new BasicDBObject("replSetInitiate", BasicDBObject.parse("{_id : \"test\",\n" +
         " members : [{_id : 0, host : \"localhost:27017\"},\n" +
         "           {_id : 1, host : \"localhost:27018\"},\n" +
         "           {_id : 2, host : \"localhost:27019\"}]}")));

Comment by Dharshan Rangegowda [ 30/Apr/15 ]

Hi Ross,

I am specifying a seed of 127.0.0.1, 127.0.0.1 (This was to get around the other issue I hit) and a readPreference of SecondaryPreferred.

I specified a single server 127.0.0.1 and removed the read preference and I still see the same error. I run the

BasicDBObject doc = new BasicDBObject("buildInfo", "1");

command before I do the initiate because we want to verify the version.

Comment by Ross Lawley [ 30/Apr/15 ]

Hi dharshanr@scalegrid.net

If you try to provide a seed list then or provide the replicaSet name then it is expected to fail. This is because the replicaSet is not configured and therefore can't be connected to for operations. From your single log message I can see the current view of the cluster is UNKNOWN and the nodes are of type REPLICA_SET_GHOST leading me to understand that the driver knows its talking to a replicaSet. The other cluster monitoring logs should provide more information about the state of the replicaSet from the drivers perspective.

I was able to connect to an uninitialised replicaSet and successfully run the replSetInitiate command by directly connecting to a single node. How are you connecting?

Removing the replicaSet parameter from the conf file will make the mongod run in standalone mode which is not what what you desire.

Ross

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