consumer.go 27 KB

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