Failover test on Atlas with Scala driver leads to an exception and interrupted processing


I set up a test with the 4.2.3 Scala driver and Atlas. The test is a straightforward one, I increment a field recursively and then start a Failover test on Atlas. The recursive incrementation continues throughout the test. In my test case, I set the recursion to go through the incrementation 200 000 times as my experimentation showed that to be long enough to last through the entire Failover test.

My expectation was that when the primary election is underway, the driver would pause operation during that time and resume once a primary was again available. However, this didn’t occur as the driver threw an exception and interrupted processing. The exception was a MongoWriteConcernException. The connection had retryWrites=true&w=majority set.

There’s not a whole lot in the documentation about how to handle Failover. I was expecting the driver to handle the election without needing exception handling in our own code. Is this assumption correct? If it is, there seems to be an issue with the driver.

Below is the relevant code from the test.

val testCollection = mongoFactory.database.getCollection("test")

private def mongoTest(userId: String, curr: Int, times: Int) = {
  if (curr >= times) Future(Ok("Complete"))
  else incrementMongoTestField(userId) flatMap { _ => 
    mongoTest(userId, curr+1, times)

def incrementMongoTestField(userId: String) : Future[UpdateResult] = {
  val query = Document("userId" -> userId)
  val update = Document("$inc" -> Document("mongoTestField" -> 1))
  testCollection.updateOne(query, update).toFuture