blue_green_process 0.1.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
+ SHA256:
3
+ metadata.gz: a12551178595d00eeb37b62eb34f7b06f577d2c8a345f4c91a18ec7fecb36f8b
4
+ data.tar.gz: 5f1b1fb9d5f967fbdd55f1a11840c090a92fb9429ce44b16b27da50e815c160a
5
+ SHA512:
6
+ metadata.gz: 438555f06ca40cea7826b851d2750d8ed7e9a068afc6446d12c8666ebfd3c13d4cc40d70d2b497a7248485e3b7653943211fc41fb9713f61a84007089a841dd2
7
+ data.tar.gz: 16f58cfe3be851db0f9ea3086a3801fcd1229eebe2ed00b7338db8f200f0cb95eb6ef3843c9692a113e94e5a1706f084a8f39672cdc89561fb6eaeed2705d962
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ SuggestExtensions: false
4
+ NewCops: disable
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Metrics/BlockLength:
21
+ Enabled: false
22
+
23
+ Metrics/AbcSize:
24
+ Enabled: false
25
+
26
+ Metrics/MethodLength:
27
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in blue_green_process.gemspec
6
+ gemspec
7
+
8
+ gem "pry"
9
+ gem "rake", "~> 13.0"
10
+ gem "rspec", "~> 3.0"
11
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ blue_green_process (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ json (2.6.2)
13
+ method_source (1.0.0)
14
+ parallel (1.22.1)
15
+ parser (3.1.2.0)
16
+ ast (~> 2.4.1)
17
+ pry (0.14.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.5.0)
23
+ rexml (3.2.5)
24
+ rspec (3.11.0)
25
+ rspec-core (~> 3.11.0)
26
+ rspec-expectations (~> 3.11.0)
27
+ rspec-mocks (~> 3.11.0)
28
+ rspec-core (3.11.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-expectations (3.11.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-mocks (3.11.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-support (3.11.0)
37
+ rubocop (1.32.0)
38
+ json (~> 2.3)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.1.0.0)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8, < 3.0)
43
+ rexml (>= 3.2.5, < 4.0)
44
+ rubocop-ast (>= 1.19.1, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 3.0)
47
+ rubocop-ast (1.19.1)
48
+ parser (>= 3.1.1.0)
49
+ ruby-progressbar (1.11.0)
50
+ unicode-display_width (2.2.0)
51
+
52
+ PLATFORMS
53
+ arm64-darwin-21
54
+ ruby
55
+ x86_64-linux
56
+
57
+ DEPENDENCIES
58
+ blue_green_process!
59
+ pry
60
+ rake (~> 13.0)
61
+ rspec (~> 3.0)
62
+ rubocop (~> 1.21)
63
+
64
+ BUNDLED WITH
65
+ 2.3.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 jiikko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # BlueGreenProcess
2
+
3
+ A library that solves GC bottlenecks with multi-process.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add blue_green_process
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install blue_green_process
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ BlueGreenProcess.configure do |config|
19
+ config.after_fork = ->{ puts 'forked!' }
20
+ end
21
+
22
+ process = BlueGreenProcess.new(
23
+ worker_instance: BlueGreenProcess::BaseWorker.new,
24
+ max_work: 14,
25
+ )
26
+
27
+ 40.times do
28
+ process.work
29
+ end
30
+
31
+ sleep(1)
32
+
33
+ process.shutdown
34
+ loop do
35
+ result = Process.waitall
36
+ if result.empty?
37
+ break
38
+ else
39
+ sleep(0.01)
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/blue_green_process.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
57
+
58
+ ## NOTE
59
+ * 前回の処理が終わっていないのにqueuingしない
60
+ * workerの処理内容は固定
61
+
62
+ ## TODO
63
+ * プロセスを入れ替えるときに変数を受け渡しをする
64
+ * queueしてからのdequeueするまでの時間を測定したい
65
+ * webサーバでよくあるqueued timeみたいな扱い
66
+ * これが伸びると致命的なのでチューニングできるようにしたいため
67
+ * inactiveからactiveへの切り替えになる時間を測定したい
68
+ * GCが長引いてactiveプロセスが処理開始に時間がかかるので
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/blue_green_process/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "blue_green_process"
7
+ spec.version = BlueGreenProcess::VERSION
8
+ spec.authors = ["jiikko"]
9
+ spec.email = ["n905i.1214@gmail.com"]
10
+
11
+ spec.summary = "A library that solves GC bottlenecks with multi-process."
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/splaplapla/blue_green_process"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.5"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/splaplapla/blue_green_process"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlueGreenProcess
4
+ class BaseWorker
5
+ def work(*); end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlueGreenProcess
4
+ class Config
5
+ def after_fork=(block)
6
+ @after_fork_block = block
7
+ end
8
+
9
+ def after_fork
10
+ @after_fork_block || -> {}
11
+ end
12
+
13
+ def reset
14
+ @after_fork_block = nil
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlueGreenProcess
4
+ class MasterProcess
5
+ def initialize(worker_instance:, max_work:)
6
+ blue = fork_process(label: :blue, worker_instance: worker_instance)
7
+ green = fork_process(label: :green, worker_instance: worker_instance)
8
+
9
+ @stage_state = true
10
+ @stage = {
11
+ true => blue.be_active,
12
+ false => green.be_inactive
13
+ }
14
+ @processes = @stage.values
15
+ @max_work = max_work
16
+ end
17
+
18
+ def fork_process(label:, worker_instance:)
19
+ child_read, parent_write = IO.pipe
20
+ parent_read, child_write = IO.pipe
21
+
22
+ pid = fork do
23
+ BlueGreenProcess.config.after_fork.call
24
+
25
+ parent_write.close
26
+ parent_read.close
27
+ process_status = :inactive
28
+
29
+ loop do
30
+ data = child_read.gets&.strip
31
+ case data
32
+ when BlueGreenProcess::PROCESS_COMMAND_DIE, nil, ""
33
+ BlueGreenProcess.debug_log "#{label}'ll die(#{$PROCESS_ID})"
34
+ exit 0
35
+ when BlueGreenProcess::PROCESS_COMMAND_BE_ACTIVE
36
+ process_status = BlueGreenProcess::PROCESS_STATUS_ACTIVE
37
+ BlueGreenProcess.debug_log "#{label}'ll be active(#{$PROCESS_ID})"
38
+ child_write.puts BlueGreenProcess::PROCESS_RESPONSE
39
+ ::GC.disable
40
+ when BlueGreenProcess::PROCESS_COMMAND_BE_INACTIVE
41
+ process_status = BlueGreenProcess::PROCESS_STATUS_INACTIVE
42
+ BlueGreenProcess.debug_log "#{label}'ll be inactive(#{$PROCESS_ID})"
43
+ child_write.puts BlueGreenProcess::PROCESS_RESPONSE
44
+ ::GC.enable
45
+ ::GC.start
46
+ when BlueGreenProcess::PROCESS_COMMAND_WORK
47
+ if process_status == BlueGreenProcess::PROCESS_STATUS_INACTIVE
48
+ warn "Should not be able to run in this status"
49
+ end
50
+
51
+ BlueGreenProcess.debug_log "#{label}'ll work(#{$PROCESS_ID})"
52
+ worker_instance.work(*label)
53
+ child_write.puts BlueGreenProcess::PROCESS_RESPONSE
54
+ else
55
+ child_write.puts "NG"
56
+ puts "unknown. from #{label}(#{$PROCESS_ID})"
57
+ exit 1
58
+ end
59
+ end
60
+
61
+ exit 0
62
+ end
63
+
64
+ child_write.close
65
+ child_read.close
66
+
67
+ BlueGreenProcess::WorkerProcess.new(pid, label, parent_read, parent_write)
68
+ end
69
+
70
+ def work
71
+ active_process do |process|
72
+ @max_work.times do
73
+ process.work
74
+ end
75
+ end
76
+
77
+ true
78
+ end
79
+
80
+ def shutdown
81
+ @processes.each do |process|
82
+ process.wpipe.puts(BlueGreenProcess::PROCESS_COMMAND_DIE)
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def active_process
89
+ active_process = @stage[@stage_state].be_active
90
+ @stage[!@stage_state].be_inactive
91
+ yield(active_process)
92
+ @stage_state = !@stage_state
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlueGreenProcess
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlueGreenProcess
4
+ class WorkerProcess
5
+ attr_accessor :pid, :label, :rpipe, :wpipe, :status
6
+
7
+ def initialize(pid, label, rpipe, wpipe)
8
+ self.pid = pid
9
+ self.label = label
10
+ self.rpipe = rpipe
11
+ self.wpipe = wpipe
12
+ self.status = BlueGreenProcess::PROCESS_STATUS_INACTIVE
13
+ end
14
+
15
+ def be_active
16
+ return self if status == BlueGreenProcess::PROCESS_STATUS_ACTIVE
17
+
18
+ write_and_await_until_read(BlueGreenProcess::PROCESS_COMMAND_BE_ACTIVE)
19
+ self.status = BlueGreenProcess::PROCESS_STATUS_ACTIVE
20
+ self
21
+ end
22
+
23
+ def be_inactive
24
+ return self if status == BlueGreenProcess::PROCESS_STATUS_INACTIVE
25
+
26
+ write_and_await_until_read(BlueGreenProcess::PROCESS_COMMAND_BE_INACTIVE)
27
+ self.status = BlueGreenProcess::PROCESS_STATUS_INACTIVE
28
+ self
29
+ end
30
+
31
+ def work
32
+ enforce_to_be_active
33
+
34
+ write_and_await_until_read(BlueGreenProcess::PROCESS_COMMAND_WORK)
35
+ end
36
+
37
+ private
38
+
39
+ def write_and_await_until_read(command)
40
+ write(command)
41
+ wait_response
42
+ end
43
+
44
+ def wait_response
45
+ response = read
46
+ raise "invalid response." unless response == BlueGreenProcess::PROCESS_RESPONSE
47
+ end
48
+
49
+ def read
50
+ rpipe.gets.strip
51
+ end
52
+
53
+ def write(token)
54
+ wpipe.puts token
55
+ end
56
+
57
+ def enforce_to_be_active
58
+ raise "activeじゃないのにrunできないです" if status != BlueGreenProcess::PROCESS_STATUS_ACTIVE
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require_relative "blue_green_process/version"
5
+ require "blue_green_process/master_process"
6
+ require "blue_green_process/worker_process"
7
+ require "blue_green_process/base_worker"
8
+ require "blue_green_process/config"
9
+
10
+ module BlueGreenProcess
11
+ PROCESS_STATUS_ACTIVE = :active
12
+ PROCESS_STATUS_INACTIVE = :inactive
13
+
14
+ PROCESS_COMMAND_DIE = "die"
15
+ PROCESS_COMMAND_BE_ACTIVE = "be_active"
16
+ PROCESS_COMMAND_BE_INACTIVE = "work"
17
+ PROCESS_COMMAND_WORK = "be_inactive"
18
+
19
+ PROCESS_RESPONSE = "ACK"
20
+
21
+ def self.new(worker_instance:, max_work:)
22
+ BlueGreenProcess::MasterProcess.new(worker_instance: worker_instance, max_work: max_work)
23
+ end
24
+
25
+ def self.debug_log(message)
26
+ return unless ENV["VERBOSE"]
27
+
28
+ puts message
29
+ end
30
+
31
+ def self.configure
32
+ @config = Config.new
33
+ yield(@config)
34
+ true
35
+ end
36
+
37
+ def self.config
38
+ @config ||= Config.new
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ module BlueGreenProcess
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blue_green_process
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jiikko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A library that solves GC bottlenecks with multi-process.
14
+ email:
15
+ - n905i.1214@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - blue_green_process.gemspec
28
+ - lib/blue_green_process.rb
29
+ - lib/blue_green_process/base_worker.rb
30
+ - lib/blue_green_process/config.rb
31
+ - lib/blue_green_process/master_process.rb
32
+ - lib/blue_green_process/version.rb
33
+ - lib/blue_green_process/worker_process.rb
34
+ - sig/blue_green_process.rbs
35
+ homepage: https://github.com/splaplapla/blue_green_process
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/splaplapla/blue_green_process
40
+ source_code_uri: https://github.com/splaplapla/blue_green_process
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '2.5'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.3.19
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A library that solves GC bottlenecks with multi-process.
60
+ test_files: []