diff --git a/balancer/mist/mist_balancer.go b/balancer/mist/mist_balancer.go index 9ecbcf67..3951fe1d 100644 --- a/balancer/mist/mist_balancer.go +++ b/balancer/mist/mist_balancer.go @@ -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() { @@ -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 } diff --git a/balancer/mist/mist_balancer_test.go b/balancer/mist/mist_balancer_test.go index 3f285363..de79e6cf 100644 --- a/balancer/mist/mist_balancer_test.go +++ b/balancer/mist/mist_balancer_test.go @@ -31,7 +31,6 @@ func start(t *testing.T) (*MistBalancer, *mockMistUtilLoad) { OwnRegion: "fra", OwnRegionTagAdjust: 1000, }, - cmd: nil, endpoint: mul.Server.URL, } // Mock startup loop diff --git a/main.go b/main.go index 3fb96845..7a70b0b1 100644 --- a/main.go +++ b/main.go @@ -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, @@ -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) {