ci-queue 0.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
+ SHA1:
3
+ metadata.gz: 11fda6e3d6ac119631a90d63086ebb9e06d628bd
4
+ data.tar.gz: 9ecad3efc2ec99b01e5a58f9defc55b772ffa65b
5
+ SHA512:
6
+ metadata.gz: 121adc5f59a3c95c62121e6bad8ebeb321890be2a67e3f3ae14ae5b6fd5febab786b8d4a4bd4e74e54e14bc29968dc07cc3a2c54d30c7230e83a1b52cfdfb58f
7
+ data.tar.gz: 658a17f26818a69bb7a60c9e563a5b2e16dc4dded98637455e49313d438b3d72fa323b1a10c0179d223c8f32b95009238cddc6e145c7db46754a7114ec596ec8
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ci-queue.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Shopify
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.
@@ -0,0 +1,45 @@
1
+ # CI::Queue
2
+
3
+ Distribute tests over many workers using a queue.
4
+
5
+ ## Why a queue?
6
+
7
+ One big problem with distributed test suites, is test imbalance. Meaning that one worker would spend 10 minutes while all the others are done after 1 minute.
8
+ There is algorithms available to balance perfectly your workers, but in practice your test performance tend to vary, and it's easier to consider tests as work unit in a queue and let workers pop them as fast as possible.
9
+
10
+ Another advantage is that if you lose workers along the way, using a queue the other workers can pick up the job, making you resilient to failures.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'ci-queue'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install ci-queue
27
+
28
+ ## Usage
29
+
30
+ TODO: Write usage instructions here
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/ci-queue.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'bundler' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("bundler", "bundler")
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ci/queue"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'ci/queue/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ci-queue'
8
+ spec.version = CI::Queue::VERSION
9
+ spec.authors = ["Jean Boussier"]
10
+ spec.email = ["jean.boussier@shopify.com"]
11
+
12
+ spec.summary = %q{Distribute tests over many workers using a queue}
13
+ spec.description = %q{To parallelize your CI without having to balance your tests}
14
+ spec.homepage = 'https://github.com/Shopify/ci-queue'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', "~> 10.0"
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'redis', '~> 3.3'
28
+ spec.add_development_dependency 'simplecov', '~> 0.12'
29
+ end
@@ -0,0 +1,3 @@
1
+ require 'ci/queue/version'
2
+ require 'ci/queue/static'
3
+ require 'ci/queue/file'
@@ -0,0 +1,11 @@
1
+ require 'ci/queue/static'
2
+
3
+ module CI
4
+ module Queue
5
+ class File < Static
6
+ def initialize(path)
7
+ super(::File.readlines(path).map(&:strip).reject(&:empty?))
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'redis'
2
+ require 'ci/queue/redis/base'
3
+ require 'ci/queue/redis/worker'
4
+ require 'ci/queue/redis/supervisor'
5
+
6
+ module CI
7
+ module Queue
8
+ module Redis
9
+ Error = Class.new(StandardError)
10
+ LostMaster = Class.new(Error)
11
+
12
+ def self.new(*args)
13
+ Worker.new(*args)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,65 @@
1
+ module CI
2
+ module Queue
3
+ module Redis
4
+ class Base
5
+ def initialize(redis:, build_id:)
6
+ @redis = redis
7
+ @key = "build:#{build_id}"
8
+ end
9
+
10
+ def empty?
11
+ size == 0
12
+ end
13
+
14
+ def size
15
+ redis.multi do
16
+ redis.llen(key('queue'))
17
+ redis.zcard(key('running'))
18
+ end.inject(:+)
19
+ end
20
+
21
+ def to_a
22
+ redis.multi do
23
+ redis.lrange(key('queue'), 0, -1)
24
+ redis.zrange(key('running'), 0, -1)
25
+ end.flatten.reverse
26
+ end
27
+
28
+ def progress
29
+ total - size
30
+ end
31
+
32
+ def wait_for_master(timeout: 10)
33
+ return true if master?
34
+ (timeout * 10 + 1).to_i.times do
35
+ case master_status
36
+ when 'ready', 'finished'
37
+ return true
38
+ else
39
+ sleep 0.1
40
+ end
41
+ end
42
+ raise LostMaster, "The master worker is still `#{master_status}` after 10 seconds waiting."
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :redis
48
+
49
+ def key(*args)
50
+ [@key, *args].join(':')
51
+ end
52
+
53
+ def master_status
54
+ redis.get(key('master-status'))
55
+ end
56
+
57
+ def eval_script(script, *args)
58
+ @scripts_cache ||= {}
59
+ sha = (@scripts_cache[script] ||= redis.script(:load, script))
60
+ redis.evalsha(sha, *args)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,18 @@
1
+ module CI
2
+ module Queue
3
+ module Redis
4
+ class Supervisor < Base
5
+ def master?
6
+ false
7
+ end
8
+
9
+ def wait_for_workers
10
+ return false unless wait_for_master
11
+
12
+ sleep 0.1 until empty?
13
+ true
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,116 @@
1
+ module CI
2
+ module Queue
3
+ module Redis
4
+ class Worker < Base
5
+ attr_reader :total
6
+
7
+ def initialize(tests, redis:, build_id:, worker_id:, timeout:)
8
+ @shutdown_required = false
9
+ super(redis: redis, build_id: build_id)
10
+ @worker_id = worker_id
11
+ @timeout = timeout
12
+ push(tests)
13
+ end
14
+
15
+ def shutdown!
16
+ @shutdown_required = true
17
+ end
18
+
19
+ def shutdown_required?
20
+ @shutdown_required
21
+ end
22
+
23
+ def master?
24
+ @master
25
+ end
26
+
27
+ def poll
28
+ wait_for_master
29
+ while test = reserve
30
+ yield test
31
+ acknowledge(test)
32
+ end
33
+ end
34
+
35
+ def retry_queue
36
+ Static.new(redis.lrange(key("worker:#{worker_id}:queue"), 0, -1).reverse)
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :worker_id, :timeout
42
+
43
+ RESERVE_TEST = %{
44
+ local queue_key = KEYS[1]
45
+ local zset_key = KEYS[2]
46
+ local current_time = ARGV[1]
47
+
48
+ local test = redis.call('rpop', queue_key)
49
+ if test then
50
+ redis.call('zadd', zset_key, current_time, test)
51
+ return test
52
+ else
53
+ return nil
54
+ end
55
+ }
56
+ def reserve
57
+ return if shutdown_required?
58
+
59
+ if test = eval_script(RESERVE_TEST, keys: [key('queue'), key('running')], argv: [Time.now.to_f])
60
+ return test
61
+ else
62
+ reserve_lost_test
63
+ end
64
+ end
65
+
66
+ RESERVE_LOST_TEST = %{
67
+ local zset_key = KEYS[1]
68
+ local current_time = ARGV[1]
69
+ local timeout = ARGV[2]
70
+
71
+ local test = redis.call('zrangebyscore', zset_key, 0, current_time - timeout)[1]
72
+ if test then
73
+ redis.call('zadd', zset_key, current_time, test)
74
+ return test
75
+ else
76
+ return nil
77
+ end
78
+ }
79
+ def reserve_lost_test
80
+ until redis.zcard(key('running')) == 0
81
+ if test = eval_script(RESERVE_LOST_TEST, keys: [key('running')], argv: [Time.now.to_f, timeout])
82
+ return test
83
+ end
84
+ sleep 0.1
85
+ end
86
+ nil
87
+ end
88
+
89
+ ACKNOWLEDGE = %{
90
+ local zset_key = KEYS[1]
91
+ local processed_count_key = KEYS[2]
92
+ local test = ARGV[1]
93
+
94
+ if redis.call('zrem', zset_key, test) == 1 then
95
+ redis.call('incr', processed_count_key)
96
+ end
97
+ }
98
+ def acknowledge(test)
99
+ eval_script(ACKNOWLEDGE, keys: [key('running'), key('processed')], argv: [test])
100
+ redis.lpush(key("worker:#{worker_id}:queue"), test)
101
+ end
102
+
103
+ def push(tests)
104
+ @total = tests.size
105
+ if @master = redis.setnx(key('master-status'), 'setup')
106
+ redis.multi do
107
+ redis.lpush(key('queue'), tests)
108
+ redis.set(key('total'), @total)
109
+ redis.set(key('master-status'), 'ready')
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,32 @@
1
+ module CI
2
+ module Queue
3
+ class Static
4
+ attr_reader :progress, :total
5
+
6
+ def initialize(tests)
7
+ @queue = tests
8
+ @progress = 0
9
+ @total = tests.size
10
+ end
11
+
12
+ def to_a
13
+ @queue.dup
14
+ end
15
+
16
+ def size
17
+ @queue.size
18
+ end
19
+
20
+ def poll
21
+ while test = @queue.shift
22
+ yield test
23
+ @progress += 1
24
+ end
25
+ end
26
+
27
+ def empty?
28
+ @queue.empty?
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module CI
2
+ module Queue
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'minitest'
2
+
3
+ module Minitest
4
+ module Queue
5
+ attr_accessor :queue
6
+
7
+ SuiteNotFound = Class.new(StandardError)
8
+
9
+ def __run(*args)
10
+ if queue
11
+ run_from_queue(*args)
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ def run_from_queue(reporter, *)
18
+ runnable_classes = Minitest::Runnable.runnables.map { |s| [s.name, s] }.to_h
19
+
20
+ queue.poll do |msg|
21
+ class_name, method = msg.split("#".freeze, 2)
22
+
23
+ if suite = runnable_classes[class_name]
24
+ Minitest::Runnable.run_one_method(suite, method, reporter)
25
+ else
26
+ raise SuiteNotFound, "Couldn't find suite matching: #{msg.inspect}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ MiniTest.singleton_class.prepend(MiniTest::Queue)
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ci-queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jean Boussier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-15 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ description: To parallelize your CI without having to balance your tests
84
+ email:
85
+ - jean.boussier@shopify.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/bundler
97
+ - bin/console
98
+ - bin/rake
99
+ - bin/setup
100
+ - ci-queue.gemspec
101
+ - lib/ci/queue.rb
102
+ - lib/ci/queue/file.rb
103
+ - lib/ci/queue/redis.rb
104
+ - lib/ci/queue/redis/base.rb
105
+ - lib/ci/queue/redis/supervisor.rb
106
+ - lib/ci/queue/redis/worker.rb
107
+ - lib/ci/queue/static.rb
108
+ - lib/ci/queue/version.rb
109
+ - lib/minitest/queue.rb
110
+ homepage: https://github.com/Shopify/ci-queue
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.5
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Distribute tests over many workers using a queue
134
+ test_files: []