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 0c537bb commit a306396
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 @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a306396

Please sign in to comment.