consumer.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. package sarama
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "sync/atomic"
  7. "time"
  8. )
  9. // ConsumerMessage encapsulates a Kafka message returned by the consumer.
  10. type ConsumerMessage struct {
  11. Key, Value []byte
  12. Topic string
  13. Partition int32
  14. Offset int64
  15. Timestamp time.Time // only set if kafka is version 0.10+
  16. }
  17. // ConsumerError is what is provided to the user when an error occurs.
  18. // It wraps an error and includes the topic and partition.
  19. type ConsumerError struct {
  20. Topic string
  21. Partition int32
  22. Err error
  23. }
  24. func (ce ConsumerError) Error() string {
  25. return fmt.Sprintf("kafka: error while consuming %s/%d: %s", ce.Topic, ce.Partition, ce.Err)
  26. }
  27. // ConsumerErrors is a type that wraps a batch of errors and implements the Error interface.
  28. // It can be returned from the PartitionConsumer's Close methods to avoid the need to manually drain errors
  29. // when stopping.
  30. type ConsumerErrors []*ConsumerError
  31. func (ce ConsumerErrors) Error() string {
  32. return fmt.Sprintf("kafka: %d errors while consuming", len(ce))
  33. }
  34. // Consumer manages PartitionConsumers which process Kafka messages from brokers. You MUST call Close()
  35. // on a consumer to avoid leaks, it will not be garbage-collected automatically when it passes out of
  36. // scope.
  37. //
  38. // Sarama's Consumer type does not currently support automatic consumer-group rebalancing and offset tracking.
  39. // For Zookeeper-based tracking (Kafka 0.8.2 and earlier), the https://github.com/wvanbergen/kafka library
  40. // builds on Sarama to add this support. For Kafka-based tracking (Kafka 0.9 and later), the
  41. // https://github.com/bsm/sarama-cluster library builds on Sarama to add this support.
  42. type Consumer interface {
  43. // Topics returns the set of available topics as retrieved from the cluster
  44. // metadata. This method is the same as Client.Topics(), and is provided for
  45. // convenience.
  46. Topics() ([]string, error)
  47. // Partitions returns the sorted list of all partition IDs for the given topic.
  48. // This method is the same as Client.Partitions(), and is provided for convenience.
  49. Partitions(topic string) ([]int32, error)
  50. // ConsumePartition creates a PartitionConsumer on the given topic/partition with
  51. // the given offset. It will return an error if this Consumer is already consuming
  52. // on the given topic/partition. Offset can be a literal offset, or OffsetNewest
  53. // or OffsetOldest
  54. ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error)
  55. // Close shuts down the consumer. It must be called after all child
  56. // PartitionConsumers have already been closed.
  57. Close() error
  58. }
  59. type consumer struct {
  60. client Client
  61. conf *Config
  62. ownClient bool
  63. lock sync.Mutex
  64. children map[string]map[int32]*partitionConsumer
  65. brokerConsumers map[*Broker]*brokerConsumer
  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. c, err := NewConsumerFromClient(client)
  74. if err != nil {
  75. return nil, err
  76. }
  77. c.(*consumer).ownClient = true
  78. return c, nil
  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. // Check that we are not dealing with a closed Client before processing any other arguments
  84. if client.Closed() {
  85. return nil, ErrClosedClient
  86. }
  87. c := &consumer{
  88. client: client,
  89. conf: client.Config(),
  90. children: make(map[string]map[int32]*partitionConsumer),
  91. brokerConsumers: make(map[*Broker]*brokerConsumer),
  92. }
  93. return c, nil
  94. }
  95. func (c *consumer) Close() error {
  96. if c.ownClient {
  97. return c.client.Close()
  98. }
  99. return nil
  100. }
  101. func (c *consumer) Topics() ([]string, error) {
  102. return c.client.Topics()
  103. }
  104. func (c *consumer) Partitions(topic string) ([]int32, error) {
  105. return c.client.Partitions(topic)
  106. }
  107. func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error) {
  108. child := &partitionConsumer{
  109. consumer: c,
  110. conf: c.conf,
  111. topic: topic,
  112. partition: partition,
  113. messages: make(chan *ConsumerMessage, c.conf.ChannelBufferSize),
  114. errors: make(chan *ConsumerError, c.conf.ChannelBufferSize),
  115. feeder: make(chan *FetchResponse, 1),
  116. trigger: make(chan none, 1),
  117. dying: make(chan none),
  118. fetchSize: c.conf.Consumer.Fetch.Default,
  119. }
  120. if err := child.chooseStartingOffset(offset); err != nil {
  121. return nil, err
  122. }
  123. var leader *Broker
  124. var err error
  125. if leader, err = c.client.Leader(child.topic, child.partition); err != nil {
  126. return nil, err
  127. }
  128. if err := c.addChild(child); err != nil {
  129. return nil, err
  130. }
  131. go withRecover(child.dispatcher)
  132. go withRecover(child.responseFeeder)
  133. child.broker = c.refBrokerConsumer(leader)
  134. child.broker.input <- child
  135. return child, nil
  136. }
  137. func (c *consumer) addChild(child *partitionConsumer) error {
  138. c.lock.Lock()
  139. defer c.lock.Unlock()
  140. topicChildren := c.children[child.topic]
  141. if topicChildren == nil {
  142. topicChildren = make(map[int32]*partitionConsumer)
  143. c.children[child.topic] = topicChildren
  144. }
  145. if topicChildren[child.partition] != nil {
  146. return ConfigurationError("That topic/partition is already being consumed")
  147. }
  148. topicChildren[child.partition] = child
  149. return nil
  150. }
  151. func (c *consumer) removeChild(child *partitionConsumer) {
  152. c.lock.Lock()
  153. defer c.lock.Unlock()
  154. delete(c.children[child.topic], child.partition)
  155. }
  156. func (c *consumer) refBrokerConsumer(broker *Broker) *brokerConsumer {
  157. c.lock.Lock()
  158. defer c.lock.Unlock()
  159. bc := c.brokerConsumers[broker]
  160. if bc == nil {
  161. bc = c.newBrokerConsumer(broker)
  162. c.brokerConsumers[broker] = bc
  163. }
  164. bc.refs++
  165. return bc
  166. }
  167. func (c *consumer) unrefBrokerConsumer(brokerWorker *brokerConsumer) {
  168. c.lock.Lock()
  169. defer c.lock.Unlock()
  170. brokerWorker.refs--
  171. if brokerWorker.refs == 0 {
  172. close(brokerWorker.input)
  173. if c.brokerConsumers[brokerWorker.broker] == brokerWorker {
  174. delete(c.brokerConsumers, brokerWorker.broker)
  175. }
  176. }
  177. }
  178. func (c *consumer) abandonBrokerConsumer(brokerWorker *brokerConsumer) {
  179. c.lock.Lock()
  180. defer c.lock.Unlock()
  181. delete(c.brokerConsumers, brokerWorker.broker)
  182. }
  183. // PartitionConsumer
  184. // PartitionConsumer processes Kafka messages from a given topic and partition. You MUST call Close()
  185. // or AsyncClose() on a PartitionConsumer to avoid leaks, it will not be garbage-collected automatically
  186. // when it passes out of scope.
  187. //
  188. // The simplest way of using a PartitionConsumer is to loop over its Messages channel using a for/range
  189. // loop. The PartitionConsumer will only stop itself in one case: when the offset being consumed is reported
  190. // as out of range by the brokers. In this case you should decide what you want to do (try a different offset,
  191. // notify a human, etc) and handle it appropriately. For all other error cases, it will just keep retrying.
  192. // By default, it logs these errors to sarama.Logger; if you want to be notified directly of all errors, set
  193. // your config's Consumer.Return.Errors to true and read from the Errors channel, using a select statement
  194. // or a separate goroutine. Check out the Consumer examples to see implementations of these different approaches.
  195. type PartitionConsumer interface {
  196. // AsyncClose initiates a shutdown of the PartitionConsumer. This method will
  197. // return immediately, after which you should wait until the 'messages' and
  198. // 'errors' channel are drained. It is required to call this function, or
  199. // Close before a consumer object passes out of scope, as it will otherwise
  200. // leak memory. You must call this before calling Close on the underlying client.
  201. AsyncClose()
  202. // Close stops the PartitionConsumer from fetching messages. It is required to
  203. // call this function (or AsyncClose) before a consumer object passes out of
  204. // scope, as it will otherwise leak memory. You must call this before calling
  205. // Close on the underlying client.
  206. Close() error
  207. // Messages returns the read channel for the messages that are returned by
  208. // the broker.
  209. Messages() <-chan *ConsumerMessage
  210. // Errors returns a read channel of errors that occurred during consuming, if
  211. // enabled. By default, errors are logged and not returned over this channel.
  212. // If you want to implement any custom error handling, set your config's
  213. // Consumer.Return.Errors setting to true, and read from this channel.
  214. Errors() <-chan *ConsumerError
  215. // HighWaterMarkOffset returns the high water mark offset of the partition,
  216. // i.e. the offset that will be used for the next message that will be produced.
  217. // You can use this to determine how far behind the processing is.
  218. HighWaterMarkOffset() int64
  219. }
  220. type partitionConsumer struct {
  221. consumer *consumer
  222. conf *Config
  223. topic string
  224. partition int32
  225. broker *brokerConsumer
  226. messages chan *ConsumerMessage
  227. errors chan *ConsumerError
  228. feeder chan *FetchResponse
  229. trigger, dying chan none
  230. responseResult error
  231. fetchSize int32
  232. offset int64
  233. highWaterMarkOffset int64
  234. }
  235. var errTimedOut = errors.New("timed out feeding messages to the user") // not user-facing
  236. func (child *partitionConsumer) sendError(err error) {
  237. cErr := &ConsumerError{
  238. Topic: child.topic,
  239. Partition: child.partition,
  240. Err: err,
  241. }
  242. if child.conf.Consumer.Return.Errors {
  243. child.errors <- cErr
  244. } else {
  245. Logger.Println(cErr)
  246. }
  247. }
  248. func (child *partitionConsumer) dispatcher() {
  249. for _ = range child.trigger {
  250. select {
  251. case <-child.dying:
  252. close(child.trigger)
  253. case <-time.After(child.conf.Consumer.Retry.Backoff):
  254. if child.broker != nil {
  255. child.consumer.unrefBrokerConsumer(child.broker)
  256. child.broker = nil
  257. }
  258. Logger.Printf("consumer/%s/%d finding new broker\n", child.topic, child.partition)
  259. if err := child.dispatch(); err != nil {
  260. child.sendError(err)
  261. child.trigger <- none{}
  262. }
  263. }
  264. }
  265. if child.broker != nil {
  266. child.consumer.unrefBrokerConsumer(child.broker)
  267. }
  268. child.consumer.removeChild(child)
  269. close(child.feeder)
  270. }
  271. func (child *partitionConsumer) dispatch() error {
  272. if err := child.consumer.client.RefreshMetadata(child.topic); err != nil {
  273. return err
  274. }
  275. var leader *Broker
  276. var err error
  277. if leader, err = child.consumer.client.Leader(child.topic, child.partition); err != nil {
  278. return err
  279. }
  280. child.broker = child.consumer.refBrokerConsumer(leader)
  281. child.broker.input <- child
  282. return nil
  283. }
  284. func (child *partitionConsumer) chooseStartingOffset(offset int64) error {
  285. newestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetNewest)
  286. if err != nil {
  287. return err
  288. }
  289. oldestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetOldest)
  290. if err != nil {
  291. return err
  292. }
  293. switch {
  294. case offset == OffsetNewest:
  295. child.offset = newestOffset
  296. case offset == OffsetOldest:
  297. child.offset = oldestOffset
  298. case offset >= oldestOffset && offset <= newestOffset:
  299. child.offset = offset
  300. default:
  301. return ErrOffsetOutOfRange
  302. }
  303. return nil
  304. }
  305. func (child *partitionConsumer) Messages() <-chan *ConsumerMessage {
  306. return child.messages
  307. }
  308. func (child *partitionConsumer) Errors() <-chan *ConsumerError {
  309. return child.errors
  310. }
  311. func (child *partitionConsumer) AsyncClose() {
  312. // this triggers whatever broker owns this child to abandon it and close its trigger channel, which causes
  313. // the dispatcher to exit its loop, which removes it from the consumer then closes its 'messages' and
  314. // 'errors' channel (alternatively, if the child is already at the dispatcher for some reason, that will
  315. // also just close itself)
  316. close(child.dying)
  317. }
  318. func (child *partitionConsumer) Close() error {
  319. child.AsyncClose()
  320. go withRecover(func() {
  321. for _ = range child.messages {
  322. // drain
  323. }
  324. })
  325. var errors ConsumerErrors
  326. for err := range child.errors {
  327. errors = append(errors, err)
  328. }
  329. if len(errors) > 0 {
  330. return errors
  331. }
  332. return nil
  333. }
  334. func (child *partitionConsumer) HighWaterMarkOffset() int64 {
  335. return atomic.LoadInt64(&child.highWaterMarkOffset)
  336. }
  337. func (child *partitionConsumer) responseFeeder() {
  338. var msgs []*ConsumerMessage
  339. feederLoop:
  340. for response := range child.feeder {
  341. msgs, child.responseResult = child.parseResponse(response)
  342. for i, msg := range msgs {
  343. select {
  344. case child.messages <- msg:
  345. case <-time.After(child.conf.Consumer.MaxProcessingTime):
  346. child.responseResult = errTimedOut
  347. child.broker.acks.Done()
  348. for _, msg = range msgs[i:] {
  349. child.messages <- msg
  350. }
  351. child.broker.input <- child
  352. continue feederLoop
  353. }
  354. }
  355. child.broker.acks.Done()
  356. }
  357. close(child.messages)
  358. close(child.errors)
  359. }
  360. func (child *partitionConsumer) parseResponse(response *FetchResponse) ([]*ConsumerMessage, error) {
  361. block := response.GetBlock(child.topic, child.partition)
  362. if block == nil {
  363. return nil, ErrIncompleteResponse
  364. }
  365. if block.Err != ErrNoError {
  366. return nil, block.Err
  367. }
  368. if len(block.MsgSet.Messages) == 0 {
  369. // We got no messages. If we got a trailing one then we need to ask for more data.
  370. // Otherwise we just poll again and wait for one to be produced...
  371. if block.MsgSet.PartialTrailingMessage {
  372. if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize == child.conf.Consumer.Fetch.Max {
  373. // we can't ask for more data, we've hit the configured limit
  374. child.sendError(ErrMessageTooLarge)
  375. child.offset++ // skip this one so we can keep processing future messages
  376. } else {
  377. child.fetchSize *= 2
  378. if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize > child.conf.Consumer.Fetch.Max {
  379. child.fetchSize = child.conf.Consumer.Fetch.Max
  380. }
  381. }
  382. }
  383. return nil, nil
  384. }
  385. // we got messages, reset our fetch size in case it was increased for a previous request
  386. child.fetchSize = child.conf.Consumer.Fetch.Default
  387. atomic.StoreInt64(&child.highWaterMarkOffset, block.HighWaterMarkOffset)
  388. incomplete := false
  389. prelude := true
  390. var messages []*ConsumerMessage
  391. for _, msgBlock := range block.MsgSet.Messages {
  392. for _, msg := range msgBlock.Messages() {
  393. if prelude && msg.Offset < child.offset {
  394. continue
  395. }
  396. prelude = false
  397. if msg.Offset >= child.offset {
  398. messages = append(messages, &ConsumerMessage{
  399. Topic: child.topic,
  400. Partition: child.partition,
  401. Key: msg.Msg.Key,
  402. Value: msg.Msg.Value,
  403. Offset: msg.Offset,
  404. Timestamp: msg.Msg.Timestamp,
  405. })
  406. child.offset = msg.Offset + 1
  407. } else {
  408. incomplete = true
  409. }
  410. }
  411. }
  412. if incomplete || len(messages) == 0 {
  413. return nil, ErrIncompleteResponse
  414. }
  415. return messages, nil
  416. }
  417. // brokerConsumer
  418. type brokerConsumer struct {
  419. consumer *consumer
  420. broker *Broker
  421. input chan *partitionConsumer
  422. newSubscriptions chan []*partitionConsumer
  423. wait chan none
  424. subscriptions map[*partitionConsumer]none
  425. acks sync.WaitGroup
  426. refs int
  427. }
  428. func (c *consumer) newBrokerConsumer(broker *Broker) *brokerConsumer {
  429. bc := &brokerConsumer{
  430. consumer: c,
  431. broker: broker,
  432. input: make(chan *partitionConsumer),
  433. newSubscriptions: make(chan []*partitionConsumer),
  434. wait: make(chan none),
  435. subscriptions: make(map[*partitionConsumer]none),
  436. refs: 0,
  437. }
  438. go withRecover(bc.subscriptionManager)
  439. go withRecover(bc.subscriptionConsumer)
  440. return bc
  441. }
  442. func (bc *brokerConsumer) subscriptionManager() {
  443. var buffer []*partitionConsumer
  444. // The subscriptionManager constantly accepts new subscriptions on `input` (even when the main subscriptionConsumer
  445. // goroutine is in the middle of a network request) and batches it up. The main worker goroutine picks
  446. // up a batch of new subscriptions between every network request by reading from `newSubscriptions`, so we give
  447. // it nil if no new subscriptions are available. We also write to `wait` only when new subscriptions is available,
  448. // so the main goroutine can block waiting for work if it has none.
  449. for {
  450. if len(buffer) > 0 {
  451. select {
  452. case event, ok := <-bc.input:
  453. if !ok {
  454. goto done
  455. }
  456. buffer = append(buffer, event)
  457. case bc.newSubscriptions <- buffer:
  458. buffer = nil
  459. case bc.wait <- none{}:
  460. }
  461. } else {
  462. select {
  463. case event, ok := <-bc.input:
  464. if !ok {
  465. goto done
  466. }
  467. buffer = append(buffer, event)
  468. case bc.newSubscriptions <- nil:
  469. }
  470. }
  471. }
  472. done:
  473. close(bc.wait)
  474. if len(buffer) > 0 {
  475. bc.newSubscriptions <- buffer
  476. }
  477. close(bc.newSubscriptions)
  478. }
  479. func (bc *brokerConsumer) subscriptionConsumer() {
  480. <-bc.wait // wait for our first piece of work
  481. // the subscriptionConsumer ensures we will get nil right away if no new subscriptions is available
  482. for newSubscriptions := range bc.newSubscriptions {
  483. bc.updateSubscriptions(newSubscriptions)
  484. if len(bc.subscriptions) == 0 {
  485. // We're about to be shut down or we're about to receive more subscriptions.
  486. // Either way, the signal just hasn't propagated to our goroutine yet.
  487. <-bc.wait
  488. continue
  489. }
  490. response, err := bc.fetchNewMessages()
  491. if err != nil {
  492. Logger.Printf("consumer/broker/%d disconnecting due to error processing FetchRequest: %s\n", bc.broker.ID(), err)
  493. bc.abort(err)
  494. return
  495. }
  496. bc.acks.Add(len(bc.subscriptions))
  497. for child := range bc.subscriptions {
  498. child.feeder <- response
  499. }
  500. bc.acks.Wait()
  501. bc.handleResponses()
  502. }
  503. }
  504. func (bc *brokerConsumer) updateSubscriptions(newSubscriptions []*partitionConsumer) {
  505. for _, child := range newSubscriptions {
  506. bc.subscriptions[child] = none{}
  507. Logger.Printf("consumer/broker/%d added subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
  508. }
  509. for child := range bc.subscriptions {
  510. select {
  511. case <-child.dying:
  512. Logger.Printf("consumer/broker/%d closed dead subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
  513. close(child.trigger)
  514. delete(bc.subscriptions, child)
  515. default:
  516. break
  517. }
  518. }
  519. }
  520. func (bc *brokerConsumer) handleResponses() {
  521. // handles the response codes left for us by our subscriptions, and abandons ones that have been closed
  522. for child := range bc.subscriptions {
  523. result := child.responseResult
  524. child.responseResult = nil
  525. switch result {
  526. case nil:
  527. break
  528. case errTimedOut:
  529. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because consuming was taking too long\n",
  530. bc.broker.ID(), child.topic, child.partition)
  531. delete(bc.subscriptions, child)
  532. case ErrOffsetOutOfRange:
  533. // there's no point in retrying this it will just fail the same way again
  534. // shut it down and force the user to choose what to do
  535. child.sendError(result)
  536. Logger.Printf("consumer/%s/%d shutting down because %s\n", child.topic, child.partition, result)
  537. close(child.trigger)
  538. delete(bc.subscriptions, child)
  539. case ErrUnknownTopicOrPartition, ErrNotLeaderForPartition, ErrLeaderNotAvailable, ErrReplicaNotAvailable:
  540. // not an error, but does need redispatching
  541. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
  542. bc.broker.ID(), child.topic, child.partition, result)
  543. child.trigger <- none{}
  544. delete(bc.subscriptions, child)
  545. default:
  546. // dunno, tell the user and try redispatching
  547. child.sendError(result)
  548. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
  549. bc.broker.ID(), child.topic, child.partition, result)
  550. child.trigger <- none{}
  551. delete(bc.subscriptions, child)
  552. }
  553. }
  554. }
  555. func (bc *brokerConsumer) abort(err error) {
  556. bc.consumer.abandonBrokerConsumer(bc)
  557. _ = bc.broker.Close() // we don't care about the error this might return, we already have one
  558. for child := range bc.subscriptions {
  559. child.sendError(err)
  560. child.trigger <- none{}
  561. }
  562. for newSubscriptions := range bc.newSubscriptions {
  563. if len(newSubscriptions) == 0 {
  564. <-bc.wait
  565. continue
  566. }
  567. for _, child := range newSubscriptions {
  568. child.sendError(err)
  569. child.trigger <- none{}
  570. }
  571. }
  572. }
  573. func (bc *brokerConsumer) fetchNewMessages() (*FetchResponse, error) {
  574. request := &FetchRequest{
  575. MinBytes: bc.consumer.conf.Consumer.Fetch.Min,
  576. MaxWaitTime: int32(bc.consumer.conf.Consumer.MaxWaitTime / time.Millisecond),
  577. }
  578. if bc.consumer.conf.Version.IsAtLeast(V0_10_0_0) {
  579. request.Version = 2
  580. }
  581. for child := range bc.subscriptions {
  582. request.AddBlock(child.topic, child.partition, child.offset, child.fetchSize)
  583. }
  584. return bc.broker.Fetch(request)
  585. }