|
| 1 | +// Copyright 2025 TiKV Project Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package handlers |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "strconv" |
| 20 | + |
| 21 | + "github.com/gin-gonic/gin" |
| 22 | + "github.com/tikv/pd/pkg/storage/endpoint" |
| 23 | + "github.com/tikv/pd/server" |
| 24 | + "github.com/tikv/pd/server/apiv2/middlewares" |
| 25 | +) |
| 26 | + |
| 27 | +// RegisterSafePoint register gc safe point related handlers to router paths. |
| 28 | +func RegisterSafePoint(r *gin.RouterGroup) { |
| 29 | + router := r.Group("gc/safepoint") |
| 30 | + router.Use(middlewares.BootstrapChecker()) |
| 31 | + router.GET("/:keyspaceID", LoadGCSafePoint) |
| 32 | +} |
| 33 | + |
| 34 | +// GCSafePoint is the response structure for gc safe point. |
| 35 | +type GCSafePoint struct { |
| 36 | + *endpoint.GCSafePointV2 |
| 37 | +} |
| 38 | + |
| 39 | +// LoadGCSafePoint returns target keyspace gc safe point. |
| 40 | +// |
| 41 | +// @Tags safepoint |
| 42 | +// @Summary Get gc safe point. |
| 43 | +// @Param keyspaceId path uint32 true "Keyspace ID" |
| 44 | +// @Produce json |
| 45 | +// @Success 200 {object} GCSafePoint |
| 46 | +// @Failure 500 {string} string "PD server failed to proceed the request." |
| 47 | +// @Router /gc/safepoint/{keyspaceID} [get] |
| 48 | +func LoadGCSafePoint(c *gin.Context) { |
| 49 | + svr := c.MustGet(middlewares.ServerContextKey).(*server.Server) |
| 50 | + manager := svr.GetSafePointV2Manager() |
| 51 | + if manager == nil { |
| 52 | + c.AbortWithStatusJSON(http.StatusInternalServerError, managerUninitializedErr) |
| 53 | + return |
| 54 | + } |
| 55 | + keyspaceIDStr := c.Param("keyspaceID") |
| 56 | + keyspaceID, err := strconv.ParseUint(keyspaceIDStr, 10, 24) |
| 57 | + if err != nil { |
| 58 | + c.AbortWithStatusJSON(http.StatusBadRequest, "invalid keyspace ID: "+keyspaceIDStr) |
| 59 | + return |
| 60 | + } |
| 61 | + resp, err := manager.LoadGCSafePoint(uint32(keyspaceID)) |
| 62 | + if err != nil { |
| 63 | + c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) |
| 64 | + return |
| 65 | + } |
| 66 | + c.IndentedJSON(http.StatusOK, &GCSafePoint{resp}) |
| 67 | +} |
0 commit comments