-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: support to query whether pd has loaded region (#8749)
close #8426, close #8748 Signed-off-by: lhy1024 <[email protected]> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c5812ee
commit da4b43a
Showing
6 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2024 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/tikv/pd/pkg/storage" | ||
"github.com/tikv/pd/server" | ||
"github.com/tikv/pd/server/apiv2/middlewares" | ||
) | ||
|
||
// ReadyStatus reflects the cluster's ready status. | ||
// NOTE: This type is exported by HTTP API. Please pay more attention when modifying it. | ||
type ReadyStatus struct { | ||
RegionLoaded bool `json:"region_loaded"` | ||
} | ||
|
||
// @Summary It will return whether pd follower is ready to became leader. | ||
// @Router /ready [get] | ||
// @Param verbose query bool false "Whether to return details." | ||
// @Success 200 | ||
// @Failure 500 | ||
func Ready(c *gin.Context) { | ||
svr := c.MustGet(middlewares.ServerContextKey).(*server.Server) | ||
s := svr.GetStorage() | ||
regionLoaded := storage.AreRegionsLoaded(s) | ||
if regionLoaded { | ||
c.Status(http.StatusOK) | ||
} else { | ||
c.Status(http.StatusInternalServerError) | ||
} | ||
|
||
if _, ok := c.GetQuery("verbose"); !ok { | ||
return | ||
} | ||
resp := &ReadyStatus{ | ||
RegionLoaded: regionLoaded, | ||
} | ||
if regionLoaded { | ||
c.IndentedJSON(http.StatusOK, resp) | ||
} else { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, resp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/pingcap/failpoint" | ||
|
||
tu "github.com/tikv/pd/pkg/utils/testutil" | ||
"github.com/tikv/pd/server/apiv2/handlers" | ||
"github.com/tikv/pd/tests" | ||
) | ||
|
||
func TestReady(t *testing.T) { | ||
re := require.New(t) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
cluster, err := tests.NewTestCluster(ctx, 1) | ||
re.NoError(err) | ||
defer cluster.Destroy() | ||
re.NoError(cluster.RunInitialServers()) | ||
re.NotEmpty(cluster.WaitLeader()) | ||
server := cluster.GetLeaderServer() | ||
re.NoError(server.BootstrapCluster()) | ||
url := server.GetConfig().ClientUrls + v2Prefix + "/ready" | ||
failpoint.Enable("github.com/tikv/pd/pkg/storage/loadRegionSlow", `return()`) | ||
checkReady(re, url, false) | ||
failpoint.Disable("github.com/tikv/pd/pkg/storage/loadRegionSlow") | ||
checkReady(re, url, true) | ||
} | ||
|
||
func checkReady(re *require.Assertions, url string, isReady bool) { | ||
expectCode := http.StatusOK | ||
if !isReady { | ||
expectCode = http.StatusInternalServerError | ||
} | ||
resp, err := tests.TestDialClient.Get(url) | ||
re.NoError(err) | ||
defer resp.Body.Close() | ||
buf, err := io.ReadAll(resp.Body) | ||
re.NoError(err) | ||
re.Empty(buf) | ||
re.Equal(expectCode, resp.StatusCode) | ||
r := &handlers.ReadyStatus{} | ||
if isReady { | ||
r.RegionLoaded = true | ||
} | ||
data, err := json.Marshal(r) | ||
re.NoError(err) | ||
err = tu.CheckGetJSON(tests.TestDialClient, url+"?verbose", data, | ||
tu.Status(re, expectCode)) | ||
re.NoError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters