Skip to content

Commit 8c9ac1c

Browse files
committed
feat(dbha-v2): add daemon-start guard for auto-restart on crash. issue: #16394
- Add RunWithGuard and GuardOptions in process/daemon.go to watch child process and restart on abnormal exit - Skip SavePid in pid.go when DBHA_UNDER_GUARD is set - Add DaemonStartCmdRunE in process/cmds.go - Add daemon-start subcommand to probe, receiver, admin, analysis - Add config.Load in admin, receiver, analysis config packages
1 parent 7cf820c commit 8c9ac1c

File tree

21 files changed

+363
-39
lines changed

21 files changed

+363
-39
lines changed

dbm-services/common/dbha-v2/Makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)
5050
# generate go code files
5151
GO_GEN_FILES=$(PROTO_FILES:$(PROTO_DIR)/%.proto=$(GEN_DIR)/%.pb.go)
5252

53-
# services list
54-
SERVICES := admin analysis receiver probe
53+
# services list and binary names with dbha- prefix
54+
SERVICES := admin analysis receiver probe
55+
BIN_PREFIX := dbha-
56+
BINARIES := $(addprefix $(BIN_PREFIX),$(SERVICES))
5557

5658
# define common go build command
5759
define GO_BUILD
5860
@mkdir -p $(BUILD_DIR)
5961
CGO_ENABLED=0 GOOS=$(GO_OS) GOARCH=amd64 go build -ldflags=$(BUILD_FLAG) \
6062
-gcflags="all=-trimpath=$(PWD)" -asmflags="all=-trimpath=$(PWD)" \
61-
-o $(BUILD_DIR)/$(1) cmd/$(1)/main.go
63+
-o $(BUILD_DIR)/$(BIN_PREFIX)$(1) cmd/$(1)/main.go
6264
endef
6365

6466
.PHONY: all proto $(SERVICES) clean format test package help
@@ -100,7 +102,7 @@ toolkits: cluster
100102
cluster:
101103
@mkdir -p $(BUILD_DIR)
102104
CGO_ENABLED=0 GOOS=${GO_OS} GOARCH=amd64 go build -ldflags=$(BUILD_FLAG) -gcflags="all=-trimpath=$(PWD)" \
103-
-asmflags="all=-trimpath=$(PWD)" -o $(BUILD_DIR)/$@ tools/cmd/cluster.go
105+
-asmflags="all=-trimpath=$(PWD)" -o $(BUILD_DIR)/$(BIN_PREFIX)$@ tools/cmd/cluster.go
104106

