-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
584 lines (501 loc) · 16.8 KB
/
handler.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package dekaf
import (
"bytes"
"context"
"errors"
"io"
"log"
"math"
"sync"
"time"
"github.com/estuary/dekaf/pkg/models/inmem"
"github.com/estuary/dekaf/protocol"
)
// Kafka Protocol Specification: https://kafka.apache.org/protocol
// Kafka API Keys and Request/Response definitions: https://kafka.apache.org/protocol#protocol_api_keys
const (
defaultMaxMessagesPerTopic = 10
defaultMessageWaitDeadline = 5 * time.Second
memberGroupIDSuffix = "-00000000-0000-0000-0000-000000000000"
)
// ClusterID we will use when talking to clients.
var ClusterID = "dekafclusterid"
// RecordsAvailableFn allows for controlling the number of available records the server should
// emulate. In a typical case, the function could return a larger and larger value based on the
// passage of time since initialization.
type RecordsAvailableFn func() int64
// Config defines the handler config
type Config struct {
// The Host we should tell Kafka clients to connect to.
AdvertiseHost string
// The Port we should tell Kafka clients to connect to.
AdvertisePort int
// The Port the server should listen on. AdvertisePort may be different than ListenPort when
// running behind a proxy, etc.
ListenPort int
// The maximum number of messages we will provide per topic.
// Defaults to 10 if not set.
MaxMessagesPerTopic int
// How long to wait for messages from the provider.
// The config value will take precedence followed by the client request time
// and finally if neither are set, will default to 5 seconds.
MessageWaitDeadline time.Duration
// Debug dumps message request/response.
Debug bool
// RecordsAvailable returns the number of available records the server should emulate.
RecordsAvailable RecordsAvailableFn
// Offsets provides a means for the handler to store offsets for a given
// topic/partition/consumer group.
Offsets OffsetStorer
// Limited API configures the handler to respond to a minimal set of requests, which represent
// the bare minimum to enable a standalone consumer that will manage its own offsets.
LimitedAPI bool
}
// A MessageProvider function is used to provide messages for a topic. The handler will request
// a message at startOffset. The MessageProvider should return a message offset, payload and error
// to the request. If there are no more messages return io.EOF for the error. This function may block
// up until the provided context.Context cancels in which case it should return io.EOF.
type MessageProvider func(ctx context.Context, startOffset int64) (int64, []byte, error)
type OffsetStorer interface {
PutOffset(topic string, group string, partition int, offset int) error
GetOffset(topic string, group string, partition int) int
}
// Handler configuration.
type Handler struct {
config Config
topics map[string]MessageProvider
sync.RWMutex
}
func NewHandler(config Config) (*Handler, error) {
if err := config.validate(); err != nil {
return nil, err
}
var h = &Handler{
config: config,
topics: make(map[string]MessageProvider),
}
// Handle defaults for unset values.
if h.config.MaxMessagesPerTopic <= 0 {
h.config.MaxMessagesPerTopic = defaultMaxMessagesPerTopic
}
return h, nil
}
// Validates configuration.
func (c *Config) validate() error {
if c.AdvertiseHost == "" || c.AdvertisePort == 0 || c.ListenPort == 0 {
return errors.New("invalid config")
}
if c.Offsets == nil {
c.Offsets = &inmem.OffsetStore{}
}
return nil
}
// AddTopic adds a new topic to the server and registers the MessageProvider with that topic.
func (h *Handler) AddTopic(name string, mp MessageProvider) {
h.Lock()
h.topics[name] = mp
h.Unlock()
}
func (h *Handler) logDisallowed(reqCtx *Context) {
var shouldLog bool
switch reqCtx.req.(type) {
case *protocol.OffsetCommitRequest:
shouldLog = true
case *protocol.JoinGroupRequest:
shouldLog = true
case *protocol.HeartbeatRequest:
shouldLog = true
case *protocol.LeaveGroupRequest:
shouldLog = true
case *protocol.SyncGroupRequest:
shouldLog = true
}
if shouldLog {
log.Println("***********************************************************")
log.Printf("DISALLOWED REQUEST: %#v", reqCtx.req)
log.Println("***********************************************************")
}
}
func (h *Handler) HandleReq(ctx context.Context, reqCtx *Context) protocol.ResponseBody {
// Log receiving any requests which should not be allowed if using the "limited" API.
if h.config.LimitedAPI {
h.logDisallowed(reqCtx)
}
var res protocol.ResponseBody
switch req := reqCtx.req.(type) {
case *protocol.FetchRequest:
res = h.handleFetch(reqCtx, req)
case *protocol.OffsetsRequest:
res = h.handleOffsets(reqCtx, req)
case *protocol.MetadataRequest:
res = h.handleMetadata(reqCtx, req)
case *protocol.OffsetCommitRequest:
res = h.handleOffsetCommit(reqCtx, req)
case *protocol.OffsetFetchRequest:
res = h.handleOffsetFetch(reqCtx, req)
case *protocol.FindCoordinatorRequest:
res = h.handleFindCoordinator(reqCtx, req)
case *protocol.JoinGroupRequest:
res = h.handleJoinGroup(reqCtx, req)
case *protocol.HeartbeatRequest:
res = h.handleHeartbeat(reqCtx, req)
case *protocol.LeaveGroupRequest:
res = h.handleLeaveGroup(reqCtx, req)
case *protocol.SyncGroupRequest:
res = h.handleSyncGroup(reqCtx, req)
case *protocol.APIVersionsRequest:
res = h.handleAPIVersions(reqCtx, req)
default:
log.Println("***********************************************************")
log.Printf("UNHANDLED REQUEST: %#v", req)
log.Println("***********************************************************")
return nil
}
if h.config.Debug {
log.Println("-----------------------------------------")
log.Printf("REQ: %#v", reqCtx.req)
log.Printf("RES: %#v", res)
}
return res
}
// Shutdown the handler.
func (h *Handler) Shutdown() error {
return nil
}
// API Versions request sent by server to see what API's are available.
func (h *Handler) handleAPIVersions(ctx *Context, req *protocol.APIVersionsRequest) *protocol.APIVersionsResponse {
vs := protocol.APIVersions
if h.config.LimitedAPI {
vs = protocol.APIVersionsLimited
}
return &protocol.APIVersionsResponse{
APIVersion: req.APIVersion,
ErrorCode: 0,
APIVersions: vs,
ThrottleTime: 0,
}
}
// Metadata request gets info about topics available and the brokers for the topics.
func (h *Handler) handleMetadata(ctx *Context, req *protocol.MetadataRequest) *protocol.MetadataResponse {
h.RLock()
defer h.RUnlock()
// For the case of no topics specified in the request, the protocol requires returning
// information about ALL the topics.
if len(req.Topics) == 0 {
for t := range h.topics {
req.Topics = append(req.Topics, t)
}
}
var topicMetadata []*protocol.TopicMetadata
for _, topicName := range req.Topics {
if _, ok := h.topics[topicName]; !ok {
continue
}
topicMetadata = append(topicMetadata, &protocol.TopicMetadata{
Topic: topicName,
TopicErrorCode: 0,
PartitionMetadata: []*protocol.PartitionMetadata{
{
PartitionErrorCode: 0,
PartitionID: 0,
Leader: 1,
Replicas: []int32{1},
ISR: []int32{1},
},
},
})
}
return &protocol.MetadataResponse{
APIVersion: req.APIVersion,
Brokers: []*protocol.Broker{
{
NodeID: 1,
Host: h.config.AdvertiseHost,
Port: int32(h.config.AdvertisePort),
},
},
ControllerID: 1,
ClusterID: &ClusterID,
TopicMetadata: topicMetadata,
}
}
// Offset request gets info about topic available messages and offsets.
func (h *Handler) handleOffsets(ctx *Context, req *protocol.OffsetsRequest) *protocol.OffsetsResponse {
h.RLock()
defer h.RUnlock()
var offsetRespones []*protocol.OffsetResponse
for _, reqTopic := range req.Topics {
if _, ok := h.topics[reqTopic.Topic]; !ok {
continue
}
var offset int64
var ts time.Time
if reqTopic.Partitions[0].Timestamp == -2 {
// Earliest = 0/Epoch
offset = 0
ts = time.Unix(0, 0)
} else if reqTopic.Partitions[0].Timestamp == -1 {
// Latest = all the data up until now
if h.config.RecordsAvailable != nil {
// Note: The returned offset is the "log end offset" (the offset of the next message
// that would be appended) and offsets are zero-indexed.
offset = h.config.RecordsAvailable()
ts = time.Now()
} else {
// TODO: Handle cases of a specific timestamp being provided.
offset = math.MaxInt64 // Unlimited data
}
}
offsetRespones = append(offsetRespones, &protocol.OffsetResponse{
Topic: reqTopic.Topic,
PartitionResponses: []*protocol.PartitionResponse{
{
Partition: 0,
ErrorCode: 0,
Timestamp: ts,
Offset: offset,
Offsets: []int64{offset},
},
},
})
}
return &protocol.OffsetsResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
Responses: offsetRespones,
}
}
// OffsetFetch returns the last committed offset value for the topic.
func (h *Handler) handleOffsetFetch(ctx *Context, req *protocol.OffsetFetchRequest) *protocol.OffsetFetchResponse {
var emptyString string
var offsetFetchTopicResponse []protocol.OffsetFetchTopicResponse
for _, reqTopic := range req.Topics {
if _, ok := h.topics[reqTopic.Topic]; !ok {
continue
}
topicPartitions := []protocol.OffsetFetchPartition{}
for _, p := range reqTopic.Partitions {
got := h.config.Offsets.GetOffset(reqTopic.Topic, req.GroupID, int(p))
topicPartitions = append(topicPartitions, protocol.OffsetFetchPartition{
Partition: p,
ErrorCode: 0,
Metadata: &emptyString,
Offset: int64(got),
})
}
offsetFetchTopicResponse = append(offsetFetchTopicResponse, protocol.OffsetFetchTopicResponse{
Topic: reqTopic.Topic,
Partitions: topicPartitions,
})
}
return &protocol.OffsetFetchResponse{
APIVersion: req.APIVersion,
Responses: offsetFetchTopicResponse,
}
}
// OffsetCommit sets the last committed offset value.
func (h *Handler) handleOffsetCommit(ctx *Context, req *protocol.OffsetCommitRequest) *protocol.OffsetCommitResponse {
var offsetCommitTopicResponse []protocol.OffsetCommitTopicResponse
for _, reqTopic := range req.Topics {
if _, ok := h.topics[reqTopic.Topic]; !ok {
continue
}
topicPartitions := []protocol.OffsetCommitPartitionResponse{}
for _, p := range reqTopic.Partitions {
// TODO: Handle errors.
_ = h.config.Offsets.PutOffset(reqTopic.Topic, req.GroupID, int(p.Partition), int(p.Offset))
topicPartitions = append(topicPartitions, protocol.OffsetCommitPartitionResponse{
Partition: p.Partition,
ErrorCode: 0,
})
}
offsetCommitTopicResponse = append(offsetCommitTopicResponse, protocol.OffsetCommitTopicResponse{
Topic: reqTopic.Topic,
PartitionResponses: topicPartitions,
})
}
return &protocol.OffsetCommitResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
Responses: offsetCommitTopicResponse,
}
}
// FindCoordinator message gets coordinator/host information.
func (h *Handler) handleFindCoordinator(ctx *Context, req *protocol.FindCoordinatorRequest) *protocol.FindCoordinatorResponse {
return &protocol.FindCoordinatorResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
ErrorCode: 0,
ErrorMessage: nil,
Coordinator: protocol.Coordinator{
NodeID: 1,
Host: h.config.AdvertiseHost,
Port: int32(h.config.AdvertisePort),
},
}
}
// Join Group asks to join a group.
func (h *Handler) handleJoinGroup(ctx *Context, req *protocol.JoinGroupRequest) *protocol.JoinGroupResponse {
var protoMetadata []byte
if len(req.GroupProtocols) > 0 {
protoMetadata = req.GroupProtocols[0].ProtocolMetadata
}
return &protocol.JoinGroupResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
ErrorCode: 0,
GenerationID: 1,
GroupProtocol: "range",
LeaderID: ctx.header.ClientID + memberGroupIDSuffix,
MemberID: ctx.header.ClientID + memberGroupIDSuffix,
Members: []protocol.Member{
{
MemberID: ctx.header.ClientID + memberGroupIDSuffix,
MemberMetadata: protoMetadata,
},
},
}
}
// Sync Group asks to sync a group which basically will tell the client that it is the main consumer for the group.
func (h *Handler) handleSyncGroup(ctx *Context, req *protocol.SyncGroupRequest) *protocol.SyncGroupResponse {
var memberAssignment []byte
if len(req.GroupAssignments) > 0 {
memberAssignment = req.GroupAssignments[0].MemberAssignment
}
return &protocol.SyncGroupResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
ErrorCode: 0,
MemberAssignment: memberAssignment,
}
}
// Heartbeat asks to heartbeat a group.
func (h *Handler) handleHeartbeat(ctx *Context, req *protocol.HeartbeatRequest) *protocol.HeartbeatResponse {
return &protocol.HeartbeatResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
ErrorCode: 0,
}
}
// Leave Group asks to leave a group.
func (h *Handler) handleLeaveGroup(ctx *Context, req *protocol.LeaveGroupRequest) *protocol.LeaveGroupResponse {
return &protocol.LeaveGroupResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
ErrorCode: 0,
}
}
// Fetch data handles returning data for the requested topics.
func (h *Handler) handleFetch(ctx *Context, req *protocol.FetchRequest) *protocol.FetchResponse {
h.RLock()
defer h.RUnlock()
// Setup the deadline to respond.
var deadline = h.config.MessageWaitDeadline
if deadline == 0 {
if req.MaxWaitTime != 0 {
deadline = req.MaxWaitTime
} else {
deadline = defaultMessageWaitDeadline
}
}
var deadlineCtx, deadlineCancel = context.WithDeadline(ctx.parent, time.Now().Add(deadline))
defer deadlineCancel()
numRecordsAvailable := int64(math.MaxInt64)
if h.config.RecordsAvailable != nil {
numRecordsAvailable = h.config.RecordsAvailable()
}
var responseChan = make(chan *protocol.FetchTopicResponse)
for _, fetchTopic := range req.Topics {
// Process all topics in parallel.
go func(fetchTopic *protocol.FetchTopic) {
// See if we have that topic message provider.
mp, ok := h.topics[fetchTopic.Topic]
if !ok {
responseChan <- nil // Not found, return nothing.
return
}
// Make sure we're requesting the zero partition and get the fetchOffset.
if len(fetchTopic.Partitions) < 1 || fetchTopic.Partitions[0].Partition != 0 {
responseChan <- nil // Invalid request, return nothing.
log.Printf("invalid partition request: %d", fetchTopic.Partitions[0].Partition)
return
}
// Build RecordSet to respond to this topic.
var buf bytes.Buffer
startingOffset := fetchTopic.Partitions[0].FetchOffset
for x := startingOffset; x < startingOffset+int64(h.config.MaxMessagesPerTopic); x++ {
// If we've exceeded the offsets for which there are messages available, there are
// no more available messages. Offsets are zero-indexed, so a requested offset of 0
// with 0 records available means no records should be returned.
if x >= numRecordsAvailable {
break
}
offset, data, err := mp(deadlineCtx, x)
if err == io.EOF {
// No more available messages.
break
} else if err != nil {
log.Printf("topic %s message provider error: %v", fetchTopic.Topic, err)
}
ms := protocol.MessageSet{
Offset: offset,
Message: &protocol.Message{
Value: data,
},
}
// Use the v1 message format for Fetch v2 or later. Fetch v2 supports either the v0
// or v1 message format, but for simplicitly we will always simulate a v1 message on
// v2+ protocols. See
// https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-Messagesets.
if req.APIVersion >= 2 {
// 0 is for v0 message format; 1 is for v1 message format.
ms.Message.MagicByte = 1
ms.Message.Timestamp = time.Now()
// Set log.message.timestamp.type = LogAppendTime, see
// https://kafka.apache.org/documentation/#messageset
ms.Message.Attributes = 0b1000
}
b, err := protocol.Encode(&ms)
if err != nil {
panic(err)
}
if _, err = buf.Write(b); err != nil {
panic(err)
}
}
// No messages means an empty record set, which needs to be encoded with a length of 0
// and not -1. Use an empty byte array here instead of nil to ensure correct encoding.
rs := buf.Bytes()
if rs == nil {
rs = []byte{}
}
responseChan <- &protocol.FetchTopicResponse{
Topic: fetchTopic.Topic,
PartitionResponses: []*protocol.FetchPartitionResponse{
{
Partition: 0,
ErrorCode: 0,
HighWatermark: numRecordsAvailable - 1,
LastStableOffset: numRecordsAvailable - 1,
AbortedTransactions: nil,
RecordSet: rs,
},
},
}
}(fetchTopic)
}
// Get the topic responses and append them to the message.
var responses []*protocol.FetchTopicResponse
for x := 0; x < len(req.Topics); x++ {
response := <-responseChan
if response == nil {
continue
}
responses = append(responses, response)
}
return &protocol.FetchResponse{
APIVersion: req.APIVersion,
ThrottleTime: 0,
Responses: responses,
}
}