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

cli,server: fix --sql-advertise-addr when --sql-addr is not specified #87412

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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