consumer.go 26 KB

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