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

statistics: get region info via core cluster inside RegionStatistics (#6804) #6818

Closed
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
7 changes: 0 additions & 7 deletions metrics/grafana/pd.json
Original file line number Diff line number Diff line change
Expand Up @@ -796,13 +796,6 @@
"intervalFactor": 2,
"legendFormat": "{{type}}",
"refId": "B"
},
{
"expr": "pd_regions_offline_status{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", type=\"offline-peer-region-count\", instance=\"$instance\"}",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "{{type}}",
"refId": "C"
}
],
"thresholds": [
Expand Down
99 changes: 25 additions & 74 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,16 @@ func (h *regionsHandler) GetStoreRegions(w http.ResponseWriter, r *http.Request)
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/miss-peer [get]
func (h *regionsHandler) GetMissPeerRegions(w http.ResponseWriter, r *http.Request) {
func (h *regionsHandler) GetMissPeerRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.MissPeer)
}

func (h *regionsHandler) getRegionsByType(
w http.ResponseWriter,
typ statistics.RegionStatisticType,
) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.MissPeer)
regions, err := handler.GetRegionsByType(typ)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -421,15 +428,8 @@ func (h *regionsHandler) GetMissPeerRegions(w http.ResponseWriter, r *http.Reque
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/extra-peer [get]
func (h *regionsHandler) GetExtraPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.ExtraPeer)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetExtraPeerRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.ExtraPeer)
}

// @Tags region
Expand All @@ -438,15 +438,8 @@ func (h *regionsHandler) GetExtraPeerRegions(w http.ResponseWriter, r *http.Requ
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/pending-peer [get]
func (h *regionsHandler) GetPendingPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.PendingPeer)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetPendingPeerRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.PendingPeer)
}

// @Tags region
Expand All @@ -455,15 +448,8 @@ func (h *regionsHandler) GetPendingPeerRegions(w http.ResponseWriter, r *http.Re
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/down-peer [get]
func (h *regionsHandler) GetDownPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.DownPeer)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetDownPeerRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.DownPeer)
}

// @Tags region
Expand All @@ -472,15 +458,8 @@ func (h *regionsHandler) GetDownPeerRegions(w http.ResponseWriter, r *http.Reque
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/learner-peer [get]
func (h *regionsHandler) GetLearnerPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.LearnerPeer)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetLearnerPeerRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.LearnerPeer)
}

// @Tags region
Expand All @@ -489,15 +468,8 @@ func (h *regionsHandler) GetLearnerPeerRegions(w http.ResponseWriter, r *http.Re
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/offline-peer [get]
func (h *regionsHandler) GetOfflinePeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetOfflinePeer(statistics.OfflinePeer)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetOfflinePeerRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.OfflinePeer)
}

// @Tags region
Expand All @@ -506,15 +478,8 @@ func (h *regionsHandler) GetOfflinePeerRegions(w http.ResponseWriter, r *http.Re
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/oversized-region [get]
func (h *regionsHandler) GetOverSizedRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.OversizedRegion)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetOverSizedRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.OversizedRegion)
}

// @Tags region
Expand All @@ -523,15 +488,8 @@ func (h *regionsHandler) GetOverSizedRegions(w http.ResponseWriter, r *http.Requ
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/undersized-region [get]
func (h *regionsHandler) GetUndersizedRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.UndersizedRegion)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetUndersizedRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.UndersizedRegion)
}

