forked from hanabokuro/activerecord-turntable
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] redisによるsequencerを追加 #5
Open
4cteru
wants to merge
11
commits into
mixigroup:tiepadrino
Choose a base branch
from
4cteru:add_sequencer_redis
base: tiepadrino
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c1b67bd
add redis sequencer
4cteru 11b2174
check the key exists before execute incrby
4cteru d266ae3
fix spec
4cteru 5931da7
add redis sequencer spec
4cteru 3ef2572
fix spec
4cteru 442d978
add sequencer spec
4cteru 4885dcf
update readme
4cteru 9255e66
remove test code
4cteru 2dcbafd
check sequence type
4cteru f013312
delete blank line
4cteru e643f34
rename redis shard cluster (readme)
4cteru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,11 @@ def initialize(klass, cluster_spec, options = {}) | |
@master_shard = MasterShard.new(klass) | ||
|
||
# setup sequencer | ||
if (seq = (@options[:seq] || @config[:seq])) | ||
@seq_shard = SeqShard.new(seq) | ||
seq = (@options[:seq] || @config[:seq]) | ||
if seq | ||
if seq.values.size > 0 && seq.values.first["seq_type"] == "mysql" | ||
@seq_shard = SeqShard.new(seq.values.first) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. をみると、hashを期待しているようなので、 |
||
end | ||
end | ||
|
||
# setup shards | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# redisを利用しての採番 | ||
# | ||
module ActiveRecord::Turntable | ||
class Sequencer | ||
class Redis < Sequencer | ||
@@clients = {} | ||
|
||
def initialize(klass, options = {}) | ||
require "redis" | ||
@klass = klass | ||
@option = options | ||
end | ||
|
||
def next_sequence_value(sequence_name, offset = 1) | ||
id = client.evalsha(lua_script_sha, argv: [sequence_name, offset] ) | ||
raise SequenceNotFoundError if id.nil? | ||
return id | ||
end | ||
|
||
def current_sequence_value(sequence_name) | ||
id = client.get(sequence_name) | ||
raise SequenceNotFoundError if id.nil? | ||
return Integer(id) rescue raise SequenceValueBrokenError | ||
end | ||
|
||
private | ||
|
||
def client | ||
@@clients[@option] ||= ::Redis.new(@option) | ||
end | ||
|
||
def lua_script_sha | ||
@lua ||= client.script(:load, "return redis.call('EXISTS', ARGV[1]) == 1 and redis.call('INCRBY', ARGV[1], ARGV[2]) or nil") | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'spec_helper' | ||
require 'redis' | ||
|
||
describe ActiveRecord::Turntable::Sequencer::Redis do | ||
let (:sequencer) { ActiveRecord::Turntable::Sequencer::Redis.new(Object, {host: "127.0.0.1"})} | ||
let (:sequencer_name) { "test_sequencer" } | ||
|
||
context "exists sequencer key" do | ||
before do | ||
client = Redis.new(host: "127.0.0.1") | ||
client.set(sequencer_name, 100) | ||
end | ||
|
||
describe "#next_sequence_value" do | ||
context "when use default offset" do | ||
it "return 101" do | ||
expect(sequencer.next_sequence_value(sequencer_name)).to eq 101 | ||
end | ||
end | ||
context "when offset 100" do | ||
it "return 200" do | ||
expect(sequencer.next_sequence_value(sequencer_name, 100)).to eq 200 | ||
end | ||
end | ||
end | ||
|
||
describe "#current_sequence_value" do | ||
it do | ||
expect(sequencer.current_sequence_value(sequencer_name)).to eq 100 | ||
end | ||
end | ||
end | ||
|
||
context "not exists sequencer key" do | ||
before do | ||
client = Redis.new(host: "127.0.0.1") | ||
client.del(sequencer_name) | ||
end | ||
|
||
describe "#next_sequence_value" do | ||
it "raise ActiveRecord::Turntable::SequenceNotFoundError" do | ||
expect { sequencer.next_sequence_value(sequencer_name) }.to raise_error(ActiveRecord::Turntable::SequenceNotFoundError) | ||
end | ||
end | ||
|
||
describe "#current_sequence_value" do | ||
it "raise ActiveRecord::Turntable::SequenceNotFoundError" do | ||
expect { sequencer.current_sequence_value(sequencer_name) }.to raise_error(ActiveRecord::Turntable::SequenceNotFoundError) | ||
end | ||
end | ||
end | ||
|
||
context "sequencer value is not number" do | ||
before do | ||
client = Redis.new(host: "127.0.0.1") | ||
client.set(sequencer_name, "abc") | ||
end | ||
|
||
describe "#next_sequence_value" do | ||
it "raise Redis::CommandError" do | ||
expect { sequencer.next_sequence_value(sequencer_name) }.to raise_error(Redis::CommandError) | ||
end | ||
end | ||
|
||
describe "#current_sequence_value" do | ||
it "raise ActiveRecord::Turntable::SequenceValueBrokenError" do | ||
expect { sequencer.current_sequence_value(sequencer_name) }.to raise_error(ActiveRecord::Turntable::SequenceValueBrokenError) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'spec_helper' | ||
|
||
describe ActiveRecord::Turntable::Sequencer do | ||
before(:all) do | ||
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml")) | ||
end | ||
|
||
before do | ||
establish_connection_to("test") | ||
end | ||
|
||
describe "#build" do | ||
context "when sequencer type mysql" do | ||
it "return ActiveRecord::Turntable::Sequencer::Mysql" do | ||
expect(ActiveRecord::Turntable::Sequencer.build(User)).to be_an_instance_of(ActiveRecord::Turntable::Sequencer::Mysql) | ||
end | ||
end | ||
|
||
context "when sequencer type redis" do | ||
it "return ActiveRecord::Turntable::Sequencer::Redis" do | ||
expect(ActiveRecord::Turntable::Sequencer.build(Friend)).to be_an_instance_of(ActiveRecord::Turntable::Sequencer::Redis) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/m4cteru/activerecord-turntable/blob/e643f3456b4c00fb55924a37ef0a11ec26e6517a/lib/active_record/turntable/sequencer.rb#L23
seq_type無指定の時はmysqlのようですので、
その場合も@seq_shardにセットする必要がありそうです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/drecom/activerecord-turntable/pull/28/files#diff-aa73b66f1c9647a7824bb29f250b55e8R49
とかみても、
seqはそもそもconfig/turntable.ymlではhash想定?なので、
seq["seq_type"]を評価する必要ありそうです。