-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
182 lines (159 loc) · 4.1 KB
/
worker.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
package main
import (
"context"
"errors"
"path"
"strconv"
"time"
)
type FuzzerWorker struct {
sync *FuzzerSync
ID int
config FuzzerConfig
network *FuzzerInterceptNetwork
logger *Logger
networkAddr string
doneCh chan struct{}
ctx context.Context
}
func NewFuzzerWorker(id int, ctx context.Context, config FuzzerConfig, sync *FuzzerSync, logger *Logger) (*FuzzerWorker, error) {
networkAddr := "localhost:" + strconv.Itoa(config.BaseNetworkPort+id)
w := &FuzzerWorker{
ID: id,
config: config,
sync: sync,
ctx: ctx,
networkAddr: networkAddr,
logger: logger,
network: NewInterceptNetwork(ctx, networkAddr, logger.With(LogParams{"type": "network"})),
}
w.network.Start()
return w, nil
}
func (w *FuzzerWorker) Shutdown() {
w.network.Shutdown()
}
func (w *FuzzerWorker) Reset() {
w.doneCh = make(chan struct{})
w.network.Reset()
}
func (w *FuzzerWorker) getCluster() *Cluster {
cConfig := w.config.ClusterConfig.Copy()
cConfig.BaseInterceptPort = 2023 + w.ID*10
cConfig.BasePort = 5000 + w.ID*10
cConfig.InterceptListenAddr = w.networkAddr
cConfig.ID = w.ID
cConfig.WorkingDir = path.Join(w.config.BaseWorkingDir, strconv.Itoa(w.ID))
return NewCluster(cConfig, w.logger.With(LogParams{"type": "cluster"}))
}
func (w *FuzzerWorker) mimic(iter int, trace *Trace) {
iterLogger := w.logger.With(LogParams{"iter": iter})
iterLogger.Debug("Starting iteration")
start := time.Now()
defer w.network.Reset()
cluster := w.getCluster()
err := cluster.Start()
if err != nil {
iterLogger.With(LogParams{"error": err}).Error("failed to start cluster")
cluster.Destroy()
logs := cluster.GetLogs()
w.sync.Update(iter, nil, nil, logs, err)
return
}
ok := w.network.WaitForNodes(w.config.NumNodes)
iterLogger.Debug("all nodes connected")
if !ok {
w.sync.Update(iter, nil, nil, "", errors.New("could not connect to all nodes"))
}
startPoints := make(map[int]int)
crashPoints := make(map[int]int)
scheduleFromNode := make([]int, w.config.Horizon)
scheduleToNode := make([]int, w.config.Horizon)
scheduleMaxMessages := make([]int, w.config.Horizon)
clientRequests := make(map[int]string)
for _, ch := range trace.Choices {
switch ch.Type {
case "Node":
scheduleFromNode[ch.Step] = ch.From
scheduleToNode[ch.Step] = ch.To
scheduleMaxMessages[ch.Step] = ch.MaxMessages
case "Start":
startPoints[ch.Step] = ch.Node
case "Crash":
crashPoints[ch.Step] = ch.Node
case "ClientRequest":
clientRequests[ch.Step] = ch.Op
}
}
crashedNodes := make(map[int]bool)
for step := 0; step < w.config.Horizon; step++ {
startNode, ok := startPoints[step]
if ok {
_, crashed := crashedNodes[startNode]
if crashed {
node, ok := cluster.GetNode(startNode)
if ok {
node.Start()
w.network.AddEvent(Event{
Name: "Add",
Node: startNode,
Params: map[string]interface{}{
"i": startNode,
},
})
delete(crashedNodes, startNode)
}
}
}
crashNode, ok := crashPoints[step]
if ok {
crashedNodes[crashNode] = true
node, ok := cluster.GetNode(crashNode)
if ok {
node.Stop()
w.network.AddEvent(Event{
Name: "Remove",
Node: crashNode,
Params: map[string]interface{}{
"i": crashNode,
},
})
}
}
if _, ok := crashedNodes[scheduleToNode[step]]; !ok {
w.network.Schedule(scheduleFromNode[step], scheduleToNode[step], scheduleMaxMessages[step])
}
op, ok := clientRequests[step]
if ok {
if op == "read" {
cluster.ExecuteAsync("GET")
} else {
cluster.ExecuteAsync("INCR", "counter")
}
}
time.Sleep(30 * time.Millisecond)
}
eventTrace := w.network.GetEventTrace()
logs := ""
err = cluster.Destroy()
if err != nil {
logs = cluster.GetLogs()
}
duration := time.Since(start)
iterLogger.With(LogParams{"duration": duration.String()}).Info("Completed iteration")
w.sync.Update(iter, trace.Copy(), eventTrace, logs, err)
}
func (w *FuzzerWorker) Run() {
for {
select {
case <-w.ctx.Done():
return
default:
}
iter, mimic, ok := w.sync.GetTrace()
if !ok {
return
}
w.mimic(iter, mimic)
}
}