3

I've had problems connecting to a replica set via the C# sharp driver when using the following code:

var client  = new MongoClient("mongodb://daluser:daluatmongo@xxx:25015,yyy:25015,zzz:25015/database?replicaSet=dal_uat_mongo");
var db = client.GetDatabase("database");
var collection = db.GetCollection<BsonDocument>("table");
var foo = collection.CountAsync(document => true).Result;

Here xxx, yyy and zzz are IP addresses and I've used anonymous names for the collection and database.

This results in the following exception: "A timeout occured after 30000ms selecting a server using CompositeServerSelector" (more details in gist link):

https://gist.github.com/bfranklinsportingindex/0270a1e958051b689e56

Strangely if we remove the replica set option and only specify one server then it works fine for two of the three servers. The server that fails gives us the familiar "target machine has refused the connection" error.

I'm currently testing directly with the latest version of the source code and the version of mongo on the server we're tryingt to connect to is 2.6.9

I've tried debugging into the source code for the driver amd I get to the following line in SelectServerAsync method in the Cluster class:

var connectedServers = description.Servers.Where(s => s.State == ServerState.Connected);

Here the connectedServers collection is empty and I would expect (with my limited understanding of driver codebase) it to be populated. Hopefully someone with more experience with this codebase can shed any light on what's going on or provide me with some pointers on where to look in the codebase.

Ben Franklin
  • 626
  • 6
  • 21
  • Why do you think that this is related to the Codebase or driver? Maybe I missed something but for me it looks like the third MongoDB server is not reachable. That could be double checked with another MongoDB client (e.g. MongoChef, Robomongo or MongoVue). – Matthias Sep 16 '15 at 12:12
  • We actually tested the same piece of code against our production environment servers and got the same results. There we have no problem with a rogue server. Also, note that this is occurring as a result of an upgrade to the driver. We don't have this problem with an older version of the driver. We were originally using 1.7.0 and upgraded to 2.0.1 which gave us the same problem as the current source code. – Ben Franklin Sep 16 '15 at 12:35
  • I have no clear idea, but I would check if I can still see the problem when I change the environment. For example using another server as the third server or try to reproduce the problem in a test system. Are you sure that the issue is caused by the driver upgrade or was this just a coincidence. The third server in production is it the same version than in your test environment? – Matthias Sep 16 '15 at 13:33
  • I'm convinced it is to do with the driver upgrade since the old version (1.7.0) is working fine when deployed out as part of an application to both test and production which both uses 2.6.9 version of mongo – Ben Franklin Sep 16 '15 at 14:29
  • What could be a possible reason that it runs in one environment and not in the other? Or did I not understand correclty (production vs. test environment). Probably there is a problem in the driver but probably also something you could do in the environment. – Matthias Sep 16 '15 at 14:32
  • Possible duplicate of [MongoDB C# 2.0 TimeoutException](https://stackoverflow.com/questions/29832622/mongodb-c-sharp-2-0-timeoutexception) – kenorb Oct 16 '17 at 16:08

1 Answers1

0

Actually we managed to trace the problem down to a network address translation issue which we tested by putting add some lines to the hosts file. The driver seems to be working fine now.

Ben Franklin
  • 626
  • 6
  • 21