Skip to content

Commit 42eaaba

Browse files
fix: correctly config multiple address for frontend (#186)
1 parent 5718739 commit 42eaaba

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

pkg/components/frontend.go

+40-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ package components
1717
import (
1818
"context"
1919
"fmt"
20+
"net"
21+
"net/http"
2022
"path"
23+
"strconv"
2124
"sync"
2225

2326
greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
@@ -74,7 +77,7 @@ func (f *frontend) Start(ctx context.Context, stop context.CancelFunc, binary st
7477
Name: dirName,
7578
logDir: frontendLogDir,
7679
pidDir: frontendPidDir,
77-
args: f.BuildArgs(),
80+
args: f.BuildArgs(i),
7881
}
7982
if err := runBinary(ctx, stop, option, f.wg, f.logger); err != nil {
8083
return err
@@ -84,16 +87,23 @@ func (f *frontend) Start(ctx context.Context, stop context.CancelFunc, binary st
8487
return nil
8588
}
8689

87-
func (f *frontend) BuildArgs(_ ...interface{}) []string {
90+
func (f *frontend) BuildArgs(params ...interface{}) []string {
8891
logLevel := f.config.LogLevel
8992
if logLevel == "" {
9093
logLevel = DefaultLogLevel
9194
}
9295

96+
nodeId := params[0].(int)
97+
9398
args := []string{
9499
fmt.Sprintf("--log-level=%s", logLevel),
95100
f.Name(), "start",
96101
fmt.Sprintf("--metasrv-addr=%s", f.metaSrvAddr),
102+
fmt.Sprintf("--http-addr=%s", generateAddrArg(f.config.HTTPAddr, nodeId)),
103+
fmt.Sprintf("--rpc-addr=%s", generateAddrArg(f.config.GRPCAddr, nodeId)),
104+
fmt.Sprintf("--mysql-addr=%s", generateAddrArg(f.config.MysqlAddr, nodeId)),
105+
fmt.Sprintf("--postgres-addr=%s", generateAddrArg(f.config.PostgresAddr, nodeId)),
106+
fmt.Sprintf("--opentsdb-addr=%s", generateAddrArg(f.config.OpentsdbAddr, nodeId)),
97107
}
98108

99109
if len(f.config.Config) > 0 {
@@ -104,6 +114,32 @@ func (f *frontend) BuildArgs(_ ...interface{}) []string {
104114
}
105115

106116
func (f *frontend) IsRunning(_ context.Context) bool {
107-
// Have not implemented the healthy checker now.
108-
return false
117+
for i := 0; i < f.config.Replicas; i++ {
118+
addr := generateAddrArg(f.config.HTTPAddr, i)
119+
healthy := fmt.Sprintf("http://%s/health", addr)
120+
121+
resp, err := http.Get(healthy)
122+
if err != nil {
123+
f.logger.V(5).Infof("Failed to get %s healthy: %s", f.Name(), err)
124+
return false
125+
}
126+
127+
if resp.StatusCode != http.StatusOK {
128+
f.logger.V(5).Infof("%s is not healthy: %s", f.Name(), resp)
129+
return false
130+
}
131+
132+
if err = resp.Body.Close(); err != nil {
133+
f.logger.V(5).Infof("%s is not healthy: %s, err: %s", f.Name(), resp, err)
134+
return false
135+
}
136+
}
137+
return true
138+
}
139+
140+
func generateAddrArg(addr string, nodeId int) string {
141+
// The "addr" is validated when set.
142+
host, port, _ := net.SplitHostPort(addr)
143+
portInt, _ := strconv.Atoi(port)
144+
return net.JoinHostPort(host, strconv.Itoa(portInt+nodeId))
109145
}

pkg/config/baremetal.go

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type Frontend struct {
7373
HTTPAddr string `yaml:"httpAddr" validate:"omitempty,hostname_port"`
7474
PostgresAddr string `yaml:"postgresAddr" validate:"omitempty,hostname_port"`
7575
MetaAddr string `yaml:"metaAddr" validate:"omitempty,hostname_port"`
76+
MysqlAddr string `yaml:"mysqlAddr" validate:"omitempty,hostname_port"`
77+
OpentsdbAddr string `yaml:"opentsdbAddr" validate:"omitempty,hostname_port"`
7678

7779
Replicas int `yaml:"replicas" validate:"gt=0"`
7880
Config string `yaml:"config" validate:"omitempty,filepath"`

0 commit comments

Comments
 (0)