MongoDB Ruby Driver 2.5.x Case-Sensitivity Issues with Hostnames on Replica Sets
Tags: mongodb, ruby, replication, hostnames
Published: 2018-06-21 13:19:36

---
title: MongoDB Ruby Driver 2.5.x Case-Sensitivity Issues with Hostnames on Replica Sets
published: true
description: Having trouble connecting to MongoDB replica sets after upgrading the MongoDB Ruby driver to 2.5.x? This post outlines the root cause issues and how to resolve.
tags: MongoDB, Ruby, Replication, Hostnames
canonical_url: https://scalegrid.io/blog/mongodb-ruby-driver-2-5-x-case-sensitivity-issues-with-hostnames-on-replica-sets/
---
<link rel="canonical" href="https://scalegrid.io/blog/mongodb-ruby-driver-2-5-x-case-sensitivity-issues-with-hostnames-on-replica-sets/" />

![ScaleGrid Blog: MongoDB Ruby Driver 2.5.x Case-Sensitivity Issues with Hostnames on Replica Sets](https://thepracticaldev.s3.amazonaws.com/i/lp7zpp7ot61cl3800pxn.png) 

<p style="text-align: justify;">Having trouble connecting to <a href="https://www.mongodb.com/" target="_blank" rel="nofollow">MongoDB </a>replica sets after upgrading the <a href="https://docs.mongodb.com/ruby-driver/master/" target="_blank">MongoDB Ruby driver</a> to 2.5.x? We've recently received a few inquiries about this issue with the latest MongoDB Ruby driver version and wrote this post to share our findings on the problem and cause.</p>
The error message that was encountered on connection attempt was -
<pre>No server is available matching preference: #&lt;Mongo::ServerSelector::Primary:...&gt;</pre>
<p style="text-align: justify;">The issue had been already reported to MongoDB and is being tracked <a href="https://jira.mongodb.org/projects/RUBY/issues/RUBY-1337" target="_blank">here</a>. We spent some time to investigate this issue and the root cause introduced in the driver code in 2.5.x.</p>

<h1>MongoDB Ruby Driver 2.5.x Issue Summary</h1>
<p style="text-align: justify;">The issue exists in the 2.5.x version(s) of the MongoDB Ruby driver and is encountered when the hostnames comprising of the replica set contain case-sensitive characters, for example, <tt>ABC-server1.example.com</tt>. Possible workarounds are:</p>

<ol>
	<li style="text-align: justify;"> Downgrade to 2.4.x or upgrade 2.6.x once it's made available.</li>
	<li style="text-align: justify;">Change the hostnames of all the members of the replica sets to downcase. For example, change the hostname in the example above to <tt>abc-server1.example.com.</tt></li>
</ol>
<h1>Details On The Issue</h1>
<p style="text-align: justify;">Enabling detailed logging on Ruby provided a clue on what was happening:</p>

<pre>...
#19140] DEBUG -- : MONGODB | Topology type 'replica set' initializing.
#19140] DEBUG -- : MONGODB | Server sg-connectiontest1-13622.servers.example.com:27017 initializing.
<strong>#19140] DEBUG -- : MONGODB | Server description for sg-connectiontest1-2.servers.example.com:27017 changed from 'unknown' to 'unknown'.</strong>
#19140] DEBUG -- : MONGODB | Server sg-connectiontest1-13623.servers.example.com:27017 initializing.
#19140] DEBUG -- : MONGODB | Server description for sg-connectiontest1-3.servers.example.com:27017 changed from 'unknown' to 'secondary'.
#19140] DEBUG -- : MONGODB | Server sg-connectiontest1-13624.servers.example.com:27017 initializing.
#19140] DEBUG -- : MONGODB | Server description for sg-connectiontest1-4.servers.example.com:27017 changed from 'unknown' to 'arbiter'.
#19140] DEBUG -- : MONGODB | There was a change in the members of the 'replica set' topology.
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/mongo-2.5.1/lib/mongo/server_selector/selectable.rb:119:in `select_server': No server is available matching preference: # using server_selection_timeout=30 and local_threshold=0.015 (Mongo::Error::NoServerAvailable)
from lib/ruby/gems/2.4.0/gems/mongo-2.5.1/lib/mongo/database.rb:157:in `command'
from lib/ruby/gems/2.4.0/gems/mongo-2.5.1/lib/mongo/client.rb:404:in `list_databases'
from lib/ruby/gems/2.4.0/gems/mongo-2.5.1/lib/mongo/client.rb:385:in `database_names'
...
</pre>
<p style="text-align: justify;">It seemed obvious that the driver was not able to detect the role of the primary of the replica set correctly. When comparing with the logs from 2.4.x, it was clear that this used to happen correctly on that version, i.e. the primary was correctly identified as the primary.</p>

<pre>] DEBUG -- : MONGODB | Topology type 'replica set' initializing.
] DEBUG -- : MONGODB | Server sg-connectiontest1-13622.servers.example.com:27017 initializing.
<strong>] DEBUG -- : MONGODB | Server description for sg-connectiontest1-2.servers.example.com:27017 changed from 'unknown' to 'primary'.</strong>
] DEBUG -- : MONGODB | Server sg-connectiontest1-13623.servers.example.com:27017 initializing.
] DEBUG -- : MONGODB | Server description for sg-connectiontest1-3.servers.example.com:27017 changed from 'unknown' to 'secondary'.
] DEBUG -- : MONGODB | Server sg-connectiontest1-13624.servers.example.com:27017 initializing.
] DEBUG -- : MONGODB | Server description for sg-connectiontest1-4.servers.example.com:27017 changed from 'unknown' to 'arbiter'.
</pre>
<p style="text-align: justify;">The other clue that we had was that our server names were slightly changed in the logs. Instead of the actual name <tt><strong>SG</strong>-connectiontest1-2.servers.example.com</tt>, it was being logged as <tt><strong>sg</strong>-connectiontest1-2.servers.example.com</tt>.</p>
<p style="text-align: justify;">On inspecting the code around how hostnames are parsed during connection initialization and roles are assigned to them, we were able to determine that the driver code was down-casing (i.e. converting ABC.example.com to abc.example.com) while parsing the connection string. Next, the driver connects to the primary to discover the topology using the <a href="https://docs.mongodb.com/manual/reference/command/isMaster/">isMaster</a> command. It's while parsing the result of this command, that the driver determines the role of each node.</p>

<p style="text-align: justify;">However, while comparing hostnames returned from the command with the ones in the connection string, the driver was ignoring to perform a case-insensitive match. This led to a mismatch in names of the primary as reported by MongoDB to what the driver had determined from the connection string. This caused the role of the primary to become unknown and led to the connection failure. The address match check in the role detection code was added in 2.5.x.</p>
<p style="text-align: justify;">The fix to the issue has been <a href="https://jira.mongodb.org/browse/RUBY-1337?focusedCommentId=1907659&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-1907659">targeted to version 2.6.0 </a>of the driver.</p>