forked from hyperledger-labs/mirbft
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_processor.go
304 lines (257 loc) · 6.85 KB
/
client_processor.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mirbft
import (
"bytes"
"container/list"
"sync"
"github.com/pkg/errors"
"github.com/IBM/mirbft/pkg/pb/msgs"
"github.com/IBM/mirbft/pkg/pb/state"
"github.com/IBM/mirbft/pkg/statemachine"
)
var ErrClientNotExist error = errors.New("client does not exist")
type RequestStore interface {
GetAllocation(clientID, reqNo uint64) ([]byte, error)
PutAllocation(clientID, reqNo uint64, digest []byte) error
GetRequest(requestAck *msgs.RequestAck) ([]byte, error)
PutRequest(requestAck *msgs.RequestAck, data []byte) error
Sync() error
}
// ClientProcessor is the client half of the processor components.
// It accepts client related actions from the state machine and injects
// new client requests.
type ClientProcessor struct {
mutex sync.Mutex
NodeID uint64
RequestStore RequestStore
Hasher Hasher
clients map[uint64]*Client
ClientWork ClientWork
}
type ClientWork struct {
mutex sync.Mutex
readyC chan struct{}
events *statemachine.EventList
}
// Ready return a channel which reads once there are
// events ready to be read via Results(). Note, this
// method must not be invoked concurrently by different
// go routines.
func (cw *ClientWork) Ready() <-chan struct{} {
cw.mutex.Lock()
defer cw.mutex.Unlock()
if cw.readyC == nil {
cw.readyC = make(chan struct{})
if cw.events != nil {
close(cw.readyC)
}
}
return cw.readyC
}
// Results fetches and clears any outstanding results. The caller
// must have successfully read from the Ready() channel before calling
// or the behavior is undefined.
func (cw *ClientWork) Results() *statemachine.EventList {
cw.mutex.Lock()
defer cw.mutex.Unlock()
cw.readyC = nil
events := cw.events
cw.events = nil
return events
}
func (cw *ClientWork) addPersistedReq(ack *msgs.RequestAck) {
cw.mutex.Lock()
defer cw.mutex.Unlock()
if cw.events == nil {
cw.events = &statemachine.EventList{}
if cw.readyC != nil {
close(cw.readyC)
}
}
cw.events.RequestPersisted(ack)
}
func (cp *ClientProcessor) Client(clientID uint64) *Client {
cp.mutex.Lock()
defer cp.mutex.Unlock()
if cp.clients == nil {
cp.clients = map[uint64]*Client{}
}
c, ok := cp.clients[clientID]
if !ok {
c = newClient(clientID, cp.Hasher, cp.RequestStore, &cp.ClientWork)
cp.clients[clientID] = c
}
return c
}
func (cp *ClientProcessor) Process(actions *statemachine.ActionList) (*statemachine.EventList, error) {
events := &statemachine.EventList{}
iter := actions.Iterator()
for action := iter.Next(); action != nil; action = iter.Next() {
switch t := action.Type.(type) {
case *state.Action_AllocatedRequest:
r := t.AllocatedRequest
client := cp.Client(r.ClientId)
digest, err := client.allocate(r.ReqNo)
if err != nil {
return nil, err
}
if digest == nil {
continue
}
events.RequestPersisted(&msgs.RequestAck{
ClientId: r.ClientId,
ReqNo: r.ReqNo,
Digest: digest,
})
case *state.Action_ForwardRequest:
// XXX address
/*
requestData, err := p.RequestStore.Get(r.RequestAck)
if err != nil {
panic(fmt.Sprintf("could not store request, unsafe to continue: %s\n", err))
}
fr := &msgs.Msg{
Type: &msgs.Msg_ForwardRequest{
&msgs.ForwardRequest{
RequestAck: r.RequestAck,
RequestData: requestData,
},
},
}
for _, replica := range r.Targets {
if replica == p.Node.Config.ID {
p.Node.Step(context.Background(), replica, fr)
} else {
p.Link.Send(replica, fr)
}
}
*/
case *state.Action_CorrectRequest:
default:
// Handled elsewhere... for now
}
}
if err := cp.RequestStore.Sync(); err != nil {
return nil, errors.WithMessage(err, "could not sync request store, unsafe to continue")
}
return events, nil
}
type Client struct {
mutex sync.Mutex
clientWork *ClientWork
hasher Hasher
clientID uint64
requestStore RequestStore
requests *list.List
reqNoMap map[uint64]*list.Element
nextReqNo uint64
}
func newClient(clientID uint64, hasher Hasher, reqStore RequestStore, clientWork *ClientWork) *Client {
return &Client{
clientID: clientID,
clientWork: clientWork,
hasher: hasher,
requestStore: reqStore,
requests: list.New(),
reqNoMap: map[uint64]*list.Element{},
}
}
type clientRequest struct {
reqNo uint64
localAllocationDigest []byte
remoteCorrectDigests [][]byte
}
func (c *Client) allocate(reqNo uint64) ([]byte, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
el, ok := c.reqNoMap[reqNo]
if ok {
clientReq := el.Value.(*clientRequest)
return clientReq.localAllocationDigest, nil
}
cr := &clientRequest{
reqNo: reqNo,
}
el = c.requests.PushBack(cr)
c.reqNoMap[reqNo] = el
digest, err := c.requestStore.GetAllocation(c.clientID, reqNo)
if err != nil {
return nil, errors.WithMessagef(err, "could not get key for %d.%d", c.clientID, reqNo)
}
cr.localAllocationDigest = digest
return digest, nil
}
func (c *Client) NextReqNo() (uint64, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.requests.Len() == 0 {
return 0, ErrClientNotExist
}
return c.nextReqNo, nil
}
func (c *Client) Propose(reqNo uint64, data []byte) error {
h := c.hasher.New()
h.Write(data)
digest := h.Sum(nil)
c.mutex.Lock()
defer c.mutex.Unlock()
if c.requests.Len() == 0 {
return ErrClientNotExist
}
if reqNo < c.nextReqNo {
return nil
}
if reqNo > c.nextReqNo {
return errors.Errorf("client must submit req_no %d next", c.nextReqNo)
}
c.nextReqNo++
el, ok := c.reqNoMap[reqNo]
previouslyAllocated := ok
if !ok {
// TODO, limit the distance ahead a client can allocate?
el = c.requests.PushBack(&clientRequest{
reqNo: reqNo,
})
c.reqNoMap[reqNo] = el
}
cr := el.Value.(*clientRequest)
if cr.localAllocationDigest != nil {
if bytes.Equal(cr.localAllocationDigest, digest) {
return nil
}
return errors.Errorf("cannot store request with digest %x, already stored request with different digest %x", digest, cr.localAllocationDigest)
}
if len(cr.remoteCorrectDigests) > 0 {
found := false
for _, rd := range cr.remoteCorrectDigests {
if bytes.Equal(rd, digest) {
found = true
break
}
}
if !found {
return errors.New("other known correct digest exist for reqno")
}
}
ack := &msgs.RequestAck{
ClientId: c.clientID,
ReqNo: reqNo,
Digest: digest,
}
err := c.requestStore.PutRequest(ack, data)
if err != nil {
return errors.WithMessage(err, "could not store requests")
}
err = c.requestStore.PutAllocation(c.clientID, reqNo, digest)
if err != nil {
return err
}
cr.localAllocationDigest = digest
if previouslyAllocated {
c.clientWork.addPersistedReq(ack)
}
return nil
}