Skip to content

Commit

Permalink
cmd, server: add env to manage the service mode (#7018)
Browse files Browse the repository at this point in the history
ref #5766

Add env `PD_SERVICE_MODE` to manage the service mode.

Signed-off-by: Ryan Leung <rleungx@gmail.com>

Co-authored-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
JmPotato and rleungx authored Aug 31, 2023
1 parent da1b937 commit 7a4e1e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 17 additions & 4 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"os"
"os/signal"
"strings"
"syscall"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
Expand Down Expand Up @@ -48,6 +49,13 @@ import (
_ "github.com/tikv/pd/pkg/mcs/tso/server/install"
)

const (
apiMode = "api"
tsoMode = "tso"
rmMode = "resource-manager"
serviceModeEnv = "PD_SERVICE_MODE"
)

func main() {
rootCmd := &cobra.Command{
Use: "pd-server",
Expand Down Expand Up @@ -81,7 +89,7 @@ func NewServiceCommand() *cobra.Command {
// NewTSOServiceCommand returns the tso service command.
func NewTSOServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tso",
Use: tsoMode,
Short: "Run the TSO service",
Run: tso.CreateServerWrapper,
}
Expand Down Expand Up @@ -121,7 +129,7 @@ func NewSchedulingServiceCommand() *cobra.Command {
// NewResourceManagerServiceCommand returns the resource manager service command.
func NewResourceManagerServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "resource-manager",
Use: rmMode,
Short: "Run the resource manager service",
Run: resource_manager.CreateServerWrapper,
}
Expand All @@ -141,7 +149,7 @@ func NewResourceManagerServiceCommand() *cobra.Command {
// NewAPIServiceCommand returns the API service command.
func NewAPIServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "api",
Use: apiMode,
Short: "Run the API service",
Run: createAPIServerWrapper,
}
Expand Down Expand Up @@ -175,7 +183,12 @@ func createAPIServerWrapper(cmd *cobra.Command, args []string) {
}

func createServerWrapper(cmd *cobra.Command, args []string) {
start(cmd, args)
mode := os.Getenv(serviceModeEnv)
if len(mode) != 0 && strings.ToLower(mode) == apiMode {
start(cmd, args, apiMode)
} else {
start(cmd, args)
}
}

func start(cmd *cobra.Command, args []string, services ...string) {
Expand Down
6 changes: 3 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ type HandlerBuilder func(context.Context, *Server) (http.Handler, apiutil.APISer
// CreateServer creates the UNINITIALIZED pd server with given configuration.
func CreateServer(ctx context.Context, cfg *config.Config, services []string, legacyServiceBuilders ...HandlerBuilder) (*Server, error) {
var mode string
if len(services) == 0 {
mode = PDMode
} else {
if len(services) != 0 {
mode = APIServiceMode
} else {
mode = PDMode
}
log.Info(fmt.Sprintf("%s config", mode), zap.Reflect("config", cfg))
serviceMiddlewareCfg := config.NewServiceMiddlewareConfig()
Expand Down

0 comments on commit 7a4e1e2

Please sign in to comment.