@@ -2,6 +2,7 @@ package broadcast
2
2
3
3
import (
4
4
"context"
5
+ "log/slog"
5
6
"time"
6
7
7
8
"google.golang.org/protobuf/reflect/protoreflect"
@@ -16,6 +17,7 @@ type BroadcastProcessor struct {
16
17
cancelFunc context.CancelFunc
17
18
started time.Time
18
19
ended time.Time
20
+ logger * slog.Logger
19
21
20
22
cancellationCtx context.Context
21
23
cancellationCtxCancel context.CancelFunc
@@ -60,12 +62,14 @@ func (p *BroadcastProcessor) handle(msg Content) {
60
62
msg .ReceiveChan <- shardResponse {
61
63
err : OutOfOrderErr {},
62
64
}
65
+ p .log ("msg: out of order" , "err" , OutOfOrderErr {}, "method" , msg .CurrentMethod , "from" , msg .SenderAddr )
63
66
} else {
64
67
msg .ReceiveChan <- shardResponse {
65
68
err : nil ,
66
69
reqCtx : p .cancellationCtx ,
67
70
enqueueBroadcast : p .enqueueBroadcast ,
68
71
}
72
+ p .log ("msg: processed" , "err" , nil , "method" , msg .CurrentMethod , "from" , msg .SenderAddr )
69
73
}
70
74
}
71
75
defer func () {
@@ -79,13 +83,15 @@ func (p *BroadcastProcessor) handle(msg Content) {
79
83
//close(p.broadcastChan)
80
84
//close(p.sendChan)
81
85
p .emptyChannels (metadata )
86
+ p .log ("processor stopped" , "err" , nil , "started" , p .started , "ended" , p .ended )
82
87
}()
83
88
for {
84
89
select {
85
90
case <- p .ctx .Done ():
86
91
return
87
92
case bMsg := <- p .broadcastChan :
88
93
if p .broadcastID != bMsg .BroadcastID {
94
+ p .log ("broadcast: wrong BroadcastID" , "err" , BroadcastIDErr {}, "type" , bMsg .MsgType .String (), "stopping" , false )
89
95
continue
90
96
}
91
97
switch bMsg .MsgType {
@@ -110,6 +116,7 @@ func (p *BroadcastProcessor) handle(msg Content) {
110
116
new .ReceiveChan <- shardResponse {
111
117
err : BroadcastIDErr {},
112
118
}
119
+ p .log ("msg: wrong BroadcastID" , "err" , BroadcastIDErr {}, "method" , new .CurrentMethod , "from" , new .SenderAddr )
113
120
continue
114
121
}
115
122
if new .IsCancellation {
@@ -119,6 +126,7 @@ func (p *BroadcastProcessor) handle(msg Content) {
119
126
new .ReceiveChan <- shardResponse {
120
127
err : nil ,
121
128
}
129
+ p .log ("msg: received cancellation" , "err" , nil , "method" , new .CurrentMethod , "from" , new .SenderAddr )
122
130
continue
123
131
}
124
132
@@ -129,6 +137,7 @@ func (p *BroadcastProcessor) handle(msg Content) {
129
137
new .ReceiveChan <- shardResponse {
130
138
err : ClientReqAlreadyReceivedErr {},
131
139
}
140
+ p .log ("msg: duplicate client req" , "err" , ClientReqAlreadyReceivedErr {}, "method" , new .CurrentMethod , "from" , new .SenderAddr )
132
141
continue
133
142
}
134
143
// important to set this option to prevent duplicate client reqs.
@@ -146,6 +155,7 @@ func (p *BroadcastProcessor) handle(msg Content) {
146
155
}
147
156
p .cancellationCtxCancel ()
148
157
}()
158
+ p .log ("msg: received client req" , "err" , nil , "method" , new .CurrentMethod , "from" , new .SenderAddr )
149
159
}
150
160
151
161
metadata .update (new )
@@ -164,6 +174,7 @@ func (p *BroadcastProcessor) handle(msg Content) {
164
174
err : err ,
165
175
}
166
176
// slog.Info("receive: late", "err", err, "id", p.broadcastID)
177
+ p .log ("msg: late msg" , "err" , err , "method" , new .CurrentMethod , "from" , new .SenderAddr )
167
178
return
168
179
}
169
180
if ! p .isInOrder (new .CurrentMethod ) {
@@ -172,22 +183,26 @@ func (p *BroadcastProcessor) handle(msg Content) {
172
183
new .ReceiveChan <- shardResponse {
173
184
err : OutOfOrderErr {},
174
185
}
186
+ p .log ("msg: out of order" , "err" , OutOfOrderErr {}, "method" , new .CurrentMethod , "from" , new .SenderAddr )
175
187
continue
176
188
}
177
189
new .ReceiveChan <- shardResponse {
178
190
err : nil ,
179
191
reqCtx : p .cancellationCtx ,
180
192
enqueueBroadcast : p .enqueueBroadcast ,
181
193
}
194
+ p .log ("msg: processed" , "err" , nil , "method" , new .CurrentMethod , "from" , new .SenderAddr )
182
195
}
183
196
}
184
197
}
185
198
186
199
func (p * BroadcastProcessor ) handleCancellation (bMsg Msg , metadata * metadata ) bool {
187
200
if bMsg .Cancellation .end {
201
+ p .log ("broadcast: broadcast.Done() called" , "err" , nil , "type" , bMsg .MsgType .String (), "stopping" , true )
188
202
return true
189
203
}
190
204
if ! metadata .SentCancellation {
205
+ p .log ("broadcast: sent cancellation" , "err" , nil , "type" , bMsg .MsgType .String (), "stopping" , false )
191
206
metadata .SentCancellation = true
192
207
go p .router .Send (p .broadcastID , "" , "" , bMsg .Cancellation )
193
208
}
@@ -201,17 +216,25 @@ func (p *BroadcastProcessor) handleBroadcast(bMsg Msg, methods []string, metadat
201
216
return false
202
217
}
203
218
p .router .Send (p .broadcastID , metadata .OriginAddr , metadata .OriginMethod , bMsg .Msg )
219
+ p .log ("broadcast: sending broadcast" , "err" , nil , "type" , bMsg .MsgType .String (), "stopping" , false , "isBroadcastCall" , metadata .isBroadcastCall ())
204
220
205
221
p .updateOrder (bMsg .Method )
206
222
p .dispatchOutOfOrderMsgs ()
207
223
return true
208
224
}
209
225
226
+ func (p * BroadcastProcessor ) log (msg string , args ... any ) {
227
+ if p .logger != nil {
228
+ p .logger .Debug (msg , args ... )
229
+ }
230
+ }
231
+
210
232
func (p * BroadcastProcessor ) handleReply (bMsg Msg , metadata * metadata ) bool {
211
233
// BroadcastCall if origin addr is non-empty.
212
234
if metadata .isBroadcastCall () {
213
235
go p .router .Send (p .broadcastID , metadata .OriginAddr , metadata .OriginMethod , bMsg .Reply )
214
236
// the request is done becuase we have sent a reply to the client
237
+ p .log ("broadcast: sending reply to client" , "err" , nil , "type" , bMsg .MsgType .String (), "stopping" , true , "isBroadcastCall" , metadata .isBroadcastCall ())
215
238
return true
216
239
}
217
240
// QuorumCall if origin addr is empty.
@@ -231,9 +254,11 @@ func (p *BroadcastProcessor) handleReply(bMsg Msg, metadata *metadata) bool {
231
254
// the request is not done yet because we have not replied to
232
255
// the client.
233
256
//slog.Info("reply: late", "err", err, "id", p.broadcastID)
257
+ p .log ("broadcast: failed to send reply to client" , "err" , err , "type" , bMsg .MsgType .String (), "stopping" , false , "isBroadcastCall" , metadata .isBroadcastCall ())
234
258
return false
235
259
}
236
260
// the request is done becuase we have sent a reply to the client
261
+ p .log ("broadcast: sending reply to client" , "err" , err , "type" , bMsg .MsgType .String (), "stopping" , true , "isBroadcastCall" , metadata .isBroadcastCall ())
237
262
return true
238
263
}
239
264
@@ -394,6 +419,7 @@ func (r *BroadcastProcessor) dispatchOutOfOrderMsgs() {
394
419
if order <= r .orderIndex {
395
420
for _ , msg := range msgs {
396
421
msg .Run (r .cancellationCtx , r .enqueueBroadcast )
422
+ r .log ("msg: dispatching out of order msg" , "err" , nil , "method" , msg .CurrentMethod , "from" , msg .SenderAddr )
397
423
}
398
424
handledMethods = append (handledMethods , method )
399
425
}
0 commit comments