-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
106131: pgrepl: parse replication statements + handle IDENTIFY_SYSTEM r=cucaroach a=otan Informs: #105130 ## pgwire: ban extended protocol with REPLICATION protocol In line with PostgreSQL, we prohibit the use of the extended protocol when the replication protocol is in effect. Release note: None ## pgrepl: implement IDENTIFY_SYSTEM command This commit implements IDENTIFY_SYSTEM in the replication protocol, which is used to retrieve metadata about replication state. Most of this commit involves changing assumptions throughout the codebase there is 1 parser - there is 2 if replication mode is enabled. We've also had to change the parser to return `statements.Statement` instead of `pgrepltree.ReplicationStatement` for compatibility with `parser.Parse...`. I've left the `LSN` fields as placeholders for now as we finalise on what we'll eventually call it. Release note: None ## sql: no-op SHOW SYNTAX in replication mode Currently on the CLI, the command must parse on the server for it to run. For replication related commands, we have not implemented `SHOW SYNTAX` yet. This is a bit of effort and seems outside the scope of what we want for now. For now, handle replication commands by no-oping them. Release note: None Co-authored-by: Oliver Tan <[email protected]>
- Loading branch information
Showing
29 changed files
with
488 additions
and
15 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#! /usr/bin/env expect -f | ||
|
||
source [file join [file dirname $argv0] common.tcl] | ||
|
||
start_server $argv | ||
|
||
start_test "Ensure that replication mode works as expected in the sql shell" | ||
|
||
# Spawn a sql shell. | ||
spawn /bin/bash | ||
|
||
send "$argv sql --url `cat server_url`'\&replication=database' -e 'IDENTIFY_SYSTEM'\r" | ||
eexpect "(1 row)" | ||
|
||
send_eof | ||
eexpect eof | ||
|
||
end_test | ||
|
||
stop_server $argv |
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/pgrepl/lsn" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgrepl/lsnutil" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgrepl/pgrepltree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
) | ||
|
||
type identifySystemNode struct { | ||
optColumnsSlot | ||
clusterID string | ||
database string | ||
lsn lsn.LSN | ||
shown bool | ||
} | ||
|
||
func (s *identifySystemNode) startExec(params runParams) error { | ||
return nil | ||
} | ||
|
||
func (s *identifySystemNode) Next(params runParams) (bool, error) { | ||
if s.shown { | ||
return false, nil | ||
} | ||
s.shown = true | ||
return true, nil | ||
} | ||
|
||
func (s *identifySystemNode) Values() tree.Datums { | ||
db := tree.DNull | ||
if s.database != "" { | ||
db = tree.NewDString(s.database) | ||
} | ||
return tree.Datums{ | ||
tree.NewDString(s.clusterID), | ||
tree.NewDInt(1), // timeline | ||
tree.NewDString(s.lsn.String()), | ||
db, | ||
} | ||
} | ||
|
||
func (s *identifySystemNode) Close(ctx context.Context) {} | ||
|
||
func (p *planner) IdentifySystem( | ||
ctx context.Context, n *pgrepltree.IdentifySystem, | ||
) (planNode, error) { | ||
return &identifySystemNode{ | ||
// TODO(#105130): correctly populate this field. | ||
lsn: lsnutil.HLCToLSN(p.Txn().ReadTimestamp()), | ||
clusterID: p.ExecCfg().NodeInfo.LogicalClusterID().String(), | ||
database: p.SessionData().Database, | ||
}, nil | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package pgrepl | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/security/username" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/jackc/pgx/v5/pgconn" | ||
"github.com/jackc/pgx/v5/pgproto3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestExtendedProtocolDisabled ensures the extended protocol is disabled | ||
// during replication mode. | ||
func TestExtendedProtocolDisabled(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
srv, db, _ := serverutils.StartServer(t, base.TestServerArgs{}) | ||
defer srv.Stopper().Stop(context.Background()) | ||
s := srv.ApplicationLayer() | ||
|
||
sqlDB := sqlutils.MakeSQLRunner(db) | ||
sqlDB.Exec(t, `CREATE USER testuser LOGIN REPLICATION`) | ||
|
||
pgURL, cleanup := sqlutils.PGUrl(t, s.AdvSQLAddr(), "pgrepl_extended_protocol_test", url.User(username.TestUser)) | ||
defer cleanup() | ||
|
||
cfg, err := pgconn.ParseConfig(pgURL.String()) | ||
require.NoError(t, err) | ||
cfg.RuntimeParams["replication"] = "database" | ||
ctx := context.Background() | ||
|
||
conn, err := pgconn.ConnectConfig(ctx, cfg) | ||
require.NoError(t, err) | ||
fe := conn.Frontend() | ||
|
||
for _, tc := range []struct { | ||
desc string | ||
msg []pgproto3.FrontendMessage | ||
}{ | ||
{desc: "parse", msg: []pgproto3.FrontendMessage{&pgproto3.Parse{Name: "a", Query: "SELECT 1"}}}, | ||
{desc: "bind", msg: []pgproto3.FrontendMessage{&pgproto3.Bind{}}}, | ||
{desc: "parse and bind", msg: []pgproto3.FrontendMessage{ | ||
&pgproto3.Parse{Name: "a", Query: "SELECT 1"}, | ||
&pgproto3.Bind{}, | ||
}}, | ||
{desc: "describe", msg: []pgproto3.FrontendMessage{&pgproto3.Describe{Name: "a"}}}, | ||
{desc: "exec", msg: []pgproto3.FrontendMessage{&pgproto3.Execute{Portal: "a"}}}, | ||
{desc: "close", msg: []pgproto3.FrontendMessage{&pgproto3.Close{}}}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
for _, msg := range tc.msg { | ||
fe.Send(msg) | ||
} | ||
fe.Send(&pgproto3.Sync{}) | ||
err := fe.Flush() | ||
require.NoError(t, err) | ||
var pgErr *pgconn.PgError | ||
done := false | ||
for !done { | ||
recv, err := fe.Receive() | ||
require.NoError(t, err) | ||
switch recv := recv.(type) { | ||
case *pgproto3.ReadyForQuery: | ||
done = true | ||
case *pgproto3.ErrorResponse: | ||
// Ensure we do not have multiple errors. | ||
require.Nil(t, pgErr) | ||
pgErr = pgconn.ErrorResponseToPgError(recv) | ||
default: | ||
t.Errorf("received unexpected message %#v", recv) | ||
} | ||
} | ||
require.NotNil(t, pgErr) | ||
require.Equal(t, pgcode.ProtocolViolation.String(), pgErr.Code) | ||
require.Contains(t, pgErr.Message, "extended query protocol not supported in a replication connection") | ||
|
||
// Ensure we can use the connection using the simple protocol. | ||
rows := conn.Exec(ctx, "SELECT 1") | ||
_, err = rows.ReadAll() | ||
require.NoError(t, err) | ||
require.NoError(t, rows.Close()) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "lsnutil", | ||
srcs = ["lsnutil.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/sql/pgrepl/lsnutil", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/sql/pgrepl/lsn", | ||
"//pkg/util/hlc", | ||
], | ||
) |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package lsnutil | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/pgrepl/lsn" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
) | ||
|
||
// HLCToLSN converts a HLC to a LSN. | ||
// It is in a separate package to prevent the `lsn` package importing `log`. | ||
func HLCToLSN(h hlc.Timestamp) lsn.LSN { | ||
// TODO(#105130): correctly populate this field. | ||
return lsn.LSN(h.WallTime/int64(time.Millisecond)) << 32 | ||
} |
Oops, something went wrong.