-
Notifications
You must be signed in to change notification settings - Fork 11
Support watch & unwatch properly #295
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
Closed
KJTsanaktsidis
wants to merge
5
commits into
redis-rb:master
from
zendesk:ktsanaktsidis/implicit_transactions
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01a6057
Don't call the block twice for #multi
b6f6c1b
Force reads issued during a multi transacton to go to the primary
KJTsanaktsidis 8c12d69
Implement RedisClient::Cluster::Command#extract_all_keys
KJTsanaktsidis c3daaa4
Implement "implicit transactions" as used by redis-rb
KJTsanaktsidis 1a6fcd2
Add a failing test for concurent transactions
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -25,36 +25,37 @@ def inspect | |
|
||
def call(*args, **kwargs, &block) | ||
command = @command_builder.generate(args, kwargs) | ||
@router.send_command(:call_v, command, &block) | ||
send_command(:call_v, command, &block) | ||
end | ||
|
||
def call_v(command, &block) | ||
command = @command_builder.generate(command) | ||
@router.send_command(:call_v, command, &block) | ||
send_command(:call_v, command, &block) | ||
end | ||
|
||
def call_once(*args, **kwargs, &block) | ||
command = @command_builder.generate(args, kwargs) | ||
@router.send_command(:call_once_v, command, &block) | ||
send_command(:call_once_v, command, &block) | ||
end | ||
|
||
def call_once_v(command, &block) | ||
command = @command_builder.generate(command) | ||
@router.send_command(:call_once_v, command, &block) | ||
send_command(:call_once_v, command, &block) | ||
end | ||
|
||
def blocking_call(timeout, *args, **kwargs, &block) | ||
command = @command_builder.generate(args, kwargs) | ||
@router.send_command(:blocking_call_v, command, timeout, &block) | ||
send_command(:blocking_call_v, command, timeout, &block) | ||
end | ||
|
||
def blocking_call_v(timeout, command, &block) | ||
command = @command_builder.generate(command) | ||
@router.send_command(:blocking_call_v, command, timeout, &block) | ||
send_command(:blocking_call_v, command, timeout, &block) | ||
end | ||
|
||
def scan(*args, **kwargs, &block) | ||
raise ArgumentError, 'block required' unless block | ||
raise ::RedisClient::Cluster::Transaction::ConsistencyError, 'scan is not valid inside a transaction' if @transaction | ||
|
||
seed = Random.new_seed | ||
cursor = ZERO_CURSOR_FOR_SCAN | ||
|
@@ -66,17 +67,17 @@ def scan(*args, **kwargs, &block) | |
end | ||
|
||
def sscan(key, *args, **kwargs, &block) | ||
node = @router.assign_node(['SSCAN', key]) | ||
node = assign_node(['SSCAN', key]) | ||
@router.try_delegate(node, :sscan, key, *args, **kwargs, &block) | ||
end | ||
|
||
def hscan(key, *args, **kwargs, &block) | ||
node = @router.assign_node(['HSCAN', key]) | ||
node = assign_node(['HSCAN', key]) | ||
@router.try_delegate(node, :hscan, key, *args, **kwargs, &block) | ||
end | ||
|
||
def zscan(key, *args, **kwargs, &block) | ||
node = @router.assign_node(['ZSCAN', key]) | ||
node = assign_node(['ZSCAN', key]) | ||
@router.try_delegate(node, :zscan, key, *args, **kwargs, &block) | ||
end | ||
|
||
|
@@ -90,7 +91,21 @@ def pipelined | |
end | ||
|
||
def multi(watch: nil, &block) | ||
::RedisClient::Cluster::Transaction.new(@router, @command_builder).execute(watch: watch, &block) | ||
# This is tricky and importnat. | ||
# If you call #multi _outside_ of a watch block, you expect that the connection | ||
# has returned to its original state at the end of the block; what was watched with the watch: | ||
# kwarg is unwatched if the transaction is not committed. | ||
# However, if you call #multi in a watch block (from Redis::Cluster), Redis::Cluster#watch actually | ||
# calls unwatch if an exception gets thrown, _including an exception from inside a multi block_. | ||
# So, we need to record whether a transaction already existed before calling multi; if so, we leave | ||
# responsibility for disposing of that transaction to the caller who created it. | ||
transaction_was_preexisting = [email protected]? | ||
build_transaction! | ||
begin | ||
@transaction.multi(watch: watch, &block) | ||
ensure | ||
@transaction = nil if @transaction&.complete? || !transaction_was_preexisting | ||
end | ||
end | ||
|
||
def pubsub | ||
|
@@ -109,7 +124,7 @@ def method_missing(name, *args, **kwargs, &block) | |
if @router.command_exists?(name) | ||
args.unshift(name) | ||
command = @command_builder.generate(args, kwargs) | ||
return @router.send_command(:call_v, command, &block) | ||
return send_command(:call_v, command, &block) | ||
end | ||
|
||
super | ||
|
@@ -120,5 +135,28 @@ def respond_to_missing?(name, include_private = false) | |
|
||
super | ||
end | ||
|
||
def build_transaction! | ||
return if @transaction | ||
|
||
@transaction = ::RedisClient::Cluster::Transaction.new(@router, @command_builder) | ||
end | ||
|
||
def send_command(method, command, *args, &block) | ||
build_transaction! if ::RedisClient::Cluster::Transaction.command_starts_transaction?(command) | ||
if @transaction | ||
begin | ||
@transaction.send_command(method, command, *args, &block) | ||
ensure | ||
@transaction = nil if @transaction&.complete? | ||
end | ||
else | ||
@router.send_command(method, command, *args, &block) | ||
end | ||
end | ||
|
||
def assign_node(command) | ||
@transaction ? @transaction.node : @router.assign_node(command) | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
I didn't end up needing this method but it was tricky enough to write, and a natural enough analogue of
determine_first_key_position
, that i'm almost tempted to suggest it be committed in case it's needed in the future.