8000 chore: minor updates, better debugging & demo images · Marvin9/distributed-algorithms@b8409ea · GitHub
[go: up one dir, main page]

Skip to content

Commit b8409ea

Browse files
committed
chore: minor updates, better debugging & demo images
1 parent da95075 commit b8409ea

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

bully-algorithm/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<img src="../demo/0.png" />
2+
3+
<img src="../demo/1.png" />

bully-algorithm/bully.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,27 @@ func (n *Network) bully() {
5050

5151
for {
5252
n.Lock()
53-
utils.Debug(fmt.Sprintf("Node %v is in process", n.Nodes[i].NodeID))
54-
utils.Debug(n.Nodes)
53+
if !n.Nodes[i].IsFailed {
54+
utils.Debug(fmt.Sprintf("Node %v is in process", n.Nodes[i].NodeID))
5555

56-
if n.IsCoordinatorFailed() {
57-
utils.Debug("Coordinator node failed")
58-
n.election(i)
56+
if n.IsCoordinatorFailed() {
57+
utils.Debug("Coordinator node failed")
58+
n.election(i)
59+
}
60+
} else {
61+
utils.Debug(fmt.Sprintf("Node %v is failed. Skipping...", n.Nodes[i].NodeID))
5962
}
60-
61-
i = (i + 1) % totalNodes
6263
n.Unlock()
64+
i = (i + 1) % totalNodes
65+
66+
n.State()
6367

6468
var in string
6569
utils.Debug("Press i for input mode, c for continue...")
6670
fmt.Scanf("%s", &in)
6771
switch in {
6872
case "i":
69-
utils.Debug(n.Nodes)
7073
n.Controll()
71-
utils.Debug(n.Nodes)
7274
case "s":
7375
continue
7476
}
@@ -85,10 +87,14 @@ func (n *Network) Controll() {
8587
for idx := range n.Nodes {
8688
if n.Nodes[idx].NodeID == nodeID {
8789
if operation == 0 {
88-
n.Nodes[idx].IsFailed = true
90+
if !n.Nodes[idx].IsFailed {
91+
n.Nodes[idx].IsFailed = true
92+
}
8993
} else {
90-
n.Nodes[idx].IsFailed = false
91-
n.election(idx)
94+
if n.Nodes[idx].IsFailed {
95+
n.Nodes[idx].IsFailed = false
96+
n.election(idx)
97+
}
9298
}
9399
return
94100
}

bully-algorithm/modals.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ func CreateNetwork() Network {
5151
}
5252
}
5353

54+
// State - network state
55+
func (n *Network) State() {
56+
failedNodes := make([]NodeIDType, 0)
57+
currentActiveNode := -1
58+
for _, node := range n.Nodes {
59+
if node.IsFailed {
60+
failedNodes = append(failedNodes, node.NodeID)
61+
continue
62+
}
63+
if node.IsCoordinator {
64+
currentActiveNode = node.NodeID
65+
}
66+
}
67+
68+
utils.Debug(fmt.Sprintf("Failed nodes: %v\nCoordinator: %v", failedNodes, currentActiveNode))
69+
}
70+
5471
// MakeCoordinator - will make one of node coordinator
5572
func (n *Network) MakeCoordinator(nodeID NodeIDType) {
5673
utils.Debug(fmt.Sprintf("Making Node %v coordinator", nodeID))

demo/0.png

65.6 KB
Loading

demo/1.png

125 KB
Loading

0 commit comments

Comments
 (0)
0