-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
used uber atomic bool instead standard in lock/unlock db #580
Conversation
Pull Request Test Coverage Report for Build 466
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! 🙏
I've been meaning to make this change for a while but haven't gotten around to it.
@@ -285,7 +285,7 @@ func (ch *ClickHouse) Lock() error { | |||
} | |||
func (ch *ClickHouse) Unlock() error { | |||
if !ch.isLocked.CAS(true, false) { | |||
return database.ErrLocked | |||
return database.ErrNotLocked |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this is my bug))
database/pgx/pgx.go
Outdated
@@ -220,7 +221,7 @@ func (p *Postgres) Close() error { | |||
|
|||
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS | |||
func (p *Postgres) Lock() error { | |||
if p.isLocked { | |||
if p.isLocked.Load() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should replace this with CAS
, release the local if the postgres lock fails, and remove the CAS
call below for a proper local lock implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of thanks for review. fixed it
database/postgres/postgres.go
Outdated
@@ -214,7 +215,7 @@ func (p *Postgres) Close() error { | |||
|
|||
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS | |||
func (p *Postgres) Lock() error { | |||
if p.isLocked { | |||
if p.isLocked.Load() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here about fixing the local lock optimization implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed it
database/sqlserver/sqlserver.go
Outdated
@@ -154,7 +155,7 @@ func (ss *SQLServer) Close() error { | |||
|
|||
// Lock creates an advisory local on the database to prevent multiple migrations from running at the same time. | |||
func (ss *SQLServer) Lock() error { | |||
if ss.isLocked { | |||
if ss.isLocked.Load() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here about fixing the local lock optimization implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thnx,fixed it
database/sqlserver/sqlserver.go
Outdated
@@ -181,7 +184,7 @@ func (ss *SQLServer) Lock() error { | |||
|
|||
// Unlock froms the migration lock from the database | |||
func (ss *SQLServer) Unlock() error { | |||
if !ss.isLocked { | |||
if !ss.isLocked.Load() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here about fixing the local lock optimization implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thnx,fixed it
database/mysql/mysql.go
Outdated
} | ||
return nil | ||
if !success { | ||
m.isLocked.Store(false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment here about how removing the local lock in this situation is desired since the local lock is an optimization and it's not trying to reflect the state of the db lock.
Actually, let's add our own CAS wrapper to automatically restore the lock state on error (in database/util.go
) so we don't need to remember to do it:
func casRestoreOnErr(lock *atomic.Bool, o, n bool, f func() error) error {
if !lock.CAS(o, n) {
return ErrLocked
}
if err := f(); err != nil {
// Automatically unlock
lock.Store(o)
return err
}
return nil
}
Also, can you add tests for this wrapper?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. Of course I will do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, review pls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for addressing my feedback! Just a few minor fixes left!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing my feedback!
Sorry for the delay, I must have missed a notification that there were more commits to review...
database/mongodb/mongodb.go
Outdated
return database.CasRestoreOnErr(&m.isLocked, false, true, database.ErrLocked, func() error { | ||
if !m.config.Locking.Enabled { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can always try to acquire the local lock before acquiring the db lock even if locking is disabled. IIRC, the locking enabled config was added for backwards compatibility and to avoid an extra network hop for existing clients
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks. Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for the PR and addressing all of the feedback!
No description provided.