Skip to content

Commit

Permalink
server: fix join address on ipv6 (#43259) (#44935)
Browse files Browse the repository at this point in the history
close #43260
  • Loading branch information
ti-chi-bot committed Jun 26, 2023
1 parent 43d4108 commit d77fae6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
9 changes: 5 additions & 4 deletions domain/infosync/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -586,7 +587,7 @@ func (is *InfoSyncer) StoreTopologyInfo(ctx context.Context) error {
return errors.Trace(err)
}
str := string(hack.String(infoBuf))
key := fmt.Sprintf("%s/%s:%v/info", TopologyInformationPath, is.info.IP, is.info.Port)
key := fmt.Sprintf("%s/%s/info", TopologyInformationPath, net.JoinHostPort(is.info.IP, strconv.Itoa(int(is.info.Port))))
// Note: no lease is required here.
err = util.PutKVToEtcd(ctx, is.etcdCli, keyOpDefaultRetryCnt, key, str)
if err != nil {
Expand Down Expand Up @@ -738,7 +739,7 @@ func (is *InfoSyncer) newTopologySessionAndStoreServerInfo(ctx context.Context,
if is.etcdCli == nil {
return nil
}
logPrefix := fmt.Sprintf("[topology-syncer] %s/%s:%d", TopologyInformationPath, is.info.IP, is.info.Port)
logPrefix := fmt.Sprintf("[topology-syncer] %s/%s", TopologyInformationPath, net.JoinHostPort(is.info.IP, strconv.Itoa(int(is.info.Port))))
session, err := util2.NewSession(ctx, logPrefix, is.etcdCli, retryCnt, TopologySessionTTL)
if err != nil {
return err
Expand All @@ -753,7 +754,7 @@ func (is *InfoSyncer) updateTopologyAliveness(ctx context.Context) error {
if is.etcdCli == nil {
return nil
}
key := fmt.Sprintf("%s/%s:%v/ttl", TopologyInformationPath, is.info.IP, is.info.Port)
key := fmt.Sprintf("%s/%s/ttl", TopologyInformationPath, net.JoinHostPort(is.info.IP, strconv.Itoa(int(is.info.Port))))
return util.PutKVToEtcd(ctx, is.etcdCli, keyOpDefaultRetryCnt, key,
fmt.Sprintf("%v", time.Now().UnixNano()),
clientv3.WithLease(is.topologySession.Lease()))
Expand Down Expand Up @@ -824,7 +825,7 @@ func (is *InfoSyncer) getPrometheusAddr() (string, error) {
if err != nil {
return "", errors.Trace(err)
}
res = fmt.Sprintf("http://%s:%v", prometheus.IP, prometheus.Port)
res = fmt.Sprintf("http://%s", net.JoinHostPort(prometheus.IP, strconv.Itoa(prometheus.Port)))
}
is.prometheusAddr = res
is.modifyTime = time.Now()
Expand Down
4 changes: 2 additions & 2 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1601,8 +1601,8 @@ func GetTiDBServerInfo(ctx sessionctx.Context) ([]ServerInfo, error) {
for _, node := range tidbNodes {
servers = append(servers, ServerInfo{
ServerType: "tidb",
Address: fmt.Sprintf("%s:%d", node.IP, node.Port),
StatusAddr: fmt.Sprintf("%s:%d", node.IP, node.StatusPort),
Address: net.JoinHostPort(node.IP, strconv.Itoa(int(node.Port))),
StatusAddr: net.JoinHostPort(node.IP, strconv.Itoa(int(node.StatusPort))),
Version: FormatTiDBVersion(node.Version, isDefaultVersion),
GitHash: node.GitHash,
StartTimestamp: node.StartTimestamp,
Expand Down
4 changes: 2 additions & 2 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func sleepWithCtx(ctx context.Context, d time.Duration) {
}

func (s *Server) listenStatusHTTPServer() error {
s.statusAddr = fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, s.cfg.Status.StatusPort)
s.statusAddr = net.JoinHostPort(s.cfg.Status.StatusHost, strconv.Itoa(int(s.cfg.Status.StatusPort)))
if s.cfg.Status.StatusPort == 0 && !RunInGoTest {
s.statusAddr = fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, defaultStatusPort)
s.statusAddr = net.JoinHostPort(s.cfg.Status.StatusHost, strconv.Itoa(defaultStatusPort))
}

logutil.BgLogger().Info("for status and metrics report", zap.String("listening on addr", s.statusAddr))
Expand Down
6 changes: 4 additions & 2 deletions server/plan_replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package server
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/gorilla/mux"
"github.com/pingcap/tidb/config"
Expand Down Expand Up @@ -74,7 +76,7 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req
name := params[pFileName]
path := handler.filePath
isForwarded := len(req.URL.Query().Get("forward")) > 0
localAddr := fmt.Sprintf("%s:%v", handler.address, handler.statusPort)
localAddr := net.JoinHostPort(handler.address, strconv.Itoa(int(handler.statusPort)))
exist, err := isExists(path)
if err != nil {
writeError(w, err)
Expand Down Expand Up @@ -126,7 +128,7 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req
if topo.IP == handler.address && topo.StatusPort == handler.statusPort {
continue
}
remoteAddr := fmt.Sprintf("%s/%v", topo.IP, topo.StatusPort)
remoteAddr := net.JoinHostPort(topo.IP, strconv.Itoa(int(topo.StatusPort)))
url := fmt.Sprintf("%s://%s/%s?forward=true", handler.scheme, remoteAddr, handler.urlPath)
resp, err := http.Get(url) // #nosec G107
if err != nil {
Expand Down

0 comments on commit d77fae6

Please sign in to comment.