Skip to content

Commit

Permalink
kwil-admin: fix setup testnet with admin address
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed May 20, 2024
1 parent 6822b3f commit 36347c8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
56 changes: 28 additions & 28 deletions cmd/kwil-admin/nodecfg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,11 @@ func persistentPeersString(genCfg *TestnetGenerateConfig, privKeys []cmtEd.PrivK

// applyUniqueAddresses applies unique addresses to the config.
// it will begin at the default port and increment by 1 for each node.
// it does not apply to the admin address.
// it does not apply to the admin address. This does NOT change the
// admin service address.
func addressSpecificConfig(c *config.KwildConfig) error {

jsonrpcAddr, err := incrementPort(c.AppCfg.JSONRPCListenAddress, 1)
jsonrpcAddr, err := incrementPort(c.AppCfg.JSONRPCListenAddress, -1) // decrement to avoid collision with admin rpc at 8485
if err != nil {
return err
}
Expand All @@ -476,42 +477,43 @@ func addressSpecificConfig(c *config.KwildConfig) error {
return nil
}

// uniqueAdminAddress applies a unique address to the config.
// uniqueAdminAddress applies a unique address to the config. This only works
// for host:port or unix socket paths, not URLs.
func uniqueAdminAddress(cfg *config.KwildConfig) error {
s := cfg.AppCfg.AdminListenAddress
addr := cfg.AppCfg.AdminListenAddress

res, err := url.Parse(s)
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
if strings.Contains(err.Error(), "missing port in address") {
host = addr // this may be a unix path, checked below
port = "8485" // for sequential addresses, use 8485 to make collision with 8484 unlikely
} else if strings.Contains(err.Error(), "too many colons in address") {
u, err := url.Parse(addr)
if err != nil {
return fmt.Errorf("unknown admin service address: %w", err)
}
host, port = u.Hostname(), u.Port()
} else {
return fmt.Errorf("unknown admin service address: %w", err)
}
}

if res.Scheme != "unix" {
s, err = incrementPort(s, 1)
if isUNIX := strings.HasPrefix(host, "/"); !isUNIX {
addr = net.JoinHostPort(host, port)
addr, err = incrementPort(addr, 1)
if err != nil {
return err
}

cfg.AppCfg.AdminListenAddress = s
cfg.AppCfg.AdminListenAddress = addr
return nil
}

// if scheme is unix, it will use Host if it is path/to/sock, but Path if it is /path/to/sock
// if unix:///socket, it will set the value to be unix:///socket_0, then unix:///socket_1, etc.
var path string
var set *string
if res.Host != "" {
path = res.Host
set = &res.Host
} else {
path = res.Path
set = &res.Path
}

extension := filepath.Ext(path)
fileWithoutExt := strings.TrimSuffix(path, extension)
extension := filepath.Ext(host)
fileWithoutExt := strings.TrimSuffix(host, extension)

// see if the file already has a number appended to it
numerToUse := 0
numberToUse := 0

nums := strings.Split(fileWithoutExt, "_")
if len(nums) > 1 {
Expand All @@ -520,13 +522,11 @@ func uniqueAdminAddress(cfg *config.KwildConfig) error {
num, err := strconv.Atoi(last)
if err == nil {
fileWithoutExt = strings.TrimSuffix(fileWithoutExt, "_"+last)
numerToUse = num + 1
numberToUse = num + 1
}
}

*set = fileWithoutExt + "_" + strconv.Itoa(numerToUse) + extension

cfg.AppCfg.AdminListenAddress = res.String()
cfg.AppCfg.AdminListenAddress = fileWithoutExt + "_" + strconv.Itoa(numberToUse) + extension

return nil
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/kwil-admin/nodecfg/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ func Test_GenerateTestnetConfig(t *testing.T) {
P2pPort: 26656,
}

err := GenerateTestnetConfig(&genCfg, nil)
err := GenerateTestnetConfig(&genCfg, &ConfigOpts{
UniquePorts: true,
})
if err != nil {
t.Fatal(err)
}

os.RemoveAll(genCfg.OutputDir)

// not unique ports
err = GenerateTestnetConfig(&genCfg, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kwild/config/test_data/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
jsonrpc_listen_addr = "192.168.1.1:8484"

# Unix socket or TCP address for the KWILD App's Admin GRPC server to listen on
admin_listen_addr="127.0.0.1:8485"
admin_listen_addr="127.0.0.1:8585"

# TCP address for the KWILD App's HTTP server to listen on
http_listen_addr = "localhost:8080"
Expand Down
5 changes: 4 additions & 1 deletion cmd/kwild/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
grpc "github.com/kwilteam/kwil-db/internal/services/grpc_server"
rpcserver "github.com/kwilteam/kwil-db/internal/services/jsonrpc"
"github.com/kwilteam/kwil-db/internal/sql/pg"
"github.com/kwilteam/kwil-db/internal/version"

// internalize
"go.uber.org/zap"
Expand Down Expand Up @@ -115,18 +116,20 @@ func New(ctx context.Context, cfg *config.KwildConfig, genesisCfg *config.Genesi

func (s *Server) Start(ctx context.Context) error {
defer func() {
s.log.Info("Closing server resources...")
err := s.closers.closeAll()
if err != nil {
s.log.Error("failed to close resource:", zap.Error(err))
}
s.log.Info("Server is now shut down.")
}()
defer func() {
if err := recover(); err != nil {
s.log.Error("kwild server panic", zap.Any("error", err))
}
}()

s.log.Info("starting server...")
s.log.Infof("Starting server (kwild version %v)...", version.KwilVersion)

cancelCtx, done := context.WithCancel(ctx)
s.cancelCtxFunc = done
Expand Down
8 changes: 4 additions & 4 deletions core/adminclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func Test_prepareHTTPDialerURL(t *testing.T) {
},
{
"just ip:port",
"127.0.0.1:8485",
"http://127.0.0.1:8485",
"127.0.0.1:6789",
"http://127.0.0.1:6789",
false,
false,
},
{
"https ip:port",
"https://127.0.0.1:8485",
"https://127.0.0.1:8485",
"https://127.0.0.1:6789",
"https://127.0.0.1:6789",
false,
false,
},
Expand Down

0 comments on commit 36347c8

Please sign in to comment.