This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
topology.go
135 lines (109 loc) · 2.81 KB
/
topology.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
package main
import "math/rand"
type Rule struct {
From string
To string
}
func fullMesh(hosts []string) []Rule {
rules := []Rule{}
for _, from := range hosts {
for _, to := range hosts {
if from == to {
continue
}
rules = append(rules, Rule{from, to})
}
}
return rules
}
// Topology is a helper to simulate weird netsplits.
type Topology struct {
Hosts []string
}
func (t *Topology) randomHosts() []string {
result := append([]string{}, t.Hosts...)
for i := range result {
j := rand.Intn(i + 1)
result[i], result[j] = result[j], result[i]
}
return result
}
func (t *Topology) Default() []Rule {
return fullMesh(t.Hosts)
}
func (t *Topology) NetSplit() []Rule {
hosts := t.randomHosts()
majorityN := len(hosts)/2 + 1
majority := hosts[0:majorityN]
minority := hosts[majorityN:]
result := []Rule{}
result = append(result, fullMesh(majority)...)
result = append(result, fullMesh(minority)...)
return result
}
func (t *Topology) Bridge() []Rule {
hosts := t.randomHosts()
majorityN := len(hosts)/2 + 1
majority := hosts[0:majorityN]
minority := hosts[majorityN:]
result := []Rule{}
result = append(result, fullMesh(majority)...)
result = append(result, fullMesh(minority)...)
// Now take one host from majority and make it see all minority hosts (bidirectional)
bridge := majority[0]
for _, host := range minority {
result = append(result, Rule{bridge, host})
result = append(result, Rule{host, bridge})
}
return result
}
// SingleBridge returns rules
func (t *Topology) SingleBridge() []Rule {
hosts := t.randomHosts()
majorityN := len(hosts)/2 + 1
majority := hosts[0:majorityN]
minority := hosts[majorityN:]
result := []Rule{}
result = append(result, fullMesh(majority)...)
result = append(result, fullMesh(minority)...)
// Now take one host from majority and make it see one minority host (bidirectional)
leftNode := majority[0]
rightNode := minority[0]
result = append(result, Rule{leftNode, rightNode})
result = append(result, Rule{rightNode, leftNode})
return result
}
func (t *Topology) Ring() []Rule {
// a ring only makes sense with at least 4 hosts.
if len(t.Hosts) < 4 {
return t.Default()
}
hosts := t.randomHosts()
result := []Rule{}
for i, from := range hosts {
var sibling string
if i+1 == len(hosts) {
sibling = hosts[0]
} else {
sibling = hosts[i+1]
}
result = append(result, Rule{from, sibling})
result = append(result, Rule{sibling, from})
}
return result
}
func (t *Topology) Invert(rules []Rule) []Rule {
all := t.Default()
newAll := []Rule{}
allRules:
for _, allRule := range all {
for _, rule := range rules {
// If the allRule also exists in our "blacklist", we ignore it
if allRule.From == rule.From && allRule.To == rule.To {
continue allRules
}
}
newAll = append(newAll, allRule)
}
return newAll
}