seamusabshere/upsert

Race condition on procedure creation? (sidekiq / mysql2)

thbar opened this issue · 4 comments

In a multithreaded environment (sikdeiq), I got one exception during batch upsert:

Mysql2::Error: Mysql2::Error: PROCEDURE upsert2_0_3_push_filters_pushes_SEL_push_filter_id_A_4228041800 already exists

[SNIP]

File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert.rb", line 72 in batch
File "[PROJECT_ROOT]/app/workers/push_preprocessor_worker.rb", line 87 in block (2 levels) in attach_push_filters_to_push!
File "[PROJECT_ROOT]/app/workers/push_preprocessor_worker.rb", line 87 in each
File "[PROJECT_ROOT]/app/workers/push_preprocessor_worker.rb", line 88 in block (3 levels) in attach_push_filters_to_push!
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert.rb", line 216 in row
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert/merge_function.rb", line 13 in execute
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert/merge_function/Mysql2_Client.rb", line 16 in execute
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert/merge_function/Mysql2_Client.rb", line 23 in rescue in execute
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert/merge_function/mysql.rb", line 28 in create!
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert/connection/Mysql2_Client.rb", line 7 in execute
File "[GEM_ROOT]/gems/upsert-2.0.3/lib/upsert/connection/Mysql2_Client.rb", line 7 in query

Is it expected? Would it be nice to retry the upsert automatically in that case?

@thbar we should wrap creation in https://github.com/mceachen/with_advisory_lock

hopefully it doesn't depend on activerecord, if so then we can extract logic i guess

Should be fixed via #47

If this is still an issue, it should be fixed via #81

Marking this as closed, please re-open if it's reproducible with the latest version of Upsert (2.2.1 as of writing).