Rails ActiveRecordで失敗したクエリのリトライ
RailsにおいてSQLクエリが失敗したときのリトライについて
7.1からallow_retryオプションがinternal APIに追加された
https://github.com/rails/rails/pull/46273
7.2からArel経由でbuildされたSELECTや、既知のattributesに対するfind, find_byは自動でリトライされるようになった
https://github.com/rails/rails/pull/51336
* SELECT queries we construct by walking the Arel tree via #to_sql_and_binds. We use a new retryable attribute on collector classes, which defaults to true for most node types, but will be set to false for non-idempotent node types (functions, SQL literals, update / delete / insert statements, etc). The retryable value is returned from #to_sql_and_binds and used by #select_all and passed down the call stack, eventually reaching the adapter's #internal_exec_query method.
> * #find and #find_by queries with known attributes. We set allow_retry: true in #cached_find_by, and pass this down to #find_by_sql and #_query_by_sql.
リトライされるのはDeadLock, MySQL Lock Wait Timeout, あるいはコネクション関連のエラーの場合のみ
https://github.com/rails/rails/blob/v8.0.2/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb#L1059-L1076
ohbarye.icon 外部から指定するにはmonkey patchが必要そう
関連
【Rails】ActiveRecordのallow_retryを使ったクエリのリトライ処理の実装