-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands.go
314 lines (270 loc) · 8.99 KB
/
commands.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
package pulsar
import (
"errors"
"fmt"
"net/url"
pb "github.com/cornelk/pulsar-go/proto"
"google.golang.org/protobuf/proto"
)
type command struct {
*pb.BaseCommand
payloadSize uint32 // may be zero
err error // TODO: currently errors are logged multiple times (in callbacks and on client level)
custom string // TODO: improve name
}
type (
commandHandler func(cmd *command) error
commands map[pb.BaseCommand_Type]commandHandler
)
// noRequestID is used inside this library to signal that a command has no request ID.
const noRequestID = 0
// ErrClientClosing is returned when a new consumer or producer should be
// created while the client is closing all connections.
var ErrClientClosing = errors.New("client is closing connections")
func (c *Client) newCommandMap() commands {
return commands{
pb.BaseCommand_CONNECTED: c.handleConnectedCommand,
pb.BaseCommand_SEND_RECEIPT: c.handleSendReceiptCommand,
pb.BaseCommand_SEND_ERROR: c.handleSendErrorCommand,
pb.BaseCommand_MESSAGE: c.handleMessage,
pb.BaseCommand_SUCCESS: c.handleSuccessCommand,
pb.BaseCommand_ERROR: c.handleErrorCommand,
pb.BaseCommand_CLOSE_CONSUMER: c.handleCloseConsumerCommand,
pb.BaseCommand_PRODUCER_SUCCESS: c.handleProducerSuccessCommand,
pb.BaseCommand_PING: c.handlePingCommand,
pb.BaseCommand_PARTITIONED_METADATA_RESPONSE: c.handlePartitionedMetadataResponseCommand,
pb.BaseCommand_LOOKUP_RESPONSE: c.handleTopicLookupResponse,
pb.BaseCommand_GET_LAST_MESSAGE_ID_RESPONSE: c.handleLastMessageIDResponse,
pb.BaseCommand_GET_TOPICS_OF_NAMESPACE_RESPONSE: c.handleGetTopicsOfNamespaceResponse,
}
}
// TODO: write unit test to proof that for every handled command we have a way to determine the request ID.
func (cmd *command) requestID() uint64 {
switch cmd.GetType() {
case pb.BaseCommand_CONNECTED, pb.BaseCommand_MESSAGE, pb.BaseCommand_PING:
return noRequestID
case pb.BaseCommand_SEND_RECEIPT:
return cmd.GetSendReceipt().GetSequenceId()
case pb.BaseCommand_SEND_ERROR:
return cmd.GetSendError().GetSequenceId()
case pb.BaseCommand_SUCCESS:
return cmd.GetSuccess().GetRequestId()
case pb.BaseCommand_ERROR:
return cmd.GetError().GetRequestId()
case pb.BaseCommand_CLOSE_CONSUMER:
return cmd.GetCloseConsumer().GetRequestId()
case pb.BaseCommand_PRODUCER_SUCCESS:
return cmd.GetProducerSuccess().GetRequestId()
case pb.BaseCommand_PARTITIONED_METADATA_RESPONSE:
return cmd.GetPartitionMetadataResponse().GetRequestId()
case pb.BaseCommand_LOOKUP_RESPONSE:
return cmd.GetLookupTopicResponse().GetRequestId()
case pb.BaseCommand_GET_LAST_MESSAGE_ID_RESPONSE:
return cmd.GetGetLastMessageIdResponse().GetRequestId()
case pb.BaseCommand_GET_TOPICS_OF_NAMESPACE_RESPONSE:
return cmd.GetGetTopicsOfNamespaceResponse().GetRequestId()
default:
return noRequestID
}
}
func (c *Client) handleRequestCallback(cmd *command) error {
reqID := cmd.requestID()
if reqID == noRequestID {
return nil
}
callback, custom := c.req.remove(reqID)
if callback == nil {
return nil
}
cmd.custom = custom
return callback(cmd)
}
func (c *Client) handleConnectedCommand(*command) error {
close(c.connected)
return nil
}
func (c *Client) handleTopicLookupResponse(cmd *command) error {
resp := cmd.LookupTopicResponse
if r := resp.GetResponse(); r != pb.CommandLookupTopicResponse_Connect {
// TODO support redirect
return fmt.Errorf("topic lookup response not supported: %v", r)
}
// TODO callback in case of error?
u, err := url.Parse(resp.GetBrokerServiceUrl())
if err != nil {
return fmt.Errorf("parsing URL: %w", err)
}
if c.host != u.Host { // is same server that client is already connected to?
_ = u // TODO support
}
return c.handleRequestCallback(cmd)
}
func (c *Client) handleMessage(base *command) error {
cmd := base.Message
msgMeta, payload, err := c.conn.readMessageMetaData(base.payloadSize)
if err != nil {
return err
}
if msgMeta.GetCompression() != pb.CompressionType_NONE {
return errors.New("compressed messages not supported") // TODO support
}
consumerID := cmd.GetConsumerId()
cons, ok := c.consumers.get(consumerID)
if !ok {
if c.closing.Load() {
return nil
}
// a message was received for a consumer that is now closed,
// do not thread it as an error.
c.log.Debugf("Consumer '%d' for message does not exist", consumerID)
return nil
}
id := cmd.MessageId
permits := uint64(1)
if num := msgMeta.GetNumMessagesInBatch(); num > 0 {
// each batch message counts as 1 permit
permits = uint64(num)
var msg []byte
for i := int32(0); i < num; i++ {
_, msg, payload, err = c.conn.readBatchedMessage(payload)
if err != nil {
return err
}
m := &Message{
consumerID: consumerID,
Body: msg,
Topic: cons.topic,
ID: &MessageID{
LedgerId: id.LedgerId,
EntryId: id.EntryId,
Partition: id.Partition,
BatchIndex: proto.Int32(i),
},
}
cons.incomingMessages <- m
}
} else {
m := &Message{
Body: payload,
ID: (*MessageID)(id),
}
cons.incomingMessages <- m
}
return cons.useMessagePermits(permits)
}
func (c *Client) handleSendReceiptCommand(base *command) error {
cmd := base.SendReceipt
prod, ok := c.producers.get(cmd.GetProducerId())
if !ok {
return errors.New("producer not found")
}
seq := cmd.GetSequenceId()
return prod.processSendResult(seq, cmd.MessageId, nil)
}
func (c *Client) handleSendErrorCommand(base *command) error {
cmd := base.SendError
prod, ok := c.producers.get(cmd.GetProducerId())
if !ok {
return errors.New("producer not found")
}
err := fmt.Errorf("%s: %s", cmd.GetError(), cmd.GetMessage())
seq := cmd.GetSequenceId()
return prod.processSendResult(seq, nil, err)
}
func (c *Client) handleSuccessCommand(cmd *command) error {
return c.handleRequestCallback(cmd)
}
func (c *Client) handleErrorCommand(cmd *command) error {
cmd.err = fmt.Errorf("%s: %s", cmd.Error.GetError(), cmd.Error.GetMessage())
return c.handleRequestCallback(cmd)
}
func (c *Client) handleProducerSuccessCommand(cmd *command) error {
return c.handleRequestCallback(cmd)
}
func (c *Client) handlePingCommand(*command) error {
return sendPongCommand(c.conn)
}
func (c *Client) handlePartitionedMetadataResponseCommand(cmd *command) error {
return c.handleRequestCallback(cmd)
}
func (c *Client) handleLastMessageIDResponse(cmd *command) error {
return c.handleRequestCallback(cmd)
}
func (c *Client) handleGetTopicsOfNamespaceResponse(cmd *command) error {
return c.handleRequestCallback(cmd)
}
func sendConnectCommand(c clientConn) error {
base := &pb.BaseCommand{
Type: pb.BaseCommand_CONNECT.Enum(),
Connect: &pb.CommandConnect{
ClientVersion: proto.String("Pulsar Go " + libraryVersion),
AuthMethodName: proto.String(""),
ProtocolVersion: proto.Int32(protocolVersion),
},
}
return c.WriteCommand(base, nil)
}
func sendPongCommand(c clientConn) error {
base := &pb.BaseCommand{
Type: pb.BaseCommand_PONG.Enum(),
Pong: &pb.CommandPong{},
}
return c.WriteCommand(base, nil)
}
func newCloseConsumerCommand(consumerID, requestID uint64) *pb.BaseCommand {
return &pb.BaseCommand{
Type: pb.BaseCommand_CLOSE_CONSUMER.Enum(),
CloseConsumer: &pb.CommandCloseConsumer{
ConsumerId: proto.Uint64(consumerID),
RequestId: proto.Uint64(requestID),
},
}
}
func newCloseProducerCommand(producerID, requestID uint64) *pb.BaseCommand {
return &pb.BaseCommand{
Type: pb.BaseCommand_CLOSE_PRODUCER.Enum(),
CloseProducer: &pb.CommandCloseProducer{
ProducerId: proto.Uint64(producerID),
RequestId: proto.Uint64(requestID),
},
}
}
func newGetTopicsOfNamespaceCommand(requestID uint64, namespace string) *pb.BaseCommand {
return &pb.BaseCommand{
Type: pb.BaseCommand_GET_TOPICS_OF_NAMESPACE.Enum(),
GetTopicsOfNamespace: &pb.CommandGetTopicsOfNamespace{
RequestId: proto.Uint64(requestID),
Namespace: proto.String(publicTenant + "/" + namespace),
Mode: pb.CommandGetTopicsOfNamespace_PERSISTENT.Enum(),
},
}
}
func newGetLastMessageIDCommand(consumerID, requestID uint64) *pb.BaseCommand {
return &pb.BaseCommand{
Type: pb.BaseCommand_GET_LAST_MESSAGE_ID.Enum(),
GetLastMessageId: &pb.CommandGetLastMessageId{
ConsumerId: proto.Uint64(consumerID),
RequestId: proto.Uint64(requestID),
},
}
}
// The server sent a close consumer command. assign the Consumer a new ID
// and reconnect it.
func (c *Client) handleCloseConsumerCommand(base *command) error {
cmd := base.CloseConsumer
consumerID := cmd.GetConsumerId()
cons, ok := c.consumers.get(consumerID)
if !ok {
return fmt.Errorf("consumer %d not found", consumerID)
}
c.consumers.delete(consumerID)
newID := c.consumers.newID()
c.consumers.add(newID, cons)
cons.clearReceiverQueue()
cons.reset(newID)
if cons.multi != nil {
cons.multi.changeConsumerID(cons, consumerID, newID)
}
go c.topicLookup(cons.topic, cons.topicLookupFinished)
return nil
}