From 88845f7d7161611a0db203543b7c409a3fda0f3a Mon Sep 17 00:00:00 2001 From: James Phillips Date: Fri, 16 Oct 2015 00:56:15 -0700 Subject: [PATCH] Cleans up after code review, adds a -short option to "consul rtt" command. --- command/rtt.go | 29 +++++-- command/rtt_test.go | 83 +++++++++++++++---- .../source/docs/commands/index.html.markdown | 2 +- .../source/docs/commands/rtt.html.markdown | 9 +- 4 files changed, 97 insertions(+), 26 deletions(-) diff --git a/command/rtt.go b/command/rtt.go index 731016a66b..fa9fbbd594 100644 --- a/command/rtt.go +++ b/command/rtt.go @@ -38,6 +38,7 @@ Usage: consul rtt [options] node1 node2 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) @@ -45,11 +46,13 @@ 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 @@ -77,6 +80,8 @@ func (c *RttCommand) Run(args []string) int { var source string var coord1, coord2 *coordinate.Coordinate if wan { + source = "WAN" + // Parse the input nodes. parts1 := strings.Split(nodes[0], ".") parts2 := strings.Split(nodes[1], ".") @@ -103,10 +108,15 @@ func (c *RttCommand) Run(args []string) int { if dc.Datacenter == dc2 && entry.Node == node2 { coord2 = entry.Coord } + + if coord1 != nil && coord2 != nil { + goto SHOW_RTT + } } } - source = "WAN" } else { + source = "LAN" + // Pull all the LAN coordinates. entries, _, err := coordClient.Nodes(nil) if err != nil { @@ -122,8 +132,11 @@ func (c *RttCommand) Run(args []string) int { if entry.Node == nodes[1] { coord2 = entry.Coord } + + if coord1 != nil && coord2 != nil { + goto SHOW_RTT + } } - source = "LAN" } // Make sure we found both coordinates. @@ -136,12 +149,18 @@ func (c *RttCommand) Run(args []string) int { return 1 } +SHOW_RTT: + // Report the round trip time. - dist := coord1.DistanceTo(coord2).Seconds() - c.Ui.Output(fmt.Sprintf("Estimated %s <-> %s rtt=%.3f ms (using %s coordinates)", nodes[0], nodes[1], dist*1000.0, source)) + 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)) + } return 0 } func (c *RttCommand) Synopsis() string { - return "Estimates round trip times between nodes" + return "Estimates network round trip time between nodes" } diff --git a/command/rtt_test.go b/command/rtt_test.go index 4722986792..77cccb68fc 100644 --- a/command/rtt_test.go +++ b/command/rtt_test.go @@ -57,6 +57,7 @@ func TestRttCommand_Run_LAN(t *testing.T) { c1 := coordinate.NewCoordinate(coordinate.DefaultConfig()) c2 := c1.Clone() c2.Vec[0] = 0.123 + dist_str := fmt.Sprintf("%.3f ms", c1.DistanceTo(c2).Seconds()*1000.0) req1 := structs.CoordinateUpdateRequest{ Datacenter: a.config.Datacenter, @@ -80,11 +81,10 @@ func TestRttCommand_Run_LAN(t *testing.T) { // Wait for the updates to get flushed to the data store. time.Sleep(2 * updatePeriod) - ui := new(cli.MockUi) - c := &RttCommand{Ui: ui} - // Try two known nodes. - func() { + { + ui := new(cli.MockUi) + c := &RttCommand{Ui: ui} args := []string{ "-http-addr=" + a.httpAddr, a.config.NodeName, @@ -96,14 +96,38 @@ func TestRttCommand_Run_LAN(t *testing.T) { } // Make sure the proper RTT was reported in the output. - dist_str := fmt.Sprintf("%.3f ms", c1.DistanceTo(c2).Seconds()*1000.0) - if !strings.Contains(ui.OutputWriter.String(), 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. + { + ui := new(cli.MockUi) + c := &RttCommand{Ui: ui} + args := []string{ + "-short", + "-http-addr=" + a.httpAddr, + a.config.NodeName, + "dogs", + } + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String()) + } + + // Make sure the proper RTT was reported in the output. + expected := fmt.Sprintf("%s\n", dist_str) + if ui.OutputWriter.String() != expected { + t.Fatalf("bad: %#v", ui.OutputWriter.String()) + } + } // Try an unknown node. - func() { + { + ui := new(cli.MockUi) + c := &RttCommand{Ui: ui} args := []string{ "-http-addr=" + a.httpAddr, a.config.NodeName, @@ -113,7 +137,7 @@ func TestRttCommand_Run_LAN(t *testing.T) { if code != 1 { t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String()) } - }() + } } func TestRttCommand_Run_WAN(t *testing.T) { @@ -121,17 +145,16 @@ func TestRttCommand_Run_WAN(t *testing.T) { defer a.Shutdown() waitForLeader(t, a.httpAddr) - ui := new(cli.MockUi) - c := &RttCommand{Ui: ui} - node := fmt.Sprintf("%s.%s", a.config.Datacenter, a.config.NodeName) // We can't easily inject WAN coordinates, so we will just query the // node with itself. - func() { + { + ui := new(cli.MockUi) + c := &RttCommand{Ui: ui} args := []string{ - "-http-addr=" + a.httpAddr, "-wan", + "-http-addr=" + a.httpAddr, node, node, } @@ -144,13 +167,37 @@ func TestRttCommand_Run_WAN(t *testing.T) { if !strings.Contains(ui.OutputWriter.String(), "rtt=") { t.Fatalf("bad: %#v", ui.OutputWriter.String()) } - }() + } + + // Try the short mode. + { + 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 { + t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String()) + } + + // Make sure there was some kind of RTT reported in the output. + if !strings.Contains(ui.OutputWriter.String(), " ms\n") { + t.Fatalf("bad: %#v", ui.OutputWriter.String()) + } + } // Try an unknown node. - func() { + { + ui := new(cli.MockUi) + c := &RttCommand{Ui: ui} args := []string{ - "-http-addr=" + a.httpAddr, "-wan", + "-http-addr=" + a.httpAddr, node, "dc1.nope", } @@ -158,5 +205,5 @@ func TestRttCommand_Run_WAN(t *testing.T) { if code != 1 { t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String()) } - }() + } } diff --git a/website/source/docs/commands/index.html.markdown b/website/source/docs/commands/index.html.markdown index d888d8996f..e466a94b44 100644 --- a/website/source/docs/commands/index.html.markdown +++ b/website/source/docs/commands/index.html.markdown @@ -39,7 +39,7 @@ Available commands are: members Lists the members of a Consul cluster monitor Stream logs from a Consul agent reload Triggers the agent to reload configuration files - rtt Estimates round trip times between nodes + rtt Estimates network round trip time between nodes version Prints the Consul version watch Watch for changes in Consul ``` diff --git a/website/source/docs/commands/rtt.html.markdown b/website/source/docs/commands/rtt.html.markdown index 3eebb704e5..261e8a8baa 100644 --- a/website/source/docs/commands/rtt.html.markdown +++ b/website/source/docs/commands/rtt.html.markdown @@ -3,7 +3,7 @@ layout: "docs" page_title: "Commands: RTT" sidebar_current: "docs-commands-rtt" description: > - The `rtt` command estimates the netowrk round trip time between two nodes using Consul's network coordinate model of the cluster. + The rtt command estimates the network round trip time between two nodes. --- # Consul RTT @@ -36,6 +36,8 @@ The list of available flags are: 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"). + * `-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 "127.0.0.1:8500" which is the default HTTP address of a Consul agent. @@ -43,12 +45,15 @@ The list of available flags are: ## Output If coordinates are available, the command will print the estimated round trip -time beteeen the given nodes: +time between the given nodes: ``` $ consul rtt n1 n2 Estimated n1 <-> n2 rtt=0.610 ms (using LAN coordinates) +$ consul rtt -short n1 n2 +0.610 ms + $ consul rtt -wan dc1.n1 dc2.n2 Estimated dc1.n1 <-> dc2.n2 rtt=1.275 ms (using WAN coordinates) ```