-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
166 lines (135 loc) · 3.03 KB
/
handlers.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
package main
import (
"context"
"encoding/json"
"sync"
"time"
maelstrom "github.com/jepsen-io/maelstrom/demo/go"
)
type counter struct {
sync.RWMutex
count map[string]int
}
func (c *counter) initialize(nodes []string) {
c.Lock()
defer c.Unlock()
for _, node := range nodes {
c.count[node] = 0
}
}
func (c *counter) add(node string, value int) {
c.Lock()
defer c.Unlock()
c.count[node] += value
}
func (c *counter) read() int {
c.RLock()
defer c.RUnlock()
res := 0
for _, nodeCount := range c.count {
res += nodeCount
}
return res
}
func (c *counter) merge(peerCounter map[string]int) {
c.Lock()
defer c.Unlock()
for k := range peerCounter {
c.count[k] = max(c.count[k], peerCounter[k])
}
}
type nodeWrapper struct {
node *maelstrom.Node
pCounter *counter
nCounter *counter
initDone chan struct{}
}
func newNodeWrapper(node *maelstrom.Node) *nodeWrapper {
return &nodeWrapper{
node: node,
pCounter: &counter{count: make(map[string]int)},
nCounter: &counter{count: make(map[string]int)},
initDone: make(chan struct{}),
}
}
func (n *nodeWrapper) initHandler(msg maelstrom.Message) error {
// NodeIDs() is guaranteed to be populated by now since
// the handler is invoked on the init message only after
// the message is processed and node IDs extracted.
nodes := n.node.NodeIDs()
n.pCounter.initialize(nodes)
n.nCounter.initialize(nodes)
n.initDone <- struct{}{}
return nil
}
func (n *nodeWrapper) addCounterHandler(msg maelstrom.Message) error {
n.node.Reply(msg, map[string]any{
"type": "add_ok",
})
var body map[string]any
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
delta := int(body["delta"].(float64))
if delta < 0 {
n.nCounter.add(n.node.ID(), delta*-1)
return nil
}
n.pCounter.add(n.node.ID(), delta)
return nil
}
func (n *nodeWrapper) readCounterHandler(msg maelstrom.Message) error {
return n.node.Reply(msg, map[string]any{
"type": "read_ok",
"value": n.pCounter.read() - n.nCounter.read(),
})
}
func (n *nodeWrapper) mergeHandler(msg maelstrom.Message) error {
var body mergeMessage
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
n.pCounter.merge(body.Pcounter)
n.nCounter.merge(body.Ncounter)
return nil
}
func (n *nodeWrapper) startAsyncReplication(ctx context.Context) {
// Wait for init message to come otherwise NodeIDs()
// will be empty and no replication will take place.
<-n.initDone
nodes := n.node.NodeIDs()
t := time.NewTicker(3 * time.Second)
defer t.Stop()
done := false
for {
select {
case <-t.C:
for _, node := range nodes {
// Don't replicate to self.
if node == n.node.ID() {
continue
}
n.node.Send(node, map[string]any{
"type": "merge",
"pcounter": n.pCounter.count,
"ncounter": n.nCounter.count,
})
}
case <-ctx.Done():
done = true
}
if done {
break
}
}
}
type mergeMessage struct {
Pcounter map[string]int `json:"pcounter"`
Ncounter map[string]int `json:"ncounter"`
}
func max(a, b int) int {
if a < b {
return b
}
return a
}