sunspot-parallel-reindex 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +18 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/lib/sunspot/parallel/reindex.rb +11 -0
- data/lib/sunspot/parallel/reindex/tasks.rb +67 -0
- data/lib/sunspot/parallel/reindex/version.rb +7 -0
- data/lib/sunspot/rails/searchable/acts_as_methods.rb +52 -0
- data/sunspot-parallel-reindex.gemspec +25 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a4c23603f10efec88022a6524889d355ffeed36
|
4
|
+
data.tar.gz: 35a156092a1897a9f28c90735973e674b9d814d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4a0f42bd2a0c333332b1c16f949243c9610d8953c73290d18931fbe2b1aa87e89471c7b1a5be4d7d10212d862db62273769dd03827bcd811af63856174698fb3
|
7
|
+
data.tar.gz: 81b9c4d27f4f651cf1b9b70f6c908816e7d8608f9fec8908e9b9cb65ca2210b0c6c34701d76e19591ee6e0110c1297841a9b31c8c57ac71d1f040a87251d181d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
2
|
+
a copy of this software and associated documentation files (the
|
3
|
+
"Software"), to deal in the Software without restriction, including
|
4
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
5
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
6
|
+
permit persons to whom the Software is furnished to do so, subject to
|
7
|
+
the following conditions:
|
8
|
+
|
9
|
+
The above copyright notice and this permission notice shall be
|
10
|
+
included in all copies or substantial portions of the Software.
|
11
|
+
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
14
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
15
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
16
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
17
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
18
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Sunspot::Parallel::Reindex
|
2
|
+
|
3
|
+
Add support for multi-process reindexing with sunspot.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sunspot-parallel-reindex'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sunspot-parallel-reindex
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Parameters are all optional, just like `rake sunspot:reindex`
|
24
|
+
|
25
|
+
$ rake sunspot:reindex:parallel[<batch_size>, <models>, <processes>]
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( https://github.com/[my-github-username]/sunspot-parallel-reindex/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create a new Pull Request
|
34
|
+
|
35
|
+
## Thanks
|
36
|
+
|
37
|
+
Code heavily inspired by https://github.com/MiraitSystems/enju_trunk/
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
namespace :sunspot do
|
2
|
+
namespace :reindex do
|
3
|
+
desc "Reindex models in parallel"
|
4
|
+
task :parallel, [:batch_size, :models, :exec_processor_size] => :environment do |t, args|
|
5
|
+
with_session(Sunspot::SessionProxy::Retry5xxSessionProxy.new(Sunspot.session)) do
|
6
|
+
reindex_options = { :batch_commit => false,
|
7
|
+
:exec_processor_size => Parallel.processor_count,
|
8
|
+
:batch_size => 1000 }
|
9
|
+
|
10
|
+
case args[:exec_processor_size]
|
11
|
+
when 'false'
|
12
|
+
reindex_options[:exec_processor_size] = 1
|
13
|
+
when /^\d+$/
|
14
|
+
reindex_options[:exec_processor_size] = args[:exec_processor_size].to_i if args[:exec_processor_size].to_i > 0
|
15
|
+
end
|
16
|
+
|
17
|
+
case args[:batch_size]
|
18
|
+
when 'false'
|
19
|
+
reindex_options[:batch_size] = 1000
|
20
|
+
when /^\d+$/
|
21
|
+
reindex_options[:batch_size] = args[:batch_size].to_i if args[:batch_size].to_i > 0
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "#{Parallel.processor_count} procesor(s)"
|
25
|
+
puts "reindex using #{reindex_options[:exec_processor_size]} procesor(s)"
|
26
|
+
|
27
|
+
# Load all the application's models. Models which invoke 'searchable' will register themselves
|
28
|
+
# in Sunspot.searchable.
|
29
|
+
Rails.application.eager_load!
|
30
|
+
Rails::Engine.subclasses.each{|engine| engine.instance.eager_load!}
|
31
|
+
|
32
|
+
if args[:models].present?
|
33
|
+
# Choose a specific subset of models, if requested
|
34
|
+
model_names = args[:models].split(/[+ ]/)
|
35
|
+
sunspot_models = model_names.map{ |m| m.constantize }
|
36
|
+
else
|
37
|
+
# By default, reindex all searchable models
|
38
|
+
sunspot_models = Sunspot.searchable
|
39
|
+
end
|
40
|
+
|
41
|
+
total_documents = sunspot_models.map { | m | m.count }.sum
|
42
|
+
sorted_models = sunspot_models.sort{|a, b| a.count <=> b.count}
|
43
|
+
|
44
|
+
# Set up progress_bar to, ah, report progress
|
45
|
+
begin
|
46
|
+
require 'progress_bar'
|
47
|
+
reindex_options[:progress_bar] = ProgressBar.new(total_documents)
|
48
|
+
rescue LoadError => e
|
49
|
+
$stdout.puts "Skipping progress bar: for progress reporting, add gem 'progress_bar' to your Gemfile"
|
50
|
+
rescue Exception => e
|
51
|
+
$stderr.puts "Error using progress bar: #{e.message}"
|
52
|
+
end
|
53
|
+
|
54
|
+
$stdout.puts "Reindex model list:"
|
55
|
+
sorted_models.each { |model|
|
56
|
+
$stdout.puts " name=#{model.name} record_size=#{model.count}"
|
57
|
+
}
|
58
|
+
$stdout.flush
|
59
|
+
|
60
|
+
sorted_models.each do |model|
|
61
|
+
model.solr_remove_all_from_index
|
62
|
+
model.solr_index_parallel(reindex_options)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Rails
|
3
|
+
module Searchable
|
4
|
+
module ActsAsMethods
|
5
|
+
def solr_index_parallel(opts={})
|
6
|
+
require 'parallel'
|
7
|
+
|
8
|
+
options = {
|
9
|
+
:batch_size => Sunspot.config.indexing.default_batch_size,
|
10
|
+
:batch_commit => true,
|
11
|
+
:include => self.sunspot_options[:include],
|
12
|
+
:start => opts.delete(:first_id) || 0
|
13
|
+
}.merge(opts)
|
14
|
+
|
15
|
+
find_in_batch_options = {
|
16
|
+
:batch_size => options[:batch_size],
|
17
|
+
:start => options[:start]
|
18
|
+
}
|
19
|
+
|
20
|
+
exec_processor_size = options[:exec_processor_size]
|
21
|
+
progress_lambda = lambda { |item, _, _|
|
22
|
+
if options[:progress_bar]
|
23
|
+
options[:progress_bar].increment!(item.size)
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
if options[:batch_size]
|
28
|
+
batch_counter = 0
|
29
|
+
self.includes(options[:include]).find_in_batches(find_in_batch_options) do |records|
|
30
|
+
::ActiveRecord::Base.establish_connection
|
31
|
+
Parallel.each(records.in_groups(exec_processor_size),
|
32
|
+
in_processes: exec_processor_size,
|
33
|
+
finish: progress_lambda) do |batch|
|
34
|
+
::ActiveRecord::Base.establish_connection
|
35
|
+
solr_benchmark(batch.size, batch_counter += 1) do
|
36
|
+
Sunspot.index(batch.select { |r| r.indexable? })
|
37
|
+
Sunspot.commit if options[:batch_commit]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
records = all(:include => options[:include]).select { |model| model.indexable? }
|
43
|
+
Sunspot.index!(records)
|
44
|
+
end
|
45
|
+
|
46
|
+
# perform a final commit if not committing in batches
|
47
|
+
Sunspot.commit unless options[:batch_commit]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sunspot/parallel/reindex/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sunspot-parallel-reindex"
|
8
|
+
spec.version = Sunspot::Parallel::Reindex::VERSION
|
9
|
+
spec.authors = ["Ben Tucker"]
|
10
|
+
spec.email = ["ben@btucker.net"]
|
11
|
+
spec.summary = %q{Provide parallel reindexing to sunspot}
|
12
|
+
spec.description = %q{Supply `rake sunspot:reindex:parallel` to support multi-process reindexing}
|
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_dependency "sunspot_rails", "~> 2.1.0"
|
24
|
+
spec.add_dependency "parallel", "~> 1.3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sunspot-parallel-reindex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Tucker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-02 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: sunspot_rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.1.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: parallel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.3.0
|
69
|
+
description: Supply `rake sunspot:reindex:parallel` to support multi-process reindexing
|
70
|
+
email:
|
71
|
+
- ben@btucker.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/sunspot/parallel/reindex.rb
|
82
|
+
- lib/sunspot/parallel/reindex/tasks.rb
|
83
|
+
- lib/sunspot/parallel/reindex/version.rb
|
84
|
+
- lib/sunspot/rails/searchable/acts_as_methods.rb
|
85
|
+
- sunspot-parallel-reindex.gemspec
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Provide parallel reindexing to sunspot
|
110
|
+
test_files: []
|