-
Notifications
You must be signed in to change notification settings - Fork 14
/
msg_queue.go
204 lines (175 loc) · 4.58 KB
/
msg_queue.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
package pbft
import (
"container/heap"
"sync"
)
// msgQueue defines the structure that holds message queues for different PBFT states
type msgQueue struct {
// Heap implementation for the round change message queue
roundChangeStateQueue msgQueueImpl
// Heap implementation for the accept state message queue
acceptStateQueue msgQueueImpl
// Heap implementation for the validate state message queue
validateStateQueue msgQueueImpl
queueLock sync.Mutex
}
// pushMessage adds a new message to a message queue
func (m *msgQueue) pushMessage(message *MessageReq) {
m.queueLock.Lock()
defer m.queueLock.Unlock()
queue := m.getQueue(msgToState(message.Type))
heap.Push(queue, message)
}
// readMessage reads the message from a message queue, based on the current state and view
func (m *msgQueue) readMessage(st State, current *View) *MessageReq {
msg, _ := m.readMessageWithDiscards(st, current)
return msg
}
func (m *msgQueue) readMessageWithDiscards(st State, current *View) (*MessageReq, []*MessageReq) {
m.queueLock.Lock()
defer m.queueLock.Unlock()
discarded := []*MessageReq{}
queue := m.getQueue(st)
for {
if queue.Len() == 0 {
return nil, discarded
}
msg := queue.head()
// check if the message is from the future
if st == RoundChangeState {
// if we are in RoundChangeState we only care about sequence
// since we are interested in knowing all the possible rounds
if msg.View.Sequence > current.Sequence {
// future message
return nil, discarded
}
} else {
// otherwise, we compare both sequence and round
if cmpView(msg.View, current) > 0 {
// future message
return nil, discarded
}
}
// at this point, 'msg' is good or old, in either case
// we have to remove it from the queue
heap.Pop(queue)
if cmpView(msg.View, current) < 0 {
// old value, try again
discarded = append(discarded, msg)
continue
}
// good value, return it
return msg, discarded
}
}
// getQueue checks the passed in state, and returns the corresponding message queue
func (m *msgQueue) getQueue(st State) *msgQueueImpl {
if st == RoundChangeState {
// round change
return &m.roundChangeStateQueue
} else if st == AcceptState {
// preprepare
return &m.acceptStateQueue
} else {
// prepare and commit
return &m.validateStateQueue
}
}
// newMsgQueue creates a new message queue structure
func newMsgQueue() *msgQueue {
return &msgQueue{
roundChangeStateQueue: msgQueueImpl{},
acceptStateQueue: msgQueueImpl{},
validateStateQueue: msgQueueImpl{},
}
}
// msgToState converts the message type to an State
func msgToState(msg MsgType) State {
if msg == MessageReq_RoundChange {
// round change
return RoundChangeState
} else if msg == MessageReq_Preprepare {
// preprepare
return AcceptState
} else if msg == MessageReq_Prepare || msg == MessageReq_Commit {
// prepare and commit
return ValidateState
}
panic("BUG: not expected")
}
func stateToMsg(st State) MsgType {
switch st {
case RoundChangeState:
return MessageReq_RoundChange
case AcceptState:
return MessageReq_Preprepare
case ValidateState:
return MessageReq_Prepare
default:
panic("BUG: not expected")
}
}
type msgQueueImpl []*MessageReq
// head returns the head of the queue
func (m msgQueueImpl) head() *MessageReq {
return m[0]
}
// Len returns the length of the queue
func (m msgQueueImpl) Len() int {
return len(m)
}
// Less compares the priorities of two items at the passed in indexes (A < B)
func (m msgQueueImpl) Less(i, j int) bool {
ti, tj := m[i], m[j]
// sort by sequence
if ti.View.Sequence != tj.View.Sequence {
return ti.View.Sequence < tj.View.Sequence
}
// sort by round
if ti.View.Round != tj.View.Round {
return ti.View.Round < tj.View.Round
}
// sort by message
return ti.Type < tj.Type
}
// Swap swaps the places of the items at the passed-in indexes
func (m msgQueueImpl) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
// Push adds a new item to the queue
func (m *msgQueueImpl) Push(x interface{}) {
*m = append(*m, x.(*MessageReq))
}
// Pop removes an item from the queue
func (m *msgQueueImpl) Pop() interface{} {
old := *m
n := len(old)
item := old[n-1]
old[n-1] = nil
*m = old[0 : n-1]
return item
}
// cmpView compares two proto views.
//
// If v.Sequence == y.Sequence && v.Round == y.Round => 0
//
// If v.Sequence < y.Sequence => -1 ELSE => 1
//
// If v.Round < y.Round => -1 ELSE 1
func cmpView(v, y *View) int {
if v.Sequence != y.Sequence {
if v.Sequence < y.Sequence {
return -1
} else {
return 1
}
}
if v.Round != y.Round {
if v.Round < y.Round {
return -1
} else {
return 1
}
}
return 0
}