-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (150 loc) · 3.33 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
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"context"
"encoding/json"
"log"
"sync"
"time"
maelstrom "github.com/jepsen-io/maelstrom/demo/go"
)
const timeout = time.Second
type server struct {
n *maelstrom.Node
kv *maelstrom.KV
store map[string]int
mu sync.RWMutex
}
func main() {
node := maelstrom.NewNode()
kv := maelstrom.NewSeqKV(node)
s := &server{
n: node,
kv: kv,
store: make(map[string]int),
}
s.n.Handle("init", s.initHandler)
s.n.Handle("add", s.addHandler)
s.n.Handle("read", s.readHandler)
s.n.Handle("getSum", s.getSumHandler)
if err := s.n.Run(); err != nil {
log.Fatal(err)
}
}
func (s *server) initHandler(msg maelstrom.Message) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := s.kv.Write(ctx, s.n.ID(), 0); err != nil {
return err
}
res := map[string]any{
"type": "init_ok",
}
return s.n.Reply(msg, res)
}
type addReq struct {
Delta int `json:"delta"`
}
func (s *server) addHandler(msg maelstrom.Message) error {
var body addReq
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
s.mu.Lock()
ctx, rCancel := context.WithTimeout(context.Background(), timeout)
defer rCancel()
sum, err := s.kv.ReadInt(ctx, s.n.ID())
if err != nil {
return err
}
err = s.kv.Write(ctx, s.n.ID(), sum+body.Delta)
s.mu.Unlock()
if err != nil {
return err
}
return s.n.Reply(msg, map[string]any{
"type": "add_ok",
})
}
type readReq struct {
Value int `json:"value"`
}
func (s *server) readHandler(msg maelstrom.Message) error {
var body readReq
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
total := 0
resultCh := make(chan int, len(s.n.NodeIDs()))
errorCh := make(chan error, len(s.n.NodeIDs()))
var wg sync.WaitGroup
for _, nID := range s.n.NodeIDs() {
wg.Add(1)
go func(nodeID string) {
defer wg.Done()
if nodeID == s.n.ID() {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
v, err := s.kv.ReadInt(ctx, s.n.ID())
if err != nil {
errorCh <- err
v = s.store[nodeID]
}
s.store[nodeID] = v
resultCh <- v
return
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// If it's not the local node, try to fetch from the remote node
res, err := s.n.SyncRPC(ctx, nodeID, map[string]any{
"type": "getSum",
})
// If it fails, get from the local store
if err != nil {
errorCh <- err
v, exists := s.store[nodeID]
if !exists {
s.store[nodeID] = 0
resultCh <- 0
return
}
resultCh <- v
return
}
if err := json.Unmarshal(res.Body, &body); err != nil {
errorCh <- err
resultCh <- 0
return
}
// Save in local cache
s.store[nodeID] = body.Value
resultCh <- body.Value
}(nID)
}
wg.Wait()
close(resultCh)
close(errorCh)
for v := range resultCh {
total += v
}
for err := range errorCh {
log.Printf("error: %v", err)
}
res := map[string]any{
"type": "read_ok",
"value": total,
}
return s.n.Reply(msg, res)
}
func (s *server) getSumHandler(msg maelstrom.Message) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
v, err := s.kv.ReadInt(ctx, s.n.ID())
if err != nil {
return err
}
return s.n.Reply(msg, map[string]any{
"type": "getSum_ok",
"value": v,
})
}