Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api :add primary api for micro service #7815

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ func PProfGoroutineWithDebugLevel(level int) string {
func MicroServiceMembers(service string) string {
return fmt.Sprintf("%s/members/%s", microServicePrefix, service)
}

// MicroServicePrimary returns the path of PD HTTP API to get the primary of microservice.
func MicroServicePrimary(service string) string {
return fmt.Sprintf("%s/primary/%s", microServicePrefix, service)
}
12 changes: 12 additions & 0 deletions client/http/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type Client interface {
GetPDVersion(context.Context) (string, error)
/* Micro Service interfaces */
GetMicroServiceMembers(context.Context, string) ([]string, error)
GetMicroServicePrimary(context.Context, string) (string, error)
DeleteOperators(context.Context) error

/* Client-related methods */
Expand Down Expand Up @@ -868,6 +869,17 @@ func (c *client) GetMicroServiceMembers(ctx context.Context, service string) ([]
return members, nil
}

// GetMicroServicePrimary gets the primary of the microservice.
func (c *client) GetMicroServicePrimary(ctx context.Context, service string) (string, error) {
var primary string
err := c.request(ctx, newRequestInfo().
WithName(getMicroServicePrimaryName).
WithURI(MicroServicePrimary(service)).
WithMethod(http.MethodGet).
WithResp(&primary))
return primary, err
}

// GetPDVersion gets the release version of the PD binary.
func (c *client) GetPDVersion(ctx context.Context) (string, error) {
var ver struct {
Expand Down
1 change: 1 addition & 0 deletions client/http/request_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
accelerateScheduleInBatchName = "AccelerateScheduleInBatch"
getMinResolvedTSByStoresIDsName = "GetMinResolvedTSByStoresIDs"
getMicroServiceMembersName = "GetMicroServiceMembers"
getMicroServicePrimaryName = "GetMicroServicePrimary"
getPDVersionName = "GetPDVersion"
resetTSName = "ResetTS"
resetBaseAllocIDName = "ResetBaseAllocID"
Expand Down
23 changes: 23 additions & 0 deletions server/apiv2/handlers/micro_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
router := r.Group("ms")
router.Use(middlewares.BootstrapChecker())
router.GET("members/:service", GetMembers)
router.GET("primary/:service", GetPrimary)
}

// GetMembers gets all members of the cluster for the specified service.
Expand Down Expand Up @@ -55,3 +56,25 @@

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

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

Check warning on line 70 in server/apiv2/handlers/micro_service.go

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/micro_service.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

if service := c.Param("service"); len(service) > 0 {
addr, _ := svr.GetServicePrimaryAddr(c.Request.Context(), service)
c.IndentedJSON(http.StatusOK, addr)
return
}

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

Check warning on line 79 in server/apiv2/handlers/micro_service.go

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/micro_service.go#L79

Added line #L79 was not covered by tests
}
11 changes: 11 additions & 0 deletions tests/integrations/mcs/members/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,14 @@ func (suite *memberTestSuite) TestMembers() {
re.NoError(err)
re.Len(members, 3)
}

func (suite *memberTestSuite) TestPrimary() {
re := suite.Require()
primary, err := suite.dialClient.GetMicroServicePrimary(suite.ctx, "tso")
re.NoError(err)
re.NotEmpty(primary)

primary, err = suite.dialClient.GetMicroServicePrimary(suite.ctx, "scheduling")
re.NoError(err)
re.NotEmpty(primary)
}
Loading