Skip to content

Commit 9ace937

Browse files
committed
Prepare next lecture: Distributed Computing
1 parent edf2067 commit 9ace937

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

docs/08-Distributed-Programming-Raft.slide

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,15 @@ Requirements and Decisions
151151
- Design: Timer implementation
152152

153153
* Exercise 8.2: Be Creative!
154-
- Write your own Raft Implementation!
155154

156-
.link https://github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise8.md
155+
Three armies, each led by a general, plan to attack a fortified city. The armies may arrive at different times to surround the city, and none of the generals can be certain whether all the armies will arrive at all. Consequently, they have not appointed a single leader.
156+
For the attack to succeed, at least two armies must strike simultaneously. Therefore, the generals need to coordinate and agree on a precise time to launch the attack.
157+
To achieve this, they decide to use a simplified version of the Raft algorithm.
157158

158159
- Write your own Raft Implementation!
159160

161+
.link https://github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise8.md
162+
160163
* Summary
161164
- Go is an excellent choice to implement an distributed protocol like Raft
162165
- You can implement the Raft specification with ca 1000-2000 Loc

docs/exercises/Exercise8.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Three armies, each led by a general, plan to attack a fortified city. The armies
1616
For the attack to succeed, at least two armies must strike simultaneously. Therefore, the generals need to coordinate and agree on a precise time to launch the attack.
1717
To achieve this, they decide to use a simplified version of the Raft algorithm.
1818

19-
In the following, we will represent the armies as nodes and facilitate communication between them using REST.
19+
In the following, we will represent the armies as nodes and facilitate communication between them using REST APIs.
2020

2121
### Define list of servers (nodes)
2222

@@ -35,7 +35,7 @@ var nodePortList = []string{
3535
On start of the program the node id should be read as argument from the console.
3636
```raft --node 0```
3737

38-
Use the `flag` package to read the arguments.
38+
Use the `flag` [package](https://pkg.go.dev/flag) to read the arguments.
3939

4040
### State
4141

@@ -45,47 +45,50 @@ Define an [enum](https://gobyexample.com/enums] containin) for the node state
4545
* Candidate
4646
* Leader
4747

48-
Implement the [stringer](https://go.dev/tour/methods/17) interface for the enum.
4948
Write a function to switch the state of the node.
5049
It should apply the following rules, otherwise stop the program.
5150

5251
* A leader can never change
53-
* A candidate can only change to leader or follower
54-
* A follower can only change to candidate
52+
* A candidate can only change to a leader or follower
53+
* A follower can only change to a candidate
5554

56-
For now, node 0 start as the leader, the others as followers.
55+
For now, node 0 starts as a leader, the others as followers.
5756

5857
### Attack Endpoint
5958

60-
Implement an "attack" endpoint and start an asynchronous running server on the port defined by the port list.
61-
The attack endpoint receives the time of the attack as string, prints the attack time on screen and returns a 200 status code.
59+
Implement an "attack" endpoint and start the server on the port defined by the port list.
60+
The "attack" endpoint uses the REST Method POST, receives the time of the attack as string, prints the attack time on screen and returns a 200 status code.
6261
You can test it via curl, httpie or wget.
6362

6463
```
6564
curl -X POST http://localhost:10000/attack -d "midnight"
6665
```
6766

68-
After a few seconds (e. g. 30 seconds) the leader should send an attack request to the other nodes to attack at the same time.
67+
If the node is the leader: After a few seconds (e.g. 30 seconds) the leader should send an attack request to the other nodes to attack at the same time.
6968
Only when the leader receives an OK response from at least one other node, the attack is successful.
7069

71-
Optional task: You can send a commit message to the followers to confirm the attack.
70+
Optional task: You can send a commit message to the followers to confirm the attack. You just have to implement a commit endpoint.
7271

73-
### Heartbeat Endpoint
72+
### Simplified Heartbeat Endpoint
7473

75-
Implement the heartbeat mechanism.
76-
Implement a heartbeat endpoint. It should just return a 200 status code.
77-
Only the leader should send a heartbeat to the other nodes in intervals of 1 second.
74+
Implement the heartbeat mechanism. Implement a heartbeat endpoint via a GET method. It should just return a 200 status code and turn the node into a follower.
75+
The leader node should send a heartbeat to the other nodes in intervals of 1 second.
76+
77+
### Simplified Vote Endpoint
78+
79+
Implement a vote endpoint.
80+
The vote endpoint uses the REST Method GET.
81+
If the node is the leader, it should return "no" to the other nodes, otherwise "yes" and turn into a "follower".
7882

7983
### Simplified Leader Election
8084

81-
From now on all nodes start from the follower state. No one is the leader
85+
From now on all nodes start from the follower state. No one is the leader.
8286

83-
- The nodes wait for heartbeats from the leader. E.g. for two seconds.
87+
- The nodes wait for heartbeats from the leader. E.g. for two seconds.(Example via the time.After function: [heartbeat.go](../../src/distributed/byzantine/heartbeat.go))
8488
- Because there is no leader, the nodes become candidates and start an election.
85-
- The nodes wait for a random time between 1 and 5 seconds and start a voting process.
86-
- for the voting they send a request to all other nodes.
87-
- The nodes vote for the candidate and become followers.
88-
- If an heartbeat is received, the nodes become followers as well and the voting stops.
89+
- The nodes wait for a random time between 1 and 3 seconds and start a voting process.
90+
- For the voting they send a request to all other nodes to the voting endpoint.
91+
- If they receive a majority of "yes" votes, they become the leader and start the heartbeats.
8992

9093
**Examples:**
9194

0 commit comments

Comments
 (0)