Updates for connection-pooled operations on replica pairs.
This commit is contained in:
parent
539274bb7b
commit
072b025cdc
@ -1,6 +1,6 @@
|
||||
= Introduction
|
||||
|
||||
This is a Ruby driver for MongoDB[http://www.mongodb.org].
|
||||
This is the 10gen-supported Ruby driver for MongoDB[http://www.mongodb.org].
|
||||
|
||||
Here is a quick code sample. See the files in the "examples" subdirectory for
|
||||
many more.
|
||||
@ -269,6 +269,11 @@ individually:
|
||||
$ rake test:pair_insert
|
||||
$ rake test:pair_query
|
||||
|
||||
It's also possible to test replica pairs with connection pooling:
|
||||
|
||||
$ rake test:pooled_pair_insert
|
||||
|
||||
|
||||
All tests now require shoulda and mocha. You can install these gems as
|
||||
follows:
|
||||
|
||||
|
5
Rakefile
5
Rakefile
@ -42,6 +42,11 @@ namespace :test do
|
||||
t.verbose = true
|
||||
end
|
||||
|
||||
Rake::TestTask.new(:pooled_pair_insert) do |t|
|
||||
t.test_files = FileList['test/replica/pooled_insert_test.rb']
|
||||
t.verbose = true
|
||||
end
|
||||
|
||||
Rake::TestTask.new(:pair_query) do |t|
|
||||
t.test_files = FileList['test/replica/query_test.rb']
|
||||
t.verbose = true
|
||||
|
@ -23,6 +23,10 @@ module Mongo
|
||||
# A connection to MongoDB.
|
||||
class Connection
|
||||
|
||||
# We need to make sure that all connection abort when
|
||||
# a ConnectionError is raised.
|
||||
Thread.abort_on_exception = true
|
||||
|
||||
DEFAULT_PORT = 27017
|
||||
STANDARD_HEADER_SIZE = 16
|
||||
RESPONSE_HEADER_SIZE = 20
|
||||
@ -292,7 +296,6 @@ module Mongo
|
||||
@reserved_connections.clear
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
# Get a socket from the pool, mapped to the current thread.
|
||||
@ -338,8 +341,12 @@ module Mongo
|
||||
# This method is called exclusively from #obtain_socket;
|
||||
# therefore, it runs within a mutex, as it must.
|
||||
def checkout_new_socket
|
||||
begin
|
||||
socket = TCPSocket.new(@host, @port)
|
||||
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
||||
rescue => ex
|
||||
raise ConnectionFailure, "Failed to connect socket: #{ex}"
|
||||
end
|
||||
@sockets << socket
|
||||
@checked_out << socket
|
||||
socket
|
||||
|
@ -4,7 +4,7 @@ require 'test/unit'
|
||||
require 'test/test_helper'
|
||||
|
||||
# NOTE: this test should be run only if a replica pair is running.
|
||||
class CountTest < Test::Unit::TestCase
|
||||
class ReplicaPairCountTest < Test::Unit::TestCase
|
||||
include Mongo
|
||||
|
||||
def setup
|
||||
|
@ -4,7 +4,7 @@ require 'test/unit'
|
||||
require 'test/test_helper'
|
||||
|
||||
# NOTE: this test should be run only if a replica pair is running.
|
||||
class ReplicaPairTest < Test::Unit::TestCase
|
||||
class ReplicaPairInsertTest < Test::Unit::TestCase
|
||||
include Mongo
|
||||
|
||||
def setup
|
||||
@ -33,7 +33,6 @@ class ReplicaPairTest < Test::Unit::TestCase
|
||||
gets
|
||||
results = []
|
||||
|
||||
# Note: need to figure out what to do here since this first find will fail.
|
||||
rescue_connection_failure do
|
||||
@coll.find.each {|r| results << r}
|
||||
[20, 30, 40, 50, 60, 70].each do |a|
|
||||
|
54
test/replica/pooled_insert_test.rb
Normal file
54
test/replica/pooled_insert_test.rb
Normal file
@ -0,0 +1,54 @@
|
||||
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||
require 'mongo'
|
||||
require 'test/unit'
|
||||
require 'test/test_helper'
|
||||
|
||||
# NOTE: this test should be run only if a replica pair is running.
|
||||
class ReplicaPairPooledInsertTest < Test::Unit::TestCase
|
||||
include Mongo
|
||||
|
||||
def setup
|
||||
@conn = Mongo::Connection.new({:left => ["localhost", 27017], :right => ["localhost", 27018]}, nil, :pool_size => 10, :timeout => 5)
|
||||
@db = @conn.db('mongo-ruby-test')
|
||||
@db.drop_collection("test-pairs")
|
||||
@coll = @db.collection("test-pairs")
|
||||
end
|
||||
|
||||
def test_insert
|
||||
expected_results = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
@coll.save({:a => -1}, :safe => true)
|
||||
puts "Please disconnect the current master."
|
||||
gets
|
||||
|
||||
threads = []
|
||||
10.times do |i|
|
||||
threads[i] = Thread.new do
|
||||
rescue_connection_failure do
|
||||
@coll.save({:a => i}, :safe => true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts "Please reconnect the old master to make sure that the new master " +
|
||||
"has synced with the previous master. Note: this may have happened already." +
|
||||
"Note also that when connection with multiple threads, you may need to wait a few seconds" +
|
||||
"after restarting the old master so that all the data has had a chance to sync." +
|
||||
"This is a case of eventual consistency."
|
||||
gets
|
||||
results = []
|
||||
|
||||
rescue_connection_failure do
|
||||
@coll.find.each {|r| results << r}
|
||||
expected_results.each do |a|
|
||||
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
|
||||
end
|
||||
end
|
||||
|
||||
@coll.save({:a => 10}, :safe => true)
|
||||
@coll.find.each {|r| results << r}
|
||||
(expected_results + [10]).each do |a|
|
||||
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -4,7 +4,7 @@ require 'test/unit'
|
||||
require 'test/test_helper'
|
||||
|
||||
# NOTE: this test should be run only if a replica pair is running.
|
||||
class ReplicaPairTest < Test::Unit::TestCase
|
||||
class ReplicaPairQueryTest < Test::Unit::TestCase
|
||||
include Mongo
|
||||
|
||||
def setup
|
||||
|
Loading…
Reference in New Issue
Block a user