From 108fe1a0b252299490cd12db4df0f15e9e23655d Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Mon, 11 Mar 2024 16:01:47 +0800 Subject: [PATCH 1/2] fix: avoid pushing invalid addr args Signed-off-by: Ruihang Xia --- pkg/components/frontend.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/pkg/components/frontend.go b/pkg/components/frontend.go index f719c882..2b8a6d74 100644 --- a/pkg/components/frontend.go +++ b/pkg/components/frontend.go @@ -101,13 +101,14 @@ func (f *frontend) BuildArgs(params ...interface{}) []string { fmt.Sprintf("--log-level=%s", logLevel), f.Name(), "start", fmt.Sprintf("--metasrv-addr=%s", f.metaSrvAddr), - fmt.Sprintf("--http-addr=%s", generateAddrArg(f.config.HTTPAddr, nodeId)), - fmt.Sprintf("--rpc-addr=%s", generateAddrArg(f.config.GRPCAddr, nodeId)), - fmt.Sprintf("--mysql-addr=%s", generateAddrArg(f.config.MysqlAddr, nodeId)), - fmt.Sprintf("--postgres-addr=%s", generateAddrArg(f.config.PostgresAddr, nodeId)), - fmt.Sprintf("--opentsdb-addr=%s", generateAddrArg(f.config.OpentsdbAddr, nodeId)), } + args = generateAddrArg("--http-addr", f.config.HTTPAddr, nodeId, args) + args = generateAddrArg("--rpc-addr", f.config.GRPCAddr, nodeId, args) + args = generateAddrArg("--mysql-addr", f.config.MysqlAddr, nodeId, args) + args = generateAddrArg("--postgres-addr", f.config.PostgresAddr, nodeId, args) + args = generateAddrArg("--opentsdb-addr", f.config.OpentsdbAddr, nodeId, args) + if len(f.config.Config) > 0 { args = append(args, fmt.Sprintf("-c=%s", f.config.Config)) } @@ -119,7 +120,7 @@ func (f *frontend) BuildArgs(params ...interface{}) []string { func (f *frontend) IsRunning(_ context.Context) bool { for i := 0; i < f.config.Replicas; i++ { - addr := generateAddrArg(f.config.HTTPAddr, i) + addr := formatAddrArg(f.config.HTTPAddr, i) healthy := fmt.Sprintf("http://%s/health", addr) resp, err := http.Get(healthy) @@ -141,10 +142,29 @@ func (f *frontend) IsRunning(_ context.Context) bool { return true } -func generateAddrArg(addr string, nodeId int) string { +// formatAddrArg formats the given addr and nodeId to a valid socket string. +// This function will return an empty string when the given addr is empty. +func formatAddrArg(addr string, nodeId int) string { + // return empty result if the address is not specified + if len(addr) == 0 { + return addr + } + // The "addr" is validated when set. host, port, _ := net.SplitHostPort(addr) portInt, _ := strconv.Atoi(port) return net.JoinHostPort(host, strconv.Itoa(portInt+nodeId)) } + +// generateAddrArg pushes arg into args array, return the new args array. +func generateAddrArg(config string, addr string, nodeId int, args []string) []string { + socketAddr := formatAddrArg(addr, nodeId) + + // don't generate param if the socket address is empty + if len(socketAddr) == 0 { + return args + } + + return append(args, fmt.Sprintf("%s=%s", config, socketAddr)) +} From cc591fa6c0e5e8abbb6a5977ebe6dfb8f5810cb8 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 12 Mar 2024 11:13:18 +0800 Subject: [PATCH 2/2] refactor metasrv and datanode to use new formatter fn Signed-off-by: Ruihang Xia --- pkg/components/datanode.go | 14 +++-------- pkg/components/frontend.go | 41 +++++-------------------------- pkg/components/metasrv.go | 13 +++------- pkg/components/utils.go | 50 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 pkg/components/utils.go diff --git a/pkg/components/datanode.go b/pkg/components/datanode.go index 783c944c..50d42fe1 100644 --- a/pkg/components/datanode.go +++ b/pkg/components/datanode.go @@ -22,7 +22,6 @@ import ( "net" "net/http" "path" - "strconv" "sync" "time" @@ -138,10 +137,10 @@ func (d *datanode) BuildArgs(params ...interface{}) []string { d.Name(), "start", fmt.Sprintf("--node-id=%d", nodeID), fmt.Sprintf("--metasrv-addr=%s", d.metaSrvAddr), - fmt.Sprintf("--rpc-addr=%s", generateDatanodeAddr(d.config.RPCAddr, nodeID)), - fmt.Sprintf("--http-addr=%s", generateDatanodeAddr(d.config.HTTPAddr, nodeID)), fmt.Sprintf("--data-home=%s", homeDir), } + args = GenerateAddrArg("--http-addr", d.config.HTTPAddr, nodeID, args) + args = GenerateAddrArg("--rpc-addr", d.config.RPCAddr, nodeID, args) if len(d.config.Config) > 0 { args = append(args, fmt.Sprintf("-c=%s", d.config.Config)) @@ -152,7 +151,7 @@ func (d *datanode) BuildArgs(params ...interface{}) []string { func (d *datanode) IsRunning(_ context.Context) bool { for i := 0; i < d.config.Replicas; i++ { - addr := generateDatanodeAddr(d.config.HTTPAddr, i) + addr := FormatAddrArg(d.config.HTTPAddr, i) _, httpPort, err := net.SplitHostPort(addr) if err != nil { d.logger.V(5).Infof("failed to split host port in %s: %s", d.Name(), err) @@ -176,10 +175,3 @@ func (d *datanode) IsRunning(_ context.Context) bool { return true } - -func generateDatanodeAddr(addr string, nodeID int) string { - // Already checked in validation. - host, port, _ := net.SplitHostPort(addr) - portInt, _ := strconv.Atoi(port) - return net.JoinHostPort(host, strconv.Itoa(portInt+nodeID)) -} diff --git a/pkg/components/frontend.go b/pkg/components/frontend.go index 2b8a6d74..fe72a4ea 100644 --- a/pkg/components/frontend.go +++ b/pkg/components/frontend.go @@ -19,10 +19,8 @@ package components import ( "context" "fmt" - "net" "net/http" "path" - "strconv" "sync" greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" @@ -103,11 +101,11 @@ func (f *frontend) BuildArgs(params ...interface{}) []string { fmt.Sprintf("--metasrv-addr=%s", f.metaSrvAddr), } - args = generateAddrArg("--http-addr", f.config.HTTPAddr, nodeId, args) - args = generateAddrArg("--rpc-addr", f.config.GRPCAddr, nodeId, args) - args = generateAddrArg("--mysql-addr", f.config.MysqlAddr, nodeId, args) - args = generateAddrArg("--postgres-addr", f.config.PostgresAddr, nodeId, args) - args = generateAddrArg("--opentsdb-addr", f.config.OpentsdbAddr, nodeId, args) + args = GenerateAddrArg("--http-addr", f.config.HTTPAddr, nodeId, args) + args = GenerateAddrArg("--rpc-addr", f.config.GRPCAddr, nodeId, args) + args = GenerateAddrArg("--mysql-addr", f.config.MysqlAddr, nodeId, args) + args = GenerateAddrArg("--postgres-addr", f.config.PostgresAddr, nodeId, args) + args = GenerateAddrArg("--opentsdb-addr", f.config.OpentsdbAddr, nodeId, args) if len(f.config.Config) > 0 { args = append(args, fmt.Sprintf("-c=%s", f.config.Config)) @@ -120,7 +118,7 @@ func (f *frontend) BuildArgs(params ...interface{}) []string { func (f *frontend) IsRunning(_ context.Context) bool { for i := 0; i < f.config.Replicas; i++ { - addr := formatAddrArg(f.config.HTTPAddr, i) + addr := FormatAddrArg(f.config.HTTPAddr, i) healthy := fmt.Sprintf("http://%s/health", addr) resp, err := http.Get(healthy) @@ -141,30 +139,3 @@ func (f *frontend) IsRunning(_ context.Context) bool { } return true } - -// formatAddrArg formats the given addr and nodeId to a valid socket string. -// This function will return an empty string when the given addr is empty. -func formatAddrArg(addr string, nodeId int) string { - // return empty result if the address is not specified - if len(addr) == 0 { - return addr - } - - // The "addr" is validated when set. - host, port, _ := net.SplitHostPort(addr) - portInt, _ := strconv.Atoi(port) - - return net.JoinHostPort(host, strconv.Itoa(portInt+nodeId)) -} - -// generateAddrArg pushes arg into args array, return the new args array. -func generateAddrArg(config string, addr string, nodeId int, args []string) []string { - socketAddr := formatAddrArg(addr, nodeId) - - // don't generate param if the socket address is empty - if len(socketAddr) == 0 { - return args - } - - return append(args, fmt.Sprintf("%s=%s", config, socketAddr)) -} diff --git a/pkg/components/metasrv.go b/pkg/components/metasrv.go index f98f9acc..f99f6fed 100644 --- a/pkg/components/metasrv.go +++ b/pkg/components/metasrv.go @@ -22,7 +22,6 @@ import ( "net" "net/http" "path" - "strconv" "sync" "time" @@ -123,9 +122,9 @@ func (m *metaSrv) BuildArgs(params ...interface{}) []string { m.Name(), "start", fmt.Sprintf("--store-addr=%s", m.config.StoreAddr), fmt.Sprintf("--server-addr=%s", m.config.ServerAddr), - fmt.Sprintf("--http-addr=%s", generateMetaSrvAddr(m.config.HTTPAddr, nodeID)), - fmt.Sprintf("--bind-addr=%s", generateMetaSrvAddr(bindAddr, nodeID)), } + args = GenerateAddrArg("--http-addr", m.config.HTTPAddr, nodeID, args) + args = GenerateAddrArg("--bind-addr", bindAddr, nodeID, args) if len(m.config.Config) > 0 { args = append(args, fmt.Sprintf("-c=%s", m.config.Config)) @@ -136,7 +135,7 @@ func (m *metaSrv) BuildArgs(params ...interface{}) []string { func (m *metaSrv) IsRunning(_ context.Context) bool { for i := 0; i < m.config.Replicas; i++ { - addr := generateMetaSrvAddr(m.config.HTTPAddr, i) + addr := FormatAddrArg(m.config.HTTPAddr, i) _, httpPort, err := net.SplitHostPort(addr) if err != nil { m.logger.V(5).Infof("failed to split host port in %s: %s", m.Name(), err) @@ -160,9 +159,3 @@ func (m *metaSrv) IsRunning(_ context.Context) bool { return true } - -func generateMetaSrvAddr(addr string, nodeID int) string { - host, port, _ := net.SplitHostPort(addr) - portInt, _ := strconv.Atoi(port) - return net.JoinHostPort(host, strconv.Itoa(portInt+nodeID)) -} diff --git a/pkg/components/utils.go b/pkg/components/utils.go new file mode 100644 index 00000000..7e632ec1 --- /dev/null +++ b/pkg/components/utils.go @@ -0,0 +1,50 @@ +/* + * Copyright 2023 Greptime Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package components + +import ( + "fmt" + "net" + "strconv" +) + +// FormatAddrArg formats the given addr and nodeId to a valid socket string. +// This function will return an empty string when the given addr is empty. +func FormatAddrArg(addr string, nodeId int) string { + // return empty result if the address is not specified + if len(addr) == 0 { + return addr + } + + // The "addr" is validated when set. + host, port, _ := net.SplitHostPort(addr) + portInt, _ := strconv.Atoi(port) + + return net.JoinHostPort(host, strconv.Itoa(portInt+nodeId)) +} + +// GenerateAddrArg pushes arg into args array, return the new args array. +func GenerateAddrArg(config string, addr string, nodeId int, args []string) []string { + socketAddr := FormatAddrArg(addr, nodeId) + + // don't generate param if the socket address is empty + if len(socketAddr) == 0 { + return args + } + + return append(args, fmt.Sprintf("%s=%s", config, socketAddr)) +}