Skip to content

Commit

Permalink
This is an automated cherry-pick of #9042
Browse files Browse the repository at this point in the history
close #9031

Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
rleungx authored and ti-chi-bot committed Feb 24, 2025
1 parent ff54072 commit 197a9c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/syncer"
"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/logutil"
"github.com/tikv/pd/pkg/utils/netutil"
Expand Down Expand Up @@ -2726,7 +2727,12 @@ func CheckHealth(client *http.Client, members []*pdpb.Member) map[uint64]*pdpb.M
for _, member := range members {
for _, cURL := range member.ClientUrls {
ctx, cancel := context.WithTimeout(context.Background(), clientTimeout)
<<<<<<< HEAD
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s%s", cURL, healthURL), nil)
=======
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s%s", cURL, healthURL), http.NoBody)
req.Header.Set(apiutil.PDAllowFollowerHandleHeader, "true")
>>>>>>> ae0da1fbc (*: prevent ping from forwarding (#9042))
if err != nil {
log.Error("failed to new request", errs.ZapError(errs.ErrNewHTTPRequest, err))
cancel()
Expand Down
36 changes: 36 additions & 0 deletions tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,42 @@ func (suite *redirectorTestSuite) TestAllowFollowerHandle() {
suite.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 {
re.NoError(svr.Run())
}
}
re.NotEmpty(suite.cluster.WaitLeader())
}

func (suite *redirectorTestSuite) TestNotLeader() {
// Find a follower.
var follower *server.Server
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"
)

// NewPingCommand return a ping subcommand of rootCmd
Expand All @@ -33,7 +35,7 @@ func NewPingCommand() *cobra.Command {

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

0 comments on commit 197a9c7

Please sign in to comment.