-
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
Postgres: use pg_try_advisory_lock
instead of pg_advisory_lock
#962
Open
AkuSilvenius
wants to merge
19
commits into
golang-migrate:master
Choose a base branch
from
AkuSilvenius:960_add_try_advisory_lock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
362c98e
Add failing test
AkuSilvenius cd50933
add simple trylock
AkuSilvenius 2f8e47b
Add comment
AkuSilvenius 5dadc1d
update test
AkuSilvenius 021b8b8
remove comment
AkuSilvenius 4ff8627
retry pipeline
AkuSilvenius a403d5d
retry pipeline
AkuSilvenius 8f10bfc
retry pipeline
AkuSilvenius c989d69
add configurable retry for lock
AkuSilvenius 6f06f0a
update README.md
AkuSilvenius e858f5e
Merge branch 'master' into 960_add_try_advisory_lock
AkuSilvenius 1644f0c
update test name
AkuSilvenius 49bee23
actually run test
AkuSilvenius b8e39d0
Apply suggestions from code review
AkuSilvenius be32ada
mention initial retry interval in readme
AkuSilvenius 993b736
add missing millisecond conversion
AkuSilvenius 8580c15
retry pipeline
AkuSilvenius a43c2f1
retry pipeline
AkuSilvenius 21469a7
retry pipeline
AkuSilvenius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,7 @@ func Test(t *testing.T) { | |
t.Run("testFailToCreateTableWithoutPermissions", testFailToCreateTableWithoutPermissions) | ||
t.Run("testCheckBeforeCreateTable", testCheckBeforeCreateTable) | ||
t.Run("testParallelSchema", testParallelSchema) | ||
t.Run("testPostgresConcurrentMigrations", testPostgresConcurrentMigrations) | ||
t.Run("testPostgresLock", testPostgresLock) | ||
t.Run("testWithInstanceConcurrent", testWithInstanceConcurrent) | ||
t.Run("testWithConnection", testWithConnection) | ||
|
@@ -628,6 +629,49 @@ func testParallelSchema(t *testing.T) { | |
}) | ||
} | ||
|
||
func testPostgresConcurrentMigrations(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ran these tests against master and can confirm they fail 🎉. This change makes the tests pass |
||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
// GIVEN - a set of concurrent processes running migrations | ||
const concurrency = 3 | ||
var wg sync.WaitGroup | ||
|
||
ip, port, err := c.FirstPort() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
addr := pgConnectionString(ip, port, "x-lock-retry-max-interval=2000") | ||
|
||
// WHEN | ||
for i := 0; i < concurrency; i++ { | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
|
||
p := &Postgres{} | ||
d, err := p.Open(addr) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer func() { | ||
if err := d.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "postgres", d) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
dt.TestMigrate(t, m) | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
// THEN | ||
}) | ||
} | ||
|
||
func testPostgresLock(t *testing.T) { | ||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
ip, port, err := c.FirstPort() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This if statement means a user can't set the max retry interval to less than the default. I can't see a good reason to limit it in this way so why not do this?
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 suggestion - the retry strategy here is to use exponential backoff, meaning if we end up retrying, the later retries should wait for longer than the previous ones. If we'd allow the user to set
maxRetryInterval
to less thanInitialRetryInterval
then the retry interval wouldn't increase which is against the idea of exponential backoffThere 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.
however your comment raises a valid point that the user might not be aware of this initial retry interval and could try setting it to less than that without knowing it would be ignored - I've added a note about this to the readme be32ada