Skip to content

Commit

Permalink
add store ids interface
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Aug 1, 2023
1 parent de985b8 commit f25f8e1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/api/min_resolved_ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"

"github.com/gorilla/mux"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"github.com/tikv/pd/server"
"github.com/unrolled/render"
Expand All @@ -43,6 +44,13 @@ type minResolvedTS struct {
PersistInterval typeutil.Duration `json:"persist_interval,omitempty"`
}

// NOTE: This type is exported by HTTP API. Please pay more attention when modifying it.
type storesMinResolvedTS struct {
IsRealTime bool `json:"is_real_time,omitempty"`
StoreMinResolvedTS map[uint64]uint64 `json:"store_min_resolved_ts"`
PersistInterval typeutil.Duration `json:"persist_interval,omitempty"`
}

// @Tags min_store_resolved_ts
// @Summary Get store-level min resolved ts.
// @Produce json
Expand All @@ -67,6 +75,27 @@ func (h *minResolvedTSHandler) GetStoreMinResolvedTS(w http.ResponseWriter, r *h
})
}

// @Tags min_store_resolved_ts__by_store_ids
// @Summary Get store-level min resolved ts by multiple store IDs.
// @Produce json
// @Success 200 {array} minResolvedTS
// @Failure 400 {string} string "The input is invalid."
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /min-resolved-ts/store_ids [get]
func (h *minResolvedTSHandler) GetMinResolvedTSByStoreIDs(w http.ResponseWriter, r *http.Request) {
c := h.svr.GetRaftCluster()
var ids []string
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &ids); err != nil {
return
}
persistInterval := c.GetPDServerConfig().MinResolvedTSPersistenceInterval
h.rd.JSON(w, http.StatusOK, storesMinResolvedTS{
StoreMinResolvedTS: c.GetMinResolvedTSByStoreIDs(ids),
PersistInterval: persistInterval,
IsRealTime: persistInterval.Duration != 0,
})
}

// @Tags min_resolved_ts
// @Summary Get cluster-level min resolved ts.
// @Produce json
Expand Down
1 change: 1 addition & 0 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
minResolvedTSHandler := newMinResolvedTSHandler(svr, rd)
registerFunc(clusterRouter, "/min-resolved-ts", minResolvedTSHandler.GetMinResolvedTS, setMethods(http.MethodGet), setAuditBackend(prometheus))
registerFunc(clusterRouter, "/min-resolved-ts/{store_id}", minResolvedTSHandler.GetStoreMinResolvedTS, setMethods(http.MethodGet), setAuditBackend(prometheus))
registerFunc(clusterRouter, "/min-resolved-ts/store_ids", minResolvedTSHandler.GetMinResolvedTSByStoreIDs, setMethods(http.MethodGet), setAuditBackend(prometheus))

// unsafe admin operation API
unsafeOperationHandler := newUnsafeOperationHandler(svr, rd)
Expand Down
14 changes: 14 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,20 @@ func (c *RaftCluster) GetStoreMinResolvedTS(storeID uint64) uint64 {
return c.GetStore(storeID).GetMinResolvedTS()
}

// GetMinResolvedTSByStoreIDs returns the min resolved ts of the stores.
func (c *RaftCluster) GetMinResolvedTSByStoreIDs(ids []string) map[uint64]uint64 {
var allMinResolvedTs map[uint64]uint64

Check failure on line 2497 in server/cluster/cluster.go

View workflow job for this annotation

GitHub Actions / statics

ST1003: var allMinResolvedTs should be allMinResolvedTS (stylecheck)
for _, idStr := range ids {
storeID, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
log.Error("parse store id failed", errs.ZapError(err))
continue
}
allMinResolvedTs[storeID] = c.GetStoreMinResolvedTS(storeID)

Check failure on line 2504 in server/cluster/cluster.go

View workflow job for this annotation

GitHub Actions / statics

SA5000: assignment to nil map (staticcheck)
}
return allMinResolvedTs
}

// GetExternalTS returns the external timestamp.
func (c *RaftCluster) GetExternalTS() uint64 {
c.RLock()
Expand Down

0 comments on commit f25f8e1

Please sign in to comment.