Skip to content

Commit

Permalink
feat: add debug ctl to rebalance agent by traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
roryye authored and SongZhen0704 committed Jul 5, 2024
1 parent e4122ad commit d69396d
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 54 deletions.
66 changes: 66 additions & 0 deletions cli/ctl/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type RebalanceType string

const RebalanceTypeNull RebalanceType = "null"

var isDebug, isAggIP bool

func RegisterAgentCommand() *cobra.Command {
agent := &cobra.Command{
Use: "agent",
Expand Down Expand Up @@ -94,6 +96,7 @@ func RegisterAgentCommand() *cobra.Command {
}

var typeStr string

rebalanceCmd := &cobra.Command{
Use: "rebalance",
Short: "rebalance controller or analyzer",
Expand All @@ -117,6 +120,8 @@ deepflow-ctl agent rebalance --type=analyzer`,
},
}
rebalanceCmd.Flags().StringVarP(&typeStr, "type", "t", "", "request type controller/analyzer")
rebalanceCmd.Flags().BoolVarP(&isDebug, "debug", "d", false, "enable debug output")
rebalanceCmd.Flags().BoolVarP(&isAggIP, "agg-ip", "", false, "aggregation based on analyzer ip, type only support analzyer")

agent.AddCommand(list)
agent.AddCommand(delete)
Expand Down Expand Up @@ -478,6 +483,9 @@ func upgadeAgent(cmd *cobra.Command, args []string) {
}

func rebalance(cmd *cobra.Command, rebalanceType RebalanceType, typeVal string) error {
if isDebug {
return rebalanceDebug(cmd, typeVal)
}
isBalance, err := ifNeedRebalance(cmd, typeVal)
if err != nil {
return err
Expand All @@ -501,6 +509,64 @@ func rebalance(cmd *cobra.Command, rebalanceType RebalanceType, typeVal string)
return nil
}

func rebalanceDebug(cmd *cobra.Command, typeVal string) error {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/rebalance-vtap/?type=%s&is_debug=true", server.IP, server.Port, typeVal)
resp, err := common.CURLPerform("POST", url, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
return err
}
data := resp.Get("DATA")

if data.Get("TRAFFIC_AZ") == nil ||
(data.Get("TRAFFIC_AZ") != nil && len(data.Get("TRAFFIC_AZ").MustArray()) == 0) {
return nil
}
fmt.Println()

t := table.New()
t.SetHeader([]string{"REGION", "AZ", "ANALYZER_IP", "ANALYZER_STATE", "AZ_TRAFFIC", "AGENT_COUNT", "ANALYZER_TRAFFIC"})
tableItems := [][]string{}
for i := range data.Get("TRAFFIC_AZ").MustArray() {
d := data.Get("TRAFFIC_AZ").GetIndex(i)
tableItems = append(tableItems, []string{
d.Get("REGION").MustString(),
d.Get("AZ").MustString(),
d.Get("ANALYZER_IP").MustString(),
strconv.Itoa(int(d.Get("ANALYZER_STATE").MustInt64())),
strconv.Itoa(int(d.Get("AZ_TRAFFIC").MustInt64())),
strconv.Itoa(int(d.Get("AGENT_COUNT").MustInt())),
strconv.Itoa(int(d.Get("ANALYZER_TRAFFIC").MustInt64())),
})
}
t.AppendBulk(tableItems)
t.Render()

if isAggIP {
fmt.Println()

t1 := table.New()
t1.SetHeader([]string{"REGION", "AZ", "ANALYZER_IP", "ANALYZER_STATE", "AGENT_COUNT", "ANALYZER_TRAFFIC"})
tableItems = [][]string{}
for i := range data.Get("TRAFFIC_ANALYZER").MustArray() {
d := data.Get("TRAFFIC_ANALYZER").GetIndex(i)
tableItems = append(tableItems, []string{
d.Get("REGION").MustString(),
d.Get("AZ").MustString(),
d.Get("ANALYZER_IP").MustString(),
strconv.Itoa(int(d.Get("ANALYZER_STATE").MustInt64())),
strconv.Itoa(int(d.Get("AGENT_COUNT").MustInt())),
strconv.Itoa(int(d.Get("ANALYZER_TRAFFIC").MustInt64())),
})
}
t1.AppendBulk(tableItems)
t1.Render()
}

return nil
}

func ifNeedRebalance(cmd *cobra.Command, typeStr string) (bool, error) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/rebalance-vtap/?check=false&type=%s", server.IP, server.Port, typeStr)
Expand Down
12 changes: 9 additions & 3 deletions server/controller/http/router/common/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ package common
import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/gin-gonic/gin"
"github.com/op/go-logging"
)

var log = logging.MustGetLogger("common/controller")

func ForwardMasterController(c *gin.Context, masterControllerName string, port int) {
requestHosts := strings.Split(c.Request.Host, ":")
if len(requestHosts) > 1 {
c.Request.Host = strings.Replace(
c.Request.Host, requestHosts[0], masterControllerName, 1,
)
if requestHosts[1] != strconv.Itoa(port) {
c.Request.Host = fmt.Sprintf("%s:%d", masterControllerName, port)
} else {
c.Request.Host = strings.Replace(c.Request.Host, requestHosts[0], masterControllerName, 1)
}
} else {
c.Request.Host = fmt.Sprintf("%s:%d", masterControllerName, port)
}
Expand Down
3 changes: 3 additions & 0 deletions server/controller/http/router/vtap.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ func rebalanceVtap(cfg *config.ControllerConfig) gin.HandlerFunc {
if value, ok := c.GetQuery("check"); ok {
args["check"] = (strings.ToLower(value) == "true")
}
if isDebug, ok := c.GetQuery("is_debug"); ok {
args["is_debug"] = (strings.ToLower(isDebug) == "true")
}
if value, ok := c.GetQuery("type"); ok {
args["type"] = value
if args["type"] != "controller" && args["type"] != "analyzer" {
Expand Down
23 changes: 18 additions & 5 deletions server/controller/http/service/rebalance/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type DB interface {
}

type DBInfo struct {
Regions []mysql.Region
AZs []mysql.AZ
Analyzers []mysql.Analyzer
AZAnalyzerConns []mysql.AZAnalyzerConnection
Expand All @@ -38,12 +39,21 @@ type DBInfo struct {
}

type AnalyzerInfo struct {
onlyWeight bool
dbInfo *DBInfo
regionToVTapNameToTraffic map[string]map[string]int64
onlyWeight bool

db DB
query Querier
dbInfo *DBInfo
db DB
query Querier

RebalanceData
}

type RebalanceData struct {
RegionToVTapNameToTraffic map[string]map[string]int64 `json:"RegionToVTapNameToTraffic"`
RegionToAZLcuuids map[string][]string `json:"RegionToAZLcuuids"`
AZToRegion map[string]string `json:"AZToRegion"`
AZToVTaps map[string][]*mysql.VTap `json:"AZToVTaps"`
AZToAnalyzers map[string][]*mysql.Analyzer `json:"AZToAnalyzers"`
}

func NewAnalyzerInfo(onlyWeight bool) *AnalyzerInfo {
Expand All @@ -57,6 +67,9 @@ func NewAnalyzerInfo(onlyWeight bool) *AnalyzerInfo {
}

func (r *DBInfo) Get(db *mysql.DB) error {
if err := db.Find(&r.Regions).Error; err != nil {
return err
}
if err := db.Find(&r.AZs).Error; err != nil {
return err
}
Expand Down
Loading

0 comments on commit d69396d

Please sign in to comment.