Skip to content

Commit

Permalink
Allow using a remote MistUtilLoad for load balancing (#1329)
Browse files Browse the repository at this point in the history
We want to allow running catalyst-api separately to catalyst, so MistUtilLoad needs to be accessed remotely.
  • Loading branch information
leszko authored Jul 19, 2024
1 parent d42faeb commit 616c219
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
30 changes: 21 additions & 9 deletions balancer/mist/mist_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,42 @@ var mistUtilLoadSingleRequestTimeout = 15 * time.Second
var mistUtilLoadLoopTimeout = 2 * time.Minute

type MistBalancer struct {
isLocal bool
config *balancer.Config
cmd *exec.Cmd
endpoint string
// Blocks until initial startup
startupOnce sync.Once
startupError error
}

// create a new load balancer instance
func NewBalancer(config *balancer.Config) balancer.Balancer {
// NewLocalBalancer creates a new local MistUtilLoad instance
func NewLocalBalancer(config *balancer.Config) balancer.Balancer {
_, err := exec.LookPath("MistUtilLoad")
if err != nil {
glog.Warning("MistUtilLoad not found, not doing meaningful balancing")
return &balancer.BalancerStub{}
}
return &MistBalancer{
isLocal: true,
config: config,
cmd: nil,
endpoint: fmt.Sprintf("http://127.0.0.1:%d", config.MistUtilLoadPort),
}
}

// NewRemoteBalancer creates a new remote MistUtilLoad instance
func NewRemoteBalancer(config *balancer.Config) balancer.Balancer {
return &MistBalancer{
config: config,
endpoint: fmt.Sprintf("http://%s:%d", config.MistHost, config.MistUtilLoadPort),
}
}

// start this load balancer instance, execing MistUtilLoad if necessary
func (b *MistBalancer) Start(ctx context.Context) error {
if !b.isLocal {
// remote load balancer instance is not managed by catalyst-api
return nil
}
b.killPreviousBalancer(ctx)

go func() {
Expand Down Expand Up @@ -288,17 +300,17 @@ func (b *MistBalancer) isBalancerRunning(ctx context.Context) bool {
func (b *MistBalancer) execBalancer(ctx context.Context, balancerArgs []string) error {
args := append(balancerArgs, "-p", fmt.Sprintf("%d", b.config.MistUtilLoadPort), "-g", "4")
glog.Infof("Running MistUtilLoad with %v", args)
b.cmd = exec.CommandContext(ctx, "MistUtilLoad", args...)
cmd := exec.CommandContext(ctx, "MistUtilLoad", args...)

b.cmd.Stdout = os.Stdout
b.cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := b.cmd.Start()
err := cmd.Start()
if err != nil {
return err
}

err = b.cmd.Wait()
err = cmd.Wait()
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion balancer/mist/mist_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func start(t *testing.T) (*MistBalancer, *mockMistUtilLoad) {
OwnRegion: "fra",
OwnRegionTagAdjust: 1000,
},
cmd: nil,
endpoint: mul.Server.URL,
}
// Mock startup loop
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func main() {
c := cluster.NewCluster(&cli)

// Start balancer
mistBalancer := mist_balancer.NewBalancer(&balancer.Config{
mistBalancerConfig := &balancer.Config{
Args: cli.BalancerArgs,
MistUtilLoadPort: uint32(cli.MistLoadBalancerPort),
MistLoadBalancerTemplate: cli.MistLoadBalancerTemplate,
Expand All @@ -284,7 +284,8 @@ func main() {
NodeName: cli.NodeName,
OwnRegion: cli.OwnRegion,
OwnRegionTagAdjust: cli.OwnRegionTagAdjust,
})
}
mistBalancer := mist_balancer.NewLocalBalancer(mistBalancerConfig)

bal := mistBalancer
if balancer.CombinedBalancerEnabled(cli.CataBalancer) {
Expand Down

0 comments on commit 616c219

Please sign in to comment.