rsolr-cloud-custom 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 835cbab73382b8e3cca43958fa2eb445ea1fcf353ec60f0ff3cc726f75aed262
4
+ data.tar.gz: 8758d7ee6081bdc7a14d612f0f555e30767aca567aa1ca5a630bfd6333332c0e
5
+ SHA512:
6
+ metadata.gz: 68ea544234e5d4eeda4390290d895320d4904fa0b20027628fdb162c8f1808d0c2514d60eb43b8c2bab2f080b3a11b197d25d3751b8408721ad15575fc0e2a8c
7
+ data.tar.gz: f35932eec4751afb8b613d231922bf15c9c8cae3caa67d9414ad40732c773103ce352c0b71c26366d24f1be7d99d6c11b39d86a2536923712a35008a5716b5b0
@@ -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/
@@ -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
@@ -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.
@@ -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
+ ```
@@ -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: [:rubocop, :spec]
9
+ rescue LoadError # rubocop:disable HandleExceptions
10
+ end
@@ -0,0 +1,3 @@
1
+ require 'rsolr/cloud/error'
2
+ require 'rsolr/cloud/connection'
3
+ require 'rsolr/cloud/version'
@@ -0,0 +1,62 @@
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'.freeze
8
+ ZNODE_CLUSTER_PROPS = '/clusterprops.json'.freeze
9
+
10
+ def initialize(zk)
11
+ super()
12
+ @zk = zk
13
+ init_url_scheme
14
+ init_live_node_watcher
15
+ end
16
+
17
+ def execute(client, request_context)
18
+ collection_name = request_context[:collection]
19
+ raise 'The :collection option must be specified.' unless collection_name
20
+ path = request_context[:path].to_s
21
+ query = request_context[:query]
22
+ query = query ? "?#{query}" : ''
23
+ url = select_node(collection_name)
24
+ raise RSolr::Cloud::Error::NotEnoughNodes unless url
25
+ request_context[:uri] = RSolr::Uri.create(url).merge(path + query)
26
+ super(client, request_context)
27
+ end
28
+
29
+ private
30
+
31
+ def init_url_scheme
32
+ @url_scheme = 'http'
33
+ if @zk.exists?(ZNODE_CLUSTER_PROPS)
34
+ json, _stat = @zk.get(ZNODE_CLUSTER_PROPS)
35
+ props = JSON.parse(json)
36
+ @url_scheme = props['urlScheme'] || 'http'
37
+ end
38
+ end
39
+
40
+ def select_node(collection)
41
+ synchronize { @live_nodes.sample + '/' + collection }
42
+ end
43
+
44
+ def init_live_node_watcher
45
+ @zk.register(ZNODE_LIVE_NODES) do
46
+ update_live_nodes
47
+ end
48
+ update_live_nodes
49
+ end
50
+
51
+ def update_live_nodes
52
+ synchronize do
53
+ @live_nodes = []
54
+ @zk.children(ZNODE_LIVE_NODES, watch: true).each do |node|
55
+ # "/" between host_and_port part of url and context is replaced with "_" in ZK
56
+ @live_nodes << @url_scheme + '://' + node.tr('_', '/')
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -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
@@ -0,0 +1,5 @@
1
+ module Rsolr
2
+ module Cloud
3
+ VERSION = '1.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,29 @@
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-custom'
8
+ spec.version = Rsolr::Cloud::VERSION
9
+ spec.authors = ['Shintaro Kimura']
10
+ spec.email = ['service@enigmo.co.jp']
11
+ spec.summary = 'The connection adopter supporting SolrCloud for RSolr'
12
+ spec.description = '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
+ spec.add_development_dependency 'rubocop', '~> 0.37.1'
29
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper.rb'
2
+
3
+ RSpec.describe RSolr::Cloud::Connection do
4
+ before do
5
+ @zk_in_solr = ZK.new
6
+ delete_with_children(@zk_in_solr, '/live_nodes')
7
+ wait_until(10) do
8
+ !@zk_in_solr.exists?('/live_nodes')
9
+ end
10
+ @zk_in_solr.create('/live_nodes')
11
+
12
+ ['192.168.1.21:8983_solr',
13
+ '192.168.1.22:8983_solr',
14
+ '192.168.1.23:8983_solr',
15
+ '192.168.1.24:8983_solr'
16
+ ].each do |node|
17
+ @zk_in_solr.create("/live_nodes/#{node}", '', mode: :ephemeral)
18
+ end
19
+ @zk = ZK.new
20
+ @subject = RSolr::Cloud::Connection.new @zk
21
+ end
22
+
23
+ let(:client) { double.as_null_object }
24
+
25
+ let(:http) { double(Net::HTTP).as_null_object }
26
+
27
+ it 'should configure Net::HTTP with one of live node in select or update request.' do
28
+ expect(@subject.instance_variable_get(:@live_nodes).sort).to eq(
29
+ ['http://192.168.1.21:8983/solr',
30
+ 'http://192.168.1.22:8983/solr',
31
+ 'http://192.168.1.23:8983/solr',
32
+ 'http://192.168.1.24:8983/solr'].sort)
33
+ expect(Net::HTTP).to receive(:new) do |host, port|
34
+ expect(host).to be_one_of(['192.168.1.21', '192.168.1.22', '192.168.1.23', '192.168.1.24'])
35
+ expect(port).to eq(8983)
36
+ http
37
+ end
38
+
39
+ expect(http).to receive(:request) do |request|
40
+ expect(request.path).to eq('/solr/collection1/select?q=*:*')
41
+ double.as_null_object
42
+ end
43
+ @subject.execute client, collection: 'collection1', method: :get, path: 'select', query: 'q=*:*'
44
+
45
+ expect(http).to receive(:request) do |request|
46
+ expect(request.path).to eq('/solr/collection1/update')
47
+ expect(request.body).to eq('the data')
48
+ double.as_null_object
49
+ end
50
+ @subject.execute client, collection: 'collection1',
51
+ method: :post,
52
+ path: 'update',
53
+ data: 'the data'
54
+ end
55
+
56
+ it 'should remove downed node and add recovered node.' do
57
+ @zk_in_solr.delete('/live_nodes/192.168.1.21:8983_solr')
58
+ expect { @subject.instance_variable_get(:@live_nodes).sort }.to become_soon(
59
+ ['http://192.168.1.22:8983/solr',
60
+ 'http://192.168.1.23:8983/solr',
61
+ 'http://192.168.1.24:8983/solr'].sort)
62
+ @zk_in_solr.create('/live_nodes/192.168.1.21:8983_solr', mode: :ephemeral)
63
+ expect { @subject.instance_variable_get(:@live_nodes).sort }.to become_soon(
64
+ ['http://192.168.1.21:8983/solr',
65
+ 'http://192.168.1.22:8983/solr',
66
+ 'http://192.168.1.23:8983/solr',
67
+ 'http://192.168.1.24:8983/solr'].sort)
68
+ end
69
+
70
+ it 'should obey url scheme.' do
71
+ @zk_in_solr.create('/clusterprops.json', '{"urlScheme": "https"}')
72
+ @subject = RSolr::Cloud::Connection.new @zk
73
+ expect(@subject.instance_variable_get(:@url_scheme)).to eq('https')
74
+ expect(@subject.instance_variable_get(:@live_nodes).sort).to eq(
75
+ ['https://192.168.1.21:8983/solr',
76
+ 'https://192.168.1.22:8983/solr',
77
+ 'https://192.168.1.23:8983/solr',
78
+ 'https://192.168.1.24:8983/solr'].sort)
79
+ end
80
+
81
+ after do
82
+ @zk_in_solr.close if @zk_in_solr
83
+ @zk.close if @zk
84
+ end
85
+ 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 # 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,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsolr-cloud-custom
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shintaro Kimura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.7'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '10.0'
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.3'
47
+ name: rspec
48
+ prerelease: false
49
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '4.2'
61
+ name: activesupport
62
+ prerelease: false
63
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 1.1.7
75
+ name: zk-server
76
+ prerelease: false
77
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 1.9.5
89
+ name: zk
90
+ prerelease: false
91
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 1.0.12
103
+ name: rsolr
104
+ prerelease: false
105
+ type: :development
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
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.37.1
117
+ name: rubocop
118
+ prerelease: false
119
+ type: :development
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.37.1
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-custom.gemspec
143
+ - spec/rsolr/cloud/connection_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: ''
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.6.13
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: The connection adopter supporting SolrCloud for RSolr
169
+ test_files:
170
+ - spec/rsolr/cloud/connection_spec.rb
171
+ - spec/spec_helper.rb