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

Fix lockyloo golang #8

Merged
merged 4 commits into from
Feb 20, 2018
Merged

Conversation

rdallman
Copy link

source: mattes/migrate#299

got wind that this is the new happening place for development

Quick summary:

  • mysql driver relies on >=go1.9 now
  • mysql driver now uses same conn for migrations to fix GET_LOCK & RELEASE_LOCK behavior
  • upped time out to 10s on GET_LOCK, 1s timeout can too easily leave us in a
    state where we think we have the lock but it has timed out (during the
    operation).
  • fixes SetVersion which was not using the tx it was intending to, and fixed a
    bug where the transaction could have been left open since Rollback or Commit
    may never have been called.

thanks for taking over maintenance of this project.

Reed Allman added 3 commits February 9, 2018 00:27
I believe this closes mattes#297 as well.

I have been working on adding testing of migrations and it requires acquiring
the lock in mysql multiple times to go up and down. After nailing this down to
GET_LOCK returning a failure for every subsequent GET_LOCK call after the
first, I decided it was time to rtfm and lo and behold:

https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_release-lock

RELEASE_LOCK will not work if called from a different thread than GET_LOCK.
The simplest solution using the golang database/sql pkg appears to be to just
get a single conn to use for every operation. since migrations are happening
sequentially, I don't think this will be a performance hit (possible
improvement). now calling Lock() and Unlock() multiple times will work;
prior to this patch, every call to RELEASE_LOCK would fail. this required
minimal changes to use the *sql.Conn methods instead of the *sql.DB methods.

other changes:

* upped time out to 10s on GET_LOCK, 1s timeout can too easily leave us in a
state where we think we have the lock but it has timed out (during the
operation).
* fixes SetVersion which was not using the tx it was intending to, and fixed a
bug where the transaction could have been left open since Rollback or Commit
may never have been called.

I added a test but it does not seem to come up in the previous patch, at least
easily, I tried some shenanigans. At least, this behavior should be fixed with
this patch and hopefully the test / comment is advisory enough.

thank you for maintaining this library, it has been relatively easy to
integrate with and most stuff works (pg works great :)
@coveralls
Copy link

coveralls commented Feb 12, 2018

Pull Request Test Coverage Report for Build 39

  • 0 of 0 (NaN%) changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 49.402%

Totals Coverage Status
Change from base Build 38: 0.0%
Covered Lines: 826
Relevant Lines: 1672

💛 - Coveralls

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for porting your PR over!

I'll hold off on merging until Go 1.10 is out (should be within the next 2 weeks) since this change will require Go 1.9+ and we should follow Go's policy where the latest 2 releases are supported.

db *sql.DB
// mysql RELEASE_LOCK must be called from the same conn, so
// just do everything over a single conn anyway.
db *sql.Conn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename db to conn

@@ -162,9 +172,9 @@ func (m *Mysql) Lock() error {
return err
}

query := "SELECT GET_LOCK(?, 1)"
query := "SELECT GET_LOCK(?, 10)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make the timeout configurable, but I think it's fine to make that change in a separate PR

@rdallman
Copy link
Author

go 1.10 is released bump :) woohoo, tests are fast now

@dhui dhui merged commit 8f6826c into golang-migrate:master Feb 20, 2018
@dhui
Copy link
Member

dhui commented Feb 21, 2018

Fixed in release v3.2.0

@rdallman rdallman deleted the fix-lockyloo-golang branch February 21, 2018 17:50
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 this pull request may close these issues.

3 participants