-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (145 loc) · 3.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"fmt"
"log"
"sync"
"time"
maelstrom "github.com/jepsen-io/maelstrom/demo/go"
"golang.org/x/exp/slices"
)
// {
// "type": "broadcast",
// "message": 1000
// }
type BroadcastRequestBody struct {
Type string `json:"type"`
Message uint64 `json:"message"`
}
// {
// "type": "broadcast_ok"
// }
type BroadcastResponseBody struct {
Type string `json:"type"`
}
// {
// "type": "read"
// }
type ReadRequestBody struct {
Type string `json:"type"`
}
// {
// "type": "read_ok",
// "messages": [1, 8, 72, 25]
// }
type ReadResponseBody struct {
Type string `json:"type"`
Messages []uint64 `json:"messages"`
}
// {
// "type": "topology",
// "topology": {
// "n1": ["n2", "n3"],
// "n2": ["n1"],
// "n3": ["n1"]
// }
// }
type TopologyRequestBody struct {
Type string `json:"type"`
Topology map[string][]string `json:"topology"`
}
type TopologyResponseBody struct {
Type string `json:"type"`
}
type BroadcastOkResponseBody struct {
InReplyTo int `json:"in_reply_to"`
Type string `json:"type"`
}
type UnansweredNodeBroadcast struct {
Message uint64
Dest string
SentAt time.Time
}
func main() {
var messages []uint64
var destinations []string
// Prevent race conditions when writing messages
var messagesMutex sync.Mutex
n := maelstrom.NewNode()
n.Handle("broadcast_ok", func(msg maelstrom.Message) error {
return nil
})
n.Handle("broadcast", func(msg maelstrom.Message) error {
// Unmarshal the message body as an loosely-typed map.
var body BroadcastRequestBody
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
// If we already received the message, stop broadcasting
if slices.Contains(messages, body.Message) {
return n.Reply(msg, BroadcastResponseBody{
Type: "broadcast_ok",
})
}
messagesMutex.Lock()
messages = append(messages, body.Message)
messagesMutex.Unlock()
go func(destinations []string, body BroadcastRequestBody) {
var successfulSentDestinations []string
var successfulSentDestinationsMutex sync.Mutex
// Send messages to the node's destinations
// Until we got none left
for len(destinations) != len(successfulSentDestinations) {
for _, node := range destinations {
n.RPC(node, body, func(msg maelstrom.Message) error {
var broadcastOkBody BroadcastOkResponseBody
if err := json.Unmarshal(msg.Body, &broadcastOkBody); err != nil {
return err
}
if broadcastOkBody.Type != "broadcast_ok" {
return fmt.Errorf("expeted type broadcast_ok, got %s", broadcastOkBody.Type)
}
log.Printf("from %s, to %s, sent %v", n.ID(), node, body)
successfulSentDestinationsMutex.Lock()
defer successfulSentDestinationsMutex.Unlock()
// Check if we already marked the destination node as successfully sent
if !slices.Contains(successfulSentDestinations, node) {
successfulSentDestinations = append(successfulSentDestinations, node)
}
return nil
})
}
time.Sleep(1 * time.Second)
}
}(destinations, body)
return n.Reply(msg, BroadcastResponseBody{
Type: "broadcast_ok",
})
})
n.Handle("read", func(msg maelstrom.Message) error {
// Unmarshal the message body as an loosely-typed map.
var body map[string]any
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
return n.Reply(msg, ReadResponseBody{
Type: "read_ok",
Messages: messages,
})
})
n.Handle("topology", func(msg maelstrom.Message) error {
// Unmarshal the message body as an loosely-typed map.
var body TopologyRequestBody
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
// Only store the destinations for our node
destinations = body.Topology[n.ID()]
return n.Reply(msg, TopologyResponseBody{
Type: "topology_ok",
})
})
if err := n.Run(); err != nil {
log.Fatal(err)
}
}