-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: add service gc safepoint commands (#2797)
Signed-off-by: Neil Shen <[email protected]>
- Loading branch information
Showing
11 changed files
with
319 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2020 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/tikv/pd/server" | ||
"github.com/tikv/pd/server/core" | ||
"github.com/unrolled/render" | ||
) | ||
|
||
type serviceGCSafepointHandler struct { | ||
svr *server.Server | ||
rd *render.Render | ||
} | ||
|
||
func newServiceGCSafepointHandler(svr *server.Server, rd *render.Render) *serviceGCSafepointHandler { | ||
return &serviceGCSafepointHandler{ | ||
svr: svr, | ||
rd: rd, | ||
} | ||
} | ||
|
||
type listServiceGCSafepoint struct { | ||
ServiceGCSafepoints []*core.ServiceSafePoint `json:"service_gc_safe_points"` | ||
GCSafePoint uint64 `json:"gc_safe_point"` | ||
} | ||
|
||
// @Tags servicegcsafepoint | ||
// @Summary Get all service GC safepoint. | ||
// @Produce json | ||
// @Success 200 {array} listServiceGCSafepoint | ||
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /gc/safepoint [get] | ||
func (h *serviceGCSafepointHandler) List(w http.ResponseWriter, r *http.Request) { | ||
storage := h.svr.GetStorage() | ||
gcSafepoint, err := storage.LoadGCSafePoint() | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
ssps, err := storage.GetAllServiceGCSafePoints() | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
list := listServiceGCSafepoint{ | ||
GCSafePoint: gcSafepoint, | ||
ServiceGCSafepoints: ssps, | ||
} | ||
h.rd.JSON(w, http.StatusOK, list) | ||
} | ||
|
||
// @Tags servicegcsafepoint | ||
// @Summary Delete a service GC safepoint. | ||
// @Param service_id path string true "Service ID" | ||
// @Produce json | ||
// @Success 200 {string} string "Delete service GC safepoint successfully." | ||
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /gc/safepoint/{service_id} [delete] | ||
// @Tags rule | ||
func (h *serviceGCSafepointHandler) Delete(w http.ResponseWriter, r *http.Request) { | ||
storage := h.svr.GetStorage() | ||
serviceID := mux.Vars(r)["service_id"] | ||
err := storage.RemoveServiceGCSafePoint(serviceID) | ||
if err != nil { | ||
h.rd.JSON(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
h.rd.JSON(w, http.StatusOK, "Delete service GC safepoint successfully.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2020 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/kvproto/pkg/metapb" | ||
"github.com/tikv/pd/pkg/apiutil" | ||
"github.com/tikv/pd/server" | ||
"github.com/tikv/pd/server/core" | ||
) | ||
|
||
var _ = Suite(&testServiceGCSafepointSuite{}) | ||
|
||
type testServiceGCSafepointSuite struct { | ||
svr *server.Server | ||
cleanup cleanUpFunc | ||
urlPrefix string | ||
} | ||
|
||
func (s *testServiceGCSafepointSuite) SetUpSuite(c *C) { | ||
s.svr, s.cleanup = mustNewServer(c) | ||
mustWaitLeader(c, []*server.Server{s.svr}) | ||
|
||
addr := s.svr.GetAddr() | ||
s.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) | ||
|
||
mustBootstrapCluster(c, s.svr) | ||
mustPutStore(c, s.svr, 1, metapb.StoreState_Up, nil) | ||
} | ||
|
||
func (s *testServiceGCSafepointSuite) TearDownSuite(c *C) { | ||
s.cleanup() | ||
} | ||
|
||
func (s *testServiceGCSafepointSuite) TestRegionStats(c *C) { | ||
sspURL := s.urlPrefix + "/gc/safepoint" | ||
|
||
storage := s.svr.GetStorage() | ||
list := &listServiceGCSafepoint{ | ||
ServiceGCSafepoints: []*core.ServiceSafePoint{ | ||
{ | ||
ServiceID: "a", | ||
ExpiredAt: time.Now().Unix() + 10, | ||
SafePoint: 1, | ||
}, | ||
{ | ||
ServiceID: "b", | ||
ExpiredAt: time.Now().Unix() + 10, | ||
SafePoint: 2, | ||
}, | ||
{ | ||
ServiceID: "c", | ||
ExpiredAt: time.Now().Unix() + 10, | ||
SafePoint: 3, | ||
}, | ||
}, | ||
GCSafePoint: 1, | ||
} | ||
for _, ssp := range list.ServiceGCSafepoints { | ||
err := storage.SaveServiceGCSafePoint(ssp) | ||
c.Assert(err, IsNil) | ||
} | ||
storage.SaveGCSafePoint(1) | ||
|
||
res, err := testDialClient.Get(sspURL) | ||
c.Assert(err, IsNil) | ||
listResp := &listServiceGCSafepoint{} | ||
err = apiutil.ReadJSON(res.Body, listResp) | ||
c.Assert(err, IsNil) | ||
c.Assert(listResp, DeepEquals, list) | ||
|
||
res, err = doDelete(testDialClient, sspURL+"/a") | ||
c.Assert(err, IsNil) | ||
c.Assert(res.StatusCode, Equals, http.StatusOK) | ||
|
||
left, err := storage.GetAllServiceGCSafePoints() | ||
c.Assert(err, IsNil) | ||
c.Assert(left, DeepEquals, list.ServiceGCSafepoints[1:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.