Skip to content
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
14 changes: 8 additions & 6 deletions dbm-services/common/dbha-v2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)
# generate go code files
GO_GEN_FILES=$(PROTO_FILES:$(PROTO_DIR)/%.proto=$(GEN_DIR)/%.pb.go)

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

# define common go build command
define GO_BUILD
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 GOOS=$(GO_OS) GOARCH=amd64 go build -ldflags=$(BUILD_FLAG) \
-gcflags="all=-trimpath=$(PWD)" -asmflags="all=-trimpath=$(PWD)" \
-o $(BUILD_DIR)/$(1) cmd/$(1)/main.go
-o $(BUILD_DIR)/$(BIN_PREFIX)$(1) cmd/$(1)/main.go
endef

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

# build protobuf to go
$(GEN_DIR)/%.pb.go: $(PROTO_DIR)/%.proto
Expand All @@ -119,8 +121,8 @@ package: $(SERVICES) toolkits
@echo "Packaging $(PKG_NAME)..."
@rm -rf $(PKG_DIR)
@mkdir -p $(PKG_DIR)/etc $(PKG_DIR)/logs $(PKG_DIR)/pids $(PKG_DIR)/toolkits
@cp $(addprefix $(BUILD_DIR)/,$(SERVICES)) $(PKG_DIR)/
@cp $(BUILD_DIR)/cluster $(PKG_DIR)/toolkits/
@cp $(addprefix $(BUILD_DIR)/,$(BINARIES)) $(PKG_DIR)/
@cp $(BUILD_DIR)/$(BIN_PREFIX)cluster $(PKG_DIR)/toolkits/
@-cp etc/*.yaml $(PKG_DIR)/etc/ 2>/dev/null || true
@cd $(BUILD_DIR) && tar -czvf $(PKG_NAME) $(PROJECT)
@rm -rf $(PKG_DIR)
Expand Down
1 change: 1 addition & 0 deletions dbm-services/common/dbha-v2/cmd/admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func main() {
rootCmd.AddCommand(admin.MigrateCmd)
rootCmd.AddCommand(admin.HealthCmd)
rootCmd.AddCommand(admin.StartCmd)
rootCmd.AddCommand(admin.DaemonStartCmd)
rootCmd.AddCommand(admin.StopCmd)
rootCmd.AddCommand(admin.RestartCmd)
rootCmd.AddCommand(admin.ReloadCmd)
Expand Down
1 change: 1 addition & 0 deletions dbm-services/common/dbha-v2/cmd/analysis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
rootCmd.AddCommand(analysis.VersionCmd)
rootCmd.AddCommand(analysis.HealthCmd)
rootCmd.AddCommand(analysis.StartCmd)
rootCmd.AddCommand(analysis.DaemonStartCmd)
rootCmd.AddCommand(analysis.StopCmd)
rootCmd.AddCommand(analysis.RestartCmd)
rootCmd.AddCommand(analysis.ReloadCmd)
Expand Down
1 change: 1 addition & 0 deletions dbm-services/common/dbha-v2/cmd/probe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func main() {
rootCmd.AddCommand(probe.VersionCmd)
rootCmd.AddCommand(probe.HealthCmd)
rootCmd.AddCommand(probe.StartCmd)
rootCmd.AddCommand(probe.DaemonStartCmd)
rootCmd.AddCommand(probe.StopCmd)
rootCmd.AddCommand(probe.RestartCmd)
rootCmd.AddCommand(probe.ReloadCmd)
Expand Down
1 change: 1 addition & 0 deletions dbm-services/common/dbha-v2/cmd/receiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
rootCmd.AddCommand(receiver.VersionCmd)
rootCmd.AddCommand(receiver.HealthCmd)
rootCmd.AddCommand(receiver.StartCmd)
rootCmd.AddCommand(receiver.DaemonStartCmd)
rootCmd.AddCommand(receiver.StopCmd)
rootCmd.AddCommand(receiver.RestartCmd)
rootCmd.AddCommand(receiver.ReloadCmd)
Expand Down
8 changes: 8 additions & 0 deletions dbm-services/common/dbha-v2/internal/admin/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func ReloadCmdRunE(cmd *cobra.Command, args []string) error {
return process.ReloadCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAdmin, StopTimeout, ForceStop)
}

func DaemonStartCmdRunE(cmd *cobra.Command, args []string) error {
configPath, _ := cmd.Root().PersistentFlags().GetString("config")
if err := config.Load(configPath); err != nil {
return err
}
return process.DaemonStartCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAdmin, process.DefaultGuardRestartDelay)
}

func HealthCmdRunE(cmd *cobra.Command, _ []string) error {
baseHealth := process.GetBaseHealthInfo(config.Cfg.PidFile, process.NameAdmin)

Expand Down
7 changes: 7 additions & 0 deletions dbm-services/common/dbha-v2/internal/admin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ var StartCmd = &cobra.Command{
RunE: cmds.StartCmdRunE,
}

// DaemonStartCmd is used to start this process with a guard that restarts it on abnormal exit.
var DaemonStartCmd = &cobra.Command{
Use: "daemon-start",
Short: "Start this process with guard (auto-restart on crash).",
RunE: cmds.DaemonStartCmdRunE,
}

// StopCmd is used to stop this process.
var StopCmd = &cobra.Command{
Use: "stop",
Expand Down
19 changes: 19 additions & 0 deletions dbm-services/common/dbha-v2/internal/admin/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"time"

"dbm-services/common/dbha-v2/pkg/logger"

"github.com/spf13/viper"
)

var Cfg = Configuration{
Expand Down Expand Up @@ -107,3 +109,20 @@ type Configuration struct {
Storage StorageConfig `yaml:"storage" mapstructure:"storage"`
Log LogConfig `yaml:"log" mapstructure:"log"`
}

// Load loads admin configuration from file
func Load(configFilePath string) error {
viper.SetConfigName("admin")
viper.SetConfigType("yaml")
viper.AddConfigPath("./etc")

if configFilePath != "" {
viper.SetConfigFile(configFilePath)
}

if err := viper.ReadInConfig(); err != nil {
return err
}

return viper.Unmarshal(&Cfg)
}
8 changes: 8 additions & 0 deletions dbm-services/common/dbha-v2/internal/analysis/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func ReloadCmdRunE(cmd *cobra.Command, args []string) error {
return process.ReloadCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAnalysis, StopTimeout, ForceStop)
}

func DaemonStartCmdRunE(cmd *cobra.Command, args []string) error {
configPath, _ := cmd.Root().PersistentFlags().GetString("config")
if err := config.Load(configPath); err != nil {
return err
}
return process.DaemonStartCmdRunE(cmd, args, config.Cfg.PidFile, process.NameAnalysis, process.DefaultGuardRestartDelay)
}

func HealthCmdRunE(cmd *cobra.Command, _ []string) error {
baseHealth := process.GetBaseHealthInfo(config.Cfg.PidFile, process.NameAnalysis)

Expand Down
7 changes: 7 additions & 0 deletions dbm-services/common/dbha-v2/internal/analysis/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ var StartCmd = &cobra.Command{
RunE: cmds.StartCmdRunE,
}

// DaemonStartCmd is used to start this process with a guard that restarts it on abnormal exit.
var DaemonStartCmd = &cobra.Command{
Use: "daemon-start",
Short: "Start this process with guard (auto-restart on crash).",
RunE: cmds.DaemonStartCmdRunE,
}

// StopCmd is used to stop this process.
var StopCmd = &cobra.Command{
Use: "stop",
Expand Down
19 changes: 19 additions & 0 deletions dbm-services/common/dbha-v2/internal/analysis/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"time"

"dbm-services/common/dbha-v2/pkg/logger"

"github.com/spf13/viper"
)

var Cfg = Configuration{
Expand Down Expand Up @@ -162,6 +164,23 @@ type Configuration struct {
Log LogConfig `yaml:"log" mapstructure:"log"`
}

// Load loads analysis configuration from file
func Load(configFilePath string) error {
viper.SetConfigName("analysis")
viper.SetConfigType("yaml")
viper.AddConfigPath("./etc")

if configFilePath != "" {
viper.SetConfigFile(configFilePath)
}

if err := viper.ReadInConfig(); err != nil {
return err
}

return viper.Unmarshal(&Cfg)
}

func init() {
Cfg.Detector.Ssh.Port = 22
Cfg.Detector.Ssh.User = "root"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/

// Package detector provides probe health detection and target selection for DBHA analysis.
package detector

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type DbhaData struct {
DB *hamysql.GormDB
}

// GetBizIDs returns all distinct business IDs from DBM metadata.
func (ha *DbhaData) GetBizIDs() ([]int, error) {
bkBizIDs := []int{}

Expand All @@ -58,6 +59,7 @@ func (ha *DbhaData) GetBizIDs() ([]int, error) {
return bkBizIDs, nil
}

// ReadMetadataCacheWithBizID reads metadata cache in batches for the given bizID.
func (ha *DbhaData) ReadMetadataCacheWithBizID(bizID int, batchCnt int,
offsetDuration time.Duration) (metaData []*hamodel.DbmMetadata, err error) {

Expand Down Expand Up @@ -164,6 +166,7 @@ func (ha *DbhaData) SaveSwitchingLog(ctx context.Context, records ...*hamodel.Db
return err
}

// ReadSwitchingStrategyWithBkBizId returns switching strategies for the given business ID.
func (ha *DbhaData) ReadSwitchingStrategyWithBkBizId(bkBizId int) ([]*hamodel.DbSwitchingStrategy, error) {
var strategies []*hamodel.DbSwitchingStrategy

Expand Down
8 changes: 8 additions & 0 deletions dbm-services/common/dbha-v2/internal/probe/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func ReloadCmdRunE(cmd *cobra.Command, args []string) error {
return process.ReloadCmdRunE(cmd, args, config.Cfg.PidFile, process.NameProbe, StopTimeout, ForceStop)
}

func DaemonStartCmdRunE(cmd *cobra.Command, args []string) error {
configPath, _ := cmd.Root().PersistentFlags().GetString("config")
if err := config.Load(configPath); err != nil {
return err
}
return process.DaemonStartCmdRunE(cmd, args, config.Cfg.PidFile, process.NameProbe, process.DefaultGuardRestartDelay)
}

func HealthCmdRunE(cmd *cobra.Command, _ []string) error {
if err := config.Load(ConfigFilePath); err != nil {
baseHealth := process.GetBaseHealthInfo(config.Cfg.PidFile, process.NameProbe)
Expand Down
7 changes: 7 additions & 0 deletions dbm-services/common/dbha-v2/internal/probe/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ var StartCmd = &cobra.Command{
RunE: cmds.StartCmdRunE,
}

// DaemonStartCmd is used to start this process with a guard that restarts it on abnormal exit.
var DaemonStartCmd = &cobra.Command{
Use: "daemon-start",
Short: "Start this process with guard (auto-restart on crash).",
RunE: cmds.DaemonStartCmdRunE,
}

// StopCmd is used to stop this process.
var StopCmd = &cobra.Command{
Use: "stop",
Expand Down
8 changes: 8 additions & 0 deletions dbm-services/common/dbha-v2/internal/receiver/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func ReloadCmdRunE(cmd *cobra.Command, args []string) error {
return process.ReloadCmdRunE(cmd, args, config.Cfg.PidFile, process.NameReceiver, StopTimeout, ForceStop)
}

func DaemonStartCmdRunE(cmd *cobra.Command, args []string) error {
configPath, _ := cmd.Root().PersistentFlags().GetString("config")
if err := config.Load(configPath); err != nil {
return err
}
return process.DaemonStartCmdRunE(cmd, args, config.Cfg.PidFile, process.NameReceiver, process.DefaultGuardRestartDelay)
}

func HealthCmdRunE(cmd *cobra.Command, _ []string) error {
baseHealth := process.GetBaseHealthInfo(config.Cfg.PidFile, process.NameReceiver)

Expand Down
7 changes: 7 additions & 0 deletions dbm-services/common/dbha-v2/internal/receiver/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ var StartCmd = &cobra.Command{
RunE: cmds.StartCmdRunE,
}

// DaemonStartCmd is used to start this process with a guard that restarts it on abnormal exit.
var DaemonStartCmd = &cobra.Command{
Use: "daemon-start",
Short: "Start this process with guard (auto-restart on crash).",
RunE: cmds.DaemonStartCmdRunE,
}

// StopCmd is used to stop this process.
var StopCmd = &cobra.Command{
Use: "stop",
Expand Down
19 changes: 19 additions & 0 deletions dbm-services/common/dbha-v2/internal/receiver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"time"

"dbm-services/common/dbha-v2/pkg/logger"

"github.com/spf13/viper"
)

var Cfg = Configuration{
Expand Down Expand Up @@ -102,3 +104,20 @@ type Configuration struct {
Service ServiceConfig `yaml:"service" mapstructure:"service"`
Log LogConfig `yaml:"log" mapstructure:"log"`
}

// Load loads receiver configuration from file
func Load(configFilePath string) error {
viper.SetConfigName("receiver")
viper.SetConfigType("yaml")
viper.AddConfigPath("./etc")

if configFilePath != "" {
viper.SetConfigFile(configFilePath)
}

if err := viper.ReadInConfig(); err != nil {
return err
}

return viper.Unmarshal(&Cfg)
}
Loading