Skip to content

Commit

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

Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
JmPotato authored and ti-chi-bot committed Jul 17, 2023
1 parent 41f8422 commit 7ccc118
Show file tree
Hide file tree
Showing 9 changed files with 2,169 additions and 29 deletions.
3 changes: 3 additions & 0 deletions metrics/grafana/pd.json
Original file line number Diff line number Diff line change
Expand Up @@ -792,13 +792,16 @@
"intervalFactor": 2,
"legendFormat": "{{type}}",
"refId": "B"
<<<<<<< HEAD
},
{
"expr": "pd_regions_offline_status{tidb_cluster=\"$tidb_cluster\", type=\"offline-peer-region-count\", instance=\"$instance\"}",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "{{type}}",
"refId": "C"
=======
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
}
],
"thresholds": [
Expand Down
150 changes: 149 additions & 1 deletion server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,82 @@ func (h *regionsHandler) GetStoreRegions(w http.ResponseWriter, r *http.Request)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

<<<<<<< HEAD
// @Tags region
// @Summary List all regions that miss peer.
// @Produce json
// @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) {
=======
// @Tags region
// @Summary List regions belongs to the given keyspace ID.
// @Param keyspace_id query string true "Keyspace ID"
// @Param limit query integer false "Limit count" default(16)
// @Produce json
// @Success 200 {object} RegionsInfo
// @Failure 400 {string} string "The input is invalid."
// @Router /regions/keyspace/id/{id} [get]
func (h *regionsHandler) GetKeyspaceRegions(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r)
vars := mux.Vars(r)
keyspaceIDStr := vars["id"]
if keyspaceIDStr == "" {
h.rd.JSON(w, http.StatusBadRequest, "keyspace id is empty")
return
}

keyspaceID64, err := strconv.ParseUint(keyspaceIDStr, 10, 32)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
keyspaceID := uint32(keyspaceID64)
keyspaceManager := h.svr.GetKeyspaceManager()
if _, err := keyspaceManager.LoadKeyspaceByID(keyspaceID); err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

limit := defaultRegionLimit
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
limit, err = strconv.Atoi(limitStr)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
}
if limit > maxRegionLimit {
limit = maxRegionLimit
}
regionBound := keyspace.MakeRegionBound(keyspaceID)
regions := rc.ScanRegions(regionBound.RawLeftBound, regionBound.RawRightBound, limit)
if limit <= 0 || limit > len(regions) {
txnRegion := rc.ScanRegions(regionBound.TxnLeftBound, regionBound.TxnRightBound, limit-len(regions))
regions = append(regions, txnRegion...)
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

// @Tags region
// @Summary List all regions that miss peer.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.MissPeer)
}

func (h *regionsHandler) getRegionsByType(
w http.ResponseWriter,
typ statistics.RegionStatisticType,
) {
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
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 @@ -391,6 +458,7 @@ func (h *regionsHandler) GetMissPeerRegions(w http.ResponseWriter, r *http.Reque
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

<<<<<<< HEAD
// @Tags region
// @Summary List all regions that has extra peer.
// @Produce json
Expand Down Expand Up @@ -491,6 +559,86 @@ func (h *regionsHandler) GetEmptyRegion(w http.ResponseWriter, r *http.Request)
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
=======
// @Tags region
// @Summary List all regions that has extra peer.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.ExtraPeer)
}

// @Tags region
// @Summary List all regions that has pending peer.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.PendingPeer)
}

// @Tags region
// @Summary List all regions that has down peer.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.DownPeer)
}

// @Tags region
// @Summary List all regions that has learner peer.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.LearnerPeer)
}

// @Tags region
// @Summary List all regions that has offline peer.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.OfflinePeer)
}

// @Tags region
// @Summary List all regions that are oversized.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.OversizedRegion)
}

// @Tags region
// @Summary List all regions that are undersized.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.UndersizedRegion)
}

// @Tags region
// @Summary List all empty regions.
// @Produce json
// @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, _ *http.Request) {
h.getRegionsByType(w, statistics.EmptyRegion)
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
}

type histItem struct {
Expand Down
23 changes: 23 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,19 @@ func (s *testRegionSuite) TestRegion(c *C) {
func (s *testRegionSuite) TestRegionCheck(c *C) {
r := newTestRegionInfo(2, 1, []byte("a"), []byte("b"))
downPeer := &metapb.Peer{Id: 13, StoreId: 2}
<<<<<<< HEAD
r = r.Clone(core.WithAddPeer(downPeer), core.WithDownPeers([]*pdpb.PeerStats{{Peer: downPeer, DownSeconds: 3600}}), core.WithPendingPeers([]*metapb.Peer{downPeer}))
mustRegionHeartbeat(c, s.svr, r)
url := fmt.Sprintf("%s/region/id/%d", s.urlPrefix, r.GetID())
=======
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())
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
r1 := &RegionInfo{}
c.Assert(readJSON(testDialClient, url, r1), IsNil)
r1.Adjust()
Expand Down Expand Up @@ -201,7 +211,20 @@ func (s *testRegionSuite) TestRegionCheck(c *C) {
r7 := make([]*histItem, 1)
c.Assert(readJSON(testDialClient, url, &r7), IsNil)
histKeys := []*histItem{{Start: 1000, End: 1999, Count: 1}}
<<<<<<< HEAD
c.Assert(r7, DeepEquals, histKeys)
=======
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)
>>>>>>> 40eaa35f2 (statistics: get region info via core cluster inside RegionStatistics (#6804))
}

func (s *testRegionSuite) TestRegions(c *C) {
Expand Down
11 changes: 11 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func (c *RaftCluster) Start(s Server) error {
if err != nil {
return err
}
<<<<<<< HEAD

c.replicationMode, err = replication.NewReplicationModeManager(s.GetConfig().ReplicationMode, s.GetStorage(), cluster, s)
if err != nil {
Expand All @@ -262,6 +263,11 @@ func (c *RaftCluster) Start(s Server) error {

c.coordinator = newCoordinator(c.ctx, cluster, s.GetHBStreams())
c.regionStats = statistics.NewRegionStatistics(c.opt, c.ruleManager)
=======
c.storeConfigManager = config.NewStoreConfigManager(c.httpClient)
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.unsafeRecoveryController = newUnsafeRecoveryController(cluster)

Expand Down Expand Up @@ -1347,6 +1353,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 {
c.RLock()
Expand All @@ -1360,6 +1367,10 @@ func (c *RaftCluster) GetOfflineRegionStatsByType(typ statistics.RegionStatistic
func (c *RaftCluster) updateRegionsLabelLevelStats(regions []*core.RegionInfo) {
c.Lock()
defer c.Unlock()
=======
// 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

0 comments on commit 7ccc118

Please sign in to comment.