consumer.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. package sarama
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "sync/atomic"
  7. "time"
  8. "github.com/rcrowley/go-metrics"
  9. )
  10. // ConsumerMessage encapsulates a Kafka message returned by the consumer.
  11. type ConsumerMessage struct {
  12. Headers []*RecordHeader // only set if kafka is version 0.11+
  13. Timestamp time.Time // only set if kafka is version 0.10+, inner message timestamp
  14. BlockTimestamp time.Time // only set if kafka is version 0.10+, outer (compressed) block timestamp
  15. Key, Value []byte
  16. Topic string
  17. Partition int32
  18. Offset int64
  19. }
  20. // ConsumerError is what is provided to the user when an error occurs.
  21. // It wraps an error and includes the topic and partition.
  22. type ConsumerError struct {
  23. Topic string
  24. Partition int32
  25. Err error
  26. }
  27. func (ce ConsumerError) Error() string {
  28. return fmt.Sprintf("kafka: error while consuming %s/%d: %s", ce.Topic, ce.Partition, ce.Err)
  29. }
  30. // ConsumerErrors is a type that wraps a batch of errors and implements the Error interface.
  31. // It can be returned from the PartitionConsumer's Close methods to avoid the need to manually drain errors
  32. // when stopping.
  33. type ConsumerErrors []*ConsumerError
  34. func (ce ConsumerErrors) Error() string {
  35. return fmt.Sprintf("kafka: %d errors while consuming", len(ce))
  36. }
  37. // Consumer manages PartitionConsumers which process Kafka messages from brokers. You MUST call Close()
  38. // on a consumer to avoid leaks, it will not be garbage-collected automatically when it passes out of
  39. // scope.
  40. type Consumer interface {
  41. // Topics returns the set of available topics as retrieved from the cluster
  42. // metadata. This method is the same as Client.Topics(), and is provided for
  43. // convenience.
  44. Topics() ([]string, error)
  45. // Partitions returns the sorted list of all partition IDs for the given topic.
  46. // This method is the same as Client.Partitions(), and is provided for convenience.
  47. Partitions(topic string) ([]int32, error)
  48. // ConsumePartition creates a PartitionConsumer on the given topic/partition with
  49. // the given offset. It will return an error if this Consumer is already consuming
  50. // on the given topic/partition. Offset can be a literal offset, or OffsetNewest
  51. // or OffsetOldest
  52. ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error)
  53. // HighWaterMarks returns the current high water marks for each topic and partition.
  54. // Consistency between partitions is not guaranteed since high water marks are updated separately.
  55. HighWaterMarks() map[string]map[int32]int64
  56. // Close shuts down the consumer. It must be called after all child
  57. // PartitionConsumers have already been closed.
  58. Close() error
  59. }
  60. type consumer struct {
  61. conf *Config
  62. children map[string]map[int32]*partitionConsumer
  63. brokerConsumers map[*Broker]*brokerConsumer
  64. client Client
  65. lock sync.Mutex
  66. }
  67. // NewConsumer creates a new consumer using the given broker addresses and configuration.
  68. func NewConsumer(addrs []string, config *Config) (Consumer, error) {
  69. client, err := NewClient(addrs, config)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return newConsumer(client)
  74. }
  75. // NewConsumerFromClient creates a new consumer using the given client. It is still
  76. // necessary to call Close() on the underlying client when shutting down this consumer.
  77. func NewConsumerFromClient(client Client) (Consumer, error) {
  78. // For clients passed in by the client, ensure we don't
  79. // call Close() on it.
  80. cli := &nopCloserClient{client}
  81. return newConsumer(cli)
  82. }
  83. func newConsumer(client Client) (Consumer, error) {
  84. // Check that we are not dealing with a closed Client before processing any other arguments
  85. if client.Closed() {
  86. return nil, ErrClosedClient
  87. }
  88. c := &consumer{
  89. client: client,
  90. conf: client.Config(),
  91. children: make(map[string]map[int32]*partitionConsumer),
  92. brokerConsumers: make(map[*Broker]*brokerConsumer),
  93. }
  94. return c, nil
  95. }
  96. func (c *consumer) Close() error {
  97. return c.client.Close()
  98. }
  99. func (c *consumer) Topics() ([]string, error) {
  100. return c.client.Topics()
  101. }
  102. func (c *consumer) Partitions(topic string) ([]int32, error) {
  103. return c.client.Partitions(topic)
  104. }
  105. func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error) {
  106. child := &partitionConsumer{
  107. consumer: c,
  108. conf: c.conf,
  109. topic: topic,
  110. partition: partition,
  111. messages: make(chan *ConsumerMessage, c.conf.ChannelBufferSize),
  112. errors: make(chan *ConsumerError, c.conf.ChannelBufferSize),
  113. feeder: make(chan *FetchResponse, 1),
  114. trigger: make(chan none, 1),
  115. dying: make(chan none),
  116. fetchSize: c.conf.Consumer.Fetch.Default,
  117. }
  118. if err := child.chooseStartingOffset(offset); err != nil {
  119. return nil, err
  120. }
  121. var leader *Broker
  122. var err error
  123. if leader, err = c.client.Leader(child.topic, child.partition); err != nil {
  124. return nil, err
  125. }
  126. if err := c.addChild(child); err != nil {
  127. return nil, err
  128. }
  129. go withRecover(child.dispatcher)
  130. go withRecover(child.responseFeeder)
  131. child.broker = c.refBrokerConsumer(leader)
  132. child.broker.input <- child
  133. return child, nil
  134. }
  135. func (c *consumer) HighWaterMarks() map[string]map[int32]int64 {
  136. c.lock.Lock()
  137. defer c.lock.Unlock()
  138. hwms := make(map[string]map[int32]int64)
  139. for topic, p := range c.children {
  140. hwm := make(map[int32]int64, len(p))
  141. for partition, pc := range p {
  142. hwm[partition] = pc.HighWaterMarkOffset()
  143. }
  144. hwms[topic] = hwm
  145. }
  146. return hwms
  147. }
  148. func (c *consumer) addChild(child *partitionConsumer) error {
  149. c.lock.Lock()
  150. defer c.lock.Unlock()
  151. topicChildren := c.children[child.topic]
  152. if topicChildren == nil {
  153. topicChildren = make(map[int32]*partitionConsumer)
  154. c.children[child.topic] = topicChildren
  155. }
  156. if topicChildren[child.partition] != nil {
  157. return ConfigurationError("That topic/partition is already being consumed")
  158. }
  159. topicChildren[child.partition] = child
  160. return nil
  161. }
  162. func (c *consumer) removeChild(child *partitionConsumer) {
  163. c.lock.Lock()
  164. defer c.lock.Unlock()
  165. delete(c.children[child.topic], child.partition)
  166. }
  167. func (c *consumer) refBrokerConsumer(broker *Broker) *brokerConsumer {
  168. c.lock.Lock()
  169. defer c.lock.Unlock()
  170. bc := c.brokerConsumers[broker]
  171. if bc == nil {
  172. bc = c.newBrokerConsumer(broker)
  173. c.brokerConsumers[broker] = bc
  174. }
  175. bc.refs++
  176. return bc
  177. }
  178. func (c *consumer) unrefBrokerConsumer(brokerWorker *brokerConsumer) {
  179. c.lock.Lock()
  180. defer c.lock.Unlock()
  181. brokerWorker.refs--
  182. if brokerWorker.refs == 0 {
  183. close(brokerWorker.input)
  184. if c.brokerConsumers[brokerWorker.broker] == brokerWorker {
  185. delete(c.brokerConsumers, brokerWorker.broker)
  186. }
  187. }
  188. }
  189. func (c *consumer) abandonBrokerConsumer(brokerWorker *brokerConsumer) {
  190. c.lock.Lock()
  191. defer c.lock.Unlock()
  192. delete(c.brokerConsumers, brokerWorker.broker)
  193. }
  194. // PartitionConsumer
  195. // PartitionConsumer processes Kafka messages from a given topic and partition. You MUST call one of Close() or
  196. // AsyncClose() on a PartitionConsumer to avoid leaks; it will not be garbage-collected automatically when it passes out
  197. // of scope.
  198. //
  199. // The simplest way of using a PartitionConsumer is to loop over its Messages channel using a for/range
  200. // loop. The PartitionConsumer will only stop itself in one case: when the offset being consumed is reported
  201. // as out of range by the brokers. In this case you should decide what you want to do (try a different offset,
  202. // notify a human, etc) and handle it appropriately. For all other error cases, it will just keep retrying.
  203. // By default, it logs these errors to sarama.Logger; if you want to be notified directly of all errors, set
  204. // your config's Consumer.Return.Errors to true and read from the Errors channel, using a select statement
  205. // or a separate goroutine. Check out the Consumer examples to see implementations of these different approaches.
  206. //
  207. // To terminate such a for/range loop while the loop is executing, call AsyncClose. This will kick off the process of
  208. // consumer tear-down & return immediately. Continue to loop, servicing the Messages channel until the teardown process
  209. // AsyncClose initiated closes it (thus terminating the for/range loop). If you've already ceased reading Messages, call
  210. // Close; this will signal the PartitionConsumer's goroutines to begin shutting down (just like AsyncClose), but will
  211. // also drain the Messages channel, harvest all errors & return them once cleanup has completed.
  212. type PartitionConsumer interface {
  213. // AsyncClose initiates a shutdown of the PartitionConsumer. This method will return immediately, after which you
  214. // should continue to service the 'Messages' and 'Errors' channels until they are empty. It is required to call this
  215. // function, or Close before a consumer object passes out of scope, as it will otherwise leak memory. You must call
  216. // this before calling Close on the underlying client.
  217. AsyncClose()
  218. // Close stops the PartitionConsumer from fetching messages. It will initiate a shutdown just like AsyncClose, drain
  219. // the Messages channel, harvest any errors & return them to the caller. Note that if you are continuing to service
  220. // the Messages channel when this function is called, you will be competing with Close for messages; consider
  221. // calling AsyncClose, instead. It is required to call this function (or AsyncClose) before a consumer object passes
  222. // out of scope, as it will otherwise leak memory. You must call this before calling Close on the underlying client.
  223. Close() error
  224. // Messages returns the read channel for the messages that are returned by
  225. // the broker.
  226. Messages() <-chan *ConsumerMessage
  227. // Errors returns a read channel of errors that occurred during consuming, if
  228. // enabled. By default, errors are logged and not returned over this channel.
  229. // If you want to implement any custom error handling, set your config's
  230. // Consumer.Return.Errors setting to true, and read from this channel.
  231. Errors() <-chan *ConsumerError
  232. // HighWaterMarkOffset returns the high water mark offset of the partition,
  233. // i.e. the offset that will be used for the next message that will be produced.
  234. // You can use this to determine how far behind the processing is.
  235. HighWaterMarkOffset() int64
  236. }
  237. type partitionConsumer struct {
  238. highWaterMarkOffset int64 // must be at the top of the struct because https://golang.org/pkg/sync/atomic/#pkg-note-BUG
  239. consumer *consumer
  240. conf *Config
  241. broker *brokerConsumer
  242. messages chan *ConsumerMessage
  243. errors chan *ConsumerError
  244. feeder chan *FetchResponse
  245. trigger, dying chan none
  246. closeOnce sync.Once
  247. topic string
  248. partition int32
  249. responseResult error
  250. fetchSize int32
  251. offset int64
  252. retries int32
  253. }
  254. var errTimedOut = errors.New("timed out feeding messages to the user") // not user-facing
  255. func (child *partitionConsumer) sendError(err error) {
  256. cErr := &ConsumerError{
  257. Topic: child.topic,
  258. Partition: child.partition,
  259. Err: err,
  260. }
  261. if child.conf.Consumer.Return.Errors {
  262. child.errors <- cErr
  263. } else {
  264. Logger.Println(cErr)
  265. }
  266. }
  267. func (child *partitionConsumer) computeBackoff() time.Duration {
  268. if child.conf.Consumer.Retry.BackoffFunc != nil {
  269. retries := atomic.AddInt32(&child.retries, 1)
  270. return child.conf.Consumer.Retry.BackoffFunc(int(retries))
  271. }
  272. return child.conf.Consumer.Retry.Backoff
  273. }
  274. func (child *partitionConsumer) dispatcher() {
  275. for range child.trigger {
  276. select {
  277. case <-child.dying:
  278. close(child.trigger)
  279. case <-time.After(child.computeBackoff()):
  280. if child.broker != nil {
  281. child.consumer.unrefBrokerConsumer(child.broker)
  282. child.broker = nil
  283. }
  284. Logger.Printf("consumer/%s/%d finding new broker\n", child.topic, child.partition)
  285. if err := child.dispatch(); err != nil {
  286. child.sendError(err)
  287. child.trigger <- none{}
  288. }
  289. }
  290. }
  291. if child.broker != nil {
  292. child.consumer.unrefBrokerConsumer(child.broker)
  293. }
  294. child.consumer.removeChild(child)
  295. close(child.feeder)
  296. }
  297. func (child *partitionConsumer) dispatch() error {
  298. if err := child.consumer.client.RefreshMetadata(child.topic); err != nil {
  299. return err
  300. }
  301. var leader *Broker
  302. var err error
  303. if leader, err = child.consumer.client.Leader(child.topic, child.partition); err != nil {
  304. return err
  305. }
  306. child.broker = child.consumer.refBrokerConsumer(leader)
  307. child.broker.input <- child
  308. return nil
  309. }
  310. func (child *partitionConsumer) chooseStartingOffset(offset int64) error {
  311. newestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetNewest)
  312. if err != nil {
  313. return err
  314. }
  315. oldestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetOldest)
  316. if err != nil {
  317. return err
  318. }
  319. switch {
  320. case offset == OffsetNewest:
  321. child.offset = newestOffset
  322. case offset == OffsetOldest:
  323. child.offset = oldestOffset
  324. case offset >= oldestOffset && offset <= newestOffset:
  325. child.offset = offset
  326. default:
  327. return ErrOffsetOutOfRange
  328. }
  329. return nil
  330. }
  331. func (child *partitionConsumer) Messages() <-chan *ConsumerMessage {
  332. return child.messages
  333. }
  334. func (child *partitionConsumer) Errors() <-chan *ConsumerError {
  335. return child.errors
  336. }
  337. func (child *partitionConsumer) AsyncClose() {
  338. // this triggers whatever broker owns this child to abandon it and close its trigger channel, which causes
  339. // the dispatcher to exit its loop, which removes it from the consumer then closes its 'messages' and
  340. // 'errors' channel (alternatively, if the child is already at the dispatcher for some reason, that will
  341. // also just close itself)
  342. child.closeOnce.Do(func() {
  343. close(child.dying)
  344. })
  345. }
  346. func (child *partitionConsumer) Close() error {
  347. child.AsyncClose()
  348. var errors ConsumerErrors
  349. for err := range child.errors {
  350. errors = append(errors, err)
  351. }
  352. if len(errors) > 0 {
  353. return errors
  354. }
  355. return nil
  356. }
  357. func (child *partitionConsumer) HighWaterMarkOffset() int64 {
  358. return atomic.LoadInt64(&child.highWaterMarkOffset)
  359. }
  360. func (child *partitionConsumer) responseFeeder() {
  361. var msgs []*ConsumerMessage
  362. expiryTicker := time.NewTicker(child.conf.Consumer.MaxProcessingTime)
  363. firstAttempt := true
  364. feederLoop:
  365. for response := range child.feeder {
  366. msgs, child.responseResult = child.parseResponse(response)
  367. if child.responseResult == nil {
  368. atomic.StoreInt32(&child.retries, 0)
  369. }
  370. for i, msg := range msgs {
  371. messageSelect:
  372. select {
  373. case <-child.dying:
  374. child.broker.acks.Done()
  375. continue feederLoop
  376. case child.messages <- msg:
  377. firstAttempt = true
  378. case <-expiryTicker.C:
  379. if !firstAttempt {
  380. child.responseResult = errTimedOut
  381. child.broker.acks.Done()
  382. remainingLoop:
  383. for _, msg = range msgs[i:] {
  384. select {
  385. case child.messages <- msg:
  386. case <-child.dying:
  387. break remainingLoop
  388. }
  389. }
  390. child.broker.input <- child
  391. continue feederLoop
  392. } else {
  393. // current message has not been sent, return to select
  394. // statement
  395. firstAttempt = false
  396. goto messageSelect
  397. }
  398. }
  399. }
  400. child.broker.acks.Done()
  401. }
  402. expiryTicker.Stop()
  403. close(child.messages)
  404. close(child.errors)
  405. }
  406. func (child *partitionConsumer) parseMessages(msgSet *MessageSet) ([]*ConsumerMessage, error) {
  407. var messages []*ConsumerMessage
  408. for _, msgBlock := range msgSet.Messages {
  409. for _, msg := range msgBlock.Messages() {
  410. offset := msg.Offset
  411. timestamp := msg.Msg.Timestamp
  412. if msg.Msg.Version >= 1 {
  413. baseOffset := msgBlock.Offset - msgBlock.Messages()[len(msgBlock.Messages())-1].Offset
  414. offset += baseOffset
  415. if msg.Msg.LogAppendTime {
  416. timestamp = msgBlock.Msg.Timestamp
  417. }
  418. }
  419. if offset < child.offset {
  420. continue
  421. }
  422. messages = append(messages, &ConsumerMessage{
  423. Topic: child.topic,
  424. Partition: child.partition,
  425. Key: msg.Msg.Key,
  426. Value: msg.Msg.Value,
  427. Offset: offset,
  428. Timestamp: timestamp,
  429. BlockTimestamp: msgBlock.Msg.Timestamp,
  430. })
  431. child.offset = offset + 1
  432. }
  433. }
  434. if len(messages) == 0 {
  435. child.offset++
  436. }
  437. return messages, nil
  438. }
  439. func (child *partitionConsumer) parseRecords(batch *RecordBatch) ([]*ConsumerMessage, error) {
  440. messages := make([]*ConsumerMessage, 0, len(batch.Records))
  441. for _, rec := range batch.Records {
  442. offset := batch.FirstOffset + rec.OffsetDelta
  443. if offset < child.offset {
  444. continue
  445. }
  446. timestamp := batch.FirstTimestamp.Add(rec.TimestampDelta)
  447. if batch.LogAppendTime {
  448. timestamp = batch.MaxTimestamp
  449. }
  450. messages = append(messages, &ConsumerMessage{
  451. Topic: child.topic,
  452. Partition: child.partition,
  453. Key: rec.Key,
  454. Value: rec.Value,
  455. Offset: offset,
  456. Timestamp: timestamp,
  457. Headers: rec.Headers,
  458. })
  459. child.offset = offset + 1
  460. }
  461. if len(messages) == 0 {
  462. child.offset++
  463. }
  464. return messages, nil
  465. }
  466. func (child *partitionConsumer) parseResponse(response *FetchResponse) ([]*ConsumerMessage, error) {
  467. var (
  468. metricRegistry = child.conf.MetricRegistry
  469. consumerBatchSizeMetric metrics.Histogram
  470. )
  471. if metricRegistry != nil {
  472. consumerBatchSizeMetric = getOrRegisterHistogram("consumer-batch-size", metricRegistry)
  473. }
  474. // If request was throttled and empty we log and return without error
  475. if response.ThrottleTime != time.Duration(0) && len(response.Blocks) == 0 {
  476. Logger.Printf(
  477. "consumer/broker/%d FetchResponse throttled %v\n",
  478. child.broker.broker.ID(), response.ThrottleTime)
  479. return nil, nil
  480. }
  481. block := response.GetBlock(child.topic, child.partition)
  482. if block == nil {
  483. return nil, ErrIncompleteResponse
  484. }
  485. if block.Err != ErrNoError {
  486. return nil, block.Err
  487. }
  488. nRecs, err := block.numRecords()
  489. if err != nil {
  490. return nil, err
  491. }
  492. consumerBatchSizeMetric.Update(int64(nRecs))
  493. if nRecs == 0 {
  494. partialTrailingMessage, err := block.isPartial()
  495. if err != nil {
  496. return nil, err
  497. }
  498. // We got no messages. If we got a trailing one then we need to ask for more data.
  499. // Otherwise we just poll again and wait for one to be produced...
  500. if partialTrailingMessage {
  501. if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize == child.conf.Consumer.Fetch.Max {
  502. // we can't ask for more data, we've hit the configured limit
  503. child.sendError(ErrMessageTooLarge)
  504. child.offset++ // skip this one so we can keep processing future messages
  505. } else {
  506. child.fetchSize *= 2
  507. if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize > child.conf.Consumer.Fetch.Max {
  508. child.fetchSize = child.conf.Consumer.Fetch.Max
  509. }
  510. }
  511. }
  512. return nil, nil
  513. }
  514. // we got messages, reset our fetch size in case it was increased for a previous request
  515. child.fetchSize = child.conf.Consumer.Fetch.Default
  516. atomic.StoreInt64(&child.highWaterMarkOffset, block.HighWaterMarkOffset)
  517. // abortedProducerIDs contains producerID which message should be ignored as uncommitted
  518. // - producerID are added when the partitionConsumer iterate over the offset at which an aborted transaction begins (abortedTransaction.FirstOffset)
  519. // - producerID are removed when partitionConsumer iterate over an aborted controlRecord, meaning the aborted transaction for this producer is over
  520. abortedProducerIDs := make(map[int64]struct{}, len(block.AbortedTransactions))
  521. abortedTransactions := block.getAbortedTransactions()
  522. messages := []*ConsumerMessage{}
  523. for _, records := range block.RecordsSet {
  524. switch records.recordsType {
  525. case legacyRecords:
  526. messageSetMessages, err := child.parseMessages(records.MsgSet)
  527. if err != nil {
  528. return nil, err
  529. }
  530. messages = append(messages, messageSetMessages...)
  531. case defaultRecords:
  532. // Consume remaining abortedTransaction up to last offset of current batch
  533. for _, txn := range abortedTransactions {
  534. if txn.FirstOffset > records.RecordBatch.LastOffset() {
  535. break
  536. }
  537. abortedProducerIDs[txn.ProducerID] = struct{}{}
  538. // Pop abortedTransactions so that we never add it again
  539. abortedTransactions = abortedTransactions[1:]
  540. }
  541. recordBatchMessages, err := child.parseRecords(records.RecordBatch)
  542. if err != nil {
  543. return nil, err
  544. }
  545. // Parse and commit offset but do not expose messages that are:
  546. // - control records
  547. // - part of an aborted transaction when set to `ReadCommitted`
  548. // control record
  549. isControl, err := records.isControl()
  550. if err != nil {
  551. // I don't know why there is this continue in case of error to begin with
  552. // Safe bet is to ignore control messages if ReadUncommitted
  553. // and block on them in case of error and ReadCommitted
  554. if child.conf.Consumer.IsolationLevel == ReadCommitted {
  555. return nil, err
  556. }
  557. continue
  558. }
  559. if isControl {
  560. controlRecord, err := records.getControlRecord()
  561. if err != nil {
  562. return nil, err
  563. }
  564. if controlRecord.Type == ControlRecordAbort {
  565. delete(abortedProducerIDs, records.RecordBatch.ProducerID)
  566. }
  567. continue
  568. }
  569. // filter aborted transactions
  570. if child.conf.Consumer.IsolationLevel == ReadCommitted {
  571. _, isAborted := abortedProducerIDs[records.RecordBatch.ProducerID]
  572. if records.RecordBatch.IsTransactional && isAborted {
  573. continue
  574. }
  575. }
  576. messages = append(messages, recordBatchMessages...)
  577. default:
  578. return nil, fmt.Errorf("unknown records type: %v", records.recordsType)
  579. }
  580. }
  581. return messages, nil
  582. }
  583. type brokerConsumer struct {
  584. consumer *consumer
  585. broker *Broker
  586. input chan *partitionConsumer
  587. newSubscriptions chan []*partitionConsumer
  588. subscriptions map[*partitionConsumer]none
  589. wait chan none
  590. acks sync.WaitGroup
  591. refs int
  592. }
  593. func (c *consumer) newBrokerConsumer(broker *Broker) *brokerConsumer {
  594. bc := &brokerConsumer{
  595. consumer: c,
  596. broker: broker,
  597. input: make(chan *partitionConsumer),
  598. newSubscriptions: make(chan []*partitionConsumer),
  599. wait: make(chan none),
  600. subscriptions: make(map[*partitionConsumer]none),
  601. refs: 0,
  602. }
  603. go withRecover(bc.subscriptionManager)
  604. go withRecover(bc.subscriptionConsumer)
  605. return bc
  606. }
  607. // The subscriptionManager constantly accepts new subscriptions on `input` (even when the main subscriptionConsumer
  608. // goroutine is in the middle of a network request) and batches it up. The main worker goroutine picks
  609. // up a batch of new subscriptions between every network request by reading from `newSubscriptions`, so we give
  610. // it nil if no new subscriptions are available. We also write to `wait` only when new subscriptions is available,
  611. // so the main goroutine can block waiting for work if it has none.
  612. func (bc *brokerConsumer) subscriptionManager() {
  613. var buffer []*partitionConsumer
  614. for {
  615. if len(buffer) > 0 {
  616. select {
  617. case event, ok := <-bc.input:
  618. if !ok {
  619. goto done
  620. }
  621. buffer = append(buffer, event)
  622. case bc.newSubscriptions <- buffer:
  623. buffer = nil
  624. case bc.wait <- none{}:
  625. }
  626. } else {
  627. select {
  628. case event, ok := <-bc.input:
  629. if !ok {
  630. goto done
  631. }
  632. buffer = append(buffer, event)
  633. case bc.newSubscriptions <- nil:
  634. }
  635. }
  636. }
  637. done:
  638. close(bc.wait)
  639. if len(buffer) > 0 {
  640. bc.newSubscriptions <- buffer
  641. }
  642. close(bc.newSubscriptions)
  643. }
  644. //subscriptionConsumer ensures we will get nil right away if no new subscriptions is available
  645. func (bc *brokerConsumer) subscriptionConsumer() {
  646. <-bc.wait // wait for our first piece of work
  647. for newSubscriptions := range bc.newSubscriptions {
  648. bc.updateSubscriptions(newSubscriptions)
  649. if len(bc.subscriptions) == 0 {
  650. // We're about to be shut down or we're about to receive more subscriptions.
  651. // Either way, the signal just hasn't propagated to our goroutine yet.
  652. <-bc.wait
  653. continue
  654. }
  655. response, err := bc.fetchNewMessages()
  656. if err != nil {
  657. Logger.Printf("consumer/broker/%d disconnecting due to error processing FetchRequest: %s\n", bc.broker.ID(), err)
  658. bc.abort(err)
  659. return
  660. }
  661. bc.acks.Add(len(bc.subscriptions))
  662. for child := range bc.subscriptions {
  663. child.feeder <- response
  664. }
  665. bc.acks.Wait()
  666. bc.handleResponses()
  667. }
  668. }
  669. func (bc *brokerConsumer) updateSubscriptions(newSubscriptions []*partitionConsumer) {
  670. for _, child := range newSubscriptions {
  671. bc.subscriptions[child] = none{}
  672. Logger.Printf("consumer/broker/%d added subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
  673. }
  674. for child := range bc.subscriptions {
  675. select {
  676. case <-child.dying:
  677. Logger.Printf("consumer/broker/%d closed dead subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
  678. close(child.trigger)
  679. delete(bc.subscriptions, child)
  680. default:
  681. // no-op
  682. }
  683. }
  684. }
  685. //handleResponses handles the response codes left for us by our subscriptions, and abandons ones that have been closed
  686. func (bc *brokerConsumer) handleResponses() {
  687. for child := range bc.subscriptions {
  688. result := child.responseResult
  689. child.responseResult = nil
  690. switch result {
  691. case nil:
  692. // no-op
  693. case errTimedOut:
  694. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because consuming was taking too long\n",
  695. bc.broker.ID(), child.topic, child.partition)
  696. delete(bc.subscriptions, child)
  697. case ErrOffsetOutOfRange:
  698. // there's no point in retrying this it will just fail the same way again
  699. // shut it down and force the user to choose what to do
  700. child.sendError(result)
  701. Logger.Printf("consumer/%s/%d shutting down because %s\n", child.topic, child.partition, result)
  702. close(child.trigger)
  703. delete(bc.subscriptions, child)
  704. case ErrUnknownTopicOrPartition, ErrNotLeaderForPartition, ErrLeaderNotAvailable, ErrReplicaNotAvailable:
  705. // not an error, but does need redispatching
  706. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
  707. bc.broker.ID(), child.topic, child.partition, result)
  708. child.trigger <- none{}
  709. delete(bc.subscriptions, child)
  710. default:
  711. // dunno, tell the user and try redispatching
  712. child.sendError(result)
  713. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
  714. bc.broker.ID(), child.topic, child.partition, result)
  715. child.trigger <- none{}
  716. delete(bc.subscriptions, child)
  717. }
  718. }
  719. }
  720. func (bc *brokerConsumer) abort(err error) {
  721. bc.consumer.abandonBrokerConsumer(bc)
  722. _ = bc.broker.Close() // we don't care about the error this might return, we already have one
  723. for child := range bc.subscriptions {
  724. child.sendError(err)
  725. child.trigger <- none{}
  726. }
  727. for newSubscriptions := range bc.newSubscriptions {
  728. if len(newSubscriptions) == 0 {
  729. <-bc.wait
  730. continue
  731. }
  732. for _, child := range newSubscriptions {
  733. child.sendError(err)
  734. child.trigger <- none{}
  735. }
  736. }
  737. }
  738. func (bc *brokerConsumer) fetchNewMessages() (*FetchResponse, error) {
  739. request := &FetchRequest{
  740. MinBytes: bc.consumer.conf.Consumer.Fetch.Min,
  741. MaxWaitTime: int32(bc.consumer.conf.Consumer.MaxWaitTime / time.Millisecond),
  742. }
  743. if bc.consumer.conf.Version.IsAtLeast(V0_9_0_0) {
  744. request.Version = 1
  745. }
  746. if bc.consumer.conf.Version.IsAtLeast(V0_10_0_0) {
  747. request.Version = 2
  748. }
  749. if bc.consumer.conf.Version.IsAtLeast(V0_10_1_0) {
  750. request.Version = 3
  751. request.MaxBytes = MaxResponseSize
  752. }
  753. if bc.consumer.conf.Version.IsAtLeast(V0_11_0_0) {
  754. request.Version = 4
  755. request.Isolation = bc.consumer.conf.Consumer.IsolationLevel
  756. }
  757. for child := range bc.subscriptions {
  758. request.AddBlock(child.topic, child.partition, child.offset, child.fetchSize)
  759. }
  760. return bc.broker.Fetch(request)
  761. }