-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor.go
405 lines (354 loc) · 11 KB
/
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Copyright 2017 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ------------------------------------------------------------------------------
*/
package processor
import (
"fmt"
"github.com/golang/protobuf/proto"
zmq "github.com/pebbe/zmq4"
"os"
"os/signal"
"runtime"
"github.com/rberg2/sawtooth-go-sdk/logging"
"github.com/rberg2/sawtooth-go-sdk/messaging"
"github.com/rberg2/sawtooth-go-sdk/protobuf/network_pb2"
"github.com/rberg2/sawtooth-go-sdk/protobuf/processor_pb2"
"github.com/rberg2/sawtooth-go-sdk/protobuf/validator_pb2"
)
var logger *logging.Logger = logging.Get()
const DEFAULT_MAX_WORK_QUEUE_SIZE = 100
// TransactionProcessor is a generic class for communicating with a validator
// and routing transaction processing requests to a registered handler. It uses
// ZMQ and channels to handle requests concurrently.
type TransactionProcessor struct {
uri string
ids map[string]string
handlers []TransactionHandler
nThreads uint
maxQueue uint
shutdown chan bool
}
// NewTransactionProcessor initializes a new Transaction Process and points it
// at the given URI. If it fails to initialize, it will panic.
func NewTransactionProcessor(uri string) *TransactionProcessor {
return &TransactionProcessor{
uri: uri,
ids: make(map[string]string),
handlers: make([]TransactionHandler, 0),
nThreads: uint(runtime.GOMAXPROCS(0)),
maxQueue: DEFAULT_MAX_WORK_QUEUE_SIZE,
shutdown: make(chan bool),
}
}
// AddHandler adds the given handler to the TransactionProcessor so it can
// receive transaction processing requests. All handlers must be added prior
// to starting the processor.
func (self *TransactionProcessor) AddHandler(handler TransactionHandler) {
self.handlers = append(self.handlers, handler)
}
// Set the number of worker threads to be created for handling requests. Must
// be set before calling Start()
func (self *TransactionProcessor) SetThreadCount(n uint) {
self.nThreads = n
}
func (self *TransactionProcessor) SetMaxQueueSize(n uint) {
self.maxQueue = n
}
// Start connects the TransactionProcessor to a validator and starts listening
// for requests and routing them to an appropriate handler.
func (self *TransactionProcessor) Start() error {
for {
context, err := zmq.NewContext()
if err != nil {
panic(fmt.Sprint("Failed to create ZMQ context: ", err))
}
reconnect, err := self.start(context)
if err != nil {
return err
}
// If the validator disconnected, then start() returns true
if !reconnect {
break
}
}
return nil
}
func (self *TransactionProcessor) start(context *zmq.Context) (bool, error) {
restart := false
// Establish a connection to the validator
validator, err := messaging.NewConnection(context, zmq.DEALER, self.uri)
if err != nil {
return restart, fmt.Errorf("Could not connect to validator: %v", err)
}
defer validator.Close()
monitor, err := validator.Monitor(zmq.EVENT_DISCONNECTED)
if err != nil {
return restart, fmt.Errorf("Could not monitor validator connection: %v", err)
}
// Setup connection to internal worker thread pool
workers, err := messaging.NewConnection(context, zmq.ROUTER, "inproc://workers")
if err != nil {
return restart, fmt.Errorf("Could not create thread pool router: %v", err)
}
// Make work queue. Buffer so the router doesn't block
queue := make(chan *validator_pb2.Message, self.maxQueue)
// Keep track of which correlation ids go to which worker threads, i.e. map
// corrId->workerThreadId
ids := make(map[string]string)
// Startup worker thread pool
for i := uint(0); i < self.nThreads; i++ {
go worker(context, "inproc://workers", queue, self.handlers)
}
// Setup shutdown thread
go shutdown(context, "inproc://workers", queue, self.shutdown)
workersLeft := self.nThreads + 1
// Setup ZMQ poller for routing messages between worker threads and validator
poller := zmq.NewPoller()
poller.Add(validator.Socket(), zmq.POLLIN)
poller.Add(monitor, zmq.POLLIN)
poller.Add(workers.Socket(), zmq.POLLIN)
// Register all handlers with the validator
for _, handler := range self.handlers {
for _, version := range handler.FamilyVersions() {
err := register(validator, handler, version, queue)
if err != nil {
return restart, fmt.Errorf(
"Error registering handler (%v, %v, %v): %v",
handler.FamilyName(), version,
handler.Namespaces(), err,
)
}
}
}
// Poll for messages from worker threads or validator
for {
polled, err := poller.Poll(-1)
if err != nil {
return restart, fmt.Errorf("Polling failed: %v", err)
}
for _, ready := range polled {
switch socket := ready.Socket; socket {
case validator.Socket():
receiveValidator(ids, validator, workers, queue)
case monitor:
restart = receiveMonitor(monitor, self.shutdown)
case workers.Socket():
receiveWorkers(ids, validator, workers, &workersLeft)
if workersLeft == 0 {
return restart, nil
}
}
}
}
}
// Shutdown sends a message to the processor telling it to deregister.
func (self *TransactionProcessor) Shutdown() {
// Initiate a clean shutdown
self.shutdown <- false
}
// ShutdownOnSignal sets up signal handling to shutdown the processor when one
// of the signals passed is received.
func (self *TransactionProcessor) ShutdownOnSignal(siglist ...os.Signal) {
// Setup signal handlers
ch := make(chan os.Signal)
signal.Notify(ch, siglist...)
go func() {
// Wait for a signal
_ = <-ch
// Reset signal handlers
signal.Reset(siglist...)
logger.Warnf("Shutting down gracefully (Press Ctrl+C again to force)")
self.Shutdown()
}()
}
// Handle incoming messages from the validator
func receiveValidator(ids map[string]string, validator, workers messaging.Connection, queue chan *validator_pb2.Message) {
defer func() {
if r := recover(); r != nil {
logger.Errorf(
"Panic occured while routing message from validator: %v", r,
)
}
}()
// Receive a message from the validator
_, data, err := validator.RecvData()
if err != nil {
logger.Errorf("Receiving message from validator failed: %v", err)
return
}
// We need to deserialize the message to get the correlation id
msg, err := messaging.LoadMsg(data)
if err != nil {
logger.Errorf("Deserializing message from validator failed: %v", err)
return
}
// Check if this is a new request or a response to a message sent by a
// worker thread.
t := msg.GetMessageType()
corrId := msg.GetCorrelationId()
// If this is a new request, put in on the work queue
switch t {
case validator_pb2.Message_TP_PROCESS_REQUEST:
select {
case queue <- msg:
default:
logger.Warnf("Work queue is full, denying request %v", corrId)
data, err := proto.Marshal(&processor_pb2.TpProcessResponse{
Status: processor_pb2.TpProcessResponse_INTERNAL_ERROR,
Message: "Work queue is full, denying request",
})
if err != nil {
logger.Errorf(
"Failed to notify validator the request is denied: %v", err,
)
}
err = validator.SendMsg(
validator_pb2.Message_TP_PROCESS_RESPONSE, data, corrId,
)
if err != nil {
logger.Errorf(
"Failed to notify validator the request is denied: %v", err,
)
}
}
return
case validator_pb2.Message_PING_REQUEST:
data, err := proto.Marshal(&network_pb2.PingResponse{})
if err != nil {
logger.Errorf(
"Failed to respond to Ping %v", err,
)
}
err = validator.SendMsg(
validator_pb2.Message_PING_RESPONSE, data, corrId,
)
return
}
// If this is a response, send it to the worker.
workerId, exists := ids[corrId]
if exists && corrId != "" {
err = workers.SendData(workerId, data)
if err != nil {
logger.Errorf(
"Failed to send response with correlationd id %v to worker %v: %v",
corrId, workerId, err,
)
return
}
delete(ids, corrId)
return
}
logger.Warnf(
"Received unexpected message from validator: (%v, %v)", t, corrId,
)
}
// Handle monitor events
func receiveMonitor(monitor *zmq.Socket, shutdown chan bool) bool {
restart := false
event, endpoint, _, err := monitor.RecvEvent(0)
if err != nil {
logger.Error(err)
} else {
if event == zmq.EVENT_DISCONNECTED {
logger.Infof("Validator '%v' disconnected", endpoint)
restart = true
} else {
logger.Errorf("Received unexpected event on monitor socket: %v", event)
}
shutdown <- true
}
return restart
}
// Handle incoming messages from the workers
func receiveWorkers(ids map[string]string, validator, workers messaging.Connection, workersLeft *uint) {
// Receive a mesasge from the workers
workerId, data, err := workers.RecvData()
if err != nil {
logger.Errorf("Receiving message from workers failed: %v", err)
return
}
msg, err := messaging.LoadMsg(data)
if err != nil {
logger.Errorf("Deserializing message from workers failed: %v", err)
return
}
t := msg.GetMessageType()
corrId := msg.GetCorrelationId()
if t == validator_pb2.Message_DEFAULT && corrId == "shutdown" {
*workersLeft = *workersLeft - 1
return
}
// Store which thread the response should be routed to
if t != validator_pb2.Message_TP_PROCESS_RESPONSE {
ids[corrId] = workerId
}
// Pass the message on to the validator
err = validator.SendData("", data)
if err != nil {
logger.Errorf("Failed to send message (%v) to validator: %v", corrId, err)
return
}
}
// Register a handler with the validator
func register(validator messaging.Connection, handler TransactionHandler, version string, queue chan *validator_pb2.Message) error {
regRequest := &processor_pb2.TpRegisterRequest{
Family: handler.FamilyName(),
Version: version,
Namespaces: handler.Namespaces(),
}
regRequestData, err := proto.Marshal(regRequest)
if err != nil {
return err
}
corrId, err := validator.SendNewMsg(
validator_pb2.Message_TP_REGISTER_REQUEST,
regRequestData,
)
if err != nil {
return err
}
// The validator is impatient and will send requests before confirming
// registration.
var msg *validator_pb2.Message
for {
_, msg, err = validator.RecvMsg()
if err != nil {
return err
}
if msg.GetCorrelationId() != corrId {
queue <- msg
} else {
break
}
}
if msg.GetMessageType() != validator_pb2.Message_TP_REGISTER_RESPONSE {
return fmt.Errorf("Received unexpected message type: %v", msg.GetMessageType())
}
regResponse := &processor_pb2.TpRegisterResponse{}
err = proto.Unmarshal(msg.GetContent(), regResponse)
if err != nil {
return err
}
if regResponse.GetStatus() != processor_pb2.TpRegisterResponse_OK {
return fmt.Errorf("Got response: %v", regResponse.GetStatus())
}
logger.Infof(
"Successfully registered handler (%v, %v, %v)",
handler.FamilyName(), version,
handler.Namespaces(),
)
return nil
}