[SERVER-31288] findAndModify using a sorting will fail when done concurrently Created: 27/Sep/17  Updated: 07/Nov/17  Resolved: 02/Oct/17

Status: Closed
Project: Core Server
Component/s: Concurrency, Querying
Affects Version/s: 3.2.16
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ori Popowski Assignee: Mark Agarunov
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-22178 Should retry a sorted findAndModify i... Closed
Operating System: ALL
Steps To Reproduce:
  1. We'll create 300 documents with a running index i (0..299).
  2. We will do a findAndModify 300 times at once, querying for i != 0 and updating i = 0
  3. We'll see that a huge amount of findAndModify s fail.

Steps:

  1. Run a MongoDB instance (3.2.16) on localhost:27017
  2. Install the latest sbt
  3. Type sbt in the shell and the interactive sbt will start.

    sbt:mongobug>
    sbt:mongobug> set libraryDependencies += "org.reactivemongo" %% "reactivemongo" % "0.12.6"
    sbt:mongobug> set libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.25"
    sbt:mongobug> console
    scala>
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    import com.typesafe.config.ConfigFactory
    import reactivemongo.api._
    import reactivemongo.api.collections.bson.BSONCollection
    import reactivemongo.bson._
     
    import scala.concurrent.Await
    import scala.concurrent.ExecutionContext.Implicits._
    import scala.concurrent.duration.Duration
     
    val driver = new MongoDriver(Some(ConfigFactory.empty()), None)
    val conn = driver.connection(Seq("localhost:27017"))
    val db = Await.result(conn.database("test"), Duration.Inf)
    val col = db.collection[BSONCollection]("col")
     
    Await.ready(col.remove(document()), Duration.Inf)
     
    for (i <- 1 to 300) Await.ready(col.insert(document("i" -> i)), Duration.Inf)
     
    val results = for (_ <- 1 to 300) yield {
      col.findAndUpdate(
        document("i" -> document("$ne" -> 0)),
        document("$set" -> document("i" -> 0)),
        sort = Some(document("i" -> 1))
      )
    }
     
    results.map(_.map(println))
     
    // Exiting paste mode, now interpreting.
    
    

After the application is ran, the output will look something like this:

FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
FindAndModifyResult(Some(UpdateLastError(true,None,1,None)),Some(BSONDocument(<non-empty>)))
FindAndModifyResult(Some(UpdateLastError(true,None,1,None)),Some(BSONDocument(<non-empty>)))
FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)
...
...

Every line that looks like this:

FindAndModifyResult(Some(UpdateLastError(false,None,0,None)),None)

means a failure in updating.

When we remove the sorting, all the updates will succeed.

Participants:

 Description   

When there are multiple {{findAndModify}}s being ran on the same time with sort, about 50% of them will fail.



 Comments   
Comment by Mark Agarunov [ 02/Oct/17 ]

Hello oripwk,

After looking into this, I believe it may be due to the same underlying issue reported in SERVER-22178. Fortunately, this has been fixed in version 3.3.4, which I have confirmed after running the script you provided on mongodb 3.3.4 and confirming that all updates succeed.

As this has been fixed in more recent versions, my recommendation would be to upgrade to the the latest version of mongodb 3.4 if possible. As this is due to the same issue as SERVER-22178, I've closed this ticket as a duplicate.

Thanks,
Mark

Comment by Kelsey Schubert [ 02/Oct/17 ]

Hi oripwk,

Thank you for reporting this behavior. Would you please clarify whether you're using WiredTiger or MMAPv1 in your tests? We're investigating and will update this ticket when we know more.

Kind regards,
Kelsey

Generated at Thu Feb 08 04:26:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.