-
Notifications
You must be signed in to change notification settings - Fork 72
/
replica.go
259 lines (236 loc) · 4.84 KB
/
replica.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package sdpaxos
import (
"time"
"github.com/ailidani/paxi"
)
type entry struct {
ballot paxi.Ballot
command paxi.Command
cquorum *paxi.Quorum
ccommit bool
gslot uint64
oquorum *paxi.Quorum
ocommit bool
request *paxi.Request
timestamp time.Time
}
type Replica struct {
paxi.Node
log map[paxi.ID]map[uint64]*entry // received log
glog map[uint64]*entry // global ordered log
gslot uint64 // global slot number
execute uint64 // last executed in global log
slot uint64 // my slot number
ballot paxi.Ballot // my ballot
sequencer paxi.ID
sequencerBallot paxi.Ballot
}
func NewReplica(id paxi.ID) *Replica {
r := new(Replica)
r.Node = paxi.NewNode(id)
r.log = make(map[paxi.ID]map[uint64]*entry)
for id := range paxi.GetConfig().Addrs {
r.log[id] = make(map[uint64]*entry)
}
r.glog = make(map[uint64]*entry)
r.ballot = paxi.NewBallot(1, id)
r.sequencer = paxi.NewID(1, 1)
r.Register(paxi.Request{}, r.handleRequest)
r.Register(CAccept{}, r.handleCAccept)
r.Register(CAck{}, r.handleCAck)
r.Register(CCommit{}, r.handleCCommit)
r.Register(OAccept{}, r.handleOAccept)
r.Register(OAck{}, r.handleOAck)
r.Register(OCommit{}, r.handleOCommit)
return r
}
func (r *Replica) handleRequest(m paxi.Request) {
_, exist := r.log[r.ID()]
if !exist {
r.log[r.ID()] = make(map[uint64]*entry)
}
r.log[r.ID()][r.slot] = &entry{
ballot: r.ballot,
command: m.Command,
cquorum: paxi.NewQuorum(),
ccommit: false,
gslot: 0,
oquorum: paxi.NewQuorum(),
ocommit: false,
request: &m,
}
r.log[r.ID()][r.slot].cquorum.ACK(r.ID())
r.Broadcast(CAccept{
Ballot: r.ballot,
From: r.ID(),
Command: m.Command,
Slot: r.slot,
})
if r.sequencer == r.ID() {
r.Broadcast(OAccept{
Ballot: r.ballot,
ID: r.ID(),
Slot: r.slot,
GSlot: r.gslot,
From: r.ID(),
})
r.gslot++
}
r.slot++
}
func (r *Replica) handleCAccept(m CAccept) {
_, exist := r.log[m.From]
if !exist {
r.log[m.From] = make(map[uint64]*entry)
}
r.log[m.From][m.Slot] = &entry{
ballot: m.Ballot,
command: m.Command,
ccommit: false,
gslot: 0,
}
r.Send(m.From, CAck{
Ballot: m.Ballot,
From: r.ID(),
Slot: m.Slot,
})
if r.sequencer == r.ID() {
r.Broadcast(OAccept{
Ballot: r.ballot,
ID: m.From,
Slot: m.Slot,
GSlot: r.gslot,
From: r.ID(),
})
r.gslot++
}
}
func (r *Replica) handleCAck(m CAck) {
if m.Ballot != r.ballot {
return
}
entry := r.log[r.ID()][m.Slot]
if entry.ccommit {
return
}
entry.cquorum.ACK(m.From)
if entry.cquorum.Majority() {
entry.ccommit = true
r.Broadcast(CCommit{
Ballot: r.ballot,
From: r.ID(),
Slot: m.Slot,
Command: entry.command,
})
if entry.ocommit {
r.glog[entry.gslot] = entry
go r.exec()
}
}
}
func (r *Replica) handleCCommit(m CCommit) {
_, exist := r.log[m.From]
if !exist {
r.log[m.From] = make(map[uint64]*entry)
}
e, exist := r.log[m.From][m.Slot]
if !exist {
r.log[m.From][m.Slot] = &entry{
ballot: m.Ballot,
command: m.Command,
ccommit: true,
}
return
}
e.ccommit = true
if e.ocommit {
go r.exec()
}
}
func (r *Replica) handleOAccept(m OAccept) {
if m.From == r.sequencer {
e, exist := r.log[m.ID][m.Slot]
if !exist {
r.log[m.ID][m.Slot] = &entry{
ccommit: false,
ocommit: false,
gslot: m.GSlot,
}
e = r.log[m.ID][m.Slot]
} else if e.ocommit {
return
}
e.gslot = m.GSlot
//r.glog[m.GSlot] = entry
if m.ID == r.ID() {
e.oquorum.ACK(r.ID())
} else {
r.Send(m.ID, OAck{
Ballot: r.ballot,
ID: m.ID,
Slot: m.Slot,
GSlot: m.GSlot,
From: r.ID(),
})
}
}
}
func (r *Replica) handleOAck(m OAck) {
if m.ID == r.ID() {
e, exist := r.log[m.ID][m.Slot]
if !exist || e.ocommit {
return
}
e.oquorum.ACK(m.From)
if e.oquorum.Majority() {
e.ocommit = true
r.glog[m.GSlot] = e
r.Broadcast(OCommit{
Ballot: r.ballot,
Slot: m.Slot,
GSlot: m.GSlot,
From: r.ID(),
})
if e.ccommit {
go r.exec()
}
}
}
}
func (r *Replica) handleOCommit(m OCommit) {
e, exist := r.log[m.From][m.Slot]
if !exist {
r.log[m.From][m.Slot] = &entry{
ballot: m.Ballot,
ocommit: true,
gslot: m.GSlot,
}
}
e.ocommit = true
e.gslot = m.GSlot
r.glog[m.GSlot] = e
if e.ccommit {
go r.exec()
}
}
func (r *Replica) exec() {
for {
e, ok := r.glog[r.execute]
if !ok || !e.ccommit || !e.ocommit {
break
}
value := r.Node.Execute(e.command)
if e.request != nil {
reply := paxi.Reply{
Command: e.command,
Value: value,
Properties: make(map[string]string),
}
e.request.Reply(reply)
e.request = nil
}
// TODO clean up the log periodically
//delete(r.glog, r.execute)
r.execute++
}
}