Skip to content

Commit

Permalink
refactor metasrv and datanode to use new formatter fn
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia committed Mar 12, 2024
1 parent 108fe1a commit cc591fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 56 deletions.
14 changes: 3 additions & 11 deletions pkg/components/datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net"
"net/http"
"path"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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))
}
41 changes: 6 additions & 35 deletions pkg/components/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package components
import (
"context"
"fmt"
"net"
"net/http"
"path"
"strconv"
"sync"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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))
}
13 changes: 3 additions & 10 deletions pkg/components/metasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net"
"net/http"
"path"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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))
}
50 changes: 50 additions & 0 deletions pkg/components/utils.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit cc591fa

Please sign in to comment.