123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914 |
- package sarama
- import (
- "errors"
- "fmt"
- "math"
- "sync"
- "sync/atomic"
- "time"
- "github.com/rcrowley/go-metrics"
- )
- type ConsumerMessage struct {
- Headers []*RecordHeader
- Timestamp time.Time
- BlockTimestamp time.Time
- Key, Value []byte
- Topic string
- Partition int32
- Offset int64
- }
- type ConsumerError struct {
- Topic string
- Partition int32
- Err error
- }
- func (ce ConsumerError) Error() string {
- return fmt.Sprintf("kafka: error while consuming %s/%d: %s", ce.Topic, ce.Partition, ce.Err)
- }
- type ConsumerErrors []*ConsumerError
- func (ce ConsumerErrors) Error() string {
- return fmt.Sprintf("kafka: %d errors while consuming", len(ce))
- }
- type Consumer interface {
-
-
-
- Topics() ([]string, error)
-
-
- Partitions(topic string) ([]int32, error)
-
-
-
-
- ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error)
-
-
- HighWaterMarks() map[string]map[int32]int64
-
-
- Close() error
- }
- type consumer struct {
- conf *Config
- children map[string]map[int32]*partitionConsumer
- brokerConsumers map[*Broker]*brokerConsumer
- client Client
- lock sync.Mutex
- }
- func NewConsumer(addrs []string, config *Config) (Consumer, error) {
- client, err := NewClient(addrs, config)
- if err != nil {
- return nil, err
- }
- return newConsumer(client)
- }
- func NewConsumerFromClient(client Client) (Consumer, error) {
-
-
- cli := &nopCloserClient{client}
- return newConsumer(cli)
- }
- func newConsumer(client Client) (Consumer, error) {
-
- if client.Closed() {
- return nil, ErrClosedClient
- }
- c := &consumer{
- client: client,
- conf: client.Config(),
- children: make(map[string]map[int32]*partitionConsumer),
- brokerConsumers: make(map[*Broker]*brokerConsumer),
- }
- return c, nil
- }
- func (c *consumer) Close() error {
- return c.client.Close()
- }
- func (c *consumer) Topics() ([]string, error) {
- return c.client.Topics()
- }
- func (c *consumer) Partitions(topic string) ([]int32, error) {
- return c.client.Partitions(topic)
- }
- func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error) {
- child := &partitionConsumer{
- consumer: c,
- conf: c.conf,
- topic: topic,
- partition: partition,
- messages: make(chan *ConsumerMessage, c.conf.ChannelBufferSize),
- errors: make(chan *ConsumerError, c.conf.ChannelBufferSize),
- feeder: make(chan *FetchResponse, 1),
- trigger: make(chan none, 1),
- dying: make(chan none),
- fetchSize: c.conf.Consumer.Fetch.Default,
- }
- if err := child.chooseStartingOffset(offset); err != nil {
- return nil, err
- }
- var leader *Broker
- var err error
- if leader, err = c.client.Leader(child.topic, child.partition); err != nil {
- return nil, err
- }
- if err := c.addChild(child); err != nil {
- return nil, err
- }
- go withRecover(child.dispatcher)
- go withRecover(child.responseFeeder)
- child.broker = c.refBrokerConsumer(leader)
- child.broker.input <- child
- return child, nil
- }
- func (c *consumer) HighWaterMarks() map[string]map[int32]int64 {
- c.lock.Lock()
- defer c.lock.Unlock()
- hwms := make(map[string]map[int32]int64)
- for topic, p := range c.children {
- hwm := make(map[int32]int64, len(p))
- for partition, pc := range p {
- hwm[partition] = pc.HighWaterMarkOffset()
- }
- hwms[topic] = hwm
- }
- return hwms
- }
- func (c *consumer) addChild(child *partitionConsumer) error {
- c.lock.Lock()
- defer c.lock.Unlock()
- topicChildren := c.children[child.topic]
- if topicChildren == nil {
- topicChildren = make(map[int32]*partitionConsumer)
- c.children[child.topic] = topicChildren
- }
- if topicChildren[child.partition] != nil {
- return ConfigurationError("That topic/partition is already being consumed")
- }
- topicChildren[child.partition] = child
- return nil
- }
- func (c *consumer) removeChild(child *partitionConsumer) {
- c.lock.Lock()
- defer c.lock.Unlock()
- delete(c.children[child.topic], child.partition)
- }
- func (c *consumer) refBrokerConsumer(broker *Broker) *brokerConsumer {
- c.lock.Lock()
- defer c.lock.Unlock()
- bc := c.brokerConsumers[broker]
- if bc == nil {
- bc = c.newBrokerConsumer(broker)
- c.brokerConsumers[broker] = bc
- }
- bc.refs++
- return bc
- }
- func (c *consumer) unrefBrokerConsumer(brokerWorker *brokerConsumer) {
- c.lock.Lock()
- defer c.lock.Unlock()
- brokerWorker.refs--
- if brokerWorker.refs == 0 {
- close(brokerWorker.input)
- if c.brokerConsumers[brokerWorker.broker] == brokerWorker {
- delete(c.brokerConsumers, brokerWorker.broker)
- }
- }
- }
- func (c *consumer) abandonBrokerConsumer(brokerWorker *brokerConsumer) {
- c.lock.Lock()
- defer c.lock.Unlock()
- delete(c.brokerConsumers, brokerWorker.broker)
- }
- type PartitionConsumer interface {
-
-
-
-
- AsyncClose()
-
-
-
-
-
- Close() error
-
-
- Messages() <-chan *ConsumerMessage
-
-
-
-
- Errors() <-chan *ConsumerError
-
-
-
- HighWaterMarkOffset() int64
- }
- type partitionConsumer struct {
- highWaterMarkOffset int64
- consumer *consumer
- conf *Config
- broker *brokerConsumer
- messages chan *ConsumerMessage
- errors chan *ConsumerError
- feeder chan *FetchResponse
- trigger, dying chan none
- closeOnce sync.Once
- topic string
- partition int32
- responseResult error
- fetchSize int32
- offset int64
- retries int32
- }
- var errTimedOut = errors.New("timed out feeding messages to the user")
- func (child *partitionConsumer) sendError(err error) {
- cErr := &ConsumerError{
- Topic: child.topic,
- Partition: child.partition,
- Err: err,
- }
- if child.conf.Consumer.Return.Errors {
- child.errors <- cErr
- } else {
- Logger.Println(cErr)
- }
- }
- func (child *partitionConsumer) computeBackoff() time.Duration {
- if child.conf.Consumer.Retry.BackoffFunc != nil {
- retries := atomic.AddInt32(&child.retries, 1)
- return child.conf.Consumer.Retry.BackoffFunc(int(retries))
- }
- return child.conf.Consumer.Retry.Backoff
- }
- func (child *partitionConsumer) dispatcher() {
- for range child.trigger {
- select {
- case <-child.dying:
- close(child.trigger)
- case <-time.After(child.computeBackoff()):
- if child.broker != nil {
- child.consumer.unrefBrokerConsumer(child.broker)
- child.broker = nil
- }
- Logger.Printf("consumer/%s/%d finding new broker\n", child.topic, child.partition)
- if err := child.dispatch(); err != nil {
- child.sendError(err)
- child.trigger <- none{}
- }
- }
- }
- if child.broker != nil {
- child.consumer.unrefBrokerConsumer(child.broker)
- }
- child.consumer.removeChild(child)
- close(child.feeder)
- }
- func (child *partitionConsumer) dispatch() error {
- if err := child.consumer.client.RefreshMetadata(child.topic); err != nil {
- return err
- }
- var leader *Broker
- var err error
- if leader, err = child.consumer.client.Leader(child.topic, child.partition); err != nil {
- return err
- }
- child.broker = child.consumer.refBrokerConsumer(leader)
- child.broker.input <- child
- return nil
- }
- func (child *partitionConsumer) chooseStartingOffset(offset int64) error {
- newestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetNewest)
- if err != nil {
- return err
- }
- oldestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetOldest)
- if err != nil {
- return err
- }
- switch {
- case offset == OffsetNewest:
- child.offset = newestOffset
- case offset == OffsetOldest:
- child.offset = oldestOffset
- case offset >= oldestOffset && offset <= newestOffset:
- child.offset = offset
- default:
- return ErrOffsetOutOfRange
- }
- return nil
- }
- func (child *partitionConsumer) Messages() <-chan *ConsumerMessage {
- return child.messages
- }
- func (child *partitionConsumer) Errors() <-chan *ConsumerError {
- return child.errors
- }
- func (child *partitionConsumer) AsyncClose() {
-
-
-
-
- child.closeOnce.Do(func() {
- close(child.dying)
- })
- }
- func (child *partitionConsumer) Close() error {
- child.AsyncClose()
- var errors ConsumerErrors
- for err := range child.errors {
- errors = append(errors, err)
- }
- if len(errors) > 0 {
- return errors
- }
- return nil
- }
- func (child *partitionConsumer) HighWaterMarkOffset() int64 {
- return atomic.LoadInt64(&child.highWaterMarkOffset)
- }
- func (child *partitionConsumer) responseFeeder() {
- var msgs []*ConsumerMessage
- expiryTicker := time.NewTicker(child.conf.Consumer.MaxProcessingTime)
- firstAttempt := true
- feederLoop:
- for response := range child.feeder {
- msgs, child.responseResult = child.parseResponse(response)
- if child.responseResult == nil {
- atomic.StoreInt32(&child.retries, 0)
- }
- for i, msg := range msgs {
- for _, interceptor := range child.conf.Consumer.Interceptors {
- msg.safelyApplyInterceptor(interceptor)
- }
- messageSelect:
- select {
- case <-child.dying:
- child.broker.acks.Done()
- continue feederLoop
- case child.messages <- msg:
- firstAttempt = true
- case <-expiryTicker.C:
- if !firstAttempt {
- child.responseResult = errTimedOut
- child.broker.acks.Done()
- remainingLoop:
- for _, msg = range msgs[i:] {
- select {
- case child.messages <- msg:
- case <-child.dying:
- break remainingLoop
- }
- }
- child.broker.input <- child
- continue feederLoop
- } else {
-
-
- firstAttempt = false
- goto messageSelect
- }
- }
- }
- child.broker.acks.Done()
- }
- expiryTicker.Stop()
- close(child.messages)
- close(child.errors)
- }
- func (child *partitionConsumer) parseMessages(msgSet *MessageSet) ([]*ConsumerMessage, error) {
- var messages []*ConsumerMessage
- for _, msgBlock := range msgSet.Messages {
- for _, msg := range msgBlock.Messages() {
- offset := msg.Offset
- timestamp := msg.Msg.Timestamp
- if msg.Msg.Version >= 1 {
- baseOffset := msgBlock.Offset - msgBlock.Messages()[len(msgBlock.Messages())-1].Offset
- offset += baseOffset
- if msg.Msg.LogAppendTime {
- timestamp = msgBlock.Msg.Timestamp
- }
- }
- if offset < child.offset {
- continue
- }
- messages = append(messages, &ConsumerMessage{
- Topic: child.topic,
- Partition: child.partition,
- Key: msg.Msg.Key,
- Value: msg.Msg.Value,
- Offset: offset,
- Timestamp: timestamp,
- BlockTimestamp: msgBlock.Msg.Timestamp,
- })
- child.offset = offset + 1
- }
- }
- if len(messages) == 0 {
- child.offset++
- }
- return messages, nil
- }
- func (child *partitionConsumer) parseRecords(batch *RecordBatch) ([]*ConsumerMessage, error) {
- messages := make([]*ConsumerMessage, 0, len(batch.Records))
- for _, rec := range batch.Records {
- offset := batch.FirstOffset + rec.OffsetDelta
- if offset < child.offset {
- continue
- }
- timestamp := batch.FirstTimestamp.Add(rec.TimestampDelta)
- if batch.LogAppendTime {
- timestamp = batch.MaxTimestamp
- }
- messages = append(messages, &ConsumerMessage{
- Topic: child.topic,
- Partition: child.partition,
- Key: rec.Key,
- Value: rec.Value,
- Offset: offset,
- Timestamp: timestamp,
- Headers: rec.Headers,
- })
- child.offset = offset + 1
- }
- if len(messages) == 0 {
- child.offset++
- }
- return messages, nil
- }
- func (child *partitionConsumer) parseResponse(response *FetchResponse) ([]*ConsumerMessage, error) {
- var (
- metricRegistry = child.conf.MetricRegistry
- consumerBatchSizeMetric metrics.Histogram
- )
- if metricRegistry != nil {
- consumerBatchSizeMetric = getOrRegisterHistogram("consumer-batch-size", metricRegistry)
- }
-
- if response.ThrottleTime != time.Duration(0) && len(response.Blocks) == 0 {
- Logger.Printf(
- "consumer/broker/%d FetchResponse throttled %v\n",
- child.broker.broker.ID(), response.ThrottleTime)
- return nil, nil
- }
- block := response.GetBlock(child.topic, child.partition)
- if block == nil {
- return nil, ErrIncompleteResponse
- }
- if block.Err != ErrNoError {
- return nil, block.Err
- }
- nRecs, err := block.numRecords()
- if err != nil {
- return nil, err
- }
- consumerBatchSizeMetric.Update(int64(nRecs))
- if nRecs == 0 {
- partialTrailingMessage, err := block.isPartial()
- if err != nil {
- return nil, err
- }
-
-
- if partialTrailingMessage {
- if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize == child.conf.Consumer.Fetch.Max {
-
- child.sendError(ErrMessageTooLarge)
- child.offset++
- } else {
- child.fetchSize *= 2
-
- if child.fetchSize < 0 {
- child.fetchSize = math.MaxInt32
- }
- if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize > child.conf.Consumer.Fetch.Max {
- child.fetchSize = child.conf.Consumer.Fetch.Max
- }
- }
- }
- return nil, nil
- }
-
- child.fetchSize = child.conf.Consumer.Fetch.Default
- atomic.StoreInt64(&child.highWaterMarkOffset, block.HighWaterMarkOffset)
-
-
-
- abortedProducerIDs := make(map[int64]struct{}, len(block.AbortedTransactions))
- abortedTransactions := block.getAbortedTransactions()
- messages := []*ConsumerMessage{}
- for _, records := range block.RecordsSet {
- switch records.recordsType {
- case legacyRecords:
- messageSetMessages, err := child.parseMessages(records.MsgSet)
- if err != nil {
- return nil, err
- }
- messages = append(messages, messageSetMessages...)
- case defaultRecords:
-
- for _, txn := range abortedTransactions {
- if txn.FirstOffset > records.RecordBatch.LastOffset() {
- break
- }
- abortedProducerIDs[txn.ProducerID] = struct{}{}
-
- abortedTransactions = abortedTransactions[1:]
- }
- recordBatchMessages, err := child.parseRecords(records.RecordBatch)
- if err != nil {
- return nil, err
- }
-
-
-
-
- isControl, err := records.isControl()
- if err != nil {
-
-
-
- if child.conf.Consumer.IsolationLevel == ReadCommitted {
- return nil, err
- }
- continue
- }
- if isControl {
- controlRecord, err := records.getControlRecord()
- if err != nil {
- return nil, err
- }
- if controlRecord.Type == ControlRecordAbort {
- delete(abortedProducerIDs, records.RecordBatch.ProducerID)
- }
- continue
- }
-
- if child.conf.Consumer.IsolationLevel == ReadCommitted {
- _, isAborted := abortedProducerIDs[records.RecordBatch.ProducerID]
- if records.RecordBatch.IsTransactional && isAborted {
- continue
- }
- }
- messages = append(messages, recordBatchMessages...)
- default:
- return nil, fmt.Errorf("unknown records type: %v", records.recordsType)
- }
- }
- return messages, nil
- }
- type brokerConsumer struct {
- consumer *consumer
- broker *Broker
- input chan *partitionConsumer
- newSubscriptions chan []*partitionConsumer
- subscriptions map[*partitionConsumer]none
- wait chan none
- acks sync.WaitGroup
- refs int
- }
- func (c *consumer) newBrokerConsumer(broker *Broker) *brokerConsumer {
- bc := &brokerConsumer{
- consumer: c,
- broker: broker,
- input: make(chan *partitionConsumer),
- newSubscriptions: make(chan []*partitionConsumer),
- wait: make(chan none),
- subscriptions: make(map[*partitionConsumer]none),
- refs: 0,
- }
- go withRecover(bc.subscriptionManager)
- go withRecover(bc.subscriptionConsumer)
- return bc
- }
- func (bc *brokerConsumer) subscriptionManager() {
- var buffer []*partitionConsumer
- for {
- if len(buffer) > 0 {
- select {
- case event, ok := <-bc.input:
- if !ok {
- goto done
- }
- buffer = append(buffer, event)
- case bc.newSubscriptions <- buffer:
- buffer = nil
- case bc.wait <- none{}:
- }
- } else {
- select {
- case event, ok := <-bc.input:
- if !ok {
- goto done
- }
- buffer = append(buffer, event)
- case bc.newSubscriptions <- nil:
- }
- }
- }
- done:
- close(bc.wait)
- if len(buffer) > 0 {
- bc.newSubscriptions <- buffer
- }
- close(bc.newSubscriptions)
- }
- func (bc *brokerConsumer) subscriptionConsumer() {
- <-bc.wait
- for newSubscriptions := range bc.newSubscriptions {
- bc.updateSubscriptions(newSubscriptions)
- if len(bc.subscriptions) == 0 {
-
-
- <-bc.wait
- continue
- }
- response, err := bc.fetchNewMessages()
- if err != nil {
- Logger.Printf("consumer/broker/%d disconnecting due to error processing FetchRequest: %s\n", bc.broker.ID(), err)
- bc.abort(err)
- return
- }
- bc.acks.Add(len(bc.subscriptions))
- for child := range bc.subscriptions {
- child.feeder <- response
- }
- bc.acks.Wait()
- bc.handleResponses()
- }
- }
- func (bc *brokerConsumer) updateSubscriptions(newSubscriptions []*partitionConsumer) {
- for _, child := range newSubscriptions {
- bc.subscriptions[child] = none{}
- Logger.Printf("consumer/broker/%d added subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
- }
- for child := range bc.subscriptions {
- select {
- case <-child.dying:
- Logger.Printf("consumer/broker/%d closed dead subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
- close(child.trigger)
- delete(bc.subscriptions, child)
- default:
-
- }
- }
- }
- func (bc *brokerConsumer) handleResponses() {
- for child := range bc.subscriptions {
- result := child.responseResult
- child.responseResult = nil
- switch result {
- case nil:
-
- case errTimedOut:
- Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because consuming was taking too long\n",
- bc.broker.ID(), child.topic, child.partition)
- delete(bc.subscriptions, child)
- case ErrOffsetOutOfRange:
-
-
- child.sendError(result)
- Logger.Printf("consumer/%s/%d shutting down because %s\n", child.topic, child.partition, result)
- close(child.trigger)
- delete(bc.subscriptions, child)
- case ErrUnknownTopicOrPartition, ErrNotLeaderForPartition, ErrLeaderNotAvailable, ErrReplicaNotAvailable:
-
- Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
- bc.broker.ID(), child.topic, child.partition, result)
- child.trigger <- none{}
- delete(bc.subscriptions, child)
- default:
-
- child.sendError(result)
- Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
- bc.broker.ID(), child.topic, child.partition, result)
- child.trigger <- none{}
- delete(bc.subscriptions, child)
- }
- }
- }
- func (bc *brokerConsumer) abort(err error) {
- bc.consumer.abandonBrokerConsumer(bc)
- _ = bc.broker.Close()
- for child := range bc.subscriptions {
- child.sendError(err)
- child.trigger <- none{}
- }
- for newSubscriptions := range bc.newSubscriptions {
- if len(newSubscriptions) == 0 {
- <-bc.wait
- continue
- }
- for _, child := range newSubscriptions {
- child.sendError(err)
- child.trigger <- none{}
- }
- }
- }
- func (bc *brokerConsumer) fetchNewMessages() (*FetchResponse, error) {
- request := &FetchRequest{
- MinBytes: bc.consumer.conf.Consumer.Fetch.Min,
- MaxWaitTime: int32(bc.consumer.conf.Consumer.MaxWaitTime / time.Millisecond),
- }
- if bc.consumer.conf.Version.IsAtLeast(V0_9_0_0) {
- request.Version = 1
- }
- if bc.consumer.conf.Version.IsAtLeast(V0_10_0_0) {
- request.Version = 2
- }
- if bc.consumer.conf.Version.IsAtLeast(V0_10_1_0) {
- request.Version = 3
- request.MaxBytes = MaxResponseSize
- }
- if bc.consumer.conf.Version.IsAtLeast(V0_11_0_0) {
- request.Version = 4
- request.Isolation = bc.consumer.conf.Consumer.IsolationLevel
- }
- if bc.consumer.conf.Version.IsAtLeast(V1_1_0_0) {
- request.Version = 7
-
-
-
- request.SessionID = 0
- request.SessionEpoch = -1
- }
- if bc.consumer.conf.Version.IsAtLeast(V2_1_0_0) {
- request.Version = 10
- }
- if bc.consumer.conf.Version.IsAtLeast(V2_3_0_0) {
- request.Version = 11
- request.RackID = bc.consumer.conf.RackID
- }
- for child := range bc.subscriptions {
- request.AddBlock(child.topic, child.partition, child.offset, child.fetchSize)
- }
- return bc.broker.Fetch(request)
- }
|