Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarity on lock retry behaviour inside and outside transactions #50

Closed
camallen opened this issue Mar 12, 2024 · 4 comments · Fixed by #51
Closed

Clarity on lock retry behaviour inside and outside transactions #50

camallen opened this issue Mar 12, 2024 · 4 comments · Fixed by #51

Comments

@camallen
Copy link
Contributor

camallen commented Mar 12, 2024

I'm seeking clarification about the lock retry behaviour when running the migration outside of a transaction.

In the docs you explicitly state the retry behaviour when the migration runs inside a migration

When statement within transaction fails - the whole transaction is retried.

However when running the migration without a transaction via disable_ddl_transaction! it's not clear in the docs but my assumption is that the LockRetrier does the same. E.g. in the test code it shows the migration is retried by the LockRetrier mechanism (if set).

Can you please clarify the behaviour is as I assume. I'm happy to get a PR up to update the configuring.md doc as well.

@fatkodima
Copy link
Owner

Yes, your assumption is correct. Please do open a PR.

camallen added a commit to camallen/online_migrations that referenced this issue Mar 13, 2024
@ghiculescu
Copy link
Contributor

Hmm, I'm not sure if

# Initial run only, then just `add_column` is retried
is as clear as it could be.

$migrate_attempts is only incremented once, here. After that, add_column :users, :name, :string tries, and fails.

The change in #51 implies that now add_column :users, :name, :string will be retried. If that's the case, I would expect this test to have an infinite loop, or to have the lock be removed and the column cleanly added. If the latter is the intended behaviour, I think the test should assert that. But I think what's more likely is that there is no retrying happening at all in this test.

Given that, I think what the docs should say is:

When a statement within transaction fails, the whole transaction is retried. If any statement fails when running outside a transaction (e.g. using disable_ddl_transaction!), that statement will not be retried.

@fatkodima @camallen what am I missing?

@fatkodima
Copy link
Owner

The $migrate_attemps is equal to 1, because we actually run the whole migration change method only once. It demonstrates, that we do not retry the whole transaction (because there is no even one).
But we run add_column multiple times.

The runtime of the whole migration looks like this:

def change
  $migrate_attemps += 1
  add_column ... => raises lock error
  add_column ... again.. => raises lock error again
end

@ghiculescu
Copy link
Contributor

Ahhh now I see what's going on.

connection.with_lock_retries { super }
is where each statement is wrapped with a lock retrier. Neat 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants