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

sql/pgwire: send down parameter status updates #42376

Merged
merged 1 commit into from
Nov 13, 2019

Conversation

otan
Copy link
Contributor

@otan otan commented Nov 11, 2019

Resolves #40854.

Server parameters such as application_name and timezone require updates
over pgwire when they are changed. This was previously not done.

In this PR, we introduce a listener-esque object on sessionDataMutator
that pgwire will add itself onto to send updates on these changes.

Coincidentally, this means a few logic tests changes because lib/pq will
encode this into the string.

Release note (sql change): This PR introduces a new pgwire update that
sends ParameterStatus messages when certain server parameters are
changed for the given session over pgwire.

@otan otan requested review from maddyblue, rafiss and a team November 11, 2019 21:13
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@maddyblue maddyblue left a comment

Choose a reason for hiding this comment

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

Does this make that JDBC test pass?

if m.onSessionDataChangeListeners == nil {
m.onSessionDataChangeListeners = make(map[string][]func(val interface{}))
}
if _, ok := m.onSessionDataChangeListeners[key]; !ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need for this block.

applicationNameChanged func(newName string)
// onSessionDataChangeListeners stores all the observers to execute when
// session data is modified, keyed by the value to change on.
onSessionDataChangeListeners map[string][]func(val interface{})
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need to be a slice? Can there ever by more than one per key?

value := connHandler.GetStatusParam(ctx, param)
if err := c.sendStatusParam(param, value); err != nil {
return sql.ConnectionHandler{}, err
}
// pgwire also expects updates when these parameters change.
connHandler.RegisterOnSessionDataChange(param, func(val interface{}) {
convertVal := val.(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can val be a string instead of interface{} to avoid the forced cast? Seems unsafe otherwise.

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

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

Yep, the tests pass with this change.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mjibson, @otan, and @rafiss)


pkg/sql/exec_util.go, line 1743 at r1 (raw file):

Previously, mjibson (Matt Jibson) wrote…

Why does this need to be a slice? Can there ever by more than one per key?

Yes, I think the application_name one can be used more than once - once for stats and once for pgwire (the change for this is somewhere else in this PR).


pkg/sql/exec_util.go, line 1752 at r1 (raw file):

Previously, mjibson (Matt Jibson) wrote…

There's no need for this block.

Ooh.


pkg/sql/pgwire/conn.go, line 620 at r1 (raw file):

Previously, mjibson (Matt Jibson) wrote…

Can val be a string instead of interface{} to avoid the forced cast? Seems unsafe otherwise.

Yes, I saw some Set params that were not strings, but they don't seem to need listeners (yet) so okay with doing this.


pkg/sql/pgwire/testdata/pgtest/connection_params, line 22 at r1 (raw file):

Previously, mjibson (Matt Jibson) wrote…

Do we support word time zones (MDT)? If so I'd like to see another test with that.

Added a test.

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mjibson and @rafiss)


pkg/sql/pgwire/testdata/pgtest/connection_params, line 1 at r1 (raw file):

Previously, mjibson (Matt Jibson) wrote…

If this file differs from what postgres does it should document that with a comment.

Done.

Copy link
Contributor

@maddyblue maddyblue left a comment

Choose a reason for hiding this comment

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

Can you flesh out the comment in the commit message about exactly why the logic test results changed? Is it because lib/pq supports reading the timezone back from the connection and sets the location data of received timestamps from it?

{"Type":"ReadyForQuery","TxStatus":"I"}

# Change the time zone.
# NOTE: the order is different than postgres, because we execute the statement
Copy link
Contributor

Choose a reason for hiding this comment

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

once at the top is enough

@@ -0,0 +1,55 @@
# Change the application name.
# NOTE: the order is different than postgres, because we execute the statement
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure this is the reason? The commit message is more unsure. (I'm also unsure, I just don't want to have an incorrect statement here.)

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

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

Can you flesh out the comment in the commit message about exactly why the logic test results changed? Is it because lib/pq supports reading the timezone back from the connection and sets the location data of received timestamps from it?
yep!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mjibson and @rafiss)


pkg/sql/pgwire/testdata/pgtest/connection_params, line 2 at r2 (raw file):

Previously, mjibson (Matt Jibson) wrote…

Are you sure this is the reason? The commit message is more unsure. (I'm also unsure, I just don't want to have an incorrect statement here.)

I dug even further into this and realised I wasn't buffering. I got mislead earlier.


pkg/sql/pgwire/testdata/pgtest/connection_params, line 22 at r2 (raw file):

Previously, mjibson (Matt Jibson) wrote…

once at the top is enough

Done.

@otan otan force-pushed the otan-application_name_fix branch 2 times, most recently from 19ce597 to 00f99b3 Compare November 12, 2019 14:48
pkg/sql/vars.go Show resolved Hide resolved
@rafiss
Copy link
Collaborator

rafiss commented Nov 12, 2019

Nice stuff! Could you take a look at pgjdbc_blacklist.go, then find the tests that are failing due to issue #40854, and update the blacklist for v20.1 if the tests pass now?

Server parameters such as `application_name` require updates over
pgwire when they are charged. This was previously not done. This applies
to time zone changes as well.

In this PR, we introduce a listener-esque object on `sessionDataMutator`
that pgwire will add itself onto to send updates on these changes.

Coincidentally, this means a few logic tests changes because lib/pq will
encode this into the string.

Release note (sql change): This PR introduces a new pgwire update that
sends ParameterStatus messages when certain server parameters are
changed for the given session over pgwire.
@otan
Copy link
Contributor Author

otan commented Nov 13, 2019

thanks!

bors r+

craig bot pushed a commit that referenced this pull request Nov 13, 2019
42376: sql/pgwire: send down parameter status updates r=otan a=otan

Resolves #40854.

Server parameters such as `application_name` and `timezone` require updates
over pgwire when they are changed. This was previously not done.

In this PR, we introduce a listener-esque object on `sessionDataMutator`
that pgwire will add itself onto to send updates on these changes.

Coincidentally, this means a few logic tests changes because lib/pq will
encode this into the string.

Release note (sql change): This PR introduces a new pgwire update that
sends ParameterStatus messages when certain server parameters are
changed for the given session over pgwire.

Co-authored-by: Oliver Tan <otan@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Nov 13, 2019

Build succeeded

@craig craig bot merged commit 08c89c4 into cockroachdb:master Nov 13, 2019
Copy link
Contributor

@andreimatei andreimatei left a comment

Choose a reason for hiding this comment

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

@otan did you attempt to implement this through the sql.CommandResult interface, instead of creating a new communication mechanism between sql and pgwire? I'm thinking the sql.CommandResultcould carry these updates, which would make the control flow much easier to reason about.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mjibson and @rafiss)

@otan
Copy link
Contributor Author

otan commented Feb 8, 2020

@andreimatei that works too, see #44883. couldn't get this to work for CommandResult, but did something similar. This will be a nice extension for the work needed for notices.

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.

sql: set ApplicationName from JDBC
5 participants