|
1 | 1 | package byzzbench.simulator;
|
2 | 2 |
|
3 |
| -import byzzbench.simulator.service.DigestService; |
4 | 3 | import byzzbench.simulator.state.CommitLog;
|
5 | 4 | import byzzbench.simulator.transport.MessagePayload;
|
6 | 5 | import byzzbench.simulator.transport.Transport;
|
7 | 6 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
8 |
| -import lombok.Getter; |
9 |
| -import lombok.ToString; |
10 |
| -import lombok.extern.java.Log; |
11 |
| - |
12 | 7 | import java.io.Serializable;
|
13 | 8 | import java.security.MessageDigest;
|
14 | 9 | import java.security.NoSuchAlgorithmException;
|
15 | 10 | import java.util.Set;
|
| 11 | +import lombok.Getter; |
| 12 | +import lombok.ToString; |
| 13 | +import lombok.extern.java.Log; |
16 | 14 |
|
| 15 | +/** |
| 16 | + * Superclass for all replicas in the system. |
| 17 | + * <p> |
| 18 | + * Each replica has a unique node ID, a set of known node IDs in the system, a |
| 19 | + * reference to the {@link Transport} layer, and a {@link CommitLog}. |
| 20 | + * |
| 21 | + * @param <T> The type of the entries in the commit log of each {@link Replica}. |
| 22 | + */ |
17 | 23 | @Log
|
18 | 24 | @Getter
|
19 | 25 | @ToString
|
20 | 26 | public abstract class Replica<T extends Serializable> implements Serializable {
|
21 |
| - @JsonIgnore |
22 |
| - static MessageDigest md; |
23 |
| - |
24 |
| - static { |
25 |
| - try { |
26 |
| - md = MessageDigest.getInstance("SHA-1"); |
27 |
| - } catch (NoSuchAlgorithmException e) { |
28 |
| - throw new RuntimeException(e); |
29 |
| - } |
| 27 | + @JsonIgnore static MessageDigest md; |
| 28 | + |
| 29 | + static { |
| 30 | + try { |
| 31 | + md = MessageDigest.getInstance("SHA-1"); |
| 32 | + } catch (NoSuchAlgorithmException e) { |
| 33 | + throw new RuntimeException(e); |
30 | 34 | }
|
| 35 | + } |
31 | 36 |
|
32 |
| - @Getter |
33 |
| - private final transient CommitLog<T> commitLog; |
| 37 | + @Getter private final transient CommitLog<T> commitLog; |
34 | 38 |
|
35 |
| - private final transient String nodeId; |
| 39 | + private final transient String nodeId; |
36 | 40 |
|
37 |
| - @JsonIgnore |
38 |
| - private final transient Set<String> nodeIds; |
| 41 | + @JsonIgnore private final transient Set<String> nodeIds; |
39 | 42 |
|
40 |
| - @JsonIgnore |
41 |
| - private final transient Transport<T> transport; |
| 43 | + @JsonIgnore private final transient Transport<T> transport; |
42 | 44 |
|
43 |
| - @JsonIgnore |
44 |
| - private DigestService digestService; |
| 45 | + protected Replica(String nodeId, Set<String> nodeIds, Transport<T> transport, |
| 46 | + CommitLog<T> commitLog) { |
| 47 | + this.nodeId = nodeId; |
| 48 | + this.nodeIds = nodeIds; |
| 49 | + this.transport = transport; |
| 50 | + this.commitLog = commitLog; |
| 51 | + } |
45 | 52 |
|
46 |
| - protected Replica(String nodeId, Set<String> nodeIds, Transport<T> transport, CommitLog<T> commitLog) { |
47 |
| - this.nodeId = nodeId; |
48 |
| - this.nodeIds = nodeIds; |
49 |
| - this.transport = transport; |
50 |
| - this.commitLog = commitLog; |
51 |
| - } |
| 53 | + protected void sendMessage(MessagePayload message, String recipient) { |
| 54 | + this.transport.sendMessage(this.nodeId, message, recipient); |
| 55 | + } |
52 | 56 |
|
53 |
| - protected void sendMessage(MessagePayload message, String recipient) { |
54 |
| - this.transport.sendMessage(this.nodeId, message, recipient); |
55 |
| - } |
| 57 | + protected void multicastMessage(MessagePayload message, |
| 58 | + Set<String> recipients) { |
| 59 | + this.transport.multicast(this.nodeId, recipients, message); |
| 60 | + } |
56 | 61 |
|
57 |
| - protected void multicastMessage(MessagePayload message, Set<String> recipients) { |
58 |
| - this.transport.multicast(this.nodeId, recipients, message); |
59 |
| - } |
| 62 | + /** |
| 63 | + * Send message to all nodes in the system (except self) |
| 64 | + * |
| 65 | + * @param message the message to broadcast |
| 66 | + */ |
| 67 | + protected void broadcastMessage(MessagePayload message) { |
| 68 | + Set<String> otherNodes = this.nodeIds.stream() |
| 69 | + .filter(nodeId -> !nodeId.equals(this.nodeId)) |
| 70 | + .collect(java.util.stream.Collectors.toSet()); |
| 71 | + this.transport.multicast(this.nodeId, otherNodes, message); |
| 72 | + } |
60 | 73 |
|
61 |
| - /** |
62 |
| - * Send message to all nodes in the system (except self) |
63 |
| - * |
64 |
| - * @param message the message to broadcast |
65 |
| - */ |
66 |
| - protected void broadcastMessage(MessagePayload message) { |
67 |
| - Set<String> otherNodes = this.nodeIds |
68 |
| - .stream() |
69 |
| - .filter(nodeId -> !nodeId.equals(this.nodeId)) |
70 |
| - .collect(java.util.stream.Collectors.toSet()); |
71 |
| - this.transport.multicast(this.nodeId, otherNodes, message); |
72 |
| - } |
| 74 | + protected void broadcastMessageIncludingSelf(MessagePayload message) { |
| 75 | + this.transport.multicast(this.nodeId, this.nodeIds, message); |
| 76 | + } |
73 | 77 |
|
74 |
| - protected void broadcastMessageIncludingSelf(MessagePayload message) { |
75 |
| - this.transport.multicast(this.nodeId, this.nodeIds, message); |
76 |
| - } |
| 78 | + public byte[] digest(Serializable message) { |
| 79 | + return md.digest(message.toString().getBytes()); |
| 80 | + } |
77 | 81 |
|
78 |
| - public byte[] digest(Serializable message) { |
79 |
| - return md.digest(message.toString().getBytes()); |
80 |
| - } |
| 82 | + @JsonIgnore |
| 83 | + public Serializable getState() { |
| 84 | + return this; |
| 85 | + } |
81 | 86 |
|
82 |
| - @JsonIgnore |
83 |
| - public Serializable getState() { |
84 |
| - return this; |
85 |
| - } |
| 87 | + public abstract void initialize(); |
86 | 88 |
|
87 |
| - public abstract void initialize(); |
| 89 | + public abstract void handleMessage(String sender, MessagePayload message) |
| 90 | + throws Exception; |
88 | 91 |
|
89 |
| - public abstract void handleMessage(String sender, MessagePayload message) throws Exception; |
| 92 | + public void commitOperation(T message) { this.commitLog.add(message); } |
90 | 93 |
|
91 |
| - public void commitOperation(T message) { |
92 |
| - this.commitLog.add(message); |
93 |
| - } |
94 |
| - |
95 |
| - public long setTimeout(Runnable r, long timeout) { |
96 |
| - return this.transport.setTimeout(this, r, timeout); |
97 |
| - } |
| 94 | + public long setTimeout(Runnable r, long timeout) { |
| 95 | + return this.transport.setTimeout(this, r, timeout); |
| 96 | + } |
98 | 97 |
|
99 |
| - public void clearAllTimeouts() { |
100 |
| - this.transport.clearReplicaTimeouts(this); |
101 |
| - } |
| 98 | + public void clearAllTimeouts() { this.transport.clearReplicaTimeouts(this); } |
102 | 99 | }
|
0 commit comments