|
1 | 1 | package byzzbench.simulator;
|
2 | 2 |
|
| 3 | +import byzzbench.simulator.faults.Fault; |
| 4 | +import byzzbench.simulator.transport.Event; |
| 5 | +import byzzbench.simulator.transport.MutateMessageEventPayload; |
| 6 | +import byzzbench.simulator.transport.TimeoutEvent; |
| 7 | +import byzzbench.simulator.transport.TransportObserver; |
3 | 8 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
4 |
| -import lombok.RequiredArgsConstructor; |
5 | 9 |
|
6 | 10 | import java.io.Serializable;
|
| 11 | +import java.time.Duration; |
7 | 12 | import java.time.Instant;
|
8 |
| -import java.util.concurrent.atomic.AtomicLong; |
| 13 | +import java.util.SortedMap; |
| 14 | +import java.util.TreeMap; |
9 | 15 |
|
10 | 16 | /**
|
11 |
| - * A timekeeper that provides timestamps to the replicas in the simulator. |
| 17 | + * A timekeeper that provides timestamps to the nodes in the simulator. |
12 | 18 | */
|
13 |
| -@RequiredArgsConstructor |
14 |
| -public class Timekeeper implements Serializable { |
| 19 | +public class Timekeeper implements Serializable, TransportObserver { |
| 20 | + /** |
| 21 | + * The increment to use each time a timestamp is requested. |
| 22 | + */ |
| 23 | + public static final Duration INCREMENT = Duration.ofMillis(1); |
| 24 | + |
15 | 25 | @JsonIgnore
|
16 |
| - private final transient Scenario scenario; |
17 |
| - private final AtomicLong counter = new AtomicLong(0); |
| 26 | + private final Scenario scenario; |
| 27 | + |
| 28 | + /** |
| 29 | + * The current time for each node |
| 30 | + */ |
| 31 | + private final SortedMap<String, Instant> times = new TreeMap<>(); |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a new timekeeper for the given scenario. |
| 35 | + * |
| 36 | + * @param scenario the scenario to create the timekeeper for |
| 37 | + */ |
| 38 | + public Timekeeper(Scenario scenario) { |
| 39 | + this.scenario = scenario; |
| 40 | + this.scenario.getTransport().addObserver(this); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Advance the time for the given node and return the new time. |
| 45 | + * |
| 46 | + * @param node the node to advance the time for |
| 47 | + * @return the new time for the node |
| 48 | + */ |
| 49 | + public Instant incrementAndGetTime(Node node) { |
| 50 | + Instant current = getTime(node); |
| 51 | + Instant next = current.plus(INCREMENT); |
| 52 | + times.put(node.getId(), next); |
| 53 | + return next; |
| 54 | + } |
18 | 55 |
|
| 56 | + /** |
| 57 | + * Get the current time for the given node. |
| 58 | + * |
| 59 | + * @param node the node to get the time for |
| 60 | + * @return the current time for the node |
| 61 | + */ |
19 | 62 | public Instant getTime(Node node) {
|
20 |
| - return Instant.ofEpochMilli(counter.incrementAndGet()); |
| 63 | + return times.computeIfAbsent(node.getId(), k -> Instant.ofEpochMilli(0)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onEventAdded(Event event) { |
| 68 | + // nothing to do |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onEventDropped(Event event) { |
| 73 | + // nothing to do |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onEventDelivered(Event event) { |
| 78 | + // check if it was a timeout |
| 79 | + if (!(event instanceof TimeoutEvent timeoutEvent)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // set counter to max of its current value and the expiration time |
| 84 | + String nodeId = timeoutEvent.getNodeId(); |
| 85 | + Instant expiration = timeoutEvent.getExpiresAt(); |
| 86 | + Instant current = times.getOrDefault(nodeId, Instant.ofEpochMilli(0)); |
| 87 | + times.put(nodeId, current.isAfter(expiration) ? current : expiration); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onMessageMutation(MutateMessageEventPayload payload) { |
| 92 | + // nothing to do |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onFault(Fault fault) { |
| 97 | + // nothing to do |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void onTimeout(TimeoutEvent event) { |
| 102 | + // nothing to do |
21 | 103 | }
|
22 | 104 | }
|
0 commit comments