|
| 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 | +} |
0 commit comments