From a306396ed3a804b0ea92ddad5167a6f341f2c64c Mon Sep 17 00:00:00 2001 From: husharp Date: Tue, 1 Aug 2023 18:17:50 +0800 Subject: [PATCH] add store ids interface Signed-off-by: husharp --- server/api/min_resolved_ts.go | 29 +++++++++++++++++++++++++++++ server/api/router.go | 1 + server/cluster/cluster.go | 14 ++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/server/api/min_resolved_ts.go b/server/api/min_resolved_ts.go index 0d30ea3395e5..e1ac7ab5bea4 100644 --- a/server/api/min_resolved_ts.go +++ b/server/api/min_resolved_ts.go @@ -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" @@ -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 @@ -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 diff --git a/server/api/router.go b/server/api/router.go index 1e0d12d53b6b..39f7cce6178d 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -349,6 +349,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) diff --git a/server/cluster/cluster.go b/server/cluster/cluster.go index b2ad25cf0cab..a6cbe655c1a3 100644 --- a/server/cluster/cluster.go +++ b/server/cluster/cluster.go @@ -2487,6 +2487,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 { + allMinResolvedTS := make(map[uint64]uint64) + 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) + } + return allMinResolvedTS +} + // GetExternalTS returns the external timestamp. func (c *RaftCluster) GetExternalTS() uint64 { c.RLock()