Skip to content

Commit

Permalink
cli,server: fix --sql-advertise-addr when --sql-addr is not speci…
Browse files Browse the repository at this point in the history
…fied

Release justification: bug fix

Release note (bug fix): The flag `--sql-advertise-addr` now properly
works even when the SQL and RPC ports are shared (because `--sql-addr`
was not specified). Note that this port sharing is a deprecated
feature in v22.2.
  • Loading branch information
knz committed Sep 7, 2022
1 parent bcc1682 commit 9971819
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
10 changes: 6 additions & 4 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,17 +1049,19 @@ func extraServerFlagInit(cmd *cobra.Command) error {
serverCfg.SplitListenSQL = changed(fs, cliflags.ListenSQLAddr.Name)

// Fill in the defaults for --advertise-sql-addr, if the flag exists on `cmd`.
advSpecified := changed(fs, cliflags.AdvertiseAddr.Name) ||
advHostSpecified := changed(fs, cliflags.AdvertiseAddr.Name) ||
changed(fs, cliflags.AdvertiseHost.Name)
advPortSpecified := changed(fs, cliflags.AdvertiseAddr.Name) ||
changed(fs, cliflags.AdvertisePort.Name)
if serverSQLAdvertiseAddr == "" {
if advSpecified {
if advHostSpecified {
serverSQLAdvertiseAddr = serverAdvertiseAddr
} else {
serverSQLAdvertiseAddr = serverSQLAddr
}
}
if serverSQLAdvertisePort == "" {
if advSpecified && !serverCfg.SplitListenSQL {
if advPortSpecified && !serverCfg.SplitListenSQL {
serverSQLAdvertisePort = serverAdvertisePort
} else {
serverSQLAdvertisePort = serverSQLPort
Expand Down Expand Up @@ -1097,7 +1099,7 @@ func extraServerFlagInit(cmd *cobra.Command) error {
serverCfg.HTTPAddr = net.JoinHostPort(serverHTTPAddr, serverHTTPPort)

if serverHTTPAdvertiseAddr == "" {
if advSpecified {
if advHostSpecified || advPortSpecified {
serverHTTPAdvertiseAddr = serverAdvertiseAddr
} else {
serverHTTPAdvertiseAddr = serverHTTPAddr
Expand Down
10 changes: 10 additions & 0 deletions pkg/cli/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ func TestServerConnSettings(t *testing.T) {
":54321", "192.168.0.111:12345",
":54321", "192.168.0.111:12345",
},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-sql-addr", "192.168.0.111", "--port", "54321", "--advertise-port", "12345"},
"127.0.0.1:54321", "127.0.0.1:12345",
"127.0.0.1:54321", "192.168.0.111:12345",
"127.0.0.1:54321", "192.168.0.111:12345",
},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-sql-addr", "192.168.0.111:12345", "--port", "54321"},
"127.0.0.1:54321", "127.0.0.1:54321",
"127.0.0.1:54321", "192.168.0.111:12345",
"127.0.0.1:54321", "192.168.0.111:12345",
},
}

for i, td := range testData {
Expand Down
12 changes: 8 additions & 4 deletions pkg/server/start_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/errors"
)

// startListenRPCAndSQL starts the RPC and SQL listeners.
Expand Down Expand Up @@ -89,11 +90,14 @@ func startListenRPCAndSQL(
pgL = m.Match(func(r io.Reader) bool {
return pgwire.Match(r)
})
// Also if the pg port is not split, the actual listen
// and advertise addresses for SQL become equal to that
// of RPC, regardless of what was configured.
// Also if the pg port is not split, the actual listen address for
// SQL become equal to that of RPC.
cfg.SQLAddr = cfg.Addr
cfg.SQLAdvertiseAddr = cfg.AdvertiseAddr
// Then we update the advertised addr with the right port, if
// the port had been auto-allocated.
if err := UpdateAddrs(ctx, &cfg.SQLAddr, &cfg.SQLAdvertiseAddr, ln.Addr()); err != nil {
return nil, nil, errors.Wrapf(err, "internal error")
}
}

anyL := m.Match(cmux.Any())
Expand Down

0 comments on commit 9971819

Please sign in to comment.