-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
642 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package rawproduce | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/segmentio/kafka-go/protocol" | ||
"github.com/segmentio/kafka-go/protocol/produce" | ||
) | ||
|
||
func init() { | ||
// Register a type override so that raw produce requests will be encoded with the correct type. | ||
req := &Request{} | ||
protocol.RegisterOverride(req, &produce.Response{}, req.TypeKey()) | ||
} | ||
|
||
type Request struct { | ||
TransactionalID string `kafka:"min=v3,max=v8,nullable"` | ||
Acks int16 `kafka:"min=v0,max=v8"` | ||
Timeout int32 `kafka:"min=v0,max=v8"` | ||
Topics []RequestTopic `kafka:"min=v0,max=v8"` | ||
} | ||
|
||
func (r *Request) ApiKey() protocol.ApiKey { return protocol.Produce } | ||
|
||
func (r *Request) TypeKey() protocol.OverrideTypeKey { return protocol.RawProduceOverride } | ||
|
||
func (r *Request) Broker(cluster protocol.Cluster) (protocol.Broker, error) { | ||
broker := protocol.Broker{ID: -1} | ||
|
||
for i := range r.Topics { | ||
t := &r.Topics[i] | ||
|
||
topic, ok := cluster.Topics[t.Topic] | ||
if !ok { | ||
return broker, NewError(protocol.NewErrNoTopic(t.Topic)) | ||
} | ||
|
||
for j := range t.Partitions { | ||
p := &t.Partitions[j] | ||
|
||
partition, ok := topic.Partitions[p.Partition] | ||
if !ok { | ||
return broker, NewError(protocol.NewErrNoPartition(t.Topic, p.Partition)) | ||
} | ||
|
||
if b, ok := cluster.Brokers[partition.Leader]; !ok { | ||
return broker, NewError(protocol.NewErrNoLeader(t.Topic, p.Partition)) | ||
} else if broker.ID < 0 { | ||
broker = b | ||
} else if b.ID != broker.ID { | ||
return broker, NewError(fmt.Errorf("mismatching leaders (%d!=%d)", b.ID, broker.ID)) | ||
} | ||
} | ||
} | ||
|
||
return broker, nil | ||
} | ||
|
||
func (r *Request) HasResponse() bool { | ||
return r.Acks != 0 | ||
} | ||
|
||
type RequestTopic struct { | ||
Topic string `kafka:"min=v0,max=v8"` | ||
Partitions []RequestPartition `kafka:"min=v0,max=v8"` | ||
} | ||
|
||
type RequestPartition struct { | ||
Partition int32 `kafka:"min=v0,max=v8"` | ||
RecordSet protocol.RawRecordSet `kafka:"min=v0,max=v8"` | ||
} | ||
|
||
var ( | ||
_ protocol.BrokerMessage = (*Request)(nil) | ||
) | ||
|
||
type Error struct { | ||
Err error | ||
} | ||
|
||
func NewError(err error) *Error { | ||
return &Error{Err: err} | ||
} | ||
|
||
func (e *Error) Error() string { | ||
return fmt.Sprintf("fetch request error: %v", e.Err) | ||
} | ||
|
||
func (e *Error) Unwrap() error { | ||
return e.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
package rawproduce_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
|
||
"github.com/segmentio/kafka-go/protocol" | ||
"github.com/segmentio/kafka-go/protocol/prototest" | ||
"github.com/segmentio/kafka-go/protocol/rawproduce" | ||
) | ||
|
||
const ( | ||
v0 = 0 | ||
v3 = 3 | ||
v5 = 5 | ||
) | ||
|
||
func TestRawProduceRequest(t *testing.T) { | ||
t0 := time.Now().Truncate(time.Millisecond) | ||
t1 := t0.Add(1 * time.Millisecond) | ||
t2 := t0.Add(2 * time.Millisecond) | ||
|
||
prototest.TestRequestWithOverride(t, v0, &rawproduce.Request{ | ||
Acks: 1, | ||
Timeout: 500, | ||
Topics: []rawproduce.RequestTopic{ | ||
{ | ||
Topic: "topic-1", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 0, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil}, | ||
), 1, 0), | ||
}, | ||
{ | ||
Partition: 1, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 1, 0), | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
Topic: "topic-2", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 0, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 1, protocol.Gzip), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
prototest.TestRequestWithOverride(t, v3, &rawproduce.Request{ | ||
TransactionalID: "1234", | ||
Acks: 1, | ||
Timeout: 500, | ||
Topics: []rawproduce.RequestTopic{ | ||
{ | ||
Topic: "topic-1", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 0, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil}, | ||
), 1, 0), | ||
}, | ||
{ | ||
Partition: 1, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 1, 0), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
headers := []protocol.Header{ | ||
{Key: "key-1", Value: []byte("value-1")}, | ||
{Key: "key-2", Value: []byte("value-2")}, | ||
{Key: "key-3", Value: []byte("value-3")}, | ||
} | ||
|
||
prototest.TestRequestWithOverride(t, v5, &rawproduce.Request{ | ||
TransactionalID: "1234", | ||
Acks: 1, | ||
Timeout: 500, | ||
Topics: []rawproduce.RequestTopic{ | ||
{ | ||
Topic: "topic-1", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 1, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 2, 0), | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
Topic: "topic-2", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 1, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 2, protocol.Snappy), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func NewRawRecordSet(reader protocol.RecordReader, version int8, attr protocol.Attributes) protocol.RawRecordSet { | ||
rs := protocol.RecordSet{Version: version, Attributes: attr, Records: reader} | ||
buf := &bytes.Buffer{} | ||
rs.WriteTo(buf) | ||
|
||
return protocol.RawRecordSet{ | ||
Reader: buf, | ||
} | ||
} | ||
|
||
func BenchmarkProduceRequest(b *testing.B) { | ||
t0 := time.Now().Truncate(time.Millisecond) | ||
t1 := t0.Add(1 * time.Millisecond) | ||
t2 := t0.Add(2 * time.Millisecond) | ||
|
||
prototest.BenchmarkRequest(b, v3, &rawproduce.Request{ | ||
TransactionalID: "1234", | ||
Acks: 1, | ||
Timeout: 500, | ||
Topics: []rawproduce.RequestTopic{ | ||
{ | ||
Topic: "topic-1", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 0, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil}, | ||
), 1, 0), | ||
}, | ||
{ | ||
Partition: 1, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 1, 0), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
headers := []protocol.Header{ | ||
{Key: "key-1", Value: []byte("value-1")}, | ||
{Key: "key-2", Value: []byte("value-2")}, | ||
{Key: "key-3", Value: []byte("value-3")}, | ||
} | ||
|
||
prototest.BenchmarkRequest(b, v5, &rawproduce.Request{ | ||
TransactionalID: "1234", | ||
Acks: 1, | ||
Timeout: 500, | ||
Topics: []rawproduce.RequestTopic{ | ||
{ | ||
Topic: "topic-1", | ||
Partitions: []rawproduce.RequestPartition{ | ||
{ | ||
Partition: 1, | ||
RecordSet: NewRawRecordSet(protocol.NewRecordReader( | ||
protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers}, | ||
protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, | ||
protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, | ||
), 2, 0), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.