Skip to content

Commit 0a94052

Browse files
committed
Transport: use synchronized getters instead of direct references
1 parent f711533 commit 0a94052

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

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

+38-28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.*;
1717
import java.util.concurrent.atomic.AtomicLong;
1818
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
1920

2021
/**
2122
* Transport layer for the simulator.
@@ -74,6 +75,7 @@ public class Transport {
7475
* List of observers for the transport layer.
7576
*/
7677
@JsonIgnore
78+
@Getter(onMethod_ = {@Synchronized})
7779
private final List<TransportObserver> observers = new ArrayList<>();
7880
@Getter
7981
private boolean isGlobalStabilizationTime = false;
@@ -84,7 +86,7 @@ public class Transport {
8486
* @param observer The observer to add.
8587
*/
8688
public synchronized void addObserver(TransportObserver observer) {
87-
this.observers.add(observer);
89+
this.getObservers().add(observer);
8890
}
8991

9092
/**
@@ -93,7 +95,7 @@ public synchronized void addObserver(TransportObserver observer) {
9395
* @param observer The observer to remove.
9496
*/
9597
public synchronized void removeObserver(TransportObserver observer) {
96-
this.observers.remove(observer);
98+
this.getObservers().remove(observer);
9799
}
98100

99101
/**
@@ -177,7 +179,7 @@ public synchronized void sendMessage(Node sender, MessagePayload message,
177179
* @return A list of events in the given state
178180
*/
179181
public synchronized List<Event> getEventsInState(Event.Status status) {
180-
return this.events.values()
182+
return this.getEvents().values()
181183
.stream()
182184
.filter(m -> m.getStatus() == status)
183185
.toList();
@@ -190,14 +192,14 @@ public synchronized List<Event> getEventsInState(Event.Status status) {
190192
*/
191193
private synchronized void appendEvent(Event event) {
192194
// add the event to the map
193-
this.events.put(event.getEventId(), event);
195+
this.getEvents().put(event.getEventId(), event);
194196

195197
// apply automatic faults
196198
this.automaticFaults.values()
197199
.forEach(f -> f.testAndAccept(new FaultContext(this.scenario, event)));
198200

199201
// notify observers
200-
this.observers.forEach(o -> o.onEventAdded(event));
202+
this.getObservers().forEach(o -> o.onEventAdded(event));
201203
}
202204

203205
/**
@@ -221,13 +223,16 @@ public synchronized void multicast(Node sender, SortedSet<String> recipients,
221223

222224
// if they don't have connectivity, drop it directly
223225
if (!router.haveConnectivity(sender.getId(), recipient)) {
226+
System.out.println("Dropped: " + sender.getId() + "->" + recipient + ": " + payload);
227+
// print partition IDs for both nodes
228+
System.out.println("Partition IDs: " + router.getPartitions().get(sender.getId()) + " => " + router.getPartitions().get(recipient));
224229
this.dropEvent(messageId);
225230
}
226231
}
227232
}
228233

229234
public synchronized void deliverEvent(long eventId) throws Exception {
230-
Event e = events.get(eventId);
235+
Event e = this.getEvents().get(eventId);
231236

232237
// check if null
233238
if (e == null) {
@@ -251,7 +256,7 @@ public synchronized void deliverEvent(long eventId) throws Exception {
251256
e.setStatus(Event.Status.DELIVERED);
252257

253258
// For timeouts, this should be called before, so the Replica time is updated
254-
this.observers.forEach(o -> o.onEventDelivered(e));
259+
this.getObservers().forEach(o -> o.onEventDelivered(e));
255260

256261
switch (e) {
257262
case ClientRequestEvent c -> {
@@ -283,14 +288,14 @@ public synchronized void dropEvent(long eventId) {
283288
}
284289

285290
// check if event is a message
286-
Event e = events.get(eventId);
291+
Event e = this.getEvents().get(eventId);
287292

288293
if (e.getStatus() != Event.Status.QUEUED) {
289294
throw new IllegalArgumentException("Event not found or not in QUEUED state");
290295
}
291296

292297
e.setStatus(Event.Status.DROPPED);
293-
this.observers.forEach(o -> o.onEventDropped(e));
298+
this.getObservers().forEach(o -> o.onEventDropped(e));
294299
log.info("Dropped: " + e);
295300
}
296301

@@ -301,7 +306,7 @@ public synchronized void dropEvent(long eventId) {
301306
* @return The event with the given ID.
302307
*/
303308
public synchronized Event getEvent(long eventId) {
304-
return events.get(eventId);
309+
return this.getEvents().get(eventId);
305310
}
306311

307312
/**
@@ -311,7 +316,7 @@ public synchronized Event getEvent(long eventId) {
311316
* @param fault The fault to apply.
312317
*/
313318
public synchronized void applyMutation(long eventId, Fault fault) {
314-
Event e = events.get(eventId);
319+
Event e = this.getEvents().get(eventId);
315320

316321
// check if event does not exist
317322
if (e == null) {
@@ -360,7 +365,7 @@ public synchronized void applyMutation(long eventId, Fault fault) {
360365
// append the event to the schedule
361366
mutateMessageEvent.setStatus(Event.Status.DELIVERED);
362367
this.scenario.getSchedule().appendEvent(mutateMessageEvent);
363-
this.observers.forEach(o -> o.onMessageMutation(mutateMessageEvent.getPayload()));
368+
this.getObservers().forEach(o -> o.onMessageMutation(mutateMessageEvent.getPayload()));
364369

365370
log.info("Mutated: " + m);
366371
}
@@ -392,7 +397,7 @@ public synchronized void applyFault(Fault fault) {
392397
.build();
393398
faultEvent.setStatus(Event.Status.DELIVERED);
394399
this.scenario.getSchedule().appendEvent(faultEvent);
395-
this.observers.forEach(o -> o.onFault(fault));
400+
this.getObservers().forEach(o -> o.onFault(fault));
396401
}
397402

398403
/**
@@ -414,7 +419,7 @@ public synchronized long setTimeout(Node node, Runnable runnable,
414419
.task(runnable)
415420
.build();
416421
this.appendEvent(timeoutEvent);
417-
this.observers.forEach(o -> o.onTimeout(timeoutEvent));
422+
this.getObservers().forEach(o -> o.onTimeout(timeoutEvent));
418423
log.info("Timeout set for " + node.getId() + " in " + timeout + "ms: " + timeoutEvent);
419424
return timeoutEvent.getEventId();
420425
}
@@ -425,7 +430,7 @@ public synchronized long setTimeout(Node node, Runnable runnable,
425430
* @param eventId The ID of the event to clear.
426431
*/
427432
public synchronized void clearTimeout(Node node, long eventId) {
428-
Event e = events.get(eventId);
433+
Event e = this.getEvents().get(eventId);
429434

430435
if (e == null) {
431436
throw new IllegalArgumentException("Event not found: " + eventId);
@@ -440,7 +445,7 @@ public synchronized void clearTimeout(Node node, long eventId) {
440445
}
441446

442447
timeoutEvent.setStatus(Event.Status.DROPPED);
443-
this.observers.forEach(o -> o.onEventDropped(timeoutEvent));
448+
this.getObservers().forEach(o -> o.onEventDropped(timeoutEvent));
444449
}
445450

446451
/**
@@ -450,7 +455,7 @@ public synchronized void clearTimeout(Node node, long eventId) {
450455
* @return A list of event IDs of queued timeouts.
451456
*/
452457
public synchronized List<Long> getQueuedTimeouts(Node node) {
453-
return this.events.values()
458+
return this.getEvents().values()
454459
.stream()
455460
.filter(e -> e instanceof TimeoutEvent t &&
456461
t.getNodeId().equals(node.getId()) &&
@@ -470,9 +475,9 @@ public synchronized void clearReplicaTimeouts(Node node) {
470475

471476
// remove all event IDs
472477
for (Long eventId : eventIds) {
473-
Event e = events.get(eventId);
478+
Event e = this.getEvents().get(eventId);
474479
e.setStatus(Event.Status.DROPPED);
475-
this.observers.forEach(o -> o.onEventDropped(e));
480+
this.getObservers().forEach(o -> o.onEventDropped(e));
476481
}
477482
}
478483

@@ -510,21 +515,26 @@ public synchronized Fault getNetworkFault(String faultId) {
510515
* </ul>
511516
*/
512517
public void globalStabilizationTime() {
513-
this.isGlobalStabilizationTime = true;
518+
// clear all network faults
519+
// XXX: Is this the right thing to do?
520+
this.networkFaults.clear();
521+
522+
// heal all partitions
523+
this.router.resetPartitions();
514524

515525
// re-queue all dropped messages
516-
this.events.values().stream()
526+
Stream<Event> droppedEvents = this.getEvents().values().stream()
527+
.filter(e -> e.getStatus() == Event.Status.DROPPED);
528+
529+
long numDroppedEvents = droppedEvents.count();
530+
System.out.println("Events dropped that will be requeued: " + numDroppedEvents);
531+
this.getEvents().values().stream()
517532
.filter(e -> e.getStatus() == Event.Status.DROPPED)
518533
.forEach(e -> {
519534
e.setStatus(Event.Status.QUEUED);
520-
this.observers.forEach(o -> o.onEventRequeued(e));
535+
this.getObservers().forEach(o -> o.onEventRequeued(e));
521536
});
522537

523-
// clear all network faults
524-
// XXX: Is this the right thing to do?
525-
this.networkFaults.clear();
526-
527-
// heal all partitions
528-
this.router.resetPartitions();
538+
this.isGlobalStabilizationTime = true;
529539
}
530540
}

0 commit comments

Comments
 (0)