Skip to content

Commit 645d297

Browse files
committed
ByzzFuzz network faults: drop messages instead of setting node partitions
1 parent 03dee8e commit 645d297

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package byzzbench.simulator.faults.behaviors;
2+
3+
import byzzbench.simulator.faults.FaultBehavior;
4+
import byzzbench.simulator.faults.FaultContext;
5+
import byzzbench.simulator.transport.Event;
6+
import byzzbench.simulator.transport.MessageEvent;
7+
import byzzbench.simulator.transport.Router;
8+
import lombok.extern.java.Log;
9+
10+
import java.util.Collection;
11+
import java.util.Optional;
12+
13+
@Log
14+
public class ByzzFuzzDropMessageBehavior implements FaultBehavior {
15+
/**
16+
* Router helper to determine if two nodes are connected
17+
*/
18+
Router router = new Router();
19+
20+
/**
21+
* Memoized name of the fault
22+
*/
23+
String name;
24+
25+
/**
26+
* Create a new CreateNetworkPartitionsBehavior
27+
*
28+
* @param partitions A list of partitions to create
29+
*/
30+
public ByzzFuzzDropMessageBehavior(String[][] partitions) {
31+
for (String[] partition : partitions) {
32+
router.isolateNodes(partition);
33+
}
34+
}
35+
36+
/**
37+
* Create a new CreateNetworkPartitionsBehavior
38+
*
39+
* @param partition A single partition to create
40+
*/
41+
public ByzzFuzzDropMessageBehavior(String[] partition) {
42+
this(new String[][]{partition});
43+
}
44+
45+
/**
46+
* Create a new CreateNetworkPartitionsBehavior
47+
*
48+
* @param partition A single partition to create
49+
*/
50+
public ByzzFuzzDropMessageBehavior(Collection<String> partition) {
51+
this(partition.toArray(new String[0]));
52+
}
53+
54+
/**
55+
* Create a new CreateNetworkPartitionsBehavior
56+
*
57+
* @param partition The ID of the node to isolate
58+
*/
59+
public ByzzFuzzDropMessageBehavior(String partition) {
60+
this(new String[]{partition});
61+
}
62+
63+
@Override
64+
public String getId() {
65+
return "createnetworkpartitions-%s".formatted(this.router.getReversePartitionsMapping());
66+
}
67+
68+
@Override
69+
public String getName() {
70+
if (this.name == null) {
71+
// convert the array of arrays into a string
72+
String partitions = this.router.getReversePartitionsMapping().stream()
73+
.map(p -> "[" + String.join(",", p) + "]")
74+
.reduce("", (a, b) -> a + b);
75+
this.name = "Drop message based on partitions %s".formatted(partitions);
76+
}
77+
return this.name;
78+
}
79+
80+
@Override
81+
public void accept(FaultContext context) {
82+
Optional<Event> event = context.getEvent();
83+
84+
if (event.isEmpty()) {
85+
log.warning("No event to mutate");
86+
return;
87+
}
88+
89+
Event e = event.get();
90+
91+
if (!(e instanceof MessageEvent messageEvent)) {
92+
log.warning("Event is not a message event");
93+
return;
94+
}
95+
96+
String sender = messageEvent.getSenderId();
97+
String recipient = messageEvent.getRecipientId();
98+
99+
// if the sender and recipient are in the same partition, do nothing
100+
if (router.haveConnectivity(sender, recipient)) {
101+
return;
102+
}
103+
104+
// otherwise, drop the message: the sender and recipient are in different partitions
105+
context.getScenario().getTransport().dropEvent(e.getEventId());
106+
}
107+
}

simulator/src/main/java/byzzbench/simulator/faults/faults/ByzzFuzzNetworkFault.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package byzzbench.simulator.faults.faults;
22

33
import byzzbench.simulator.faults.BaseFault;
4-
import byzzbench.simulator.faults.behaviors.CreateNetworkPartitionsBehavior;
4+
import byzzbench.simulator.faults.behaviors.ByzzFuzzDropMessageBehavior;
55
import byzzbench.simulator.faults.predicates.MessageRoundPredicate;
66

77
import java.util.Set;
@@ -23,7 +23,7 @@ public ByzzFuzzNetworkFault(Set<String> partition, int round) {
2323
super(
2424
"byzzfuzznetworkfault-%d-%s".formatted(round, String.join("-", partition)),
2525
new MessageRoundPredicate(round),
26-
new CreateNetworkPartitionsBehavior(partition)
26+
new ByzzFuzzDropMessageBehavior(partition)
2727
);
2828
}
2929
}

simulator/src/main/java/byzzbench/simulator/transport/Transport.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ private synchronized void appendEvent(Event event) {
190190
// add the event to the map
191191
this.events.put(event.getEventId(), event);
192192

193+
// notify observers
194+
this.observers.forEach(o -> o.onEventAdded(event));
195+
193196
// apply automatic faults
194197
this.automaticFaults.values()
195198
.forEach(f -> f.testAndAccept(new FaultContext(this.scenario, event)));
196-
197-
// notify observers
198-
this.observers.forEach(o -> o.onEventAdded(event));
199199
}
200200

201201
/**

0 commit comments

Comments
 (0)