mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 04:54:18 +02:00
tune cluster ls: parallized and use tcp connect instead of https (#63)
This commit is contained in:
@@ -17,10 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package cfg
|
package cfg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -185,42 +185,24 @@ func (conf *Config) SwitchCluster(name string) error {
|
|||||||
|
|
||||||
// We do NOT use go-elasticsearch to check for cluster reachability,
|
// We do NOT use go-elasticsearch to check for cluster reachability,
|
||||||
// because at this stage, auth may not have been configured. So
|
// because at this stage, auth may not have been configured. So
|
||||||
// instead we just connect to the cluster using plan net/http, ignore
|
// instead we just connect to the cluster using plan net/tcp
|
||||||
// HTTP response status and return true if we could just reach ith
|
|
||||||
func (cluster *Cluster) IsReachable() (bool, error) {
|
func (cluster *Cluster) IsReachable() (bool, error) {
|
||||||
ctx, cancel := context.WithTimeout(
|
timeout := 500 * time.Millisecond
|
||||||
context.Background(),
|
|
||||||
time.Duration(500)*time.Millisecond)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(
|
url := strings.TrimPrefix(strings.TrimPrefix(cluster.Uri, "https://"), "http://")
|
||||||
ctx,
|
|
||||||
"GET",
|
|
||||||
cluster.Uri,
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
host := strings.Split(url, "/")
|
||||||
|
|
||||||
|
if !strings.Contains(host[0], ":") {
|
||||||
|
host[0] += ":443"
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.DialTimeout("tcp", host[0], timeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{Transport: &http.Transport{
|
return true, conn.Close()
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
}}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp != nil {
|
|
||||||
// at this stage we do not care if the elasticsearch cluster
|
|
||||||
// accepts our request or if it's misconfigured in some way
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conf *Config) SetupES() error {
|
func (conf *Config) SetupES() error {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = `v0.0.24`
|
Version string = `v0.0.25`
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -137,6 +137,18 @@ func (conf *Config) Init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := conf.determineDefaultCluster(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
conf.HaveJQ = isJQinstalled()
|
||||||
|
|
||||||
|
conf.PrintDebug()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conf *Config) determineDefaultCluster() error {
|
||||||
if conf.CurrentCluster != "" {
|
if conf.CurrentCluster != "" {
|
||||||
// -C specified, set current cluster explicitly, no matter what the config says
|
// -C specified, set current cluster explicitly, no matter what the config says
|
||||||
current, exists := conf.Clusters[conf.CurrentCluster]
|
current, exists := conf.Clusters[conf.CurrentCluster]
|
||||||
@@ -172,10 +184,6 @@ func (conf *Config) Init() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.HaveJQ = isJQinstalled()
|
|
||||||
|
|
||||||
conf.PrintDebug()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,44 +31,62 @@ import (
|
|||||||
|
|
||||||
type ClusterIndices map[string]map[string]*types.IndicesRecord
|
type ClusterIndices map[string]map[string]*types.IndicesRecord
|
||||||
|
|
||||||
func ClusterList(conf *cfg.Config) error {
|
type clusterReachable struct {
|
||||||
table := printer.NewTable(conf, 5, len(conf.Clusters))
|
reachable bool
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClusterList(conf *cfg.Config) error {
|
||||||
|
var mu sync.Mutex
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
reachable := make(map[string]clusterReachable, len(conf.Clusters))
|
||||||
|
|
||||||
|
// check endpoints in parallel to speed things up
|
||||||
|
for name, cluster := range conf.Clusters {
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
online, err := cluster.IsReachable()
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
reachable[name] = clusterReachable{reachable: online, err: err}
|
||||||
|
mu.Unlock()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
table := printer.NewTable(conf, 5, len(conf.Clusters))
|
||||||
table.Addheaders("cluster", "uri", "reachable", "current", "error")
|
table.Addheaders("cluster", "uri", "reachable", "current", "error")
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
|
|
||||||
for name, cluster := range conf.Clusters {
|
for name, cluster := range conf.Clusters {
|
||||||
reachable := "no"
|
reachableStr := "no"
|
||||||
current := "no"
|
current := "no"
|
||||||
errmsg := ""
|
errmsg := ""
|
||||||
|
|
||||||
online, err := cluster.IsReachable()
|
if reachable[name].reachable {
|
||||||
|
reachableStr = printer.Colorize(conf, "green", "reachable")
|
||||||
if online {
|
|
||||||
reachable = printer.Colorize(conf, "green", "reachable")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cluster.Default {
|
if cluster.Default {
|
||||||
current = printer.Colorize(conf, "green", "yes")
|
current = printer.Colorize(conf, "green", "yes")
|
||||||
|
|
||||||
if !online {
|
if !reachable[name].reachable {
|
||||||
reachable = printer.Colorize(conf, "red", "no")
|
reachableStr = printer.Colorize(conf, "red", "no")
|
||||||
errmsg = err.Error()
|
errmsg = reachable[name].err.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Entries[idx] = []string{name, cluster.Uri, reachable, current, errmsg}
|
table.Entries[idx] = []string{name, cluster.Uri, reachableStr, current, errmsg}
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Sort()
|
table.Sort()
|
||||||
|
|
||||||
if err := table.Print(); err != nil {
|
return table.Print()
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We're using goroutines here to parallelize API requests, since we
|
// We're using goroutines here to parallelize API requests, since we
|
||||||
|
|||||||
Reference in New Issue
Block a user