Ability to scale peatio daemons
Symptoms:
When running stress tests at 50 or 200 orders per second we notice there is an rapidly increasing number of orders waiting in the queue.
After 25min of high load we accumulated about 100,000 orders waiting to be executed into trades.
We also noticed that most of these orders were already executed or invalid, with empty provision, or state "done"
We are still at the stage of understanding the origin of the problem but we suspect the large scaling to be causing repetitive processing, we process too many times the same order.
Deadlock found when trying to get lock; try restarting transaction:
SELECT `accounts`.* FROM `accounts`
WHERE `accounts`.`member_id` = 594 AND `accounts`.`currency_id` = 'eur'
LIMIT 1 FOR UPDATE
Error trace:
#<ActiveRecord::StatementInvalid: Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction: SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`member_id` = 594 AND `accounts`.`currency_id` = 'eur' LIMIT 1 FOR UPDATE>
/opt/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `_query'
/opt/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `block in query'
/opt/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `handle_interrupt'
/opt/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:119:in `query'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:317:in `block in execute'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/opt/vendor/bundle/ruby/2.5.0/gems/activesupport-4.2.10/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:317:in `execute'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/mysql2_adapter.rb:217:in `execute'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/mysql2_adapter.rb:221:in `exec_query'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:356:in `select'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/querying.rb:39:in `find_by_sql'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation.rb:639:in `exec_queries'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation.rb:515:in `load'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation.rb:243:in `to_a'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation/finder_methods.rb:475:in `find_take'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation/finder_methods.rb:105:in `take'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation/finder_methods.rb:111:in `take!'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation/finder_methods.rb:92:in `find_by!'
/home/app/app/models/order_bid.rb:25:in `expect_account!'
/home/app/app/models/order.rb:76:in `strike'
/home/app/app/trading/matching/executor.rb:71:in `block in create_trade_and_strike_orders'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/opt/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:220:in `transaction'
/home/app/app/trading/matching/executor.rb:51:in `create_trade_and_strike_orders'
/home/app/app/trading/matching/executor.rb:31:in `execute!'
/home/app/app/trading/matching/executor.rb:18:in `execute'
/home/app/app/workers/worker/trade_executor.rb:7:in `process'
lib/daemons/amqp_daemon.rb:60:in `call'
lib/daemons/amqp_daemon.rb:60:in `block (2 levels) in <main>'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/consumer.rb:56:in `call'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/channel.rb:1717:in `block in handle_frameset'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/consumer_work_pool.rb:107:in `block (2 levels) in run_loop'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/consumer_work_pool.rb:102:in `loop'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/consumer_work_pool.rb:102:in `block in run_loop'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/consumer_work_pool.rb:101:in `catch'
/opt/vendor/bundle/ruby/2.5.0/gems/bunny-2.9.2/lib/bunny/consumer_work_pool.rb:101:in `run_loop'
@dkkoval This is normal behavior. Deadlocks may happen when doing concurrent inserts. Now the daemon will requeue only if catches exception which is instance of TradeExecutionError. In case ActiveRecord error it will totally fail and Bunny will put job back to RabbitMQ so it will be repeated. This daemons is scalable.
This table shows if daemons are scalable.
amqp:deposit_coin: YES
amqp:deposit_coin_address: YES
amqp:market_ticker: YES
amqp:matching: IMPOSSIBLE
amqp:order_processor: YES
amqp:pusher_market: YES
amqp:pusher_member: YES
amqp:slave_book: NO
amqp:trade_executor: YES
amqp:withdraw_coin: YES
coins: YES
global_state: YES
k: YES
payment_transaction: YES
withdraw_audit: YES