-
Notifications
You must be signed in to change notification settings - Fork 533
RUBY-932 Update Server Discovery and Monitoring code and tests #637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
estolfo
merged 18 commits into
mongodb:2.0-stable
from
estolfo:RUBY-932-duplicate-servers
Jun 22, 2015
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
695cf25
RUBY-932 Update Server Discovery and Monitoring code and tests
estolfo aa7c479
RUBY-932 Add locks around servers list access and update
estolfo d29f423
RUBY-932 Add locks around addresses list access and update
estolfo a25ccb5
RUBY-932 Consistent syntax
estolfo 4a64b12
RUBY-932 syntax and naming updates
estolfo f8cde1a
RUBY-932 Replica Set topology minor update and tests
estolfo 32744f6
RUBY-932 Add Single topology tests
estolfo c076b7a
RUBY-932 Sharded specs
estolfo c916190
RUBY-932 Description tests
estolfo 81c2d36
RUBY-932 Cluster tests
estolfo 9d16b4c
RUBY-932 Add missing Topology::Unknown tests
estolfo a86bfeb
RUBY-932 reject returns list of items for which block returns false
estolfo eeecd4c
RUBY-932 Add test for Cluster#remove
estolfo 732bc6d
RUBY-932 Return copy of servers and addresses
estolfo 45f5aac
RUBY-932 Only use one lock in cluster
estolfo fcd7b7d
RUBY-932 Rebase revert
estolfo 3f72d31
Update Server#initialize uses
estolfo 4f8b224
Update versions
estolfo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,6 @@ class Cluster | |
include Event::Subscriber | ||
include Loggable | ||
|
||
# @return [ Array<String> ] The provided seed addresses. | ||
attr_reader :addresses | ||
|
||
# @return [ Hash ] The options hash. | ||
attr_reader :options | ||
|
||
|
@@ -69,9 +66,9 @@ def add(host) | |
if !addresses.include?(address) | ||
if addition_allowed?(address) | ||
log_debug([ "Adding #{address.to_s} to the cluster." ]) | ||
addresses.push(address) | ||
@update_lock.synchronize { @addresses.push(address) } | ||
server = Server.new(address, self, event_listeners, options) | ||
@servers.push(server) | ||
@update_lock.synchronize { @servers.push(server) } | ||
server | ||
end | ||
end | ||
|
@@ -92,9 +89,9 @@ def initialize(seeds, options = {}) | |
@event_listeners = Event::Listeners.new | ||
@options = options.freeze | ||
@topology = Topology.initial(seeds, options) | ||
@update_lock = Mutex.new | ||
|
||
subscribe_to(Event::SERVER_ADDED, Event::ServerAdded.new(self)) | ||
subscribe_to(Event::SERVER_REMOVED, Event::ServerRemoved.new(self)) | ||
subscribe_to(Event::DESCRIPTION_CHANGED, Event::DescriptionChanged.new(self)) | ||
subscribe_to(Event::PRIMARY_ELECTED, Event::PrimaryElected.new(self)) | ||
|
||
seeds.each{ |seed| add(seed) } | ||
|
@@ -136,10 +133,10 @@ def next_primary | |
# | ||
# @since 2.0.0 | ||
def elect_primary!(description) | ||
@topology = topology.elect_primary(description, @servers) | ||
@topology = topology.elect_primary(description, servers_list) | ||
end | ||
|
||
# Removed the server from the cluster for the provided address, if it | ||
# Remove the server from the cluster for the provided address, if it | ||
# exists. | ||
# | ||
# @example Remove the server from the cluster. | ||
|
@@ -151,9 +148,10 @@ def elect_primary!(description) | |
def remove(host) | ||
log_debug([ "#{host} being removed from the cluster." ]) | ||
address = Address.new(host) | ||
removed_servers = @servers.reject!{ |server| server.address == address } | ||
removed_servers = @servers.select { |s| s.address == address } | ||
@update_lock.synchronize { @servers = @servers - removed_servers } | ||
removed_servers.each{ |server| server.disconnect! } if removed_servers | ||
addresses.reject!{ |addr| addr == address } | ||
@update_lock.synchronize { @addresses.reject! { |addr| addr == address } } | ||
end | ||
|
||
# Force a scan of all known servers in the cluster. | ||
|
@@ -168,7 +166,7 @@ def remove(host) | |
# | ||
# @since 2.0.0 | ||
def scan! | ||
@servers.each{ |server| server.scan! } and true | ||
servers_list.each{ |server| server.scan! } and true | ||
end | ||
|
||
# Get a list of server candidates from the cluster that can have operations | ||
|
@@ -181,7 +179,37 @@ def scan! | |
# | ||
# @since 2.0.0 | ||
def servers | ||
topology.servers(@servers.compact).compact | ||
topology.servers(servers_list.compact).compact | ||
end | ||
|
||
# Add hosts in a description to the cluster. | ||
# | ||
# @example Add hosts in a description to the cluster. | ||
# cluster.add_hosts(description) | ||
# | ||
# @param [ Mongo::Server::Description ] description The description. | ||
# | ||
# @since 2.0.6 | ||
def add_hosts(description) | ||
if topology.add_hosts?(description, servers_list) | ||
description.servers.each { |s| add(s) } | ||
end | ||
end | ||
|
||
# Remove hosts in a description from the cluster. | ||
# | ||
# @example Remove hosts in a description from the cluster. | ||
# cluster.remove_hosts(description) | ||
# | ||
# @param [ Mongo::Server::Description ] description The description. | ||
# | ||
# @since 2.0.6 | ||
def remove_hosts(description) | ||
if topology.remove_hosts?(description) | ||
servers_list.each do |s| | ||
remove(s.address.to_s) if topology.remove_server?(description, s) | ||
end | ||
end | ||
end | ||
|
||
# Create a cluster for the provided client, for use when we don't want the | ||
|
@@ -202,6 +230,18 @@ def self.create(client) | |
client.instance_variable_set(:@cluster, cluster) | ||
end | ||
|
||
# The addresses in the cluster. | ||
# | ||
# @example Get the addresses in the cluster. | ||
# cluster.addresses | ||
# | ||
# @return [ Array<Mongo::Address> ] The addresses. | ||
# | ||
# @since 2.0.6 | ||
def addresses | ||
addresses_list | ||
end | ||
|
||
private | ||
|
||
def direct_connection?(address) | ||
|
@@ -211,5 +251,21 @@ def direct_connection?(address) | |
def addition_allowed?(address) | ||
[email protected]? || direct_connection?(address) | ||
end | ||
|
||
def servers_list | ||
@update_lock.synchronize do | ||
@servers.reduce([]) do |servers, server| | ||
servers << server | ||
end | ||
end | ||
end | ||
|
||
def addresses_list | ||
@update_lock.synchronize do | ||
@addresses.reduce([]) do |addresses, address| | ||
addresses << address | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this return a copy of the list? Doesn't it need to return a copy in order to avoid overlapping with a thread that's updating the servers while holding the servers_update lock? E.g. it seems like if one thread is in scan! and slowly iterating the list while another thread is in remove_hosts, removing servers while holding the lock, you can still get a runtime err.