github.com/rbisecke/kafka-go@v0.4.27/protocol/fetch/fetch.go (about)

     1  package fetch
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rbisecke/kafka-go/protocol"
     7  )
     8  
     9  func init() {
    10  	protocol.Register(&Request{}, &Response{})
    11  }
    12  
    13  type Request struct {
    14  	ReplicaID       int32                   `kafka:"min=v0,max=v11"`
    15  	MaxWaitTime     int32                   `kafka:"min=v0,max=v11"`
    16  	MinBytes        int32                   `kafka:"min=v0,max=v11"`
    17  	MaxBytes        int32                   `kafka:"min=v3,max=v11"`
    18  	IsolationLevel  int8                    `kafka:"min=v4,max=v11"`
    19  	SessionID       int32                   `kafka:"min=v7,max=v11"`
    20  	SessionEpoch    int32                   `kafka:"min=v7,max=v11"`
    21  	Topics          []RequestTopic          `kafka:"min=v0,max=v11"`
    22  	ForgottenTopics []RequestForgottenTopic `kafka:"min=v7,max=v11"`
    23  	RackID          string                  `kafka:"min=v11,max=v11"`
    24  }
    25  
    26  func (r *Request) ApiKey() protocol.ApiKey { return protocol.Fetch }
    27  
    28  func (r *Request) Broker(cluster protocol.Cluster) (protocol.Broker, error) {
    29  	broker := protocol.Broker{ID: -1}
    30  
    31  	for i := range r.Topics {
    32  		t := &r.Topics[i]
    33  
    34  		topic, ok := cluster.Topics[t.Topic]
    35  		if !ok {
    36  			return broker, NewError(protocol.NewErrNoTopic(t.Topic))
    37  		}
    38  
    39  		for j := range t.Partitions {
    40  			p := &t.Partitions[j]
    41  
    42  			partition, ok := topic.Partitions[p.Partition]
    43  			if !ok {
    44  				return broker, NewError(protocol.NewErrNoPartition(t.Topic, p.Partition))
    45  			}
    46  
    47  			if b, ok := cluster.Brokers[partition.Leader]; !ok {
    48  				return broker, NewError(protocol.NewErrNoLeader(t.Topic, p.Partition))
    49  			} else if broker.ID < 0 {
    50  				broker = b
    51  			} else if b.ID != broker.ID {
    52  				return broker, NewError(fmt.Errorf("mismatching leaders (%d!=%d)", b.ID, broker.ID))
    53  			}
    54  		}
    55  	}
    56  
    57  	return broker, nil
    58  }
    59  
    60  type RequestTopic struct {
    61  	Topic      string             `kafka:"min=v0,max=v11"`
    62  	Partitions []RequestPartition `kafka:"min=v0,max=v11"`
    63  }
    64  
    65  type RequestPartition struct {
    66  	Partition          int32 `kafka:"min=v0,max=v11"`
    67  	CurrentLeaderEpoch int32 `kafka:"min=v9,max=v11"`
    68  	FetchOffset        int64 `kafka:"min=v0,max=v11"`
    69  	LogStartOffset     int64 `kafka:"min=v5,max=v11"`
    70  	PartitionMaxBytes  int32 `kafka:"min=v0,max=v11"`
    71  }
    72  
    73  type RequestForgottenTopic struct {
    74  	Topic      string  `kafka:"min=v7,max=v11"`
    75  	Partitions []int32 `kafka:"min=v7,max=v11"`
    76  }
    77  
    78  type Response struct {
    79  	ThrottleTimeMs int32           `kafka:"min=v1,max=v11"`
    80  	ErrorCode      int16           `kafka:"min=v7,max=v11"`
    81  	SessionID      int32           `kafka:"min=v7,max=v11"`
    82  	Topics         []ResponseTopic `kafka:"min=v0,max=v11"`
    83  }
    84  
    85  func (r *Response) ApiKey() protocol.ApiKey { return protocol.Fetch }
    86  
    87  type ResponseTopic struct {
    88  	Topic      string              `kafka:"min=v0,max=v11"`
    89  	Partitions []ResponsePartition `kafka:"min=v0,max=v11"`
    90  }
    91  
    92  type ResponsePartition struct {
    93  	Partition            int32                 `kafka:"min=v0,max=v11"`
    94  	ErrorCode            int16                 `kafka:"min=v0,max=v11"`
    95  	HighWatermark        int64                 `kafka:"min=v0,max=v11"`
    96  	LastStableOffset     int64                 `kafka:"min=v4,max=v11"`
    97  	LogStartOffset       int64                 `kafka:"min=v5,max=v11"`
    98  	AbortedTransactions  []ResponseTransaction `kafka:"min=v4,max=v11"`
    99  	PreferredReadReplica int32                 `kafka:"min=v11,max=v11"`
   100  	RecordSet            protocol.RecordSet    `kafka:"min=v0,max=v11"`
   101  }
   102  
   103  type ResponseTransaction struct {
   104  	ProducerID  int64 `kafka:"min=v4,max=v11"`
   105  	FirstOffset int64 `kafka:"min=v4,max=v11"`
   106  }
   107  
   108  var (
   109  	_ protocol.BrokerMessage = (*Request)(nil)
   110  )
   111  
   112  type Error struct {
   113  	Err error
   114  }
   115  
   116  func NewError(err error) *Error {
   117  	return &Error{Err: err}
   118  }
   119  
   120  func (e *Error) Error() string {
   121  	return fmt.Sprintf("fetch request error: %v", e.Err)
   122  }
   123  
   124  func (e *Error) Unwrap() error {
   125  	return e.Err
   126  }