Skip to content

Commit

Permalink
merging in upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Feb 20, 2013
2 parents ecbb5a4 + a96bc6e commit 85948f3
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 69 deletions.
22 changes: 10 additions & 12 deletions lib/mongo/cursor.rb
Expand Up @@ -324,16 +324,11 @@ def close
message.put_int(1)
message.put_long(@cursor_id)
log(:debug, "Cursor#close #{@cursor_id}")
begin
socket = @pool.checkout
@connection.send_message(
Mongo::Constants::OP_KILL_CURSORS,
message,
:socket => socket
)
ensure
socket.checkin
end
@connection.send_message(
Mongo::Constants::OP_KILL_CURSORS,
message,
:pool => @pool
)
end
@cursor_id = 0
@closed = true
Expand Down Expand Up @@ -516,7 +511,7 @@ def send_get_more
message.put_long(@cursor_id)
log(:debug, "cursor.refresh() for cursor #{@cursor_id}") if @logger

socket = checkout_socket_from_connection
socket = @pool.checkout

begin
results, @n_received, @cursor_id = @connection.receive_message(
Expand All @@ -532,7 +527,9 @@ def send_get_more

def checkout_socket_from_connection
begin
if @command && !Mongo::Support::secondary_ok?(@selector)
if @pool
socket = @pool.checkout
elsif @command && !Mongo::Support::secondary_ok?(@selector)
socket = @connection.checkout_reader(:primary)
else
socket = @connection.checkout_reader(@read, @tag_sets, @acceptable_latency)
Expand All @@ -542,6 +539,7 @@ def checkout_socket_from_connection
raise ex
end
@pool = socket.pool
#puts "checkout_socket_from_connection caller:#{caller[0][/:in `([^']+)'/,1]} self:#{self.object_id} @pool.port:#{@pool.port}"
socket
end

Expand Down
8 changes: 5 additions & 3 deletions lib/mongo/networking.rb
Expand Up @@ -26,10 +26,12 @@ def send_message(operation, message, opts={})
add_message_headers(message, operation)
packed_message = message.to_s

sock = opts.fetch(:socket, nil)
sock = nil
pool = opts.fetch(:pool, nil)
begin
if operation == Mongo::Constants::OP_KILL_CURSORS && @read != :primary
sock ||= checkout_reader
if pool
#puts "send_message pool.port:#{pool.port}"
sock = pool.checkout
else
sock ||= checkout_writer
end
Expand Down
2 changes: 1 addition & 1 deletion test/bson/bson_test.rb
Expand Up @@ -255,7 +255,7 @@ def test_default_internal_is_honored
def test_code
code = Code.new('this.a.b < this.b')
assert_equal 17, code.length
assert_match /<BSON::Code:\d+ @data="this.a.b < this.b" @scope="{}">/, code.inspect
assert_match /<BSON::Code:.*@data="this.a.b < this.b".*>/, code.inspect
doc = {'$where' => code}
assert_doc_pass(doc)
code = 'this.c.d < this.e'.to_bson_code # core_ext.rb
Expand Down
2 changes: 1 addition & 1 deletion test/functional/collection_test.rb
Expand Up @@ -840,7 +840,7 @@ def test_map_reduce_with_collection_output_to_other_db
r = Code.new("function(k,vals) { return 1; }")
oh = BSON::OrderedHash.new
oh[:replace] = 'foo'
oh[:db] = 'somedb'
oh[:db] = MONGO_TEST_DB
res = @@test.map_reduce(m, r, :out => (oh))
assert res["result"]
assert res["counts"]
Expand Down
15 changes: 7 additions & 8 deletions test/functional/timeout_test.rb
Expand Up @@ -3,24 +3,24 @@
class TestTimeout < Test::Unit::TestCase
def test_op_timeout
connection = standard_connection(:op_timeout => 1)

admin = connection.db('admin')

command = {:eval => "sleep(500)"}
# Should not timeout
assert admin.command(command)

# Should timeout
command = {:eval => "sleep(1500)"}
assert_raise Mongo::OperationTimeout do
admin.command(command)
admin.command(command)
end
end

def test_external_timeout_does_not_leave_socket_in_bad_state
client = Mongo::MongoClient.new
db = client['testdb']
coll = db['testcoll']
db = client[MONGO_TEST_DB]
coll = db['timeout-tests']

# prepare the database
coll.drop
Expand All @@ -43,14 +43,13 @@ def test_external_timeout_does_not_leave_socket_in_bad_state
=begin
def test_ssl_op_timeout
connection = standard_connection(:op_timeout => 1, :ssl => true)
coll = connection.db(MONGO_TEST_DB).collection("test")
coll.insert({:a => 1})
# Should not timeout
assert coll.find_one({"$where" => "sleep(100); return true;"})
# Should timeout
# Should timeout
assert_raise Mongo::OperationTimeout do
coll.find_one({"$where" => "sleep(5 * 1000); return true;"})
end
Expand Down
5 changes: 2 additions & 3 deletions test/replica_set/basic_test.rb
Expand Up @@ -65,9 +65,8 @@ def test_accessors
assert_equal @rs.primary_name, [client.host, client.port].join(':')
assert_equal client.host, client.primary_pool.host
assert_equal client.port, client.primary_pool.port
assert_equal 1, client.secondaries.length
assert_equal 1, client.arbiters.length
assert_equal 1, client.secondary_pools.length
assert_equal 2, client.secondaries.length
assert_equal 2, client.secondary_pools.length
assert_equal @rs.repl_set_name, client.replica_set_name
assert client.secondary_pools.include?(client.read_pool(:secondary))
assert_equal 90, client.refresh_interval
Expand Down
6 changes: 3 additions & 3 deletions test/replica_set/client_test.rb
Expand Up @@ -48,7 +48,7 @@ def test_connect_with_last_secondary_node_terminated

def test_connect_with_primary_stepped_down
@client = MongoReplicaSetClient.new @rs.repl_set_seeds
@client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
@client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
assert @client[MONGO_TEST_DB]['bar'].find_one

primary = Mongo::MongoClient.new(*@client.primary)
Expand All @@ -66,7 +66,7 @@ def test_connect_with_primary_stepped_down
def test_connect_with_primary_killed
@client = MongoReplicaSetClient.new @rs.repl_set_seeds
assert @client.connected?
@client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
@client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
assert @client[MONGO_TEST_DB]['bar'].find_one

@rs.primary.kill(Signal.list['KILL'])
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_save_with_primary_stepped_down

#def test_connect_with_first_node_removed
# @client = MongoReplicaSetClient.new @rs.repl_set_seeds
# @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
# @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
#
# old_primary = [@client.primary_pool.host, @client.primary_pool.port]
# old_primary_conn = Mongo::MongoClient.new(*old_primary)
Expand Down
8 changes: 6 additions & 2 deletions test/replica_set/connection_test.rb
Expand Up @@ -48,11 +48,13 @@ def test_connect_with_connection_string_in_env_var
ENV['MONGODB_URI'] = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
@connection = ReplSetConnection.new
assert !@connection.nil?
assert_equal 2, @connection.seeds.length
assert_equal 3, @connection.seeds.length
assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
assert_equal @rs.replicas[2].host, @connection.seeds[2][0]
assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
assert_equal @rs.replicas[2].port, @connection.seeds[2][1]
assert_equal @rs.repl_set_name, @connection.replica_set_name
assert @connection.connected?
end
Expand All @@ -61,11 +63,13 @@ def test_connect_with_connection_string_in_implicit_mongodb_uri
ENV['MONGODB_URI'] = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
@connection = Connection.from_uri
assert !@connection.nil?
assert_equal 2, @connection.seeds.length
assert_equal 3, @connection.seeds.length
assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
assert_equal @rs.replicas[2].host, @connection.seeds[2][0]
assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
assert_equal @rs.replicas[2].port, @connection.seeds[2][1]
assert_equal @rs.repl_set_name, @connection.replica_set_name
assert @connection.connected?
end
Expand Down
6 changes: 3 additions & 3 deletions test/replica_set/count_test.rb
Expand Up @@ -17,7 +17,7 @@ def teardown
end

def test_correct_count_after_insertion_reconnect
@coll.insert({:a => 20}, :w => 2, :wtimeout => 10000)
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
assert_equal 1, @coll.count

# Kill the current master node
Expand All @@ -32,15 +32,15 @@ def test_correct_count_after_insertion_reconnect
end

def test_count_command_sent_to_primary
@coll.insert({:a => 20}, :w => 2, :wtimeout => 10000)
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
assert_equal 1, @coll.count
count_after = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
assert_equal 2, count_after - count_before
end

def test_count_with_read
@coll.insert({:a => 20}, :w => 2, :wtimeout => 10000)
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
assert_equal 1, @coll.count(:read => :secondary)
assert_equal 1, @coll.find({}, :read => :secondary).count()
Expand Down
4 changes: 2 additions & 2 deletions test/replica_set/insert_test.rb
Expand Up @@ -15,7 +15,7 @@ def teardown
end

def test_insert
@coll.save({:a => 20}, :w => 2)
@coll.save({:a => 20}, :w => 3)

@rs.primary.stop

Expand All @@ -40,7 +40,7 @@ def test_insert
end
end

@coll.save({:a => 80}, :w => 2)
@coll.save({:a => 80}, :w => 3)
@coll.find.each {|r| results << r}
[20, 30, 40, 50, 60, 70, 80].each do |a|
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
Expand Down
8 changes: 4 additions & 4 deletions test/replica_set/query_test.rb
Expand Up @@ -15,9 +15,9 @@ def teardown
end

def test_query
@coll.save({:a => 20}, :w => 2)
@coll.save({:a => 30}, :w => 2)
@coll.save({:a => 40}, :w => 2)
@coll.save({:a => 20}, :w => 3)
@coll.save({:a => 30}, :w => 3)
@coll.save({:a => 40}, :w => 3)
results = []
@coll.find.each {|r| results << r}
[20, 30, 40].each do |a|
Expand All @@ -43,7 +43,7 @@ def test_query
# primary, where it does not exist.
# def test_secondary_getmore
# 200.times do |i|
# @coll.save({:a => i}, :w => 2)
# @coll.save({:a => i}, :w => 3)
# end
# as = []
# # Set an explicit batch size, in case the default ever changes.
Expand Down
4 changes: 2 additions & 2 deletions test/replica_set/refresh_test.rb
Expand Up @@ -30,7 +30,7 @@ def test_connect_and_manual_refresh_with_secondaries_down
# Refresh and ensure state
client.refresh
assert_equal client.read_pool, client.primary_pool
assert_equal 1, client.secondaries.length
assert_equal 2, client.secondaries.length
end

def test_automated_refresh_with_secondaries_down
Expand All @@ -56,7 +56,7 @@ def test_automated_refresh_with_secondaries_down

assert client.refresh_version > old_refresh_version,
"Refresh version hasn't changed."
assert client.secondaries.length == 1,
assert client.secondaries.length == 2,
"No secondaries have been added."
assert client.manager.read_pool != client.manager.primary,
"Read pool and primary pool are identical."
Expand Down
20 changes: 10 additions & 10 deletions test/replica_set/replication_ack_test.rb
Expand Up @@ -23,49 +23,49 @@ def teardown

def test_safe_mode_with_w_failure
assert_raise_error OperationFailure, "timeout" do
@col.insert({:foo => 1}, :w => 3, :wtimeout => 1, :fsync => true)
@col.insert({:foo => 1}, :w => 4, :wtimeout => 1, :fsync => true)
end
assert_raise_error OperationFailure, "timeout" do
@col.update({:foo => 1}, {:foo => 2}, :w => 3, :wtimeout => 1, :fsync => true)
@col.update({:foo => 1}, {:foo => 2}, :w => 4, :wtimeout => 1, :fsync => true)
end
assert_raise_error OperationFailure, "timeout" do
@col.remove({:foo => 2}, :w => 3, :wtimeout => 1, :fsync => true)
@col.remove({:foo => 2}, :w => 4, :wtimeout => 1, :fsync => true)
end
assert_raise_error OperationFailure do
@col.insert({:foo => 3}, :w => "test-tag")
end
end

def test_safe_mode_replication_ack
@col.insert({:baz => "bar"}, :w => 2, :wtimeout => 5000)
@col.insert({:baz => "bar"}, :w => 3, :wtimeout => 5000)

assert @col.insert({:foo => "0" * 5000}, :w => 2, :wtimeout => 5000)
assert @col.insert({:foo => "0" * 5000}, :w => 3, :wtimeout => 5000)
assert_equal 2, @slave1[MONGO_TEST_DB]["test-sets"].count

assert @col.update({:baz => "bar"}, {:baz => "foo"}, :w => 2, :wtimeout => 5000)
assert @col.update({:baz => "bar"}, {:baz => "foo"}, :w => 3, :wtimeout => 5000)
assert @slave1[MONGO_TEST_DB]["test-sets"].find_one({:baz => "foo"})

assert @col.insert({:foo => "bar"}, :w => "majority")

assert @col.insert({:bar => "baz"}, :w => :majority)

assert @col.remove({}, :w => 2, :wtimeout => 5000)
assert @col.remove({}, :w => 3, :wtimeout => 5000)
assert_equal 0, @slave1[MONGO_TEST_DB]["test-sets"].count
end

def test_last_error_responses
20.times { @col.insert({:baz => "bar"}) }
response = @db.get_last_error(:w => 2, :wtimeout => 5000)
response = @db.get_last_error(:w => 3, :wtimeout => 5000)
assert response['ok'] == 1
assert response['lastOp']

@col.update({}, {:baz => "foo"})
response = @db.get_last_error(:w => 2, :wtimeout => 5000)
response = @db.get_last_error(:w => 3, :wtimeout => 5000)
assert response['ok'] == 1
assert response['lastOp']

@col.remove({})
response = @db.get_last_error(:w => 2, :wtimeout => 5000)
response = @db.get_last_error(:w => 3, :wtimeout => 5000)
assert response['ok'] == 1
assert response['n'] == 20
assert response['lastOp']
Expand Down
13 changes: 0 additions & 13 deletions test/replica_set/z_cluster_shutdown.rb

This file was deleted.

4 changes: 2 additions & 2 deletions test/tools/mongo_config.rb
Expand Up @@ -25,7 +25,7 @@ def debug(level, arg)
module Mongo
class Config
DEFAULT_BASE_OPTS = { :host => 'localhost', :dbpath => 'data', :logpath => 'data/log'}
DEFAULT_REPLICA_SET = DEFAULT_BASE_OPTS.merge( :replicas => 2, :arbiters => 1 )
DEFAULT_REPLICA_SET = DEFAULT_BASE_OPTS.merge( :replicas => 3, :arbiters => 0 )
DEFAULT_SHARDED_SIMPLE = DEFAULT_BASE_OPTS.merge( :shards => 2, :configs => 1, :routers => 4 )
DEFAULT_SHARDED_REPLICA = DEFAULT_SHARDED_SIMPLE.merge( :replicas => 3, :arbiters => 0)

Expand Down Expand Up @@ -354,7 +354,7 @@ def repl_set_get_config

def repl_set_config
members = []
@config[:replicas].each{|s| members << { :_id => s[:_id], :host => "#{s[:host]}:#{s[:port]}" } }
@config[:replicas].each{|s| members << { :_id => s[:_id], :host => "#{s[:host]}:#{s[:port]}", :tags => { :node => s[:_id].to_s } } }
@config[:arbiters].each{|s| members << { :_id => s[:_id], :host => "#{s[:host]}:#{s[:port]}", :arbiterOnly => true } }
{
:_id => @config[:replicas].first[:replSet],
Expand Down

0 comments on commit 85948f3

Please sign in to comment.