Uploaded image for project: 'PHP Legacy Driver'
  1. PHP Legacy Driver
  2. PHP-1460

Query with limit leaves open cursors on server

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 1.6.9
    • Fix Version/s: 1.6.10
    • Component/s: pecl-mongo
    • Labels:
    • Environment:
      This has been reproduced with both Mac OSX Yosemite and CentOS 6.
    • # Replies:
      4
    • Last comment by Customer:
      true
    • Sprint:
      PHP Sprint 21

      Description

      The PHP Mongo Driver version does not properly close cursors on the server if a limit was used, and there are more documents available (e.g. using a limit of 20 on a query that would return 30 documents). This issue was introduced in 1.6.0 and still persists in 1.6.9. In 1.5.8, the problem is not reproducible.

      I am guessing this is related to the behavior where PHP is supposed to kill the cursor in the dtor (e.g. as referenced in https://jira.mongodb.org/browse/PHPC-225)

      MORE INFO:

      This is easily reproducible (see below steps) using MongoDB 2.6.10 server running on Mac OS via home-brew, but the same has been observed in our production servers running MongoDB 2.6.9 on CentOS 6. This was originally noticed in production when we upgraded from PHP Mongo Driver 1.5.4 to PHP Mongo Driver 1.6.9 on one test server, and then the MongoDB server showed an exponential growth in open cursors.

      When reproduced locally in Mac OSX, it was a solo Mongod. In Production, it was a replica set with one primary, one secondary, and one arbiter. Most of our reads which use the limit option were secondary preferred, and so we saw the cursor count spike on the secondary, not the primary.

      Aside: In production, having thousands of open cursors caused from this bug correlated to high spikes in lock % (up to 100% in some cases, whereas prior to this the spikes were never above 10%), which could make this issue much more serious. However, we have not been able to prove that causality, so the lock % rising may be unrelated.

      REPRODUCTION STEPS

      mongod> use foo
      mongod> for ( var i = 0; i < 30 ; i++ ) { db.bar.insert({a:2}) }
      

      mongo_test.php

      <?php
       
      $m = new MongoClient();
      $bar = $m->foo->bar;
      $cursor = $bar->find(['a' => 2])->limit(20);
       
      $i = 0;
      foreach($cursor as $b) {
      	echo ".";
      	$i++;
      }
      echo "\ndone! iterated through $i items\n";
      

      mongod> db.serverStatus().cursors.totalOpen
      

      Each execution of the above PHP script will increase the number of totalOpen cursors by 1. These cursors will persist until cursorTimeoutMillis has passed [and in production, our cursorTimeoutMillis is extremely high for other reasons].

      I have tested this using the following versions:
      1.6.9 - problem happens!
      1.6.8 - problem happens!
      1.6.3 - problem happens!
      1.6.0 - problem happens!
      1.5.8 - problem not reproducible
      1.5.4 - problem not reproducible

      This leads me to believe the problem was introduced in 1.6.0 and has not been fixed in the latest 1.6.9 version of the driver.

        Attachments

          Activity

            People

            Assignee:
            jmikola Jeremy Mikola
            Reporter:
            rwilliams Rob Williams
            Participants:
            Last commenter:
            Rathi Gnanasekaran
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since reply:
              4 years, 49 weeks, 3 days ago
              Date of 1st Reply: