-
Notifications
You must be signed in to change notification settings - Fork 190
ci: fix flaky prepared statements test #1026
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -286,6 +286,7 @@ PLATFORMS | |
| x86_64-linux-musl | ||
|
|
||
| DEPENDENCIES | ||
| concurrent-ruby | ||
| datadog (~> 2.0) | ||
| pg | ||
| rails | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [general] | ||
| prepared_statements = "full" | ||
| default_pool_size = 2 | ||
| default_pool_size = 5 | ||
|
|
||
| [[databases]] | ||
| name = "pgdog" | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -29,31 +29,28 @@ | |
| conn.close | ||
| end | ||
|
|
||
| # Mirror of disabled/'fails SQL PREPARE/EXECUTE in transaction pool mode'. | ||
| # full mode intercepts PREPARE, renames the statement to an internal name | ||
| # (__pgdog_N), and replays the PREPARE on any backend that hasn't seen it | ||
| # before executing. 5 threads × 20 iterations with pool_size = 2 generates | ||
| # the same backend crossings as the disabled test, but all succeed. | ||
| # Test that a statement created with SQL PREPARE inside the connection | ||
| # can be reliably executed with prepared_statements=full even when the | ||
| # EXECUTE lands on another backend connection, because pgdog replays the | ||
| # PREPARE onto it. | ||
| it 'rewrites simple-protocol PREPARE / EXECUTE in transaction pool mode' do | ||
| errors = [] | ||
| mutex = Mutex.new | ||
| conn = connect | ||
| # PREPARE and EXECUTE both route to the primary pool (pgdog treats them as | ||
| # writes), the same pool the pin holds a backend in, so each EXECUTE is forced | ||
| # onto a backend that never saw the PREPARE -- full mode replays it there, so | ||
| # they still succeed. | ||
| conn.exec('PREPARE sql_stmt AS SELECT $1::bigint * 2 AS val') | ||
|
|
||
| threads = 5.times.map do | ||
| Thread.new do | ||
| conn = connect | ||
| begin | ||
| conn.exec('PREPARE sql_stmt AS SELECT $1::bigint * 2') | ||
| 20.times { |i| conn.exec("EXECUTE sql_stmt(#{i})") } | ||
| rescue PG::Error => e | ||
| mutex.synchronize { errors << e.message } | ||
| ensure | ||
| conn.close rescue nil | ||
| with_pinned_backend do # hold the backend conn prepared on | ||
| with_load do | ||
| 20.times do |i| | ||
| res = conn.exec("EXECUTE sql_stmt(#{i})") | ||
| expect(res[0]['val'].to_i).to eq(i * 2) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| threads.each(&:join) | ||
| expect(errors).to be_empty | ||
| ensure | ||
| conn.close rescue nil | ||
| end | ||
|
|
||
| # Session mode gives each client its own dedicated backend, so two session | ||
|
|
@@ -70,36 +67,23 @@ | |
| conn2.close | ||
| end | ||
|
|
||
| # Mirror of disabled/'fails named extended-protocol statements in | ||
| # transaction pool mode'. full mode renames each frontend's Parse to an | ||
| # internal name (__pgdog_N, unique per frontend) and replays it on any | ||
| # backend before the Bind. 5 threads × 20 iterations with pool_size = 2 | ||
| # forces genuine crossings \ the replay ensures all succeed. | ||
| # Result values are also verified to guard against silent data corruption. | ||
| # Test that the prepared statement that is created inside the connection | ||
| # can be reliably executed with prepared_statements=full even when it lands | ||
| # on another backend connection, because pgdog replays the prepare onto it. | ||
| it 'executes named extended-protocol statements in transaction pool mode' do | ||
| errors = [] | ||
| mutex = Mutex.new | ||
| conn = connect | ||
|
Contributor
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. Same issue as the previous comment here. Can you clarify why we no longer care about the behavior with multiple competing threads? |
||
| conn.prepare('ext_stmt', 'SELECT $1::bigint AS val') | ||
|
|
||
| threads = 5.times.map do | ||
| Thread.new do | ||
| conn = connect | ||
| begin | ||
| conn.prepare('ext_stmt', 'SELECT $1::bigint AS val') | ||
| 20.times do |i| | ||
| res = conn.exec_prepared('ext_stmt', [i]) | ||
| val = res[0]['val'].to_i | ||
| raise "expected #{i}, got #{val}" unless val == i | ||
| end | ||
| rescue => e | ||
| mutex.synchronize { errors << e.message } | ||
| ensure | ||
| conn.close rescue nil | ||
| with_pinned_backend do # hold the backend conn prepared on | ||
| with_load do | ||
| 20.times do |i| | ||
| res = conn.exec_prepared('ext_stmt', [i]) | ||
| expect(res[0]['val'].to_i).to eq(i) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| threads.each(&:join) | ||
| expect(errors).to be_empty | ||
| ensure | ||
| conn.close rescue nil | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -47,61 +47,45 @@ | |
| conn2.close | ||
| end | ||
|
|
||
| # extended mode renames each frontend's Parse to an internal name | ||
| # (__pgdog_N, unique per frontend) and replays it on any backend before | ||
| # the Bind. 15 threads × 20 iterations with a default pool of 10 guarantees | ||
| # genuine crossings via the pigeonhole principle; the replay ensures all succeed. | ||
| # Result values are verified to guard against silent data corruption. | ||
| # Test that the prepared statement that is created inside the connection | ||
| # can be reliably executed with prepared_statements=extended even when it | ||
| # lands on another backend connection, because pgdog replays the prepare | ||
| # onto it. | ||
| it 'executes named extended-protocol statements in transaction pool mode' do | ||
| errors = [] | ||
| mutex = Mutex.new | ||
| conn = connect | ||
|
Contributor
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. Same thing here |
||
| conn.prepare('ext_stmt', 'SELECT $1::bigint AS val') | ||
|
|
||
| threads = 15.times.map do | ||
| Thread.new do | ||
| conn = connect | ||
| begin | ||
| conn.prepare('ext_stmt', 'SELECT $1::bigint AS val') | ||
| 20.times do |i| | ||
| res = conn.exec_prepared('ext_stmt', [i]) | ||
| val = res[0]['val'].to_i | ||
| raise "expected #{i}, got #{val}" unless val == i | ||
| end | ||
| rescue => e | ||
| mutex.synchronize { errors << e.message } | ||
| ensure | ||
| conn.close rescue nil | ||
| with_pinned_backend do | ||
| with_load do | ||
| 20.times do |i| | ||
| res = conn.exec_prepared('ext_stmt', [i]) | ||
| expect(res[0]['val'].to_i).to eq(i) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| threads.each(&:join) | ||
| expect(errors).to be_empty | ||
| ensure | ||
| conn.close rescue nil | ||
| end | ||
|
|
||
| # extended mode does NOT intercept SQL PREPARE / EXECUTE — those are | ||
| # forwarded as-is. With 15 threads and a default pool of 10, the pigeonhole | ||
| # principle guarantees crossings: at least 5 threads hit a backend that | ||
| # already holds 'sql_stmt' ('already exists') or one that never saw the | ||
| # PREPARE ('does not exist'). Either way, errors accumulate. | ||
| # Test that a statement created with SQL PREPARE inside the connection | ||
| # could not be reliably executed with prepared_statements=extended, which | ||
| # does not intercept SQL PREPARE/EXECUTE, so the EXECUTE could land on | ||
| # another backend connection that doesn't have this statement prepared. | ||
| it 'fails SQL PREPARE/EXECUTE in transaction pool mode' do | ||
| errors = [] | ||
| mutex = Mutex.new | ||
| conn = connect | ||
| # PREPARE and EXECUTE both route to the primary pool (pgdog treats them as | ||
| # writes), the same pool the pin holds a backend in. extended mode does not | ||
| # intercept SQL PREPARE/EXECUTE, so every EXECUTE forced onto another backend fails. | ||
| conn.exec('PREPARE sql_stmt AS SELECT $1::bigint * 2 AS val') | ||
|
|
||
| threads = 15.times.map do | ||
| Thread.new do | ||
| conn = connect | ||
| begin | ||
| conn.exec('PREPARE sql_stmt AS SELECT $1::bigint * 2') | ||
| 20.times { |i| conn.exec("EXECUTE sql_stmt(#{i})") } | ||
| rescue PG::Error => e | ||
| mutex.synchronize { errors << e.message } | ||
| ensure | ||
| conn.close rescue nil | ||
| with_pinned_backend do # hold the backend conn prepared on | ||
| with_load do | ||
| 20.times do | ||
| expect { conn.exec('EXECUTE sql_stmt(1)') }.to raise_error(PG::Error) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| threads.each(&:join) | ||
| expect(errors).not_to be_empty | ||
| ensure | ||
| conn.close rescue nil | ||
| end | ||
| end | ||
This file contains hidden or 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
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 seems like it pretty fundamentally changes the semantics of the test. Do we not care about the behavior of multiple competing threads? Can you clarify why we don't care in your commit message or PR description?
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've changed the tests and the pr description to address this and other comments related to prepared statements, hope this helps