Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-610

Client should retry every .5 sec until server selection timeout

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.2-beta0
    • Affects Version/s: 1.2.0
    • Component/s: None
    • Labels:
      None

      *Summary (Jesse)*: this was two bugs in the single-threaded implementation of the Server Discovery And Monitoring and Server Selection specs. First, after failing an initial connection, the client never re-attempted connection. Second, it spun in a tight loop until the server selection timeout (default 30 seconds) expired. The fix is to actually re-attempt a connection after each connection failure (until the timeout), and to pause half a second between attempts (the minHeartbeatFrequencyMS in Server Discovery And Monitoring).

      *Original Report (Hannes)*:

      Straight from the example docs - modifying the port to not-a-mongod will result in an endless loop.

      #include <bson.h>
      #include <mongoc.h>
      #include <stdio.h>
      
      int
      main (int   argc,
            char *argv[])
      {
          mongoc_client_t *client;
          mongoc_collection_t *collection;
          mongoc_cursor_t *cursor;
          bson_error_t error;
          bson_oid_t oid;
          bson_t *doc;
      
          mongoc_init ();
      
          client = mongoc_client_new ("mongodb://localhost:27016/");
          collection = mongoc_client_get_collection (client, "test", "test");
      
          doc = bson_new ();
          bson_oid_init (&oid, NULL);
          BSON_APPEND_OID (doc, "_id", &oid);
          BSON_APPEND_UTF8 (doc, "hello", "world");
      
          if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
              printf ("Insert failed: %s\n", error.message);
          }
      
          bson_destroy (doc);
      
          doc = bson_new ();
          BSON_APPEND_OID (doc, "_id", &oid);
      
          if (!mongoc_collection_delete (collection, MONGOC_DELETE_SINGLE_REMOVE, doc, NULL, &error)) {
              printf ("Delete failed: %s\n", error.message);
          }
      
          bson_destroy (doc);
          mongoc_collection_destroy (collection);
          mongoc_client_destroy (client);
      
          return 0;
      }
      

      mongoc1.2.x

      vagrant@precise64:~/mongo-c-driver$ time ./uds
      Insert failed: Timed out trying to select a server
      Delete failed: Timed out trying to select a server
      
      real	3m0.011s
      

      mongoc1.1.x resulted in correct:

      015/04/07 18:32:18.0622: [10104]:    DEBUG:      cluster: Client initialized in direct mode.
      2015/04/07 18:32:18.0623: [10104]:  WARNING:       client: Failed to connect to: ipv4 127.0.0.1:27016, error: 111, Connection refused
      
      Insert failed: Failed to connect to target host: localhost:27016
      2015/04/07 18:32:18.0624: [10104]:  WARNING:       client: Failed to connect to: ipv4 127.0.0.1:27016, error: 111, Connection refused
      
      Delete failed: Failed to connect to target host: localhost:27016
      

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            bjori Hannes Magnusson
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: