@@ -5,96 +5,101 @@ require 'bundler/setup'
55require 'redis_cluster_client'
66
77module SinglePipTxDebug
8+ WAIT_SEC = 2.0
9+
810 module_function
911
10- def spawn_single ( cli )
11- Thread . new ( cli ) do |r |
12- role = ' Single'
12+ def spawn_single ( client , key )
13+ Thread . new ( client , key ) do |cli , k |
14+ role = 'Single'
1315
1416 loop do
1517 handle_errors ( role ) do
16- reply = r . call ( 'incr' , 'single' )
17- log " #{ role } : #{ reply } "
18+ reply = cli . call ( 'incr' , k )
19+ log ( role , k , reply )
1820 end
1921 ensure
20- sleep 1.0
22+ sleep WAIT_SEC
2123 end
2224 rescue StandardError => e
23- log " #{ role } : dead: #{ e . class } : #{ e . message } "
25+ log ( role , :dead , e . class , e . message )
2426 raise
2527 end
2628 end
2729
28- def spawn_pipeline ( cli )
29- Thread . new ( cli ) do |r |
30- role = ' Pipeline'
30+ def spawn_pipeline ( client , key )
31+ Thread . new ( client , key ) do |cli , k |
32+ role = 'Pipeline'
3133
3234 loop do
3335 handle_errors ( role ) do
34- reply = r . pipelined do |pi |
35- pi . call ( 'incr' , 'pipeline' )
36- pi . call ( 'incr' , 'pipeline' )
36+ reply = cli . pipelined do |pi |
37+ pi . call ( 'incr' , k )
38+ pi . call ( 'incr' , k )
3739 end
3840
39- log " #{ role } : #{ reply } "
41+ log ( role , k , reply . last )
4042 end
4143 ensure
42- sleep 1.0
44+ sleep WAIT_SEC
4345 end
4446 rescue StandardError => e
45- log " #{ role } : dead: #{ e . class } : #{ e . message } "
47+ log ( role , :dead , e . class , e . message )
4648 raise
4749 end
4850 end
4951
50- def spawn_transaction ( cli )
51- Thread . new ( cli ) do |r |
52+ def spawn_transaction ( client , key )
53+ Thread . new ( client , key ) do |cli , k |
5254 role = 'Transaction'
5355 i = 0
5456
5557 loop do
5658 handle_errors ( role ) do
57- reply = r . multi ( watch : i . odd? ? %w[ transaction ] : nil ) do |tx |
58- i += 1
59- tx . call ( 'incr' , 'transaction' )
60- tx . call ( 'incr' , 'transaction' )
61- tx . call ( 'incr' , 'transaction' )
59+ reply = cli . multi ( watch : i . odd? ? [ k ] : nil ) do |tx |
60+ tx . call ( 'incr' , k )
61+ tx . call ( 'incr' , k )
6262 end
6363
64- log "#{ role } : #{ reply } "
64+ log ( role , k , reply . last )
65+ i += 1
6566 end
6667 ensure
67- sleep 1.0
68+ sleep WAIT_SEC
6869 end
6970 rescue StandardError => e
70- log " #{ role } : dead: #{ e . class } : #{ e . message } "
71+ log ( role , :dead , e . class , e . message )
7172 raise
7273 end
7374 end
7475
7576 def handle_errors ( role ) # rubocop:disable Metrics/AbcSize
7677 yield
7778 rescue RedisClient ::ConnectionError , RedisClient ::Cluster ::InitialSetupError , RedisClient ::Cluster ::NodeMightBeDown => e
78- log " #{ role } : #{ e . class } "
79+ log ( role , e . class )
7980 rescue RedisClient ::CommandError => e
80- log " #{ role } : #{ e . class } : #{ e . message } "
81+ log ( role , e . class , e . message )
8182 raise unless e . message . start_with? ( 'CLUSTERDOWN' )
8283 rescue RedisClient ::Cluster ::ErrorCollection => e
83- log " #{ role } : #{ e . class } : #{ e . message } "
84+ log ( role , e . class , e . message )
8485 raise unless e . errors . values . all? do |err |
8586 err . message . start_with? ( 'CLUSTERDOWN' ) || err . is_a? ( ::RedisClient ::ConnectionError )
8687 end
8788 rescue StandardError => e
88- log " #{ role } : #{ e . class } : #{ e . message } "
89+ log ( role , e . class , e . message )
8990 raise
9091 end
9192
92- def log ( msg )
93- print "#{ msg } \n "
93+ def log ( *texts )
94+ return if texts . nil? || texts . empty?
95+
96+ message = texts . map { |text | "#{ ' ' * [ 15 - text . to_s . size , 0 ] . max } #{ text } " } . join ( ': ' )
97+ print "#{ message } \n "
9498 end
9599end
96100
97- clients = Array . new ( 3 ) { RedisClient . cluster ( connect_with_original_config : true ) . new_client }
101+ nodes = ( 6379 ..6384 ) . map { |port | "redis://127.0.0.1:#{ port } " } . freeze
102+ clients = Array . new ( 9 ) { RedisClient . cluster ( nodes : nodes , connect_with_original_config : true ) . new_client } . freeze
98103threads = [ ]
99104
100105Signal . trap ( :INT ) do
@@ -104,7 +109,16 @@ Signal.trap(:INT) do
104109 exit 0
105110end
106111
107- threads << SinglePipTxDebug . spawn_single ( clients [ 0 ] )
108- threads << SinglePipTxDebug . spawn_pipeline ( clients [ 1 ] )
109- threads << SinglePipTxDebug . spawn_transaction ( clients [ 2 ] )
112+ %w[ single1 single3 single4 ] . each_with_index do |key , i |
113+ threads << SinglePipTxDebug . spawn_single ( clients [ i ] , key )
114+ end
115+
116+ %w[ pipeline1 pipeline2 pipeline4 ] . each_with_index do |key , i |
117+ threads << SinglePipTxDebug . spawn_pipeline ( clients [ i + 3 ] , key )
118+ end
119+
120+ %w[ transaction1 transaction3 transaction4 ] . each_with_index do |key , i |
121+ threads << SinglePipTxDebug . spawn_transaction ( clients [ i + 6 ] , key )
122+ end
123+
110124threads . each ( &:join )
0 commit comments