-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
323 lines (288 loc) · 10 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
/*
Package simulation provides tools meant to visualize or test aspects
of a Cockroach cluster on a single host.
Gossip
Gossip creates a gossip network of up to 250 nodes and outputs
successive visualization of the gossip network graph via dot.
Uses tcp sockets for connecting 3, 10, 25, 50, 100 or 250
nodes. Generates .dot graph output files for each cycle of the
simulation.
To run:
go install github.com/cockroachdb/cockroach/cmd/gossipsim
gossipsim -size=(small|medium|large|huge|ginormous)
Log output includes instructions for displaying the graph output as a
series of images to visualize the evolution of the network.
Running the large through ginormous simulations will require the open
files limit be increased either for the shell running the simulation,
or system wide. For Linux:
# For the current shell:
ulimit -n 65536
# System-wide:
sysctl fs.file-max
fs.file-max = 50384
For MacOS:
# To view current limits (soft / hard):
launchctl limit maxfiles
# To edit, add/edit the following line in /etc/launchd.conf and
# restart for the new file limit to take effect.
#
# limit maxfiles 16384 32768
sudo vi /etc/launchd.conf
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"strconv"
"strings"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/gossip/simulation"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
)
const (
// minDotFontSize is the minimum font size for scaling node sizes
// proportional to the number of incoming connections.
minDotFontSize = 12
// maxDotFontSize is the maximum font size for scaling node sizes.
maxDotFontSize = 24
)
var (
size = flag.String("size", "medium", "size of network (tiny|small|medium|large|huge|ginormous)")
)
// edge is a helper struct which describes an edge in the dot output graph.
type edge struct {
dest roachpb.NodeID // Node ID of destination
added bool // True if edge was recently added
deleted bool // True if edge was recently deleted
}
// edgeMap is a map from node address to a list of edges. A helper
// method is provided to simplify adding edges.
type edgeMap map[roachpb.NodeID][]edge
// addEdge creates a list of edges if one doesn't yet exist for the
// specified node ID.
func (em edgeMap) addEdge(nodeID roachpb.NodeID, e edge) {
if _, ok := em[nodeID]; !ok {
em[nodeID] = make([]edge, 0, 1)
}
em[nodeID] = append(em[nodeID], e)
}
// outputDotFile generates a .dot file describing the current state of
// the gossip network. nodes is a map from network address to gossip
// node. edgeSet is empty on the first invocation, but
// its content is set to encompass the entire set of edges in the
// network when this method returns. It should be resupplied with each
// successive invocation, as it is used to determine which edges are
// new and which have been deleted and show those changes visually in
// the output graph. New edges are drawn green; edges which were
// removed over the course of the last simulation step(s) are drawn in
// a lightly-dashed red.
//
// The format of the output looks like this:
//
// digraph G {
// node [shape=record];
// node1 [fontsize=12,label="{Node 1|MH=3}"]
// node1 -> node3 [color=green]
// node1 -> node4
// node1 -> node5 [color=red,style=dotted]
// node2 [fontsize=24,label="{Node 2|MH=2}"]
// node2 -> node5
// node3 [fontsize=18,label="{Node 3|MH=5}"]
// node3 -> node5
// node3 -> node4
// node4 [fontsize=24,label="{Node 4|MH=4}"]
// node4 -> node2
// node5 [fontsize=24,label="{Node 5|MH=1}"]
// node5 -> node2
// node5 -> node3
// }
//
// Returns the name of the output file and a boolean for whether or not
// the network has quiesced (that is, no new edges, and all nodes are
// connected).
func outputDotFile(
dotFN string, cycle int, network *simulation.Network, edgeSet map[string]edge,
) (string, bool) {
f, err := os.Create(dotFN)
if err != nil {
log.Fatalf(context.TODO(), "unable to create temp file: %s", err)
}
defer f.Close()
// Determine maximum number of incoming connections. Create outgoing
// edges, keeping track of which are new since last time (added=true).
outgoingMap := make(edgeMap)
var maxIncoming int
quiescent := true
// The order the graph file is written influences the arrangement
// of nodes in the output image, so it makes sense to eliminate
// randomness here. Unfortunately with graphviz it's fairly hard
// to get a consistent ordering.
for _, simNode := range network.Nodes {
node := simNode.Gossip
incoming := node.Incoming()
for _, iNode := range incoming {
e := edge{dest: node.NodeID.Get()}
key := fmt.Sprintf("%d:%d", iNode, node.NodeID.Get())
if _, ok := edgeSet[key]; !ok {
e.added = true
quiescent = false
}
delete(edgeSet, key)
outgoingMap.addEdge(iNode, e)
}
if len(incoming) > maxIncoming {
maxIncoming = len(incoming)
}
}
// Find all edges which were deleted.
for key, e := range edgeSet {
e.added = false
e.deleted = true
quiescent = false
nodeID, err := strconv.Atoi(strings.Split(key, ":")[0])
if err != nil {
log.Fatal(context.TODO(), err)
}
outgoingMap.addEdge(roachpb.NodeID(nodeID), e)
delete(edgeSet, key)
}
fmt.Fprintln(f, "digraph G {")
fmt.Fprintln(f, "node [shape=record];")
for _, simNode := range network.Nodes {
node := simNode.Gossip
var missing []roachpb.NodeID
var totalAge int64
for _, otherNode := range network.Nodes {
if otherNode == simNode {
continue // skip the node's own info
}
infoKey := otherNode.Addr().String()
// GetInfo returns an error if the info is missing.
if info, err := node.GetInfo(infoKey); err != nil {
missing = append(missing, otherNode.Gossip.NodeID.Get())
quiescent = false
} else {
_, val, err := encoding.DecodeUint64Ascending(info)
if err != nil {
log.Fatalf(context.TODO(), "bad decode of node info cycle: %s", err)
}
totalAge += int64(cycle) - int64(val)
}
}
log.Infof(context.TODO(), "node %d: missing infos for nodes %s", node.NodeID.Get(), missing)
var sentinelAge int64
// GetInfo returns an error if the info is missing.
if info, err := node.GetInfo(gossip.KeySentinel); err != nil {
log.Infof(context.TODO(), "error getting info for sentinel gossip key %q: %s", gossip.KeySentinel, err)
} else {
_, val, err := encoding.DecodeUint64Ascending(info)
if err != nil {
log.Fatalf(context.TODO(), "bad decode of sentinel cycle: %s", err)
}
sentinelAge = int64(cycle) - int64(val)
}
var age, nodeColor string
if len(missing) > 0 {
nodeColor = "color=red,"
age = fmt.Sprintf("missing %d", len(missing))
} else {
age = strconv.FormatFloat(float64(totalAge)/float64(len(network.Nodes)-1-len(missing)), 'f', 4, 64)
}
fontSize := minDotFontSize
if maxIncoming > 0 {
fontSize = minDotFontSize + int(math.Floor(float64(len(node.Incoming())*
(maxDotFontSize-minDotFontSize))/float64(maxIncoming)))
}
fmt.Fprintf(f, "\t%s [%sfontsize=%d,label=\"{%s|AA=%s, MH=%d, SA=%d}\"]\n",
node.NodeID.Get(), nodeColor, fontSize, node.NodeID.Get(), age, node.MaxHops(), sentinelAge)
outgoing := outgoingMap[node.NodeID.Get()]
for _, e := range outgoing {
destSimNode, ok := network.GetNodeFromID(e.dest)
if !ok {
continue
}
dest := destSimNode.Gossip
style := ""
if e.added {
style = " [color=green]"
} else if e.deleted {
style = " [color=red,style=dotted]"
}
fmt.Fprintf(f, "\t%s -> %s%s\n", node.NodeID.Get(), dest.NodeID.Get(), style)
if !e.deleted {
edgeSet[fmt.Sprintf("%d:%d", node.NodeID.Get(), e.dest)] = e
}
}
}
fmt.Fprintln(f, "}")
return f.Name(), quiescent
}
func main() {
// Seed the random number generator for non-determinism across
// multiple runs.
randutil.SeedForTests()
if f := flag.Lookup("logtostderr"); f != nil {
fmt.Println("Starting simulation. Add -logtostderr to see progress.")
}
flag.Parse()
dirName, err := ioutil.TempDir("", "gossip-simulation-")
if err != nil {
log.Fatalf(context.TODO(), "could not create temporary directory for gossip simulation output: %s", err)
}
// Simulation callbacks to run the simulation for cycleCount
// cycles. At each cycle % outputEvery, a dot file showing the
// state of the network graph is output.
nodeCount := 3
switch *size {
case "tiny":
// Use default parameters.
case "small":
nodeCount = 10
case "medium":
nodeCount = 25
case "large":
nodeCount = 50
case "huge":
nodeCount = 100
case "ginormous":
nodeCount = 250
default:
log.Fatalf(context.TODO(), "unknown simulation size: %s", *size)
}
edgeSet := make(map[string]edge)
stopper := stop.NewStopper()
defer stopper.Stop(context.TODO())
n := simulation.NewNetwork(stopper, nodeCount, true)
n.SimulateNetwork(
func(cycle int, network *simulation.Network) bool {
// Output dot graph.
dotFN := fmt.Sprintf("%s/sim-cycle-%03d.dot", dirName, cycle)
_, quiescent := outputDotFile(dotFN, cycle, network, edgeSet)
// Run until network has quiesced.
return !quiescent
},
)
// Output instructions for viewing graphs.
fmt.Printf("To view simulation graph output run (you must install graphviz):\n\nfor f in %s/*.dot ; do circo $f -Tpng -o $f.png ; echo $f.png ; done\n", dirName)
}