consumer.go 24 KB

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