checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 966f35e6906e7a295a44bc67ff66f68cb1bf49d9
4
+ data.tar.gz: 81d5df04acb5ba7d857b29cf12b3b5ed79ec068c
5
+ SHA512:
6
+ metadata.gz: 03c026b86a0a8b45cbd398a32934f0496a08c51d2b0a02fa4492a96c664e1cbc099eb630db416611dd290ffd04dbd71cdccfdd4b06b34320845aef894f31237c
7
+ data.tar.gz: 69ab1920f310843110d73e528be2fdb6435c58903139ac0f424a2444fa91c0844d56d76df5fb54c322aa43f8666e476d4fc0f6dc5126fdaf70354fb2ecf0fbc9
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /zookeeper/
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-02-10 13:14:48 +0900 using RuboCop version 0.37.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ Metrics/LineLength:
10
+ Max: 100
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rsolr-cloud.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kimura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # RSolr::Cloud
2
+
3
+ A RSolr's connection adopter supporting SolrCloud.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rsolr-cloud'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rsolr-cloud
20
+
21
+ ## Example
22
+
23
+ ```ruby
24
+ require 'zk'
25
+ require 'rsolr/cloud'
26
+
27
+ # Create Zookeeper client for the Zookeeper ensemble in SolrCloud.
28
+ zk = ZK.new('localhost:2181,localhost:2182,localhost:2183')
29
+
30
+ # Connecting the SolrCloud through the Zookeeper.
31
+ cloud_connection = RSolr::Cloud::Connection.new(zk)
32
+
33
+ # Get rsolr client for solr_cloud.
34
+ solr_client = RSolr::Client.new(cloud_connection,
35
+ read_timeout: 60,
36
+ open_timeout: 60)
37
+
38
+ # You can use rsolr as usual but :collection option must be specified with the name of the collection.
39
+ response = solr.get('select', collection: 'collection1', params: {q: '*:*'})
40
+
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/rsolr-cloud/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
50
+
51
+ ## Development
52
+
53
+ To install gems which are necessary for development and testing:
54
+
55
+ ```
56
+ $ bundle install
57
+ ```
58
+
59
+ To run the test suite:
60
+
61
+ ```
62
+ $ rake
63
+ ```
64
+
65
+ The default rake task contains RuboCop and RSpec. Each task can be run separately:
66
+
67
+ ```
68
+ $ rake rubocop
69
+ ```
70
+ ```
71
+ $ rake spec
72
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ require 'rubocop/rake_task'
7
+ RuboCop::RakeTask.new
8
+ task default: %i[rubocop spec]
9
+ rescue LoadError # rubocop:disable HandleExceptions
10
+ end
data/lib/rsolr/cloud.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'rsolr/cloud/error'
2
+ require 'rsolr/cloud/connection'
3
+ require 'rsolr/cloud/version'
data/lib/rsolr/cloud/connection.rb ADDED
@@ -0,0 +1,136 @@
1
+ module RSolr
2
+ # rubocop:disable Metrics/ClassLength
3
+ module Cloud
4
+ # RSolr connection adapter for SolrCloud
5
+ class Connection < RSolr::Connection
6
+ include MonitorMixin
7
+
8
+ ZNODE_LIVE_NODES = '/live_nodes'.freeze
9
+ ZNODE_COLLECTIONS = '/collections'.freeze
10
+
11
+ def initialize(zk)
12
+ super()
13
+ @zk = zk
14
+ init_live_node_watcher
15
+ init_collections_watcher
16
+ update_urls
17
+ end
18
+
19
+ def execute(client, request_context)
20
+ collection_name = request_context[:collection]
21
+ raise 'The :collection option must be specified.' unless collection_name
22
+ path = request_context[:path].to_s
23
+ query = request_context[:query]
24
+ query = query ? "?#{query}" : ''
25
+ url = select_node(collection_name, path == 'update')
26
+ raise RSolr::Cloud::Error::NotEnoughNodes unless url
27
+ request_context[:uri] = RSolr::Uri.create(url).merge(path + query)
28
+ super(client, request_context)
29
+ end
30
+
31
+ private
32
+
33
+ def select_node(collection, leader_only = false)
34
+ if leader_only
35
+ synchronize { @leader_urls[collection].to_a.sample }
36
+ else
37
+ synchronize { @all_urls[collection].to_a.sample }
38
+ end
39
+ end
40
+
41
+ def init_live_node_watcher
42
+ @zk.register(ZNODE_LIVE_NODES) do
43
+ update_live_nodes
44
+ update_urls
45
+ end
46
+ update_live_nodes
47
+ end
48
+
49
+ def init_collections_watcher
50
+ @zk.register(ZNODE_COLLECTIONS) do
51
+ update_collections
52
+ update_urls
53
+ end
54
+ update_collections
55
+ end
56
+
57
+ def init_collection_state_watcher(collection)
58
+ @zk.register(collection_state_znode_path(collection)) do
59
+ update_collection_state(collection)
60
+ update_urls
61
+ end
62
+ update_collection_state(collection)
63
+ end
64
+
65
+ def update_urls
66
+ synchronize do
67
+ @all_urls = {}
68
+ @leader_urls = {}
69
+ @collections.each do |name, state|
70
+ @all_urls[name], @leader_urls[name] = available_urls(name, state)
71
+ end
72
+ end
73
+ end
74
+
75
+ def update_live_nodes
76
+ synchronize do
77
+ @live_nodes = {}
78
+ @zk.children(ZNODE_LIVE_NODES, watch: true).each do |node|
79
+ @live_nodes[node] = true
80
+ end
81
+ end
82
+ end
83
+
84
+ def update_collections
85
+ collections = @zk.children(ZNODE_COLLECTIONS, watch: true)
86
+ created = []
87
+ synchronize do
88
+ @collections ||= {}
89
+ deleted = @collections.keys - collections
90
+ created = collections - @collections.keys
91
+ deleted.each { |collection| @collections.delete(collection) }
92
+ end
93
+ created.each { |collection| init_collection_state_watcher(collection) }
94
+ end
95
+
96
+ def update_collection_state(collection)
97
+ synchronize do
98
+ collection_state_json, _stat =
99
+ @zk.get(collection_state_znode_path(collection), watch: true)
100
+ @collections.merge!(JSON.parse(collection_state_json))
101
+ end
102
+ end
103
+
104
+ def available_urls(collection_name, collection_state)
105
+ leader_urls = []
106
+ all_urls = []
107
+ all_nodes(collection_state).each do |node|
108
+ next unless active_node?(node)
109
+ url = "#{node['base_url']}/#{collection_name}"
110
+ leader_urls << url if leader_node?(node)
111
+ all_urls << url
112
+ end
113
+ [all_urls, leader_urls]
114
+ end
115
+
116
+ def all_nodes(collection_state)
117
+ nodes = collection_state['shards'].values.map do |shard|
118
+ shard['replicas'].values
119
+ end
120
+ nodes.flatten
121
+ end
122
+
123
+ def collection_state_znode_path(collection_name)
124
+ "/collections/#{collection_name}/state.json"
125
+ end
126
+
127
+ def active_node?(node)
128
+ @live_nodes[node['node_name']] && node['state'] == 'active'
129
+ end
130
+
131
+ def leader_node?(node)
132
+ node['leader'] == 'true'
133
+ end
134
+ end
135
+ end
136
+ end
data/lib/rsolr/cloud/error.rb ADDED
@@ -0,0 +1,12 @@
1
+ module RSolr
2
+ module Cloud
3
+ module Error
4
+ # This error occurs when all solr nodes aren't active.
5
+ class NotEnoughNodes < RuntimeError
6
+ def to_s
7
+ 'Not enough nodes to handle the request.'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/rsolr/cloud/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Rsolr
2
+ module Cloud
3
+ VERSION = '1.1.1'.freeze
4
+ end
5
+ end
data/rsolr-cloud.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'rsolr/cloud/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'rsolr-cloud'
9
+ spec.version = Rsolr::Cloud::VERSION
10
+ spec.authors = ['Shintaro Kimura']
11
+ spec.email = ['service@enigmo.co.jp']
12
+ spec.summary = 'The connection adopter supporting SolrCloud for RSolr'
13
+ spec.description = 'The connection adopter supporting SolrCloud for RSolr'
14
+ spec.homepage = ''
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.3'
25
+ spec.add_development_dependency 'activesupport', '~> 4.2'
26
+ spec.add_development_dependency 'zk-server', '~> 1.1.7'
27
+ spec.add_development_dependency 'zk', '~> 1.9.5'
28
+ spec.add_development_dependency 'rsolr', '~> 1.0.12'
29
+ spec.add_development_dependency 'rubocop', '~> 0.49.0'
30
+ end
data/spec/files/collection1_all_nodes_alive.json ADDED
@@ -0,0 +1,36 @@
1
+ {"collection1":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection1_shard1_replica1",
10
+ "base_url":"http://192.168.1.23:8983/solr",
11
+ "node_name":"192.168.1.23:8983_solr",
12
+ "state":"active"},
13
+ "core_node2":{
14
+ "core":"collection1_shard1_replica2",
15
+ "base_url":"http://192.168.1.22:8983/solr",
16
+ "node_name":"192.168.1.22:8983_solr",
17
+ "state":"active",
18
+ "leader":"true"}}},
19
+ "shard2":{
20
+ "range":"0-7fffffff",
21
+ "state":"active",
22
+ "replicas":{
23
+ "core_node3":{
24
+ "core":"collection1_shard2_replica1",
25
+ "base_url":"http://192.168.1.24:8983/solr",
26
+ "node_name":"192.168.1.24:8983_solr",
27
+ "state":"active",
28
+ "leader":"true"},
29
+ "core_node4":{
30
+ "core":"collection1_shard2_replica2",
31
+ "base_url":"http://192.168.1.21:8983/solr",
32
+ "node_name":"192.168.1.21:8983_solr",
33
+ "state":"active"}}}},
34
+ "router":{"name":"compositeId"},
35
+ "maxShardsPerNode":"1",
36
+ "autoAddReplicas":"false"}}
data/spec/files/collection1_leader_down.json ADDED
@@ -0,0 +1,36 @@
1
+ {"collection1":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection1_shard1_replica1",
10
+ "base_url":"http://192.168.1.23:8983/solr",
11
+ "node_name":"192.168.1.23:8983_solr",
12
+ "state":"active",
13
+ "leader":"true"},
14
+ "core_node2":{
15
+ "core":"collection1_shard1_replica2",
16
+ "base_url":"http://192.168.1.22:8983/solr",
17
+ "node_name":"192.168.1.22:8983_solr",
18
+ "state":"down"}}},
19
+ "shard2":{
20
+ "range":"0-7fffffff",
21
+ "state":"active",
22
+ "replicas":{
23
+ "core_node3":{
24
+ "core":"collection1_shard2_replica1",
25
+ "base_url":"http://192.168.1.24:8983/solr",
26
+ "node_name":"192.168.1.24:8983_solr",
27
+ "state":"active",
28
+ "leader":"true"},
29
+ "core_node4":{
30
+ "core":"collection1_shard2_replica2",
31
+ "base_url":"http://192.168.1.21:8983/solr",
32
+ "node_name":"192.168.1.21:8983_solr",
33
+ "state":"active"}}}},
34
+ "router":{"name":"compositeId"},
35
+ "maxShardsPerNode":"1",
36
+ "autoAddReplicas":"false"}}
data/spec/files/collection1_leader_node_recovered.json ADDED
@@ -0,0 +1,36 @@
1
+ {"collection1":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection1_shard1_replica1",
10
+ "base_url":"http://192.168.1.23:8983/solr",
11
+ "node_name":"192.168.1.23:8983_solr",
12
+ "state":"active",
13
+ "leader":"true"},
14
+ "core_node2":{
15
+ "core":"collection1_shard1_replica2",
16
+ "base_url":"http://192.168.1.22:8983/solr",
17
+ "node_name":"192.168.1.22:8983_solr",
18
+ "state":"active"}}},
19
+ "shard2":{
20
+ "range":"0-7fffffff",
21
+ "state":"active",
22
+ "replicas":{
23
+ "core_node3":{
24
+ "core":"collection1_shard2_replica1",
25
+ "base_url":"http://192.168.1.24:8983/solr",
26
+ "node_name":"192.168.1.24:8983_solr",
27
+ "state":"active",
28
+ "leader":"true"},
29
+ "core_node4":{
30
+ "core":"collection1_shard2_replica2",
31
+ "base_url":"http://192.168.1.21:8983/solr",
32
+ "node_name":"192.168.1.21:8983_solr",
33
+ "state":"active"}}}},
34
+ "router":{"name":"compositeId"},
35
+ "maxShardsPerNode":"1",
36
+ "autoAddReplicas":"false"}}
data/spec/files/collection1_leader_recovering.json ADDED
@@ -0,0 +1,36 @@
1
+ {"collection1":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection1_shard1_replica1",
10
+ "base_url":"http://192.168.1.23:8983/solr",
11
+ "node_name":"192.168.1.23:8983_solr",
12
+ "state":"active",
13
+ "leader":"true"},
14
+ "core_node2":{
15
+ "core":"collection1_shard1_replica2",
16
+ "base_url":"http://192.168.1.22:8983/solr",
17
+ "node_name":"192.168.1.22:8983_solr",
18
+ "state":"recovering"}}},
19
+ "shard2":{
20
+ "range":"0-7fffffff",
21
+ "state":"active",
22
+ "replicas":{
23
+ "core_node3":{
24
+ "core":"collection1_shard2_replica1",
25
+ "base_url":"http://192.168.1.24:8983/solr",
26
+ "node_name":"192.168.1.24:8983_solr",
27
+ "state":"active",
28
+ "leader":"true"},
29
+ "core_node4":{
30
+ "core":"collection1_shard2_replica2",
31
+ "base_url":"http://192.168.1.21:8983/solr",
32
+ "node_name":"192.168.1.21:8983_solr",
33
+ "state":"active"}}}},
34
+ "router":{"name":"compositeId"},
35
+ "maxShardsPerNode":"1",
36
+ "autoAddReplicas":"false"}}
data/spec/files/collection1_replica_down.json ADDED
@@ -0,0 +1,36 @@
1
+ {"collection1":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection1_shard1_replica1",
10
+ "base_url":"http://192.168.1.23:8983/solr",
11
+ "node_name":"192.168.1.23:8983_solr",
12
+ "state":"active"},
13
+ "core_node2":{
14
+ "core":"collection1_shard1_replica2",
15
+ "base_url":"http://192.168.1.22:8983/solr",
16
+ "node_name":"192.168.1.22:8983_solr",
17
+ "state":"active",
18
+ "leader":"true"}}},
19
+ "shard2":{
20
+ "range":"0-7fffffff",
21
+ "state":"active",
22
+ "replicas":{
23
+ "core_node3":{
24
+ "core":"collection1_shard2_replica1",
25
+ "base_url":"http://192.168.1.24:8983/solr",
26
+ "node_name":"192.168.1.24:8983_solr",
27
+ "state":"active",
28
+ "leader":"true"},
29
+ "core_node4":{
30
+ "core":"collection1_shard2_replica2",
31
+ "base_url":"http://192.168.1.21:8983/solr",
32
+ "node_name":"192.168.1.21:8983_solr",
33
+ "state":"down"}}}},
34
+ "router":{"name":"compositeId"},
35
+ "maxShardsPerNode":"1",
36
+ "autoAddReplicas":"false"}}
data/spec/files/collection2_all_nodes_alive.json ADDED
@@ -0,0 +1,21 @@
1
+ {"collection2":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection2_shard1_replica1",
10
+ "base_url":"http://192.168.1.23:8983/solr",
11
+ "node_name":"192.168.1.23:8983_solr",
12
+ "state":"recoveryng"},
13
+ "core_node2":{
14
+ "core":"collection2_shard1_replica2",
15
+ "base_url":"http://192.168.1.22:8983/solr",
16
+ "node_name":"192.168.1.22:8983_solr",
17
+ "state":"active",
18
+ "leader":"true"}}}},
19
+ "router":{"name":"compositeId"},
20
+ "maxShardsPerNode":"1",
21
+ "autoAddReplicas":"false"}}
data/spec/files/collection3_all_nodes_alive.json ADDED
@@ -0,0 +1,21 @@
1
+ {"collection3":{
2
+ "replicationFactor":"2",
3
+ "shards":{
4
+ "shard1":{
5
+ "range":"80000000-ffffffff",
6
+ "state":"active",
7
+ "replicas":{
8
+ "core_node1":{
9
+ "core":"collection3_shard1_replica1",
10
+ "base_url":"http://192.168.1.21:8983/solr",
11
+ "node_name":"192.168.1.21:8983_solr",
12
+ "state":"active"},
13
+ "core_node2":{
14
+ "core":"collection3_shard1_replica2",
15
+ "base_url":"http://192.168.1.24:8983/solr",
16
+ "node_name":"192.168.1.24:8983_solr",
17
+ "state":"active",
18
+ "leader":"true"}}}},
19
+ "router":{"name":"compositeId"},
20
+ "maxShardsPerNode":"1",
21
+ "autoAddReplicas":"false"}}
data/spec/rsolr/cloud/connection_spec.rb ADDED
@@ -0,0 +1,192 @@
1
+ require 'spec_helper.rb'
2
+
3
+ # rubocop:disable Metrics/BlockLength
4
+ RSpec.describe RSolr::Cloud::Connection do
5
+ before do
6
+ @zk_in_solr = ZK.new
7
+ delete_with_children(@zk_in_solr, '/live_nodes')
8
+ delete_with_children(@zk_in_solr, '/collections')
9
+ wait_until(10) do
10
+ !@zk_in_solr.exists?('/live_nodes')
11
+ end
12
+ wait_until(10) do
13
+ !@zk_in_solr.exists?('/collections')
14
+ end
15
+ @zk_in_solr.create('/live_nodes')
16
+ @zk_in_solr.create('/collections')
17
+
18
+ ['192.168.1.21:8983_solr',
19
+ '192.168.1.22:8983_solr',
20
+ '192.168.1.23:8983_solr',
21
+ '192.168.1.24:8983_solr'].each do |node|
22
+ @zk_in_solr.create("/live_nodes/#{node}", '', mode: :ephemeral)
23
+ end
24
+ %w[collection1 collection2].each do |collection|
25
+ @zk_in_solr.create("/collections/#{collection}")
26
+ json = File.read("spec/files/#{collection}_all_nodes_alive.json")
27
+ @zk_in_solr.create("/collections/#{collection}/state.json",
28
+ json,
29
+ mode: :ephemeral)
30
+ end
31
+ @zk = ZK.new
32
+ @subject = RSolr::Cloud::Connection.new @zk
33
+ end
34
+
35
+ let(:client) { double.as_null_object }
36
+
37
+ let(:http) { double(Net::HTTP).as_null_object }
38
+
39
+ it 'should configure Net::HTTP with one of active node in select request.' do
40
+ expect(@subject.instance_variable_get(:@leader_urls)['collection1'].sort).to eq(
41
+ ['http://192.168.1.22:8983/solr/collection1',
42
+ 'http://192.168.1.24:8983/solr/collection1'].sort
43
+ )
44
+ expect(@subject.instance_variable_get(:@all_urls)['collection1'].sort).to eq(
45
+ ['http://192.168.1.21:8983/solr/collection1',
46
+ 'http://192.168.1.22:8983/solr/collection1',
47
+ 'http://192.168.1.23:8983/solr/collection1',
48
+ 'http://192.168.1.24:8983/solr/collection1'].sort
49
+ )
50
+ expect(Net::HTTP).to receive(:new) do |host, port|
51
+ expect(host).to be_one_of(['192.168.1.21', '192.168.1.22', '192.168.1.23', '192.168.1.24'])
52
+ expect(port).to eq(8983)
53
+ http
54
+ end
55
+ expect(http).to receive(:request) do |request|
56
+ expect(request.path).to eq('/solr/collection1/select?q=*:*')
57
+ double.as_null_object
58
+ end
59
+ @subject.execute client, collection: 'collection1', method: :get, path: 'select', query: 'q=*:*'
60
+ end
61
+
62
+ it 'should configure Net::HTTP with one of leader node in update request' do
63
+ expect(@subject.instance_variable_get(:@leader_urls)['collection1'].sort).to eq(
64
+ ['http://192.168.1.22:8983/solr/collection1',
65
+ 'http://192.168.1.24:8983/solr/collection1'].sort
66
+ )
67
+ expect(@subject.instance_variable_get(:@all_urls)['collection1'].sort).to eq(
68
+ ['http://192.168.1.21:8983/solr/collection1',
69
+ 'http://192.168.1.22:8983/solr/collection1',
70
+ 'http://192.168.1.23:8983/solr/collection1',
71
+ 'http://192.168.1.24:8983/solr/collection1'].sort
72
+ )
73
+ expect(Net::HTTP).to receive(:new) do |host, port|
74
+ expect(host).to be_one_of(['192.168.1.22', '192.168.1.24'])
75
+ expect(port).to eq(8983)
76
+ http
77
+ end
78
+ expect(http).to receive(:request) do |request|
79
+ expect(request.path).to eq('/solr/collection1/update')
80
+ expect(request.body).to eq('the data')
81
+ double.as_null_object
82
+ end
83
+ @subject.execute client, collection: 'collection1',
84
+ method: :post,
85
+ path: 'update',
86
+ data: 'the data'
87
+ end
88
+
89
+ it 'should remove downed replica node and add recovered node' do
90
+ @zk_in_solr.delete('/live_nodes/192.168.1.21:8983_solr')
91
+ @zk_in_solr.set('/collections/collection1/state.json',
92
+ File.read('spec/files/collection1_replica_down.json'))
93
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
94
+ ['http://192.168.1.22:8983/solr/collection1',
95
+ 'http://192.168.1.24:8983/solr/collection1'].sort
96
+ )
97
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
98
+ ['http://192.168.1.22:8983/solr/collection1',
99
+ 'http://192.168.1.23:8983/solr/collection1',
100
+ 'http://192.168.1.24:8983/solr/collection1'].sort
101
+ )
102
+ @zk_in_solr.create('/live_nodes/192.168.1.21:8983_solr', mode: :ephemeral)
103
+ @zk_in_solr.set('/collections/collection1/state.json',
104
+ File.read('spec/files/collection1_all_nodes_alive.json'))
105
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
106
+ ['http://192.168.1.22:8983/solr/collection1',
107
+ 'http://192.168.1.24:8983/solr/collection1'].sort
108
+ )
109
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
110
+ ['http://192.168.1.21:8983/solr/collection1',
111
+ 'http://192.168.1.22:8983/solr/collection1',
112
+ 'http://192.168.1.23:8983/solr/collection1',
113
+ 'http://192.168.1.24:8983/solr/collection1'].sort
114
+ )
115
+ end
116
+
117
+ it 'should remove downed leader node and add recovered node' do
118
+ @zk_in_solr.delete('/live_nodes/192.168.1.22:8983_solr')
119
+ @zk_in_solr.set('/collections/collection1/state.json',
120
+ File.read('spec/files/collection1_leader_down.json'))
121
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
122
+ ['http://192.168.1.23:8983/solr/collection1',
123
+ 'http://192.168.1.24:8983/solr/collection1'].sort
124
+ )
125
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
126
+ ['http://192.168.1.21:8983/solr/collection1',
127
+ 'http://192.168.1.23:8983/solr/collection1',
128
+ 'http://192.168.1.24:8983/solr/collection1'].sort
129
+ )
130
+ @zk_in_solr.create('/live_nodes/192.168.1.22:8983_solr', mode: :ephemeral)
131
+ @zk_in_solr.set('/collections/collection1/state.json',
132
+ File.read('spec/files/collection1_all_nodes_alive.json'))
133
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
134
+ ['http://192.168.1.22:8983/solr/collection1',
135
+ 'http://192.168.1.24:8983/solr/collection1'].sort
136
+ )
137
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
138
+ ['http://192.168.1.21:8983/solr/collection1',
139
+ 'http://192.168.1.22:8983/solr/collection1',
140
+ 'http://192.168.1.23:8983/solr/collection1',
141
+ 'http://192.168.1.24:8983/solr/collection1'].sort
142
+ )
143
+ end
144
+
145
+ it 'should remove recovering leader node and add recovered node' do
146
+ @zk_in_solr.set('/collections/collection1/state.json',
147
+ File.read('spec/files/collection1_leader_recovering.json'))
148
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
149
+ ['http://192.168.1.23:8983/solr/collection1',
150
+ 'http://192.168.1.24:8983/solr/collection1'].sort
151
+ )
152
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
153
+ ['http://192.168.1.21:8983/solr/collection1',
154
+ 'http://192.168.1.23:8983/solr/collection1',
155
+ 'http://192.168.1.24:8983/solr/collection1'].sort
156
+ )
157
+ @zk_in_solr.set('/collections/collection1/state.json',
158
+ File.read('spec/files/collection1_all_nodes_alive.json'))
159
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
160
+ ['http://192.168.1.23:8983/solr/collection1',
161
+ 'http://192.168.1.24:8983/solr/collection1'].sort
162
+ )
163
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
164
+ ['http://192.168.1.21:8983/solr/collection1',
165
+ 'http://192.168.1.22:8983/solr/collection1',
166
+ 'http://192.168.1.23:8983/solr/collection1',
167
+ 'http://192.168.1.24:8983/solr/collection1'].sort
168
+ )
169
+ end
170
+
171
+ it 'should add new created collection.' do
172
+ @zk_in_solr.create('/collections/collection3')
173
+ @zk_in_solr.create('/collections/collection3/state.json',
174
+ File.read('spec/files/collection3_all_nodes_alive.json'))
175
+ expect { @subject.instance_variable_get(:@leader_urls)['collection3'].to_a.sort }
176
+ .to become_soon(['http://192.168.1.24:8983/solr/collection3'])
177
+ expect { @subject.instance_variable_get(:@all_urls)['collection3'].to_a.sort }
178
+ .to become_soon(['http://192.168.1.21:8983/solr/collection3',
179
+ 'http://192.168.1.24:8983/solr/collection3'].sort)
180
+ end
181
+
182
+ it 'should remove deleted collection.' do
183
+ delete_with_children(@zk_in_solr, '/collections/collection2')
184
+ expect { @subject.instance_variable_get(:@leader_urls).keys }.to become_soon(['collection1'])
185
+ expect { @subject.instance_variable_get(:@all_urls).keys }.to become_soon(['collection1'])
186
+ end
187
+
188
+ after do
189
+ @zk_in_solr.close if @zk_in_solr
190
+ @zk.close if @zk
191
+ end
192
+ end # rubocop:enable Metrics/BlockLength
data/spec/spec_helper.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'rsolr'
2
+ require 'rsolr/cloud'
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require 'zk'
6
+ require 'zk-server'
7
+ require 'rspec/expectations'
8
+
9
+ module Helpers
10
+ def delete_with_children(zk, path)
11
+ zk.children(path).each do |node|
12
+ delete_with_children(zk, File.join(path, node))
13
+ end
14
+ zk.delete(path)
15
+ rescue ZK::Exceptions::NoNode # rubocop:disable HandleExceptions
16
+ # don't care if it already exists or not.
17
+ end
18
+
19
+ def wait_until(timeout = 10)
20
+ started_on = Time.now
21
+ result = false
22
+ loop do
23
+ result = yield
24
+ break if result || started_on < timeout.second.ago
25
+ Thread.pass
26
+ end
27
+ raise 'Timed out' unless result
28
+ end
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ config.include Helpers
33
+ config.before(:suite) do
34
+ ZK::Server.run do |c|
35
+ c.force_sync = false
36
+ end
37
+ end
38
+ config.after(:suite) do
39
+ ZK::Server.server.clobber!
40
+ end
41
+ end
42
+
43
+ RSpec::Matchers.define :be_one_of do |expected|
44
+ match do |actual|
45
+ expected.include?(actual)
46
+ end
47
+ end
48
+
49
+ RSpec::Matchers.define :become_soon do |expected|
50
+ match do |actual|
51
+ begin
52
+ wait_until do
53
+ actual.call == expected
54
+ end
55
+ rescue
56
+ false
57
+ else
58
+ true
59
+ end
60
+ end
61
+
62
+ def supports_block_expectations?
63
+ true
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsolr-cloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Shintaro Kimura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: zk-server
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.7
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.7
83
+ - !ruby/object:Gem::Dependency
84
+ name: zk
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.9.5
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.9.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: rsolr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.12
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.12
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.49.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.49.0
125
+ description: The connection adopter supporting SolrCloud for RSolr
126
+ email:
127
+ - service@enigmo.co.jp
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rubocop.yml"
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - lib/rsolr/cloud.rb
139
+ - lib/rsolr/cloud/connection.rb
140
+ - lib/rsolr/cloud/error.rb
141
+ - lib/rsolr/cloud/version.rb
142
+ - rsolr-cloud.gemspec
143
+ - spec/files/collection1_all_nodes_alive.json
144
+ - spec/files/collection1_leader_down.json
145
+ - spec/files/collection1_leader_node_recovered.json
146
+ - spec/files/collection1_leader_recovering.json
147
+ - spec/files/collection1_replica_down.json
148
+ - spec/files/collection2_all_nodes_alive.json
149
+ - spec/files/collection3_all_nodes_alive.json
150
+ - spec/rsolr/cloud/connection_spec.rb
151
+ - spec/spec_helper.rb
152
+ homepage: ''
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.6.11
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: The connection adopter supporting SolrCloud for RSolr
176
+ test_files:
177
+ - spec/files/collection1_all_nodes_alive.json
178
+ - spec/files/collection1_leader_down.json
179
+ - spec/files/collection1_leader_node_recovered.json
180
+ - spec/files/collection1_leader_recovering.json
181
+ - spec/files/collection1_replica_down.json
182
+ - spec/files/collection2_all_nodes_alive.json
183
+ - spec/files/collection3_all_nodes_alive.json
184
+ - spec/rsolr/cloud/connection_spec.rb
185
+ - spec/spec_helper.rb