consumer.go 20 KB

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