[GODRIVER-674] server selection timeout error when connecting to replica set Created: 05/Dec/18  Updated: 03/Apr/23  Resolved: 26/Dec/18

Status: Closed
Project: Go Driver
Component/s: Connections
Affects Version/s: 0.0.18
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: 성욱 조 Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

2 mongodb with docker (replica set configured successfully, shell connection works fine)
macOS 10.14.1
go 1.11.2 with go module



 Description   

ctx := context.Background()
 
clientOptions := &options.ClientOptions{}
clientOptions.SetHosts([]string\{"mongodb://localhost:27017", "mongodb://localhost:27018"})
clientOptions.SetReadPreference(readpref.Primary())
clientOptions.SetServerSelectionTimeout(time.Duration(2*time.Second))
 
client, err := mongo.NewClientWithOptions("mongodb://localhost:27017,localhost:27018", clientOptions)
if err != nil {
 panic(err)
}
 
err = client.Connect(ctx)
if err != nil {
 panic(err)
}
 
dbs, err := client.ListDatabaseNames(ctx, bson.D{})
if err != nil {
 panic(err)
}
 
fmt.Println(dbs)

but result is: panic: server selection timeout
I also added replicaSet=rs0 option but result was same.
Does this driver not yet support replica set?



 Comments   
Comment by Jeffrey Yemin [ 26/Dec/18 ]

Yes, it's expected. The application has to be able to connect to the hostname/port as contained in the replica set configuration.

Comment by 성욱 조 [ 12/Dec/18 ]

I checked the test program in the docker network and it works! I really appreciate it

BUT  I thought I can connect with docker-published  ports. 

I run 3 nodes at docker. And I published each node's port to 27017, 27018, 27019.

In this case, server selection timeout is normal?

Comment by Kristofer Brandow (Inactive) [ 11/Dec/18 ]

Hi ghatdev,

It looks like the replica set are on the host mongo-node* domains while you're attempting to connect over localhost. Can you use the host name that the }}{{rs.status is reporting?

--Kris

Comment by 성욱 조 [ 11/Dec/18 ]

@Kris Brandow

I added third node and tested again but still server selection timeout error occurrences.

This is my rs.status() output:

{
	"set" : "rs0",
	"date" : ISODate("2018-12-11T06:05:31.557Z"),
	"myState" : 1,
	"term" : NumberLong(6),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1544508327, 1),
			"t" : NumberLong(6)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1544508327, 1),
			"t" : NumberLong(6)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1544508327, 1),
			"t" : NumberLong(6)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1544508327, 1),
			"t" : NumberLong(6)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1544508321, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "mongo-node1:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 308,
			"optime" : {
				"ts" : Timestamp(1544508327, 1),
				"t" : NumberLong(6)
			},
			"optimeDate" : ISODate("2018-12-11T06:05:27Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1544508035, 1),
			"electionDate" : ISODate("2018-12-11T06:00:35Z"),
			"configVersion" : 4,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "mongo-node2:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 302,
			"optime" : {
				"ts" : Timestamp(1544508327, 1),
				"t" : NumberLong(6)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1544508327, 1),
				"t" : NumberLong(6)
			},
			"optimeDate" : ISODate("2018-12-11T06:05:27Z"),
			"optimeDurableDate" : ISODate("2018-12-11T06:05:27Z"),
			"lastHeartbeat" : ISODate("2018-12-11T06:05:31.432Z"),
			"lastHeartbeatRecv" : ISODate("2018-12-11T06:05:31.441Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "mongo-node1:27017",
			"syncSourceHost" : "mongo-node1:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 4
		},
		{
			"_id" : 2,
			"name" : "mongo-node3:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 4,
			"optime" : {
				"ts" : Timestamp(1544508327, 1),
				"t" : NumberLong(6)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1544508327, 1),
				"t" : NumberLong(6)
			},
			"optimeDate" : ISODate("2018-12-11T06:05:27Z"),
			"optimeDurableDate" : ISODate("2018-12-11T06:05:27Z"),
			"lastHeartbeat" : ISODate("2018-12-11T06:05:31.440Z"),
			"lastHeartbeatRecv" : ISODate("2018-12-11T06:05:31.140Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 4
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1544508327, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1544508327, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

And this is my code.
I also tried without each options.

        clientOptions := &options.ClientOptions{}
	clientOptions.SetHosts([]string{"mongodb://localhost:27017", "mongodb://localhost:27018", "mongodb://localhost:27019"})
	clientOptions.SetReplicaSet("rs0")
	clientOptions.SetReadPreference(readpref.Primary())
	clientOptions.SetServerSelectionTimeout(time.Duration(2*time.Second))
 
 
	client, err := mongo.NewClientWithOptions("mongodb://localhost:27017,localhost:27018,localhost:27019", clientOptions)
	if err != nil {
		panic(err)
	}

Thanks!

Comment by Kristofer Brandow (Inactive) [ 10/Dec/18 ]

Hi ghatdev,

You must run at least 3 nodes in a replica set for an election to happen. Since you only have 2 nodes, neither of them can be elected primary and the driver only has secondaries to choose from. Can you configure the replica set with 3 nodes and try again?

Thanks,

Kris

Comment by 성욱 조 [ 05/Dec/18 ]

correction: go version is 1.11.1 (not 1.11.2) sorry

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