Skip to content
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

Add ability to specify multiple hosts in sunspot.yml as an array (fixes #608) #796

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,33 @@ test:
log_level: WARNING
```

### Solr Cloud

You can specify an array of nodes, so if you need to reboot a node later you can do this safely, without interruption of your search service. Sunspot will catch the error and try another node. Only if all nodes failed you will receive an error.

```yaml
development:
solr:
hosts:
- localhost:8080/solr/crm-prod
- localhost:8081/solr/crm-prod
- localhost:8090/solr/crm-prod
- localhost:8091/solr/crm-prod
```

You can specify `master_hosts` same way:

```yaml
development:
solr:
hosts:
- localhost:8080/solr/crm-prod
- localhost:8081/solr/crm-prod
master_hosts:
- localhost:8090/solr/crm-prod
- localhost:8091/solr/crm-prod
```

You may want to use SSL for production environments with a username and password. For example, set `SOLR_URL` to `https://username:[email protected]/solr`.

You can examine the value of `Sunspot::Rails.configuration` at runtime.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
production:
solr:
hostname: localhost
port: 8983
hosts:
- 'localhost:8983/solr/production'
log_level: WARNING
path: /solr/production
# read_timeout: 2
# open_timeout: 0.5

development:
solr:
hostname: localhost
port: 8982
hosts:
- 'localhost:8982/solr/development'
log_level: INFO
path: /solr/development

test:
solr:
hostname: localhost
port: 8981
hosts:
- 'localhost:8981/solr/test'
log_level: WARNING
path: /solr/test

54 changes: 34 additions & 20 deletions sunspot_rails/lib/sunspot/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,44 @@ def build_session(configuration = self.configuration)
private

def master_config(sunspot_rails_configuration)
config = Sunspot::Configuration.build
builder = sunspot_rails_configuration.scheme == 'http' ? URI::HTTP : URI::HTTPS
config.solr.url = builder.build(
:host => sunspot_rails_configuration.master_hostname,
:port => sunspot_rails_configuration.master_port,
:path => sunspot_rails_configuration.master_path,
:userinfo => sunspot_rails_configuration.userinfo
).to_s
config.solr.read_timeout = sunspot_rails_configuration.read_timeout
config.solr.open_timeout = sunspot_rails_configuration.open_timeout
config.solr.proxy = sunspot_rails_configuration.proxy
config.solr.update_format = sunspot_rails_configuration.update_format
config
build_config(sunspot_rails_configuration) do |config|
config.solr.url = if sunspot_rails_configuration.master_hosts.empty?
builder = sunspot_rails_configuration.scheme == 'http' ? URI::HTTP : URI::HTTPS

builder.build(
:host => sunspot_rails_configuration.master_hostname,
:port => sunspot_rails_configuration.master_port,
:path => sunspot_rails_configuration.master_path,
:userinfo => sunspot_rails_configuration.userinfo
).to_s
else
sunspot_rails_configuration.hosts
end
end
end

def slave_config(sunspot_rails_configuration)
build_config(sunspot_rails_configuration) do |config|
config.solr.url = if sunspot_rails_configuration.hosts.empty?
builder = sunspot_rails_configuration.scheme == 'http' ? URI::HTTP : URI::HTTPS

builder.build(
:host => sunspot_rails_configuration.hostname,
:port => sunspot_rails_configuration.port,
:path => sunspot_rails_configuration.path,
:userinfo => sunspot_rails_configuration.userinfo
).to_s
else
sunspot_rails_configuration.hosts
end
end
end

def build_config(sunspot_rails_configuration)
config = Sunspot::Configuration.build
builder = sunspot_rails_configuration.scheme == 'http' ? URI::HTTP : URI::HTTPS
config.solr.url = builder.build(
:host => sunspot_rails_configuration.hostname,
:port => sunspot_rails_configuration.port,
:path => sunspot_rails_configuration.path,
:userinfo => sunspot_rails_configuration.userinfo
).to_s

yield config

config.solr.read_timeout = sunspot_rails_configuration.read_timeout
config.solr.open_timeout = sunspot_rails_configuration.open_timeout
config.solr.proxy = sunspot_rails_configuration.proxy
Expand Down
35 changes: 34 additions & 1 deletion sunspot_rails/lib/sunspot/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,25 @@ class Configuration
# ActiveSupport log levels are integers; this array maps them to the
# appropriate java.util.logging.Level constant
LOG_LEVELS = %w(FINE INFO WARNING SEVERE SEVERE INFO)
SCHEME_EXP = /\Ahttps?:\/\//

attr_writer :user_configuration
#
# The host names array at which to connect to Solr.
#
# ==== Returns
#
# Array:: hosts
#
def hosts
unless defined?(@hosts)
@hosts = [user_configuration_from_key('solr', 'hosts')].flatten.compact.
map { |host| host.prepend("#{scheme}://") unless host[SCHEME_EXP] }
end

@hosts
end

#
# The host name at which to connect to Solr. Default 'localhost'.
#
Expand Down Expand Up @@ -147,6 +164,22 @@ def path
@path
end

#
# The host names array at which to connect to the master Solr instances.
#
# ==== Returns
#
# Array:: master_hosts
#
def master_hosts
unless defined?(@master_hosts)
@master_hosts = [user_configuration_from_key('solr', 'master_hosts')].flatten.compact.
map { |host| host.prepend("#{scheme}://") unless host[SCHEME_EXP] }
end

@master_hosts
end

#
# The host name at which to connect to the master Solr instance. Defaults
# to the 'hostname' configuration option.
Expand Down Expand Up @@ -191,7 +224,7 @@ def master_path
# Boolean:: bool
#
def has_master?
@has_master = !!user_configuration_from_key('master_solr')
@has_master = !!user_configuration_from_key('master_solr') || !!user_configuration_from_key('master_hosts')
end

#
Expand Down
16 changes: 16 additions & 0 deletions sunspot_rails/spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
end
end

it "should set hosts to an empty array when not set" do
@config.hosts.should eq []
end

it "should set master_hosts to an empty array when not set" do
@config.master_hosts.should eq []
end

it "should set the read timeout to nil when not set" do
expect(@config.read_timeout).to be_nil
end
Expand Down Expand Up @@ -111,6 +119,14 @@
@config = Sunspot::Rails::Configuration.new
end

it "should parse hosts array" do
@config.hosts.should eq ['http://localhost:8081/solr/test', 'http://localhost:8091/solr/test']
end

it "should parse master_hosts array" do
@config.master_hosts.should eq ['http://localhost:8080/solr/test', 'http://localhost:8090/solr/test']
end

it "should handle the 'scheme' property when set" do
expect(@config.scheme).to eq("http")
end
Expand Down
6 changes: 6 additions & 0 deletions sunspot_rails/spec/rails_app/config/sunspot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ development:
port: 8981
config_test:
solr:
hosts:
- 'localhost:8081/solr/test'
- 'localhost:8091/solr/test'
master_hosts:
- 'localhost:8080/solr/test'
- 'localhost:8090/solr/test'
scheme: http
user: user
pass: pass
Expand Down