Skip to content

server,sql: add an interface for internal SQL sessions#95702

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
andreimatei:sql.loopback-interface
Feb 7, 2023
Merged

server,sql: add an interface for internal SQL sessions#95702
craig[bot] merged 1 commit intocockroachdb:masterfrom
andreimatei:sql.loopback-interface

Conversation

@andreimatei
Copy link
Copy Markdown
Contributor

This patch adds a way for a CRDB node to open pgwire connections to itself. This is implemented through the use of net.Conn's implemented by in-memory pipes.

I want to use this in particular in order to embed the Obs Service into CRBD. The Obs Service is a library that wants to be given a handle to the backend database in the form of a pgx connection pool. Now such a connection pool can be created based on this new "network" interface, without any network actually being involved. Beyond that, I think this is a generally useful feature that we should have had for a long time - frequently it's useful internally to have a SQL session against the cluster; so far we've been doing that with the InternalExecutor, except that guy doesn't actually support sessions (it mostly supports single statements).

Release note: None
Epic: None

@andreimatei andreimatei requested review from a team and knz January 23, 2023 22:58
@andreimatei andreimatei requested review from a team as code owners January 23, 2023 22:58
@andreimatei andreimatei requested review from a team January 23, 2023 22:58
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Jan 23, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@andreimatei
Copy link
Copy Markdown
Contributor Author

This is used in #95703; I've made it a separate PR because it probably deserves more focused review.

Copy link
Copy Markdown
Contributor Author

@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.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


pkg/sql/pgwire/hba_conf.go line 220 at r1 (raw file):

	}

	// If not rule for ConnInternalLoopback connections has been specified, add

Rafa, does this look good to you?
You expressed doubt that the stanza above does the right thing, btw, given that it only deals with one entry but we used to have two by default. I'm not sure whether it should also deal with the 2nd or not; perhaps take a look.

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

Yes, this generally works thanks.
I still don't understand your motivation for not using connManager like the other listeners. I find it generally beneficial and important to reuse existing patterns because it makes the code easier to learn and maintain by others.

The specific problem you've encountered when you tried it (something about observing shutdown? I'm not sure I fully understand) can probably be remediated by other ways.

Reviewed 11 of 11 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andreimatei)


pkg/server/tenant.go line 748 at r1 (raw file):

// internal loopback interface.
func (s *SQLServerWrapper) AcceptInternalClients(ctx context.Context) error {
	serveConn := func(ctx context.Context, conn net.Conn, status pgwire.PreServeStatus) error {

Bringing my comment over from #95703.
I don't see why this can't be served by the same connManager-based logic as the others.


pkg/sql/pgwire/hba_conf.go line 220 at r1 (raw file):

Previously, andreimatei (Andrei Matei) wrote…

Rafa, does this look good to you?
You expressed doubt that the stanza above does the right thing, btw, given that it only deals with one entry but we used to have two by default. I'm not sure whether it should also deal with the 2nd or not; perhaps take a look.

Yes this looks good now.

Coming back to your comment from #95703: I would be OK with a special case instead of an HBA rule, if you'd prefer this not be exposed in the configurable HBA rules. However, your code here is following pre-existing patterns so it also works for me.


pkg/sql/pgwire/hba_conf.go line 303 at r1 (raw file):

host  all all  all cert-password # built-in CockroachDB default
local all all      password      # built-in CockroachDB default
loopback all all all trust       # built-in CockroachDB default

If you decide to keep this, you can move it to the first line since this is what your logic above does.
Also please align the columns.

@andreimatei andreimatei force-pushed the sql.loopback-interface branch from bf81f46 to 7a670db Compare February 6, 2023 21:24
@andreimatei andreimatei requested a review from a team as a code owner February 6, 2023 21:24
Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

👍 i'll have another look when you ask me to.

Reviewed 20 of 22 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andreimatei)

Copy link
Copy Markdown
Contributor Author

@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.

I still don't understand your motivation for not using connManager like the other listeners. I find it generally beneficial and important to reuse existing patterns because it makes the code easier to learn and maintain by others.

done

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


pkg/server/tenant.go line 748 at r1 (raw file):

Previously, knz (Raphael 'kena' Poss) wrote…

Bringing my comment over from #95703.
I don't see why this can't be served by the same connManager-based logic as the others.

Done.


pkg/sql/pgwire/hba_conf.go line 220 at r1 (raw file):

Previously, knz (Raphael 'kena' Poss) wrote…

Yes this looks good now.

Coming back to your comment from #95703: I would be OK with a special case instead of an HBA rule, if you'd prefer this not be exposed in the configurable HBA rules. However, your code here is following pre-existing patterns so it also works for me.

left it as is


pkg/sql/pgwire/hba_conf.go line 303 at r1 (raw file):

Previously, knz (Raphael 'kena' Poss) wrote…

If you decide to keep this, you can move it to the first line since this is what your logic above does.
Also please align the columns.

Done.

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

LGTM modulo nits

Reviewed 2 of 22 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andreimatei)


pkg/server/initial_sql.go line 33 at r2 (raw file):

	ctx context.Context, startSingleNode bool, adminUser, adminPassword string,
) error {
	log.Infof(ctx, "!!! RunInitialSQL")

nit


pkg/server/server.go line 1982 at r2 (raw file):

// internal loopback interface.
func (s *Server) AcceptInternalClients(ctx context.Context) error {
	log.Infof(ctx, "!!! AcceptInternalClients 1")

nit here and below

@andreimatei andreimatei force-pushed the sql.loopback-interface branch from 7a670db to 29e28c8 Compare February 7, 2023 19:07
Copy link
Copy Markdown
Contributor Author

@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.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


pkg/server/server.go line 1982 at r2 (raw file):

Previously, knz (Raphael 'kena' Poss) wrote…

nit here and below

done


pkg/server/initial_sql.go line 33 at r2 (raw file):

Previously, knz (Raphael 'kena' Poss) wrote…

nit

done

This patch adds a way for a CRDB node to open pgwire connections to
itself. This is implemented through the use of net.Conn's implemented by
in-memory pipes.

I want to use this in particular in order to embed the Obs Service into
CRBD. The Obs Service is a library that wants to be given a handle to
the backend database in the form of a pgx connection pool. Now such a
connection pool can be created based on this new "network" interface,
without any network actually being involved.  Beyond that, I think this
is a generally useful feature that we should have had for a long time -
frequently it's useful internally to have a SQL session against the
cluster; so far we've been doing that with the InternalExecutor, except
that guy doesn't actually support sessions (it mostly supports single
statements).

Release note: None
Epic: None
@andreimatei andreimatei force-pushed the sql.loopback-interface branch from 29e28c8 to f5f5d9a Compare February 7, 2023 20:57
@andreimatei
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 7, 2023

Build succeeded:

@craig craig bot merged commit 247a543 into cockroachdb:master Feb 7, 2023
@andreimatei andreimatei deleted the sql.loopback-interface branch February 8, 2023 00:03
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