|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require 'redis_client'
|
| 4 | +require 'redis_client/cluster/pipeline' |
| 5 | +require 'redis_client/cluster/key_slot_converter' |
4 | 6 |
|
5 | 7 | class RedisClient
|
6 | 8 | class Cluster
|
7 | 9 | class Transaction
|
8 | 10 | ConsistencyError = Class.new(::RedisClient::Error)
|
9 | 11 |
|
10 |
| - def initialize(router, command_builder) |
| 12 | + def initialize(router, command_builder, watch) |
11 | 13 | @router = router
|
12 | 14 | @command_builder = command_builder
|
13 |
| - @node_key = nil |
| 15 | + @watch = watch |
| 16 | + @retryable = true |
| 17 | + @pipeline = ::RedisClient::Pipeline.new(@command_builder) |
| 18 | + @buffer = [] |
| 19 | + @node = nil |
14 | 20 | end
|
15 | 21 |
|
16 |
| - def call(*command, **kwargs, &_) |
| 22 | + def call(*command, **kwargs, &block) |
17 | 23 | command = @command_builder.generate(command, kwargs)
|
18 |
| - ensure_node_key(command) |
| 24 | + if prepare(command) |
| 25 | + @pipeline.call_v(command, &block) |
| 26 | + else |
| 27 | + @buffer << -> { @pipeline.call_v(command, &block) } |
| 28 | + end |
19 | 29 | end
|
20 | 30 |
|
21 |
| - def call_v(command, &_) |
| 31 | + def call_v(command, &block) |
22 | 32 | command = @command_builder.generate(command)
|
23 |
| - ensure_node_key(command) |
| 33 | + if prepare(command) |
| 34 | + @pipeline.call_v(command, &block) |
| 35 | + else |
| 36 | + @buffer << -> { @pipeline.call_v(command, &block) } |
| 37 | + end |
24 | 38 | end
|
25 | 39 |
|
26 |
| - def call_once(*command, **kwargs, &_) |
| 40 | + def call_once(*command, **kwargs, &block) |
| 41 | + @retryable = false |
27 | 42 | command = @command_builder.generate(command, kwargs)
|
28 |
| - ensure_node_key(command) |
| 43 | + if prepare(command) |
| 44 | + @pipeline.call_once_v(command, &block) |
| 45 | + else |
| 46 | + @buffer << -> { @pipeline.call_once_v(command, &block) } |
| 47 | + end |
29 | 48 | end
|
30 | 49 |
|
31 |
| - def call_once_v(command, &_) |
| 50 | + def call_once_v(command, &block) |
| 51 | + @retryable = false |
32 | 52 | command = @command_builder.generate(command)
|
33 |
| - ensure_node_key(command) |
| 53 | + if prepare(command) |
| 54 | + @pipeline.call_once_v(command, &block) |
| 55 | + else |
| 56 | + @buffer << -> { @pipeline.call_once_v(command, &block) } |
| 57 | + end |
34 | 58 | end
|
35 | 59 |
|
36 |
| - def execute(watch: nil, &block) |
37 |
| - yield self |
38 |
| - raise ArgumentError, 'empty transaction' if @node_key.nil? |
| 60 | + def execute |
| 61 | + @buffer.each(&:call) |
39 | 62 |
|
40 |
| - node = @router.find_node(@node_key) |
41 |
| - @router.try_delegate(node, :multi, watch: watch, &block) |
| 63 | + raise ArgumentError, 'empty transaction' if @pipeline._empty? |
| 64 | + raise ConsistencyError, "couldn't determine the node: #{@pipeline._commands}" if @node.nil? |
| 65 | + raise ConsistencyError, "unsafe watch: #{@watch.join(' ')}" unless safe_watch? |
| 66 | + |
| 67 | + settle |
42 | 68 | end
|
43 | 69 |
|
44 | 70 | private
|
45 | 71 |
|
46 |
| - def ensure_node_key(command) |
| 72 | + def watch? |
| 73 | + !@watch.nil? && !@watch.empty? |
| 74 | + end |
| 75 | + |
| 76 | + def safe_watch? |
| 77 | + return true unless watch? |
| 78 | + return false if @node.nil? |
| 79 | + |
| 80 | + slots = @watch.map do |k| |
| 81 | + return false if k.nil? || k.empty? |
| 82 | + |
| 83 | + ::RedisClient::Cluster::KeySlotConverter.convert(k) |
| 84 | + end |
| 85 | + |
| 86 | + return false if slots.uniq.size != 1 |
| 87 | + |
| 88 | + @router.find_primary_node_by_slot(slots.first) == @node |
| 89 | + end |
| 90 | + |
| 91 | + def prepare(command) |
| 92 | + return true unless @node.nil? |
| 93 | + |
47 | 94 | node_key = @router.find_primary_node_key(command)
|
48 |
| - raise ConsistencyError, "Client couldn't determine the node to be executed the transaction by: #{command}" if node_key.nil? |
| 95 | + return false if node_key.nil? |
| 96 | + |
| 97 | + @node = @router.find_node(node_key) |
| 98 | + @pipeline.call('WATCH', *@watch) if watch? |
| 99 | + @pipeline.call('MULTI') |
| 100 | + @buffer.each(&:call) |
| 101 | + @buffer.clear |
| 102 | + true |
| 103 | + end |
| 104 | + |
| 105 | + def settle |
| 106 | + @pipeline.call('EXEC') |
| 107 | + @pipeline.call('UNWATCH') if watch? |
| 108 | + send_transaction(@node, redirect: true) |
| 109 | + end |
49 | 110 |
|
50 |
| - @node_key ||= node_key |
51 |
| - raise ConsistencyError, "The transaction should be done for single node: #{@node_key}, #{node_key}" if node_key != @node_key |
| 111 | + def send_transaction(client, redirect:) |
| 112 | + case client |
| 113 | + when ::RedisClient then send_pipeline(client, redirect: redirect) |
| 114 | + when ::RedisClient::Pooled then client.with { |c| send_pipeline(c, redirect: redirect) } |
| 115 | + else raise NotImplementedError, "#{client.class.name}#multi for cluster client" |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + def send_pipeline(client, redirect:) |
| 120 | + results = client.ensure_connected_cluster_scoped(retryable: @retryable) do |connection| |
| 121 | + commands = @pipeline._commands |
| 122 | + client.middlewares.call_pipelined(commands, client.config) do |
| 123 | + connection.call_pipelined(commands, nil) |
| 124 | + rescue ::RedisClient::CommandError => e |
| 125 | + return handle_command_error!(commands, e) if redirect |
| 126 | + |
| 127 | + raise |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + @pipeline._coerce!(results) |
| 132 | + results[watch? ? -2 : -1] |
| 133 | + end |
| 134 | + |
| 135 | + def handle_command_error!(commands, err) |
| 136 | + if err.message.start_with?('CROSSSLOT') |
| 137 | + raise ConsistencyError, "#{err.message}: #{err.command}" |
| 138 | + elsif err.message.start_with?('MOVED', 'ASK') |
| 139 | + ensure_the_same_node!(commands) |
| 140 | + handle_redirection(err) |
| 141 | + else |
| 142 | + raise err |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + def ensure_the_same_node!(commands) |
| 147 | + commands.each do |command| |
| 148 | + node_key = @router.find_primary_node_key(command) |
| 149 | + next if node_key.nil? |
| 150 | + |
| 151 | + node = @router.find_node(node_key) |
| 152 | + next if @node == node |
| 153 | + |
| 154 | + raise ConsistencyError, "the transaction should be executed to a slot in a node: #{commands}" |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + def handle_redirection(err) |
| 159 | + if err.message.start_with?('MOVED') |
| 160 | + node = @router.assign_redirection_node(err.message) |
| 161 | + send_transaction(node, redirect: false) |
| 162 | + elsif err.message.start_with?('ASK') |
| 163 | + node = @router.assign_asking_node(err.message) |
| 164 | + try_asking(node) ? send_transaction(node, redirect: false) : err |
| 165 | + else |
| 166 | + raise err |
| 167 | + end |
| 168 | + end |
52 | 169 |
|
53 |
| - nil |
| 170 | + def try_asking(node) |
| 171 | + node.call('ASKING') == 'OK' |
| 172 | + rescue StandardError |
| 173 | + false |
54 | 174 | end
|
55 | 175 | end
|
56 | 176 | end
|
|
0 commit comments