lets_shard 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: 8e1242830aacecf1dfdb28c0253b37728055c0cb8b7c622b298e96c5ee743cc4
4
+ data.tar.gz: a02dfb193dd088631e62ba56b9e896a254097189630ce0a0f86f7fac0f70a17b
5
+ SHA512:
6
+ metadata.gz: 8c768ca8d7cddec5be799fff150780fb945b16139344788ce70b933d7573b69fa36d77f1d1354135625ed8db72ec7f55e13d6ed29c97ebe6dc489506e8fce599
7
+ data.tar.gz: 1f380c18b3d65cbfd8315272e81fa11a29d96b57ba6ba76f116ebbdc0b5ce9a21d029a65d06e36d1b1f3ed4bfd877811645e13c64013033df8bbd69954078b46
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+ .ruby-version
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at omar.abdula@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lets_shard.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Omar Alshboul
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # LetsShard
2
+
3
+ LetsShard is an easy way to shard and distribute load/use on any set of objects!
4
+ One of the best uses is to shard [Redis](https://redis.io/)
5
+
6
+ So simply run your instances and let **LetsShard** shard your usage on them!
7
+
8
+ **LetsShard** distributes and shards things in a way somehow similar to [Redis Cluster](https://redis.io/topics/cluster-tutorial)
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'lets_shard'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install lets_shard
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'lets_shard'
31
+
32
+ objects = (0..10).map { |_| AnyObject.new } # AnyObject is an arbitrary class and not included in the gem.
33
+
34
+ my_shards = LetsShard.new(objects)
35
+
36
+ # To see all objects including the intervals they cover
37
+ my_shards.shards
38
+
39
+ # To get the object that covers "blah"
40
+ my_object = my_shards.get_object("blah")
41
+ my_object.foo
42
+
43
+ # To get the shard (including the intervals and the object it self) that covers "blah"
44
+ my_shard = my_shards.get_shard("blah")
45
+
46
+ # To see slots/intervals that "my_shard" covers
47
+ my_shard.start_slot
48
+ my_shard.end_slot
49
+
50
+ # To use the object of this interval
51
+ my_shard.object.foo
52
+ ```
53
+
54
+ ### An example with redis
55
+
56
+ ```ruby
57
+ require 'lets_shard'
58
+ require 'redis'
59
+
60
+ redis1 = Redis.new(host: "10.0.0.1", port: 6379)
61
+ redis2 = Redis.new(host: "10.0.0.2", port: 6379)
62
+
63
+ redis_shards = LetsShard.new([redis1, redis2])
64
+
65
+ redis_shards.get_object('key').set('key', 'value')
66
+ redis_shards.get_object('key').get('key')
67
+
68
+ # redis_shards.get_object('key')... any redis command!
69
+ ```
70
+
71
+ ### LetsShard let you distribute objects usage better!
72
+
73
+ If you are use 3 redis instances in 3 servers, but one of these servers has more
74
+ powerful specifications than the others and you want to utilize its power.
75
+
76
+ **LetsShard** can solve it for you, easily!
77
+
78
+ ```ruby
79
+ require 'lets_shard'
80
+ require 'redis'
81
+
82
+ redis1 = Redis.new(host: "10.0.0.1", port: 6379) # The most powerful server
83
+ redis2 = Redis.new(host: "10.0.0.2", port: 6379)
84
+ redis3 = Redis.new(host: "10.0.0.3", port: 6379)
85
+
86
+ redises = [redis1, redis2, redis3]
87
+ weights = [2, 1, 1] # By default, the weight of each object is 1
88
+
89
+ redis_shards = LetsShard.new(redises, weights)
90
+
91
+ # So now LetsShard will give redis1 more slots to utilize it more!
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/oshboul/lets_shard. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/lets_shard/blob/master/CODE_OF_CONDUCT.md).
97
+
98
+
99
+ ## Code of Conduct
100
+
101
+ Everyone interacting in the LetsShard project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lets_shard/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lets_shard"
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(__FILE__)
data/bin/setup ADDED
@@ -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,23 @@
1
+ require_relative 'lib/lets_shard/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'lets_shard'
5
+ spec.version = LetsShard::VERSION
6
+ spec.authors = ['oshboul']
7
+ spec.email = ['omar.abdula@gmail.com']
8
+
9
+ spec.summary = spec.description = 'An easy way to shard anything!'
10
+ spec.homepage = 'https://github.com/oshboul/lets_shard'
11
+
12
+ # Specify which files should be added to the gem when it is released.
13
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
14
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ end
19
+
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+ end
@@ -0,0 +1,51 @@
1
+ class LetsShard
2
+ module Digest
3
+ extend self
4
+
5
+ X_MODEM_CRC16_LOOKUP = [
6
+ 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
7
+ 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
8
+ 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
9
+ 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
10
+ 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
11
+ 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
12
+ 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
13
+ 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
14
+ 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
15
+ 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
16
+ 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
17
+ 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
18
+ 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
19
+ 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
20
+ 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
21
+ 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
22
+ 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
23
+ 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
24
+ 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
25
+ 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
26
+ 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
27
+ 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
28
+ 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
29
+ 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
30
+ 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
31
+ 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
32
+ 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
33
+ 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
34
+ 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
35
+ 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
36
+ 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
37
+ 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
38
+ ]
39
+
40
+ def crc16(key)
41
+ result = 0
42
+
43
+ key.each_byte do |b|
44
+ result = ((result << 8) & 0xffff) ^
45
+ X_MODEM_CRC16_LOOKUP[((result >> 8) ^ b) & 0xff]
46
+ end
47
+
48
+ result
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ class LetsShard
2
+ class Shard
3
+ attr_reader :object, :start_slot, :end_slot
4
+
5
+ def initialize(object, start_slot, end_slot)
6
+ @object = object
7
+ @start_slot = start_slot
8
+ @end_slot = end_slot
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class LetsShard
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lets_shard.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'lets_shard/version'
2
+ require 'lets_shard/digest'
3
+ require 'lets_shard/shard'
4
+
5
+ class LetsShard
6
+ attr_reader :shards
7
+
8
+ SLOTS = 16384
9
+
10
+ def initialize(objects, weights = [])
11
+ validate_parameters!(objects, weights)
12
+
13
+ @objects = objects
14
+ @weights = weights.any? ? weights : Array.new(@objects.size, 1)
15
+
16
+ set_unit_weight!
17
+ generate_shards!
18
+ end
19
+
20
+ def get_shard(key)
21
+ hkey = get_hkey(key)
22
+
23
+ @shards.find do |shard|
24
+ (shard.start_slot..shard.end_slot).include?(hkey)
25
+ end
26
+ end
27
+
28
+ def get_object(key)
29
+ get_shard(key).object
30
+ end
31
+
32
+ private
33
+
34
+ def generate_shards!
35
+ @shards = []
36
+ start_slot = 0
37
+
38
+ @objects.each_with_index do |object, index|
39
+ end_slot = [start_slot + (@weights[index] * @unit_weight), SLOTS - 1].min
40
+
41
+ @shards << Shard.new(object, start_slot, end_slot)
42
+
43
+ start_slot = end_slot + 1
44
+ end
45
+ end
46
+
47
+ def set_unit_weight!
48
+ @unit_weight = (SLOTS.to_f / @weights.inject(0, :+)).ceil
49
+ end
50
+
51
+ def get_hkey(key)
52
+ Digest.crc16(key.to_s) % SLOTS
53
+ end
54
+
55
+ def validate_parameters!(objects, weights)
56
+ unless objects.is_a?(Array)
57
+ raise LetsShardError, 'Objects should be an array!'
58
+ end
59
+
60
+ unless weights.is_a?(Array)
61
+ raise LetsShardError, 'Weights should be an array!'
62
+ end
63
+
64
+ unless objects.any?
65
+ raise LetsShardError, 'Objects array should not be empty!'
66
+ end
67
+
68
+ if weights.any? && (objects.size != weights.size)
69
+ raise LetsShardError, 'Weights should be as size as objects!'
70
+ end
71
+
72
+ if weights.any? && !weights.all? { |w| w.is_a?(Integer) }
73
+ raise LetsShardError, 'All weights should be integers!'
74
+ end
75
+ end
76
+
77
+ class LetsShardError < StandardError; end
78
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lets_shard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - oshboul
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An easy way to shard anything!
14
+ email:
15
+ - omar.abdula@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - lets_shard.gemspec
29
+ - lib/lets_shard.rb
30
+ - lib/lets_shard/digest.rb
31
+ - lib/lets_shard/shard.rb
32
+ - lib/lets_shard/version.rb
33
+ homepage: https://github.com/oshboul/lets_shard
34
+ licenses: []
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.1.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: An easy way to shard anything!
55
+ test_files: []