// @Tags region
Expand All @@ -540,15 +498,8 @@ func (h *regionsHandler) GetUndersizedRegions(w http.ResponseWriter, r *http.Req
// @Success 200 {object} RegionsInfo
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /regions/check/empty-region [get]
func (h *regionsHandler) GetEmptyRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
regions, err := handler.GetRegionsByType(statistics.EmptyRegion)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
func (h *regionsHandler) GetEmptyRegions(w http.ResponseWriter, _ *http.Request) {
h.getRegionsByType(w, statistics.EmptyRegion)
}

type histItem struct {
Expand Down
14 changes: 13 additions & 1 deletion server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ func (suite *regionTestSuite) TestRegion() {
func (suite *regionTestSuite) TestRegionCheck() {
r := newTestRegionInfo(2, 1, []byte("a"), []byte("b"))
downPeer := &metapb.Peer{Id: 13, StoreId: 2}
r = r.Clone(core.WithAddPeer(downPeer), core.WithDownPeers([]*pdpb.PeerStats{{Peer: downPeer, DownSeconds: 3600}}), core.WithPendingPeers([]*metapb.Peer{downPeer}))
r = r.Clone(
core.WithAddPeer(downPeer),
core.WithDownPeers([]*pdpb.PeerStats{{Peer: downPeer, DownSeconds: 3600}}),
core.WithPendingPeers([]*metapb.Peer{downPeer}))
re := suite.Require()
mustRegionHeartbeat(re, suite.svr, r)
url := fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, r.GetID())
Expand Down Expand Up @@ -220,6 +223,15 @@ func (suite *regionTestSuite) TestRegionCheck() {
suite.NoError(tu.ReadGetJSON(re, testDialClient, url, &r7))
histKeys := []*histItem{{Start: 1000, End: 1999, Count: 1}}
suite.Equal(histKeys, r7)

mustPutStore(re, suite.svr, 2, metapb.StoreState_Offline, metapb.NodeState_Removing, []*metapb.StoreLabel{})
mustRegionHeartbeat(re, suite.svr, r)
url = fmt.Sprintf("%s/regions/check/%s", suite.urlPrefix, "offline-peer")
r8 := &RegionsInfo{}
suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r8))
r4.Adjust()
suite.Equal(1, r8.Count)
suite.Equal(r.GetID(), r8.Regions[0].ID)
}

func (suite *regionTestSuite) TestRegions() {
Expand Down
10 changes: 10 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,13 @@ func (c *RaftCluster) Start(s Server) error {
return err
}
c.storeConfigManager = config.NewStoreConfigManager(c.httpClient)
<<<<<<< HEAD
c.coordinator = newCoordinator(c.ctx, cluster, s.GetHBStreams())
c.regionStats = statistics.NewRegionStatistics(c.opt, c.ruleManager, c.storeConfigManager)
=======
c.coordinator = schedule.NewCoordinator(c.ctx, cluster, s.GetHBStreams())
c.regionStats = statistics.NewRegionStatistics(c.core, c.opt, c.ruleManager, c.storeConfigManager)
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
c.limiter = NewStoreLimiter(s.GetPersistOptions())
c.externalTS, err = c.storage.LoadExternalTS()
if err != nil {
Expand Down Expand Up @@ -1948,6 +1953,7 @@ func (c *RaftCluster) GetRegionStatsByType(typ statistics.RegionStatisticType) [
return c.regionStats.GetRegionStatsByType(typ)
}

<<<<<<< HEAD
// GetOfflineRegionStatsByType gets the status of the offline region by types.
func (c *RaftCluster) GetOfflineRegionStatsByType(typ statistics.RegionStatisticType) []*core.RegionInfo {
if c.regionStats == nil {
Expand All @@ -1957,6 +1963,10 @@ func (c *RaftCluster) GetOfflineRegionStatsByType(typ statistics.RegionStatistic
}

func (c *RaftCluster) updateRegionsLabelLevelStats(regions []*core.RegionInfo) {
=======
// UpdateRegionsLabelLevelStats updates the status of the region label level by types.
func (c *RaftCluster) UpdateRegionsLabelLevelStats(regions []*core.RegionInfo) {
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
for _, region := range regions {
c.labelLevelStats.Observe(region, c.getStoresWithoutLabelLocked(region, core.EngineKey, core.EngineTiFlash), c.opt.GetLocationLabels())
}
Expand Down
Loading
Loading