Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Dec 18, 2023
1 parent 22f01fd commit 02c694a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 97 deletions.
2 changes: 1 addition & 1 deletion client/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const (
Status = "/pd/api/v1/status"
Version = "/pd/api/v1/version"
// Micro Service
microServicePrefix = "/pd/api/v1/ms"
microServicePrefix = "/pd/api/v2/ms"
)

// RegionByID returns the path of PD HTTP API to get region by ID.
Expand Down
91 changes: 0 additions & 91 deletions server/api/micro_service.go

This file was deleted.

4 changes: 0 additions & 4 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,6 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
registerFunc(apiRouter, "/leader/resign", leaderHandler.ResignLeader, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))
registerFunc(apiRouter, "/leader/transfer/{next_leader}", leaderHandler.TransferLeader, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))

msHandler := newMicroServiceHandlerHandler(svr, rd)
registerFunc(apiRouter, "/ms/members/{service}", msHandler.GetMembers, setMethods(http.MethodGet), setAuditBackend(prometheus))
registerFunc(apiRouter, "/ms/primary/{service}", msHandler.GetPrimary, setMethods(http.MethodGet), setAuditBackend(prometheus))

statsHandler := newStatsHandler(svr, rd)
registerFunc(clusterRouter, "/stats/region", statsHandler.GetRegionStatus, setMethods(http.MethodGet), setAuditBackend(prometheus))

Expand Down
106 changes: 106 additions & 0 deletions server/apiv2/handlers/micro_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2023 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package handlers

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/mcs/discovery"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/apiv2/middlewares"
"go.uber.org/zap"
)

// RegisterMicroService registers microservice handler to the router.
func RegisterMicroService(r *gin.RouterGroup) {
router := r.Group("ms")
router.Use(middlewares.BootstrapChecker())
router.GET("members/:service", GetMembers)
router.GET("primary/:service", GetPrimary)
}

// @Tags members
// @Summary Get all members of the cluster for the specified service.
// @Produce json
// @Success 200 {object} []string
// @Router /ms/members/{service} [get]
func GetMembers(c *gin.Context) {
log.Info("Get members")
svr := c.MustGet(middlewares.ServerContextKey).(*server.Server)
if !svr.IsAPIServiceMode() {
c.AbortWithStatusJSON(http.StatusServiceUnavailable, "not support micro service")
return
}

if service := c.Param("service"); len(service) > 0 {
resps, err := discovery.GetMembers(service, svr.GetClient())
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
return
}
if resps == nil {
c.AbortWithStatusJSON(http.StatusNotFound, fmt.Sprintf("no members for %s", service))
return
}

var addrs []string
for _, resp := range resps.Responses {
for _, keyValue := range resp.GetResponseRange().GetKvs() {
var entry discovery.ServiceRegistryEntry
if err = entry.Deserialize(keyValue.Value); err != nil {
log.Info("deserialize failed", zap.String("key", string(keyValue.Key)), zap.Error(err))
}
addrs = append(addrs, entry.ServiceAddr)
}
}
c.IndentedJSON(http.StatusOK, addrs)
return
}

c.AbortWithStatusJSON(http.StatusInternalServerError, "please specify service")
}

// @Tags Primary
// @Summary Get the primary of the cluster for the specified service.
// @Produce json
// @Success 200 {object} pdpb.Member
// @Router /ms/primary/{service} [get]
func GetPrimary(c *gin.Context) {
log.Info("GetPrimary")
svr := c.MustGet(middlewares.ServerContextKey).(*server.Server)
if !svr.IsAPIServiceMode() {
c.AbortWithStatusJSON(http.StatusServiceUnavailable, "not support micro service")
return
}
if service := c.Param("service"); len(service) > 0 {
keyspaceID, _ := c.GetQuery("keyspace_id")
primary, _, err := discovery.GetMCSPrimary(service, svr.GetClient(), keyspaceID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
return
}
if primary == nil {
c.AbortWithStatusJSON(http.StatusNotFound, fmt.Sprintf("no primary for %s", service))
return
}
c.IndentedJSON(http.StatusOK, primary)
return
}

c.AbortWithStatusJSON(http.StatusInternalServerError, "please specify service")
}
1 change: 1 addition & 0 deletions server/apiv2/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ func NewV2Handler(_ context.Context, svr *server.Server) (http.Handler, apiutil.
root := router.Group(apiV2Prefix)
handlers.RegisterKeyspace(root)
handlers.RegisterTSOKeyspaceGroup(root)
handlers.RegisterMicroService(root)
return router, group, nil
}
2 changes: 1 addition & 1 deletion tests/integrations/mcs/members/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ func (suite *memberTestSuite) TestPrimary() {
leader, err := suite.dialClient.GetMicroServicePrimary(suite.ctx, "tso")
re.NotNil(leader)
re.NoError(err)

leader, err = suite.dialClient.GetMicroServicePrimary(suite.ctx, "scheduling")
re.NotNil(leader)
re.NoError(err)
}

func (suite *memberTestSuite) TestMembers() {
re := suite.Require()

members, err := suite.dialClient.GetMicroServiceMembers(suite.ctx, "tso")
re.NoError(err)
re.Len(members, utils.DefaultKeyspaceGroupReplicaCount)
Expand Down

0 comments on commit 02c694a

Please sign in to comment.