Skip to content

Instantly share code, notes, and snippets.

@repeatedly
Created January 20, 2012 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save repeatedly/1650238 to your computer and use it in GitHub Desktop.
Save repeatedly/1650238 to your computer and use it in GitHub Desktop.
data loss in mongo-ruby-driver with Replica Set

Code

require 'mongo'

def rescue_connection_failure(num_retries = 30)
  retries = 0
  begin
    puts "retries: #{retries}"
    yield
  rescue Mongo::ConnectionFailure => e
    retries += 1
    raise if retries > num_retries

    puts "Failed to connect to Replica Set. Try to retry: retry number = #{retries}"
    sleep 1
    retry
  end
end

collection = Mongo::ReplSetConnection.new(['localhost', 27017], ['localhost', 27018], ['localhost', 27019]).db('d').collection('c')
i = 0
loop { 
  rescue_connection_failure {
    collection.insert({'num' => i})
  }
  i += 1
  sleep 2
}

Primary down at writing operation.

Result

PRIMARY> db.c.find()
{ "_id" : ObjectId("4f19f72285c102aeeb000001"), "num" : 0 }
{ "_id" : ObjectId("4f19f72485c102aeeb000002"), "num" : 1 }
{ "_id" : ObjectId("4f19f72685c102aeeb000003"), "num" : 2 }
{ "_id" : ObjectId("4f19f72885c102aeeb000004"), "num" : 3 }
{ "_id" : ObjectId("4f19f72d85c102aeeb000007"), "num" : 5 }
{ "_id" : ObjectId("4f19f72f85c102aeeb000008"), "num" : 6 }
{ "_id" : ObjectId("4f19f73185c102aeeb000009"), "num" : 7 }

4 number object lossed...

Environment

mongo-ruby-driver: 1.5.2
mongod: osx-x86_64-2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment