forked from hashicorp/serf
/
reachability.go
171 lines (149 loc) · 4.35 KB
/
reachability.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package command
import (
"flag"
"fmt"
"github.com/hashicorp/serf/client"
"github.com/hashicorp/serf/serf"
"github.com/mitchellh/cli"
"strings"
"time"
)
const (
tooManyAcks = `This could mean Serf is detecting false-failures due to a misconfiguration or network issue.`
tooFewAcks = `This could mean Serf gossip packets are being lost due to a misconfiguration or network issue.`
duplicateResponses = `Duplicate responses means there is a misconfiguration. Verify that node names are unique.`
troubleshooting = `
Troubleshooting tips:
* Ensure that the bind addr:port is accessible by all other nodes
* If an advertise address is set, ensure it routes to the bind address
* Check that no nodes are behind a NAT
* If nodes are behind firewalls or iptables, check that Serf traffic is permitted (UDP and TCP)
* Verify networking equipment is functional`
)
// ReachabilityCommand is a Command implementation that is used to trigger
// a new reachability test
type ReachabilityCommand struct {
ShutdownCh <-chan struct{}
Ui cli.Ui
}
func (c *ReachabilityCommand) Help() string {
helpText := `
Usage: serf reachability [options]
Tests the network reachability of this node
Options:
-rpc-addr=127.0.0.1:7373 RPC address of the Serf agent.
-rpc-auth="" RPC auth token of the Serf agent.
-verbose Verbose mode
`
return strings.TrimSpace(helpText)
}
func (c *ReachabilityCommand) Run(args []string) int {
var verbose bool
cmdFlags := flag.NewFlagSet("reachability", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
cmdFlags.BoolVar(&verbose, "verbose", false, "verbose mode")
rpcAddr := RPCAddrFlag(cmdFlags)
rpcAuth := RPCAuthFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
return 1
}
cl, err := RPCClient(*rpcAddr, *rpcAuth)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Serf agent: %s", err))
return 1
}
defer cl.Close()
ackCh := make(chan string, 128)
// Get the list of members
members, err := cl.Members()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting members: %s", err))
return 1
}
// Get only the live members
liveMembers := make(map[string]struct{})
for _, m := range members {
if m.Status == "alive" {
liveMembers[m.Name] = struct{}{}
}
}
c.Ui.Output(fmt.Sprintf("Total members: %d, live members: %d", len(members), len(liveMembers)))
// Start the query
params := client.QueryParam{
RequestAck: true,
Name: serf.InternalQueryPrefix + "ping",
AckCh: ackCh,
}
if err := cl.Query(¶ms); err != nil {
c.Ui.Error(fmt.Sprintf("Error sending query: %s", err))
return 1
}
c.Ui.Output("Starting reachability test...")
start := time.Now()
last := time.Now()
// Track responses and acknowledgements
exit := 0
dups := false
numAcks := 0
acksFrom := make(map[string]struct{}, len(members))
OUTER:
for {
select {
case a := <-ackCh:
if a == "" {
break OUTER
}
if verbose {
c.Ui.Output(fmt.Sprintf("\tAck from '%s'", a))
}
numAcks++
if _, ok := acksFrom[a]; ok {
dups = true
c.Ui.Output(fmt.Sprintf("Duplicate response from '%v'", a))
}
acksFrom[a] = struct{}{}
last = time.Now()
case <-c.ShutdownCh:
c.Ui.Error("Test interrupted")
return 1
}
}
if verbose {
total := float64(time.Now().Sub(start)) / float64(time.Second)
timeToLast := float64(last.Sub(start)) / float64(time.Second)
c.Ui.Output(fmt.Sprintf("Query time: %0.2f sec, time to last response: %0.2f sec", total, timeToLast))
}
// Print troubleshooting info for duplicate responses
if dups {
c.Ui.Output(duplicateResponses)
exit = 1
}
n := len(liveMembers)
if numAcks == n {
c.Ui.Output("Successfully contacted all live nodes")
} else if numAcks > n {
c.Ui.Output("Received more acks than live nodes! Acks from non-live nodes:")
for m := range acksFrom {
if _, ok := liveMembers[m]; !ok {
c.Ui.Output(fmt.Sprintf("\t%s", m))
}
}
c.Ui.Output(tooManyAcks)
c.Ui.Output(troubleshooting)
return 1
} else if numAcks < n {
c.Ui.Output("Received less acks than live nodes! Missing acks from:")
for m := range liveMembers {
if _, ok := acksFrom[m]; !ok {
c.Ui.Output(fmt.Sprintf("\t%s", m))
}
}
c.Ui.Output(tooFewAcks)
c.Ui.Output(troubleshooting)
return 1
}
return exit
}
func (c *ReachabilityCommand) Synopsis() string {
return "Test network reachability"
}