Skip to content

Commit

Permalink
Add remove region storage api (tikv#48)
Browse files Browse the repository at this point in the history
* remove region meta when remove region cache

Signed-off-by: David <8039876+AmoebaProtozoa@users.noreply.github.com>

* add delete region storage method

Signed-off-by: David <8039876+AmoebaProtozoa@users.noreply.github.com>

* fix comments

Signed-off-by: David <8039876+AmoebaProtozoa@users.noreply.github.com>

---------

Signed-off-by: David <8039876+AmoebaProtozoa@users.noreply.github.com>
  • Loading branch information
AmoebaProtozoa authored and rleungx committed Jul 27, 2023
1 parent 3197165 commit 9191a3e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions server/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"strconv"

"github.com/gorilla/mux"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/server"
"github.com/unrolled/render"
"go.uber.org/zap"
)

type adminHandler struct {
Expand Down Expand Up @@ -59,6 +62,43 @@ func (h *adminHandler) DeleteRegionCache(w http.ResponseWriter, r *http.Request)
h.rd.JSON(w, http.StatusOK, "The region is removed from server cache.")
}

// @Tags admin
// @Summary Remove target region from region cache and storage.
// @Param id path integer true "Region Id"
// @Produce json
// @Success 200 {string} string "The region is removed from server storage."
// @Failure 400 {string} string "The input is invalid."
// @Router /admin/storage/region/{id} [delete]
func (h *adminHandler) DeleteRegionStorage(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r)
vars := mux.Vars(r)
regionIDStr := vars["id"]
regionID, err := strconv.ParseUint(regionIDStr, 10, 64)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
targetRegion := rc.GetRegion(regionID)
if targetRegion == nil {
h.rd.JSON(w, http.StatusBadRequest, "failed to get target region from cache")
return
}

// Remove region from storage
if err = rc.GetStorage().DeleteRegion(targetRegion.GetMeta()); err != nil {
log.Error("failed to delete region from storage",
zap.Uint64("region-id", targetRegion.GetID()),
zap.Stringer("region-meta", core.RegionToHexMeta(targetRegion.GetMeta())),
errs.ZapError(err))
h.rd.JSON(w, http.StatusOK, "failed to delete region from storage.")
return
}
// Remove region from cache.
rc.DropCacheRegion(regionID)

h.rd.JSON(w, http.StatusOK, "The region is removed from server cache and region meta storage.")
}

// @Tags admin
// @Summary Drop all regions from cache.
// @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 @@ -297,6 +297,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {

adminHandler := newAdminHandler(svr, rd)
registerFunc(clusterRouter, "/admin/cache/region/{id}", adminHandler.DeleteRegionCache, setMethods(http.MethodDelete), setAuditBackend(localLog, prometheus))
registerFunc(clusterRouter, "/admin/storage/region/{id}", adminHandler.DeleteRegionStorage, setMethods(http.MethodDelete), setAuditBackend(localLog, prometheus))
registerFunc(clusterRouter, "/admin/cache/regions", adminHandler.DeleteAllRegionCache, setMethods(http.MethodDelete), setAuditBackend(localLog, prometheus))
registerFunc(apiRouter, "/admin/persist-file/{file_name}", adminHandler.SavePersistFile, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))
registerFunc(apiRouter, "/admin/cluster/markers/snapshot-recovering", adminHandler.IsSnapshotRecovering, setMethods(http.MethodGet), setAuditBackend(localLog, prometheus))
Expand Down

0 comments on commit 9191a3e

Please sign in to comment.