105107
# build protobuf to go
106108
$(GEN_DIR)/%.pb.go: $(PROTO_DIR)/%.proto
@@ -119,8 +121,8 @@ package: $(SERVICES) toolkits
119121
@echo "Packaging $(PKG_NAME)..."
120122
@rm -rf $(PKG_DIR)
121123
@mkdir -p $(PKG_DIR)/etc $(PKG_DIR)/logs $(PKG_DIR)/pids $(PKG_DIR)/toolkits
122-
@cp $(addprefix $(BUILD_DIR)/,$(SERVICES)) $(PKG_DIR)/
123-
@cp $(BUILD_DIR)/cluster $(PKG_DIR)/toolkits/
124+
@cp $(addprefix $(BUILD_DIR)/,$(BINARIES)) $(PKG_DIR)/
125+
@cp $(BUILD_DIR)/$(BIN_PREFIX)cluster $(PKG_DIR)/toolkits/
124126
@-cp etc/*.yaml $(PKG_DIR)/etc/ 2>/dev/null || true
125127
@cd $(BUILD_DIR) && tar -czvf $(PKG_NAME) $(PROJECT)
126128
@rm -rf $(PKG_DIR)

dbm-services/common/dbha-v2/cmd/admin/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func main() {
4646
rootCmd.AddCommand(admin.MigrateCmd)
4747
rootCmd.AddCommand(admin.HealthCmd)
4848
rootCmd.AddCommand(admin.StartCmd)
49+
rootCmd.AddCommand(admin.DaemonStartCmd)
4950
rootCmd.AddCommand(admin.StopCmd)
5051
rootCmd.AddCommand(admin.RestartCmd)
5152
rootCmd.AddCommand(admin.ReloadCmd)

dbm-services/common/dbha-v2/cmd/analysis/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func main() {
4545
rootCmd.AddCommand(analysis.VersionCmd)
4646
rootCmd.AddCommand(analysis.HealthCmd)
4747
rootCmd.AddCommand(analysis.StartCmd)
48+
rootCmd.AddCommand(analysis.DaemonStartCmd)
4849
rootCmd.AddCommand(analysis.StopCmd)
4950
rootCmd.AddCommand(analysis.RestartCmd)
5051
rootCmd.AddCommand(analysis.ReloadCmd)

dbm-services/common/dbha-v2/cmd/probe/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func main() {
4646
rootCmd.AddCommand(probe.VersionCmd)
4747
rootCmd.AddCommand(probe.HealthCmd)
4848
rootCmd.AddCommand(probe.StartCmd)
49+
rootCmd.AddCommand(probe.DaemonStartCmd)
4950
rootCmd.AddCommand(probe.StopCmd)
5051
rootCmd.AddCommand(probe.RestartCmd)
5152
rootCmd.AddCommand(probe.ReloadCmd)

dbm-services/common/dbha-v2/cmd/receiver/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func main() {
4444
rootCmd.AddCommand(receiver.VersionCmd)
4545
rootCmd.AddCommand(receiver.HealthCmd)
4646
rootCmd.AddCommand(receiver.StartCmd)
47+
rootCmd.AddCommand(receiver.DaemonStartCmd)
4748
rootCmd.AddCommand(receiver.StopCmd)
4849
rootCmd.AddCommand(receiver.RestartCmd)
4950
rootCmd.AddCommand(receiver.ReloadCmd)

dbm-services/common/dbha-v2/internal/admin/cmds/cmds.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func ReloadCmdRunE(cmd *cobra.Command, args []string) error {
5656
return process.ReloadCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAdmin, StopTimeout, ForceStop)
5757
}
5858

59+
func DaemonStartCmdRunE(cmd *cobra.Command, args []string) error {
60+
configPath, _ := cmd.Root().PersistentFlags().GetString("config")
61+
if err := config.Load(configPath); err != nil {
62+
return err
63+
}
64+
return process.DaemonStartCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAdmin, process.DefaultGuardRestartDelay)
65+
}
66+
5967
func HealthCmdRunE(cmd *cobra.Command, _ []string) error {
6068
baseHealth := process.GetBaseHealthInfo(config.Cfg.PidFile, process.NameAdmin)
6169

dbm-services/common/dbha-v2/internal/admin/command.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ var StartCmd = &cobra.Command{
8989
RunE: cmds.StartCmdRunE,
9090
}
9191

92+
// DaemonStartCmd is used to start this process with a guard that restarts it on abnormal exit.
93+
var DaemonStartCmd = &cobra.Command{
94+
Use: "daemon-start",
95+
Short: "Start this process with guard (auto-restart on crash).",
96+
RunE: cmds.DaemonStartCmdRunE,
97+
}
98+
9299
// StopCmd is used to stop this process.
93100
var StopCmd = &cobra.Command{
94101
Use: "stop",

dbm-services/common/dbha-v2/internal/admin/config/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"time"
3030

3131
"dbm-services/common/dbha-v2/pkg/logger"
32+
33+
"github.com/spf13/viper"
3234
)
3335

3436
var Cfg = Configuration{
@@ -107,3 +109,20 @@ type Configuration struct {
107109
Storage StorageConfig `yaml:"storage" mapstructure:"storage"`
108110
Log LogConfig `yaml:"log" mapstructure:"log"`
109111
}
112+
113+
// Load loads admin configuration from file
114+
func Load(configFilePath string) error {
115+
viper.SetConfigName("admin")
116+
viper.SetConfigType("yaml")
117+
viper.AddConfigPath("./etc")
118+
119+
if configFilePath != "" {
120+
viper.SetConfigFile(configFilePath)
121+
}
122+
123+
if err := viper.ReadInConfig(); err != nil {
124+
return err
125+
}
126+
127+
return viper.Unmarshal(&Cfg)
128+
}

dbm-services/common/dbha-v2/internal/analysis/cmds/cmds.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func ReloadCmdRunE(cmd *cobra.Command, args []string) error {
5656
return process.ReloadCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAnalysis, StopTimeout, ForceStop)
5757
}
5858

59+
func DaemonStartCmdRunE(cmd *cobra.Command, args []string) error {
60+
configPath, _ := cmd.Root().PersistentFlags().GetString("config")
61+
if err := config.Load(configPath); err != nil {
62+
return err
63+
}
64+
return process.DaemonStartCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAnalysis, process.DefaultGuardRestartDelay)
65+
}
66+
5967
func HealthCmdRunE(cmd *cobra.Command, _ []string) error {
6068
baseHealth := process.GetBaseHealthInfo(config.Cfg.PidFile, process.NameAnalysis)
6169

dbm-services/common/dbha-v2/internal/analysis/command.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ var StartCmd = &cobra.Command{
5353
RunE: cmds.StartCmdRunE,
5454
}
5555

56+
// DaemonStartCmd is used to start this process with a guard that restarts it on abnormal exit.
57+
var DaemonStartCmd = &cobra.Command{
58+
Use: "daemon-start",
59+
Short: "Start this process with guard (auto-restart on crash).",
60+
RunE: cmds.DaemonStartCmdRunE,
61+
}
62+
5663
// StopCmd is used to stop this process.
5764
var StopCmd = &cobra.Command{
5865
Use: "stop",

0 commit comments

Comments
 (0)