Skip to content

Commit

Permalink
prevent ping from forwarding
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Feb 7, 2025
1 parent 0c43ce5 commit ab75292
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
"github.com/tikv/pd/pkg/syncer"
"github.com/tikv/pd/pkg/tso"
"github.com/tikv/pd/pkg/unsaferecovery"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/keypath"
"github.com/tikv/pd/pkg/utils/logutil"
Expand Down Expand Up @@ -2504,6 +2505,7 @@ func CheckHealth(client *http.Client, members []*pdpb.Member) map[uint64]*pdpb.M
for _, cURL := range member.ClientUrls {
ctx, cancel := context.WithTimeout(context.Background(), clientTimeout)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s%s", cURL, healthURL), http.NoBody)
req.Header.Set(apiutil.PDAllowFollowerHandleHeader, "true")
if err != nil {
log.Error("failed to new request", errs.ZapError(errs.ErrNewHTTPRequest, err))
cancel()
Expand Down
35 changes: 35 additions & 0 deletions tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,41 @@ func (suite *redirectorTestSuite) TestAllowFollowerHandle() {
re.NoError(err)
}

func (suite *redirectorTestSuite) TestPing() {
re := suite.Require()
// Find a follower.
var follower *server.Server
leader := suite.cluster.GetLeaderServer()
for _, svr := range suite.cluster.GetServers() {
if svr != leader {
follower = svr.GetServer()
break
}
}

for _, svr := range suite.cluster.GetServers() {
if svr.GetServer() != follower {
svr.Stop()
}
}
addr := follower.GetAddr() + "/pd/api/v1/ping"
request, err := http.NewRequest(http.MethodGet, addr, http.NoBody)
// ping request should not be redirected.
request.Header.Add(apiutil.PDAllowFollowerHandleHeader, "true")
re.NoError(err)
resp, err := tests.TestDialClient.Do(request)
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
_, err = io.ReadAll(resp.Body)
re.NoError(err)
for _, svr := range suite.cluster.GetServers() {
if svr.GetServer() != follower {
svr.Run()
}
}
}

func (suite *redirectorTestSuite) TestNotLeader() {
re := suite.Require()
// Find a follower.
Expand Down
4 changes: 3 additions & 1 deletion tools/pd-ctl/pdctl/command/ping_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"time"

"github.com/spf13/cobra"

"github.com/tikv/pd/pkg/utils/apiutil"
)

const pingPrefix = "pd/api/v1/ping"
Expand All @@ -35,7 +37,7 @@ func NewPingCommand() *cobra.Command {

func showPingCommandFunc(cmd *cobra.Command, _ []string) {
start := time.Now()
_, err := doRequest(cmd, pingPrefix, http.MethodGet, http.Header{})
_, err := doRequest(cmd, pingPrefix, http.MethodGet, http.Header{apiutil.PDAllowFollowerHandleHeader: {"true"}}, nil)
if err != nil {
cmd.Println(err)
return
Expand Down

0 comments on commit ab75292

Please sign in to comment.