mirror of https://github.com/status-im/consul.git
Defaults second node to agent if not given. Removes -short option and tweaks output formatting.
This commit is contained in:
parent
88845f7d71
commit
99cfbb8a47
|
@ -18,27 +18,27 @@ type RttCommand struct {
|
|||
|
||||
func (c *RttCommand) Help() string {
|
||||
helpText := `
|
||||
Usage: consul rtt [options] node1 node2
|
||||
Usage: consul rtt [options] node1 [node2]
|
||||
|
||||
Estimates the round trip time between two nodes using Consul's network
|
||||
coordinate model of the cluster.
|
||||
|
||||
At least one node name is required. If the second node name isn't given, it
|
||||
is set to the agent's node name. Note that these are node names as known to
|
||||
Consul as "consul members" would show, not IP addresses.
|
||||
|
||||
By default, the two nodes are assumed to be nodes in the local datacenter
|
||||
and the LAN coordinates are used. If the -wan option is given, then the WAN
|
||||
coordinates are used, and the node names must be prefixed by the datacenter
|
||||
and a period (eg. "dc1.sever").
|
||||
coordinates are used, and the node names must be suffixed by a period and
|
||||
the datacenter (eg. "myserver.dc1").
|
||||
|
||||
It is not possible to measure between LAN coordinates and WAN coordinates
|
||||
because they are maintained by independent Serf gossip pools, so they are
|
||||
not compatible.
|
||||
|
||||
The two node names are required. Note that these are node names as known to
|
||||
Consul as "consul members" would show, not IP addresses.
|
||||
|
||||
Options:
|
||||
|
||||
-wan Use WAN coordinates instead of LAN coordinates.
|
||||
-short Print just the round trip time (eg. "1.234 ms").
|
||||
-http-addr=127.0.0.1:8500 HTTP address of the Consul agent.
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
|
@ -46,13 +46,11 @@ Options:
|
|||
|
||||
func (c *RttCommand) Run(args []string) int {
|
||||
var wan bool
|
||||
var short bool
|
||||
|
||||
cmdFlags := flag.NewFlagSet("rtt", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
||||
|
||||
cmdFlags.BoolVar(&wan, "wan", false, "wan")
|
||||
cmdFlags.BoolVar(&short, "short", false, "short")
|
||||
httpAddr := HTTPAddrFlag(cmdFlags)
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
|
@ -60,8 +58,8 @@ func (c *RttCommand) Run(args []string) int {
|
|||
|
||||
// They must provide a pair of nodes.
|
||||
nodes := cmdFlags.Args()
|
||||
if len(nodes) != 2 {
|
||||
c.Ui.Error("Two node names must be specified")
|
||||
if len(nodes) < 1 || len(nodes) > 2 {
|
||||
c.Ui.Error("One or two node names must be specified")
|
||||
c.Ui.Error("")
|
||||
c.Ui.Error(c.Help())
|
||||
return 1
|
||||
|
@ -82,6 +80,19 @@ func (c *RttCommand) Run(args []string) int {
|
|||
if wan {
|
||||
source = "WAN"
|
||||
|
||||
// Default the second node to the agent if none was given.
|
||||
if len(nodes) < 2 {
|
||||
agent := client.Agent()
|
||||
self, err := agent.Self()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Unable to look up agent info: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
node, dc := self["Config"]["NodeName"], self["Config"]["Datacenter"]
|
||||
nodes = append(nodes, fmt.Sprintf("%s.%s", node, dc))
|
||||
}
|
||||
|
||||
// Parse the input nodes.
|
||||
parts1 := strings.Split(nodes[0], ".")
|
||||
parts2 := strings.Split(nodes[1], ".")
|
||||
|
@ -89,8 +100,8 @@ func (c *RttCommand) Run(args []string) int {
|
|||
c.Ui.Error("Node names must be specified as <datacenter>.<node name> with -wan")
|
||||
return 1
|
||||
}
|
||||
dc1, node1 := parts1[0], parts1[1]
|
||||
dc2, node2 := parts2[0], parts2[1]
|
||||
node1, dc1 := parts1[0], parts1[1]
|
||||
node2, dc2 := parts2[0], parts2[1]
|
||||
|
||||
// Pull all the WAN coordinates.
|
||||
dcs, err := coordClient.Datacenters()
|
||||
|
@ -117,6 +128,17 @@ func (c *RttCommand) Run(args []string) int {
|
|||
} else {
|
||||
source = "LAN"
|
||||
|
||||
// Default the second node to the agent if none was given.
|
||||
if len(nodes) < 2 {
|
||||
agent := client.Agent()
|
||||
node, err := agent.NodeName()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Unable to look up agent info: %s", err))
|
||||
return 1
|
||||
}
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
|
||||
// Pull all the LAN coordinates.
|
||||
entries, _, err := coordClient.Nodes(nil)
|
||||
if err != nil {
|
||||
|
@ -153,11 +175,7 @@ SHOW_RTT:
|
|||
|
||||
// Report the round trip time.
|
||||
dist := fmt.Sprintf("%.3f ms", coord1.DistanceTo(coord2).Seconds()*1000.0)
|
||||
if short {
|
||||
c.Ui.Output(dist)
|
||||
} else {
|
||||
c.Ui.Output(fmt.Sprintf("Estimated %s <-> %s rtt=%s (using %s coordinates)", nodes[0], nodes[1], dist, source))
|
||||
}
|
||||
c.Ui.Output(fmt.Sprintf("Estimated %s <-> %s rtt: %s (using %s coordinates)", nodes[0], nodes[1], dist, source))
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
@ -24,10 +24,6 @@ func TestRttCommand_Run_BadArgs(t *testing.T) {
|
|||
t.Fatalf("expected return code 1, got %d", code)
|
||||
}
|
||||
|
||||
if code := c.Run([]string{"node1"}); code != 1 {
|
||||
t.Fatalf("expected return code 1, got %d", code)
|
||||
}
|
||||
|
||||
if code := c.Run([]string{"node1", "node2", "node3"}); code != 1 {
|
||||
t.Fatalf("expected return code 1, got %d", code)
|
||||
}
|
||||
|
@ -36,11 +32,11 @@ func TestRttCommand_Run_BadArgs(t *testing.T) {
|
|||
t.Fatalf("expected return code 1, got %d", code)
|
||||
}
|
||||
|
||||
if code := c.Run([]string{"-wan", "dc1.node1", "node2"}); code != 1 {
|
||||
if code := c.Run([]string{"-wan", "node1.dc1", "node2"}); code != 1 {
|
||||
t.Fatalf("expected return code 1, got %d", code)
|
||||
}
|
||||
|
||||
if code := c.Run([]string{"-wan", "node1", "dc1.node2"}); code != 1 {
|
||||
if code := c.Run([]string{"-wan", "node1", "node2.dc1"}); code != 1 {
|
||||
t.Fatalf("expected return code 1, got %d", code)
|
||||
}
|
||||
}
|
||||
|
@ -96,20 +92,18 @@ func TestRttCommand_Run_LAN(t *testing.T) {
|
|||
}
|
||||
|
||||
// Make sure the proper RTT was reported in the output.
|
||||
expected := fmt.Sprintf("rtt=%s", dist_str)
|
||||
expected := fmt.Sprintf("rtt: %s", dist_str)
|
||||
if !strings.Contains(ui.OutputWriter.String(), expected) {
|
||||
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Try the short mode.
|
||||
// Default to the agent's node.
|
||||
{
|
||||
ui := new(cli.MockUi)
|
||||
c := &RttCommand{Ui: ui}
|
||||
args := []string{
|
||||
"-short",
|
||||
"-http-addr=" + a.httpAddr,
|
||||
a.config.NodeName,
|
||||
"dogs",
|
||||
}
|
||||
code := c.Run(args)
|
||||
|
@ -118,8 +112,8 @@ func TestRttCommand_Run_LAN(t *testing.T) {
|
|||
}
|
||||
|
||||
// Make sure the proper RTT was reported in the output.
|
||||
expected := fmt.Sprintf("%s\n", dist_str)
|
||||
if ui.OutputWriter.String() != expected {
|
||||
expected := fmt.Sprintf("rtt: %s", dist_str)
|
||||
if !strings.Contains(ui.OutputWriter.String(), expected) {
|
||||
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
@ -145,7 +139,7 @@ func TestRttCommand_Run_WAN(t *testing.T) {
|
|||
defer a.Shutdown()
|
||||
waitForLeader(t, a.httpAddr)
|
||||
|
||||
node := fmt.Sprintf("%s.%s", a.config.Datacenter, a.config.NodeName)
|
||||
node := fmt.Sprintf("%s.%s", a.config.NodeName, a.config.Datacenter)
|
||||
|
||||
// We can't easily inject WAN coordinates, so we will just query the
|
||||
// node with itself.
|
||||
|
@ -164,21 +158,19 @@ func TestRttCommand_Run_WAN(t *testing.T) {
|
|||
}
|
||||
|
||||
// Make sure there was some kind of RTT reported in the output.
|
||||
if !strings.Contains(ui.OutputWriter.String(), "rtt=") {
|
||||
if !strings.Contains(ui.OutputWriter.String(), "rtt: ") {
|
||||
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Try the short mode.
|
||||
// Default to the agent's node.
|
||||
{
|
||||
ui := new(cli.MockUi)
|
||||
c := &RttCommand{Ui: ui}
|
||||
args := []string{
|
||||
"-wan",
|
||||
"-short",
|
||||
"-http-addr=" + a.httpAddr,
|
||||
node,
|
||||
node,
|
||||
}
|
||||
code := c.Run(args)
|
||||
if code != 0 {
|
||||
|
@ -186,7 +178,7 @@ func TestRttCommand_Run_WAN(t *testing.T) {
|
|||
}
|
||||
|
||||
// Make sure there was some kind of RTT reported in the output.
|
||||
if !strings.Contains(ui.OutputWriter.String(), " ms\n") {
|
||||
if !strings.Contains(ui.OutputWriter.String(), "rtt: ") {
|
||||
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,19 +24,19 @@ not compatible.
|
|||
|
||||
## Usage
|
||||
|
||||
Usage: `consul rtt [options] node1 node2`
|
||||
Usage: `consul rtt [options] node1 [node2]`
|
||||
|
||||
The two node names are required. Note that these are node names as known to
|
||||
At least one node name is required. If the second node name isn't given, it
|
||||
is set to the agent's node name. Note that these are node names as known to
|
||||
Consul as `consul members` would show, not IP addresses.
|
||||
|
||||
The list of available flags are:
|
||||
|
||||
* `-wan` - Instructs the command to use WAN coordinates instead of LAN
|
||||
coordinates. If the -wan option is given, then the node names must be prefixed
|
||||
by the datacenter and a period (eg. "dc1.sever"). By default, the two nodes are
|
||||
assumed to be nodes in the local datacenter the LAN coordinates are used.
|
||||
|
||||
* `-short` - Abbreviates the output to just the round trip time (eg. "1.234 ms").
|
||||
coordinates. By default, the two nodes are assumed to be nodes in the local
|
||||
datacenter and the LAN coordinates are used. If the -wan option is given,
|
||||
then the WAN coordinates are used, and the node names must be suffixed by a period
|
||||
and the datacenter (eg. "myserver.dc1").
|
||||
|
||||
* `-http-addr` - Address to the HTTP server of the agent you want to contact
|
||||
to send this command. If this isn't specified, the command will contact
|
||||
|
@ -49,11 +49,11 @@ time between the given nodes:
|
|||
|
||||
```
|
||||
$ consul rtt n1 n2
|
||||
Estimated n1 <-> n2 rtt=0.610 ms (using LAN coordinates)
|
||||
Estimated n1 <-> n2 rtt: 0.610 ms (using LAN coordinates)
|
||||
|
||||
$ consul rtt -short n1 n2
|
||||
0.610 ms
|
||||
$ consul rtt n2 # Running from n1
|
||||
Estimated n1 <-> n2 rtt: 0.610 ms (using LAN coordinates)
|
||||
|
||||
$ consul rtt -wan dc1.n1 dc2.n2
|
||||
Estimated dc1.n1 <-> dc2.n2 rtt=1.275 ms (using WAN coordinates)
|
||||
$ consul rtt -wan n1.dc1 n2.dc2
|
||||
Estimated n1.dc1 <-> n2.dc2 rtt: 1.275 ms (using WAN coordinates)
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue