rsolr-cloud 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 67996b618f2a307aa85db06af61c37e744eb30aa
4
+ data.tar.gz: 8c879200aad976428384acc68e27d2f31df848a6
5
+ SHA512:
6
+ metadata.gz: 177b18e3144e07006fc623e0d2a12a3215a1d4c50d72f9ccb7869811434b17f4a9ea7dfff9a12c8ab830a3664940c65a4a5e78a8e778cb99ba1d04075615951a
7
+ data.tar.gz: dda865676ade93118eb4bfcd434482f2d6444dfa4e1ae02a55ad72e407e5b755c1f2470432ff18f9b1dc09bc0e84f0fe562930f217d557a121de78adcb16e63b
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/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,49 @@
1
+ # RSolr::Cloud
2
+
3
+ The connection adopter supporting SolrCloud for RSolr.
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
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
@@ -0,0 +1,3 @@
1
+ require 'rsolr/cloud/error'
2
+ require 'rsolr/cloud/connection'
3
+ require 'rsolr/cloud/version'
@@ -0,0 +1,135 @@
1
+ module RSolr
2
+ module Cloud
3
+ # RSolr connection adapter for SolrCloud
4
+ class Connection < RSolr::Connection
5
+ include MonitorMixin
6
+
7
+ ZNODE_LIVE_NODES = '/live_nodes'
8
+ ZNODE_COLLECTIONS = '/collections'
9
+
10
+ def initialize(zk)
11
+ super()
12
+ @zk = zk
13
+ init_live_node_watcher
14
+ init_collections_watcher
15
+ update_urls
16
+ end
17
+
18
+ def execute(client, request_context)
19
+ collection_name = request_context[:collection]
20
+ fail 'The :collection option must be specified.' unless collection_name
21
+ path = request_context[:path].to_s
22
+ query = request_context[:query]
23
+ query = query ? "?#{query}" : ''
24
+ url = select_node(collection_name, leader_only: path == 'update')
25
+ fail RSolr::Cloud::Error::NotEnoughNodes unless url
26
+ request_context[:uri] = RSolr::Uri.create(url).merge(path + query)
27
+ super(client, request_context)
28
+ end
29
+
30
+ private
31
+
32
+ def select_node(collection, leader_only: false)
33
+ if leader_only
34
+ synchronize { @leader_urls[collection].to_a.sample }
35
+ else
36
+ synchronize { @all_urls[collection].to_a.sample }
37
+ end
38
+ end
39
+
40
+ def init_live_node_watcher
41
+ @zk.register(ZNODE_LIVE_NODES) do
42
+ update_live_nodes
43
+ update_urls
44
+ end
45
+ update_live_nodes
46
+ end
47
+
48
+ def init_collections_watcher
49
+ @zk.register(ZNODE_COLLECTIONS) do
50
+ update_collections
51
+ update_urls
52
+ end
53
+ update_collections
54
+ end
55
+
56
+ def init_collection_state_watcher(collection)
57
+ @zk.register(collection_state_znode_path(collection)) do
58
+ update_collection_state(collection)
59
+ update_urls
60
+ end
61
+ update_collection_state(collection)
62
+ end
63
+
64
+ def update_urls
65
+ synchronize do
66
+ @all_urls = {}
67
+ @leader_urls = {}
68
+ @collections.each do |name, state|
69
+ @all_urls[name], @leader_urls[name] = available_urls(name, state)
70
+ end
71
+ end
72
+ end
73
+
74
+ def update_live_nodes
75
+ synchronize do
76
+ @live_nodes = {}
77
+ @zk.children(ZNODE_LIVE_NODES, watch: true).each do |node|
78
+ @live_nodes[node] = true
79
+ end
80
+ end
81
+ end
82
+
83
+ def update_collections
84
+ collections = @zk.children(ZNODE_COLLECTIONS, watch: true)
85
+ created = []
86
+ synchronize do
87
+ @collections ||= {}
88
+ deleted = @collections.keys - collections
89
+ created = collections - @collections.keys
90
+ deleted.each { |collection| @collections.delete(collection) }
91
+ end
92
+ created.each { |collection| init_collection_state_watcher(collection) }
93
+ end
94
+
95
+ def update_collection_state(collection)
96
+ synchronize do
97
+ collection_state_json, _stat =
98
+ @zk.get(collection_state_znode_path(collection), watch: true)
99
+ @collections.merge!(JSON.parse(collection_state_json))
100
+ end
101
+ end
102
+
103
+ def available_urls(collection_name, collection_state)
104
+ leader_urls = []
105
+ all_urls = []
106
+ all_nodes(collection_state).each do |node|
107
+ next unless active_node?(node)
108
+ url = "#{node['base_url']}/#{collection_name}"
109
+ leader_urls << url if leader_node?(node)
110
+ all_urls << url
111
+ end
112
+ [all_urls, leader_urls]
113
+ end
114
+
115
+ def all_nodes(collection_state)
116
+ nodes = collection_state['shards'].values.map do |shard|
117
+ shard['replicas'].values
118
+ end
119
+ nodes.flatten
120
+ end
121
+
122
+ def collection_state_znode_path(collection_name)
123
+ "/collections/#{collection_name}/state.json"
124
+ end
125
+
126
+ def active_node?(node)
127
+ @live_nodes[node['node_name']] && node['state'] == 'active'
128
+ end
129
+
130
+ def leader_node?(node)
131
+ node['leader'] == 'true'
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,13 @@
1
+ module RSolr
2
+ module Cloud
3
+ module Error
4
+
5
+ class NotEnoughNodes < RuntimeError
6
+ def to_s
7
+ 'Not enough nodes to handle the request.'
8
+ end
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Rsolr
2
+ module Cloud
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rsolr/cloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rsolr-cloud"
8
+ spec.version = Rsolr::Cloud::VERSION
9
+ spec.authors = ["Shintaro Kimura"]
10
+ spec.email = ["service@enigmo.co.jp"]
11
+ spec.summary = %q{The connection adopter supporting SolrCloud for RSolr}
12
+ spec.description = %q{The connection adopter supporting SolrCloud for RSolr}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.3"
24
+ spec.add_development_dependency 'activesupport', '~> 4.2'
25
+ spec.add_development_dependency 'zk-server', '~> 1.1.7'
26
+ spec.add_development_dependency "zk", "~> 1.9.5"
27
+ spec.add_development_dependency "rsolr", "~> 1.0.12"
28
+ end
@@ -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"}}
@@ -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"}}
@@ -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"}}
@@ -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"}}
@@ -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"}}
@@ -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"}}
@@ -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"}}
@@ -0,0 +1,157 @@
1
+ require 'spec_helper.rb'
2
+
3
+ RSpec.describe RSolr::Cloud::Connection do
4
+
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'
22
+ ].each do |node|
23
+ @zk_in_solr.create("/live_nodes/#{node}", '', mode: :ephemeral)
24
+ end
25
+ %w(collection1 collection2).each do |collection|
26
+ @zk_in_solr.create("/collections/#{collection}")
27
+ json = File.read("spec/files/#{collection}_all_nodes_alive.json")
28
+ @zk_in_solr.create("/collections/#{collection}/state.json", json, mode: :ephemeral)
29
+ end
30
+ @zk = ZK.new
31
+ @subject = RSolr::Cloud::Connection.new @zk
32
+ end
33
+
34
+ let(:client) { double.as_null_object }
35
+
36
+ let(:http) { double(Net::HTTP).as_null_object }
37
+
38
+ it 'should configure Net::HTTP with one of active node in select request.' do
39
+ expect(@subject.instance_variable_get(:@leader_urls)['collection1'].sort).to eq(
40
+ ['http://192.168.1.22:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
41
+ expect(@subject.instance_variable_get(:@all_urls)['collection1'].sort).to eq(
42
+ ['http://192.168.1.21:8983/solr/collection1',
43
+ 'http://192.168.1.22:8983/solr/collection1',
44
+ 'http://192.168.1.23:8983/solr/collection1',
45
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
46
+ expect(Net::HTTP).to receive(:new) do |host, port|
47
+ expect(host).to be_one_of(['192.168.1.21', '192.168.1.22', '192.168.1.23', '192.168.1.24'])
48
+ expect(port).to eq(8983)
49
+ http
50
+ end
51
+ expect(http).to receive(:request) do |request|
52
+ expect(request.path).to eq('/solr/collection1/select?q=*:*')
53
+ double.as_null_object
54
+ end
55
+ @subject.execute client, collection: 'collection1', method: :get, path: 'select', query: 'q=*:*'
56
+ end
57
+
58
+ it 'should configure Net::HTTP with one of leader node in update request' do
59
+ expect(@subject.instance_variable_get(:@leader_urls)['collection1'].sort).to eq(
60
+ ['http://192.168.1.22:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
61
+ expect(@subject.instance_variable_get(:@all_urls)['collection1'].sort).to eq(
62
+ ['http://192.168.1.21:8983/solr/collection1',
63
+ 'http://192.168.1.22:8983/solr/collection1',
64
+ 'http://192.168.1.23:8983/solr/collection1',
65
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
66
+ expect(Net::HTTP).to receive(:new) do |host, port|
67
+ expect(host).to be_one_of(['192.168.1.22', '192.168.1.24'])
68
+ expect(port).to eq(8983)
69
+ http
70
+ end
71
+ expect(http).to receive(:request) do |request|
72
+ expect(request.path).to eq('/solr/collection1/update')
73
+ expect(request.body).to eq('the data')
74
+ double.as_null_object
75
+ end
76
+ @subject.execute client, collection: 'collection1', method: :post, path: 'update', data: 'the data'
77
+ end
78
+
79
+ it 'should remove downed replica node and add recovered node' do
80
+ @zk_in_solr.delete('/live_nodes/192.168.1.21:8983_solr')
81
+ @zk_in_solr.set('/collections/collection1/state.json', File.read('spec/files/collection1_replica_down.json'))
82
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
83
+ ['http://192.168.1.22:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
84
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
85
+ ['http://192.168.1.22:8983/solr/collection1',
86
+ 'http://192.168.1.23:8983/solr/collection1',
87
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
88
+ @zk_in_solr.create('/live_nodes/192.168.1.21:8983_solr', mode: :ephemeral)
89
+ @zk_in_solr.set('/collections/collection1/state.json', File.read('spec/files/collection1_all_nodes_alive.json'))
90
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
91
+ ['http://192.168.1.22:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
92
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
93
+ ['http://192.168.1.21:8983/solr/collection1',
94
+ 'http://192.168.1.22:8983/solr/collection1',
95
+ 'http://192.168.1.23:8983/solr/collection1',
96
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
97
+ end
98
+
99
+ it 'should remove downed leader node and add recovered node' do
100
+ @zk_in_solr.delete('/live_nodes/192.168.1.22:8983_solr')
101
+ @zk_in_solr.set('/collections/collection1/state.json', File.read('spec/files/collection1_leader_down.json'))
102
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
103
+ ['http://192.168.1.23:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
104
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
105
+ ['http://192.168.1.21:8983/solr/collection1',
106
+ 'http://192.168.1.23:8983/solr/collection1',
107
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
108
+ @zk_in_solr.create('/live_nodes/192.168.1.22:8983_solr', mode: :ephemeral)
109
+ @zk_in_solr.set('/collections/collection1/state.json', File.read('spec/files/collection1_all_nodes_alive.json'))
110
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
111
+ ['http://192.168.1.22:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
112
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
113
+ ['http://192.168.1.21:8983/solr/collection1',
114
+ 'http://192.168.1.22:8983/solr/collection1',
115
+ 'http://192.168.1.23:8983/solr/collection1',
116
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
117
+ end
118
+
119
+ it 'should remove recovering leader node and add recovered node' do
120
+ @zk_in_solr.set('/collections/collection1/state.json', File.read('spec/files/collection1_leader_recovering.json'))
121
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
122
+ ['http://192.168.1.23:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
123
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
124
+ ['http://192.168.1.21:8983/solr/collection1',
125
+ 'http://192.168.1.23:8983/solr/collection1',
126
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
127
+ @zk_in_solr.set('/collections/collection1/state.json', File.read('spec/files/collection1_all_nodes_alive.json'))
128
+ expect { @subject.instance_variable_get(:@leader_urls)['collection1'].sort }.to become_soon(
129
+ ['http://192.168.1.23:8983/solr/collection1', 'http://192.168.1.24:8983/solr/collection1'].sort)
130
+ expect { @subject.instance_variable_get(:@all_urls)['collection1'].sort }.to become_soon(
131
+ ['http://192.168.1.21:8983/solr/collection1',
132
+ 'http://192.168.1.22:8983/solr/collection1',
133
+ 'http://192.168.1.23:8983/solr/collection1',
134
+ 'http://192.168.1.24:8983/solr/collection1'].sort)
135
+ end
136
+
137
+ it 'should add new created collection.' do
138
+ @zk_in_solr.create('/collections/collection3')
139
+ @zk_in_solr.create('/collections/collection3/state.json', File.read('spec/files/collection3_all_nodes_alive.json'))
140
+ expect { @subject.instance_variable_get(:@leader_urls)['collection3'].to_a.sort }.to become_soon(
141
+ ['http://192.168.1.24:8983/solr/collection3'])
142
+ expect { @subject.instance_variable_get(:@all_urls)['collection3'].to_a.sort }.to become_soon(
143
+ ['http://192.168.1.21:8983/solr/collection3', 'http://192.168.1.24:8983/solr/collection3'].sort)
144
+ end
145
+
146
+ it 'should remove deleted collection.' do
147
+ delete_with_children(@zk_in_solr, '/collections/collection2')
148
+ expect { @subject.instance_variable_get(:@leader_urls).keys }.to become_soon(['collection1'])
149
+ expect { @subject.instance_variable_get(:@all_urls).keys }.to become_soon(['collection1'])
150
+ end
151
+
152
+ after do
153
+ @zk_in_solr.close if @zk_in_solr
154
+ @zk.close if @zk
155
+ end
156
+
157
+ end
@@ -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
16
+ end
17
+
18
+ def wait_until(timeout = 10)
19
+ started_on = Time.now
20
+ result = false
21
+ loop do
22
+ result = yield
23
+ break if result || started_on < timeout.second.ago
24
+ Thread.pass
25
+ end
26
+ fail 'Timed out' unless result
27
+ end
28
+ end
29
+
30
+ RSpec.configure do |config|
31
+ config.include Helpers
32
+ config.before(:suite) do
33
+ ZK::Server.run do |c|
34
+ c.force_sync = false
35
+ end
36
+ end
37
+ config.after(:suite) do
38
+ ZK::Server.server.clobber!
39
+ end
40
+ end
41
+
42
+ RSpec::Matchers.define :be_one_of do |expected|
43
+ match do |actual|
44
+ expected.include?(actual)
45
+ end
46
+ end
47
+
48
+ RSpec::Matchers.define :become_soon do |expected|
49
+ match do |actual|
50
+ begin
51
+ wait_until do
52
+ actual.call == expected
53
+ end
54
+ rescue
55
+ false
56
+ else
57
+ true
58
+ end
59
+ end
60
+
61
+ def supports_block_expectations?
62
+ true
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsolr-cloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shintaro Kimura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-20 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
+ description: The connection adopter supporting SolrCloud for RSolr
112
+ email:
113
+ - service@enigmo.co.jp
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/rsolr/cloud.rb
124
+ - lib/rsolr/cloud/connection.rb
125
+ - lib/rsolr/cloud/error.rb
126
+ - lib/rsolr/cloud/version.rb
127
+ - rsolr-cloud.gemspec
128
+ - spec/files/collection1_all_nodes_alive.json
129
+ - spec/files/collection1_leader_down.json
130
+ - spec/files/collection1_leader_node_recovered.json
131
+ - spec/files/collection1_leader_recovering.json
132
+ - spec/files/collection1_replica_down.json
133
+ - spec/files/collection2_all_nodes_alive.json
134
+ - spec/files/collection3_all_nodes_alive.json
135
+ - spec/rsolr/cloud/connection_spec.rb
136
+ - spec/spec_helper.rb
137
+ homepage: ''
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.3
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: The connection adopter supporting SolrCloud for RSolr
161
+ test_files:
162
+ - spec/files/collection1_all_nodes_alive.json
163
+ - spec/files/collection1_leader_down.json
164
+ - spec/files/collection1_leader_node_recovered.json
165
+ - spec/files/collection1_leader_recovering.json
166
+ - spec/files/collection1_replica_down.json
167
+ - spec/files/collection2_all_nodes_alive.json
168
+ - spec/files/collection3_all_nodes_alive.json
169
+ - spec/rsolr/cloud/connection_spec.rb
170
+ - spec/spec_helper.rb