hawatel_tlb 0.1.0
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 +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CONTRIBUTING.md +70 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hawatel_tlb.gemspec +27 -0
- data/lib/hawatel_tlb.rb +4 -0
- data/lib/hawatel_tlb/client.rb +207 -0
- data/lib/hawatel_tlb/mode.rb +5 -0
- data/lib/hawatel_tlb/mode/dynamicratio.rb +75 -0
- data/lib/hawatel_tlb/mode/fastest.rb +111 -0
- data/lib/hawatel_tlb/mode/ratio.rb +101 -0
- data/lib/hawatel_tlb/mode/roundrobin.rb +50 -0
- data/lib/hawatel_tlb/mode/weighted.rb +102 -0
- data/lib/hawatel_tlb/version.rb +3 -0
- data/lib/hawatel_tlb/watchdog.rb +38 -0
- data/spec/client_spec.rb +76 -0
- data/spec/modes/dynamicratio_spec.rb +43 -0
- data/spec/modes/fastest_spec.rb +46 -0
- data/spec/modes/ratio_spec.rb +74 -0
- data/spec/modes/roundrobin_spec.rb +42 -0
- data/spec/modes/weighted_spec.rb +40 -0
- data/spec/spec_helper.rb +2 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f55182a59c4798296a0a990bd9395d5f551cef41
|
4
|
+
data.tar.gz: 6d0c51e51a1eefcf66bd00b136d032d64bba85a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18fcca9f92f324ff62ac4de08a6845388b94fe10bc4a65fb9f2595dd6ad6c54329144e9368ca18e98d6b8fb1e91fff8a232ace75d40a389f3b3eaa2d9722b02c
|
7
|
+
data.tar.gz: b6f30dffc784a7cd7df5ee8fb8a387c3d11d371b2079e9d7bd6e978a18df0f6dfbecf4c0ff98d0f9b4fb0ebdd77032a72783b234e63ea41bd99f07b027aca835
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hawatel/hawatel_tlb. This project is intended to be a safe, welcoming space for collaboration, and contributors.
|
4
|
+
|
5
|
+
1. [Fork](https://help.github.com/articles/fork-a-repo/) the project, clone your fork,
|
6
|
+
and configure the remotes:
|
7
|
+
|
8
|
+
```sh
|
9
|
+
# Clone your fork of the repo into the current directory
|
10
|
+
git clone https://github.com/<your-username>/hawatel_tlb
|
11
|
+
# Navigate to the newly cloned directory
|
12
|
+
cd hawatel_tlb
|
13
|
+
# Assign the original repo to a remote called "upstream"
|
14
|
+
git remote add upstream https://github.com/hawatel/hawatel_tlb
|
15
|
+
```
|
16
|
+
|
17
|
+
2. If you cloned a while ago, get the latest changes from upstream:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
git checkout master
|
21
|
+
git pull upstream master
|
22
|
+
```
|
23
|
+
|
24
|
+
3. Create a new topic branch (off of `master`) to contain your feature, change,
|
25
|
+
or fix.
|
26
|
+
|
27
|
+
**IMPORTANT**: Making changes in `master` is discouraged. You should always
|
28
|
+
keep your local `master` in sync with upstream `master` and make your
|
29
|
+
changes in topic branches.
|
30
|
+
|
31
|
+
```sh
|
32
|
+
git checkout -b <topic-branch-name>
|
33
|
+
```
|
34
|
+
|
35
|
+
4. Commit your changes in logical chunks. Keep your commit messages organized,
|
36
|
+
with a short description in the first line and more detailed information on
|
37
|
+
the following lines. Feel free to use Git's
|
38
|
+
[interactive rebase](https://help.github.com/articles/about-git-rebase/)
|
39
|
+
feature to tidy up your commits before making them public.
|
40
|
+
|
41
|
+
5. Make sure all the tests are still passing.
|
42
|
+
|
43
|
+
```sh
|
44
|
+
rspec spec
|
45
|
+
```
|
46
|
+
|
47
|
+
6. Push your topic branch up to your fork:
|
48
|
+
|
49
|
+
```sh
|
50
|
+
git push origin <topic-branch-name>
|
51
|
+
```
|
52
|
+
|
53
|
+
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
|
54
|
+
with a clear title and description.
|
55
|
+
|
56
|
+
8. If you haven't updated your pull request for a while, you should consider
|
57
|
+
rebasing on master and resolving any conflicts.
|
58
|
+
|
59
|
+
**IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
|
60
|
+
should always `git rebase` on `master` to bring your changes up to date when
|
61
|
+
necessary.
|
62
|
+
|
63
|
+
```sh
|
64
|
+
git checkout master
|
65
|
+
git pull upstream master
|
66
|
+
git checkout <your-topic-branch>
|
67
|
+
git rebase master
|
68
|
+
```
|
69
|
+
|
70
|
+
Thank you for your contributions!
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Daniel Iwaniuk
|
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,65 @@
|
|
1
|
+
# Hawatel_tlb
|
2
|
+
|
3
|
+
Hawatel_tlb is a ruby version of load balancer which the purpose is to dynamic return selected address IP/hostname based on specified algorithm. Depend on settings gem have built features like regularly monitoring status backend nodes, response time, calculating fastest node based on history, flapping detection and more. Currently, supported algorithms are round robin, dynamic ration, fastest, ratio and weighted.
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'hawatel_tlb'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install hawatel_tlb
|
19
|
+
|
20
|
+
|
21
|
+
## Available algorithms
|
22
|
+
|Name|Description|
|
23
|
+
|---|---|
|
24
|
+
|dynamicratio| Dynamic load balancing algorithm based on dynamic ratio weights. Weight for each node is setting up by following formula: 100 - (respond_time / sum_respond_time) * 100 |
|
25
|
+
|fastest|Algorithm based on history statistics about respond time and 'online' states (flapping detection). Return host with fastest responds time.|
|
26
|
+
|ratio|Static load balancing algorithm based on ratio weights
|
27
|
+
|roundrobin|Standard [Round-robin](https://en.wikipedia.org/wiki/Round-robin_scheduling) algorithm with fail node and flapping detection|
|
28
|
+
|weighted|Algorithm based on specified by user weight, return currently available host with highest metric. |
|
29
|
+
|
30
|
+
## Example Usage
|
31
|
+
|
32
|
+
###### Create client
|
33
|
+
``` ruby
|
34
|
+
client = HawatelTlb::Client.new
|
35
|
+
```
|
36
|
+
|
37
|
+
###### Add hosts to group
|
38
|
+
``` ruby
|
39
|
+
client.add({:host => 'api.example.com', :port => 80, :weight => 0, :state => 'enable'})
|
40
|
+
client.add({:host => 'api.example2.com', :port => 80, :weight => 0, :state => 'enable'})
|
41
|
+
client.add({:host => 'api.example3.com', :port => 80, :weight => 0, :state => 'enable'})
|
42
|
+
```
|
43
|
+
|
44
|
+
###### Set algorithm
|
45
|
+
``` ruby
|
46
|
+
client.configure({:mode => 'fastest'})
|
47
|
+
```
|
48
|
+
|
49
|
+
###### Call API request
|
50
|
+
``` ruby
|
51
|
+
p client.node
|
52
|
+
{:host=>"example2.com", :port=>80}
|
53
|
+
|
54
|
+
Net::HTTP.get(client.node[:host], '/q=seach_example')
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
See [CONTRIBUTING](CONTRIBUTING.md)
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
65
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hawatel_tlb"
|
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
|
data/bin/setup
ADDED
data/hawatel_tlb.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hawatel_tlb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hawatel_tlb"
|
8
|
+
spec.version = HawatelTlb::VERSION
|
9
|
+
spec.authors = ['Daniel Iwaniuk', 'Przemyslaw Mantaj']
|
10
|
+
spec.email = ['daniel.iwaniuk@hawatel.com', 'przemyslaw.mantaj@hawatel.com']
|
11
|
+
|
12
|
+
spec.summary = "Ruby gem for failover detection and balancing hosts group"
|
13
|
+
spec.description = %q{Hawatel_tlb is a ruby version load balancing which the purpose is to dynamic return selected address IP/domainame based on specified algorithm}
|
14
|
+
spec.homepage = "http://github.com/hawatel/hawatel_tlb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 1.9'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
data/lib/hawatel_tlb.rb
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'hawatel_tlb/watchdog'
|
2
|
+
require 'hawatel_tlb/mode'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
module HawatelTlb
|
6
|
+
class Client
|
7
|
+
include HawatelTlb::WatchDog
|
8
|
+
|
9
|
+
attr_reader :group, :mode, :interval
|
10
|
+
|
11
|
+
WORKING_MODE = {
|
12
|
+
'dynamicratio' => Mode::DynamicRatio,
|
13
|
+
'roundrobin' => Mode::RoundRobin,
|
14
|
+
'fastest' => Mode::Fastest,
|
15
|
+
'ratio' => Mode::Ratio,
|
16
|
+
'weighted' => Mode::Weighted
|
17
|
+
}
|
18
|
+
DEFAULT_STATE = 'enable'
|
19
|
+
DEFAULT_PORT = 80
|
20
|
+
DEFAULT_WEIGHT = 1
|
21
|
+
DEFAULT_TIMEOUT = 5
|
22
|
+
DEFAULT_INTERVAL = 5
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@group = Array.new
|
26
|
+
@interval = DEFAULT_INTERVAL
|
27
|
+
watchdog_thread()
|
28
|
+
end
|
29
|
+
|
30
|
+
# Add host to TLB group
|
31
|
+
#
|
32
|
+
# @param args [Hash]
|
33
|
+
# @option args [String] :host hostname or ip address
|
34
|
+
# @option args [Integer] :port port number
|
35
|
+
# @option args [Integer] :timeout default 5s.
|
36
|
+
# @option args [String] :state Available options: enable(default), disable.
|
37
|
+
# @option args [Integer] :wight priority of host, higher value is equal lower priority
|
38
|
+
# @example
|
39
|
+
# client.add({:value => 'example'})
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# @return [Integer]
|
43
|
+
def add(args)
|
44
|
+
host = args[:host]
|
45
|
+
port = args[:port] || DEFAULT_PORT
|
46
|
+
weight = args[:weight] || DEFAULT_WEIGHT
|
47
|
+
timeout = args[:timeout] || DEFAULT_TIMEOUT
|
48
|
+
state = args[:state] || DEFAULT_STATE
|
49
|
+
|
50
|
+
status = validate_host_settings({:host => host, :port => port, :weight => weight, :timeout => timeout,
|
51
|
+
:state => state})
|
52
|
+
if status == 'success'
|
53
|
+
host_cfg = OpenStruct.new(:id => host_id.to_i, :host => host, :port => port, :weight => weight,
|
54
|
+
:timeout => timeout, :state => state, :status => {:time => 0, :state => 'offline',
|
55
|
+
:respond_time => 0})
|
56
|
+
@group.push(host_cfg)
|
57
|
+
end
|
58
|
+
@mode.refresh(@group) if @mode
|
59
|
+
return status
|
60
|
+
end
|
61
|
+
|
62
|
+
# Delete host from TLB group
|
63
|
+
#
|
64
|
+
# @param id [Integer] host id
|
65
|
+
# @example
|
66
|
+
# client.del(2)
|
67
|
+
#
|
68
|
+
# @return [String]
|
69
|
+
def del(id)
|
70
|
+
if id.is_a?(Fixnum)
|
71
|
+
if @group[id-1]
|
72
|
+
@group.delete(id-1)
|
73
|
+
@mode.refresh(@group) if @mode
|
74
|
+
return 'host successful deleted'
|
75
|
+
else
|
76
|
+
return 'invalid host id'
|
77
|
+
end
|
78
|
+
else
|
79
|
+
'invalid value'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
# Return hosts list
|
85
|
+
#
|
86
|
+
# @example
|
87
|
+
# client.add({:host => 'example.com', :port => '80', weight => '5'})
|
88
|
+
# client.list
|
89
|
+
#
|
90
|
+
# @return [Array<String>]
|
91
|
+
def list
|
92
|
+
return @group
|
93
|
+
end
|
94
|
+
|
95
|
+
# Set settings for group
|
96
|
+
#
|
97
|
+
# @param args [Hash]
|
98
|
+
# @option args [String] :mode failover, roundrobin
|
99
|
+
# @example
|
100
|
+
# client.configure({:mode => 'roundrobin'})
|
101
|
+
#
|
102
|
+
# @return [Integer]
|
103
|
+
def configure(args)
|
104
|
+
if @group[0]
|
105
|
+
watcher if @group[0].status[:time] == 0
|
106
|
+
end
|
107
|
+
|
108
|
+
mode = args[:mode] || 'roundrobin'
|
109
|
+
@interval if args[:interval]
|
110
|
+
return 'invalid mode' if !WORKING_MODE.key?(mode)
|
111
|
+
if @mode
|
112
|
+
@mode.destroy if @mode.respond_to?(:destroy)
|
113
|
+
@mode = nil
|
114
|
+
else
|
115
|
+
@mode = nil
|
116
|
+
end
|
117
|
+
@mode = WORKING_MODE[mode].new(@group)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
# Description
|
122
|
+
#
|
123
|
+
# @param args [Hash]
|
124
|
+
# @option args [String] :value description
|
125
|
+
# @example
|
126
|
+
# counte({:value => 'example'})
|
127
|
+
#
|
128
|
+
#
|
129
|
+
# @return [Integer]
|
130
|
+
def node
|
131
|
+
return @mode.node if @mode
|
132
|
+
false
|
133
|
+
end
|
134
|
+
|
135
|
+
# Description
|
136
|
+
#
|
137
|
+
# @param args [Hash]
|
138
|
+
# @option args [String] :value description
|
139
|
+
# @example
|
140
|
+
# counte({:value => 'example'})
|
141
|
+
#
|
142
|
+
#
|
143
|
+
# @return [Integer]
|
144
|
+
def status
|
145
|
+
@group.each do |node|
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
# start watcher, thread responsible for online monitoring
|
152
|
+
def watchdog_thread
|
153
|
+
@thr = Thread.new {
|
154
|
+
while(true)
|
155
|
+
watcher
|
156
|
+
sleep(@interval)
|
157
|
+
end
|
158
|
+
}
|
159
|
+
rescue
|
160
|
+
false
|
161
|
+
end
|
162
|
+
|
163
|
+
# Validate host settings
|
164
|
+
def validate_host_settings(args)
|
165
|
+
msg = ''
|
166
|
+
msg << 'host with the same configuration already exists in the group, ' if !validate_list?(args)
|
167
|
+
msg << 'incorrect host name or ip address, ' if !(valid_v4?(args[:host]) || valid_domain?(args[:host]))
|
168
|
+
msg << 'incorrect port number, ' if !valid_port?(args[:port])
|
169
|
+
msg << 'incorrect weight value, ' if !args[:weight].is_a?(Fixnum)
|
170
|
+
msg << 'incorrect timeout value, ' if !args[:timeout].is_a?(Fixnum)
|
171
|
+
msg.empty? ? 'success' : msg
|
172
|
+
end
|
173
|
+
|
174
|
+
# Check if specified host, with the same port dosen't already exist in group
|
175
|
+
def validate_list?(args)
|
176
|
+
@group.each do |item|
|
177
|
+
return false if item[:host] == args[:host] && item[:port] == args[:port]
|
178
|
+
end
|
179
|
+
return true
|
180
|
+
end
|
181
|
+
|
182
|
+
# validate ipv4
|
183
|
+
def valid_v4?(addr)
|
184
|
+
if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
|
185
|
+
return $~.captures.all? {|i| i.to_i < 256}
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# Validate domainname
|
190
|
+
def valid_domain?(host)
|
191
|
+
true if /([a-z]|[0-9])*\.[a-z]{2,5}$/ =~ host
|
192
|
+
end
|
193
|
+
|
194
|
+
# Validate port
|
195
|
+
def valid_port?(port)
|
196
|
+
if port.is_a?(Fixnum)
|
197
|
+
true if port <= 65535 && port > 0
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# Generate host id
|
202
|
+
def host_id
|
203
|
+
return @group.length + 1
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
end
|