2019-11-18 20:33:15 +03:00
|
|
|
package sarama
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type protocolBody interface {
|
|
|
|
encoder
|
|
|
|
versionedDecoder
|
|
|
|
key() int16
|
|
|
|
version() int16
|
2020-06-25 00:20:22 +03:00
|
|
|
headerVersion() int16
|
2019-11-18 20:33:15 +03:00
|
|
|
requiredVersion() KafkaVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
correlationID int32
|
|
|
|
clientID string
|
|
|
|
body protocolBody
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:20:22 +03:00
|
|
|
func (r *request) encode(pe packetEncoder) error {
|
2019-11-18 20:33:15 +03:00
|
|
|
pe.push(&lengthField{})
|
|
|
|
pe.putInt16(r.body.key())
|
|
|
|
pe.putInt16(r.body.version())
|
|
|
|
pe.putInt32(r.correlationID)
|
2020-06-25 00:20:22 +03:00
|
|
|
|
|
|
|
if r.body.headerVersion() >= 1 {
|
|
|
|
err := pe.putString(r.clientID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
|
|
|
if r.body.headerVersion() >= 2 {
|
|
|
|
// we don't use tag headers at the moment so we just put an array length of 0
|
|
|
|
pe.putUVarint(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.body.encode(pe)
|
2019-11-18 20:33:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
return pe.pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) decode(pd packetDecoder) (err error) {
|
2020-06-25 00:20:22 +03:00
|
|
|
key, err := pd.getInt16()
|
|
|
|
if err != nil {
|
2019-11-18 20:33:15 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
|
|
|
version, err := pd.getInt16()
|
|
|
|
if err != nil {
|
2019-11-18 20:33:15 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
|
|
|
r.correlationID, err = pd.getInt32()
|
|
|
|
if err != nil {
|
2019-11-18 20:33:15 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
r.clientID, err = pd.getString()
|
2020-06-25 00:20:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 20:33:15 +03:00
|
|
|
|
|
|
|
r.body = allocateBody(key, version)
|
|
|
|
if r.body == nil {
|
|
|
|
return PacketDecodingError{fmt.Sprintf("unknown request key (%d)", key)}
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
|
|
|
if r.body.headerVersion() >= 2 {
|
|
|
|
// tagged field
|
|
|
|
_, err = pd.getUVarint()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
return r.body.decode(pd, version)
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:20:22 +03:00
|
|
|
func decodeRequest(r io.Reader) (*request, int, error) {
|
|
|
|
var (
|
|
|
|
bytesRead int
|
|
|
|
lengthBytes = make([]byte, 4)
|
|
|
|
)
|
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
if _, err := io.ReadFull(r, lengthBytes); err != nil {
|
|
|
|
return nil, bytesRead, err
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:20:22 +03:00
|
|
|
bytesRead += len(lengthBytes)
|
2019-11-18 20:33:15 +03:00
|
|
|
length := int32(binary.BigEndian.Uint32(lengthBytes))
|
2020-06-25 00:20:22 +03:00
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
if length <= 4 || length > MaxRequestSize {
|
|
|
|
return nil, bytesRead, PacketDecodingError{fmt.Sprintf("message of length %d too large or too small", length)}
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedReq := make([]byte, length)
|
|
|
|
if _, err := io.ReadFull(r, encodedReq); err != nil {
|
|
|
|
return nil, bytesRead, err
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
bytesRead += len(encodedReq)
|
|
|
|
|
2020-06-25 00:20:22 +03:00
|
|
|
req := &request{}
|
2019-11-18 20:33:15 +03:00
|
|
|
if err := decode(encodedReq, req); err != nil {
|
|
|
|
return nil, bytesRead, err
|
|
|
|
}
|
2020-06-25 00:20:22 +03:00
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
return req, bytesRead, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func allocateBody(key, version int16) protocolBody {
|
|
|
|
switch key {
|
|
|
|
case 0:
|
|
|
|
return &ProduceRequest{}
|
|
|
|
case 1:
|
2020-06-25 00:20:22 +03:00
|
|
|
return &FetchRequest{Version: version}
|
2019-11-18 20:33:15 +03:00
|
|
|
case 2:
|
|
|
|
return &OffsetRequest{Version: version}
|
|
|
|
case 3:
|
|
|
|
return &MetadataRequest{}
|
|
|
|
case 8:
|
|
|
|
return &OffsetCommitRequest{Version: version}
|
|
|
|
case 9:
|
|
|
|
return &OffsetFetchRequest{}
|
|
|
|
case 10:
|
2020-06-25 00:20:22 +03:00
|
|
|
return &FindCoordinatorRequest{}
|
2019-11-18 20:33:15 +03:00
|
|
|
case 11:
|
|
|
|
return &JoinGroupRequest{}
|
|
|
|
case 12:
|
|
|
|
return &HeartbeatRequest{}
|
|
|
|
case 13:
|
|
|
|
return &LeaveGroupRequest{}
|
|
|
|
case 14:
|
|
|
|
return &SyncGroupRequest{}
|
|
|
|
case 15:
|
|
|
|
return &DescribeGroupsRequest{}
|
|
|
|
case 16:
|
|
|
|
return &ListGroupsRequest{}
|
|
|
|
case 17:
|
|
|
|
return &SaslHandshakeRequest{}
|
|
|
|
case 18:
|
|
|
|
return &ApiVersionsRequest{}
|
2020-06-25 00:20:22 +03:00
|
|
|
case 19:
|
|
|
|
return &CreateTopicsRequest{}
|
|
|
|
case 20:
|
|
|
|
return &DeleteTopicsRequest{}
|
|
|
|
case 21:
|
|
|
|
return &DeleteRecordsRequest{}
|
|
|
|
case 22:
|
|
|
|
return &InitProducerIDRequest{}
|
|
|
|
case 24:
|
|
|
|
return &AddPartitionsToTxnRequest{}
|
|
|
|
case 25:
|
|
|
|
return &AddOffsetsToTxnRequest{}
|
|
|
|
case 26:
|
|
|
|
return &EndTxnRequest{}
|
|
|
|
case 28:
|
|
|
|
return &TxnOffsetCommitRequest{}
|
|
|
|
case 29:
|
|
|
|
return &DescribeAclsRequest{}
|
|
|
|
case 30:
|
|
|
|
return &CreateAclsRequest{}
|
|
|
|
case 31:
|
|
|
|
return &DeleteAclsRequest{}
|
|
|
|
case 32:
|
|
|
|
return &DescribeConfigsRequest{}
|
|
|
|
case 33:
|
|
|
|
return &AlterConfigsRequest{}
|
|
|
|
case 35:
|
|
|
|
return &DescribeLogDirsRequest{}
|
|
|
|
case 36:
|
|
|
|
return &SaslAuthenticateRequest{}
|
|
|
|
case 37:
|
|
|
|
return &CreatePartitionsRequest{}
|
|
|
|
case 42:
|
|
|
|
return &DeleteGroupsRequest{}
|
|
|
|
case 45:
|
|
|
|
return &AlterPartitionReassignmentsRequest{}
|
|
|
|
case 46:
|
|
|
|
return &ListPartitionReassignmentsRequest{}
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|