duancong7358
2015-04-01 18:24
浏览 248
已采纳

使用Shopify Sarama的Kafka错误处理

So I am trying to use Kafka for my application which has a producer logging actions into the Kafka MQ and the consumer which reads it off the MQ.Since my application is in Go, I am using the Shopify Sarama to make this possible.

Right now, I'm able to read off the MQ and print the message contents using a

fmt.Printf

Howeveer, I would really like the error handling to be better than console printing and I am willing to go the extra mile.

Code right now for consumer connection:

mqCfg := sarama.NewConfig()

master, err := sarama.NewConsumer([]string{brokerConnect}, mqCfg)
if err != nil {
    panic(err) // Don't want to panic when error occurs, instead handle it
}

And the processing of messages:

    go func() {
    defer wg.Done()
    for message := range consumer.Messages() {
        var msgContent Message
        _ = json.Unmarshal(message.Value, &msgContent)
        fmt.Printf("Reading message of type %s with id : %d
", msgContent.Type, msgContent.ContentId) //Don't want to print it
    }
}()

My questions (I am new to testing Kafka and new to kafka in general):

  1. Where could errors occur in the above program, so that I can handle them? Any sample code will be great for me to start with. The error conditions I could think of are when the msgContent doesn't really contain any Type of ContentId fields in the JSON.

  2. In kafka, are there situations when the consumer is trying to read at the current offset, but for some reason was not able to (even when the JSON is well formed)? Is it possible for my consumer to backtrack to say x steps above the failed offset read and re-process the offsets? Or is there a better way to do this? again, what could these situations be?

I'm open to reading and trying things.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulin6088 2015-04-02 02:58
    已采纳

    Regarding 1) Check where I log error messages below. This is more or less what I would do.

    Regarding 2) I don't know about trying to walk backwards in a topic. Its very much possible by just creating a consumer over and over, with its starting offset minus one each time. But I wouldn't advise it, as most likely you'll end up replaying the same message over and over. I do advice saving your offset often so you can recover if things go south.

    Below is code that I believe addresses most of your questions. I haven't tried compiling this. And sarama api has been changing lately, so the api may currently differ a bit.

    func StartKafkaReader(wg *sync.WaitGroup, lastgoodoff int64, out chan<- *Message) (error) {
        wg.Add(1)
        go func(){
            defer wg.Done()
            //to track the last known good offset we processed, which is 
            // updated after each successfully processed event. 
            saveprogress := func(off int64){
                //Save the offset somewhere...a file... 
                //Ive also used kafka to store progress 
                //using a special topic as a WAL
            }
            defer saveprogress(lastgoodoffset)
    
            client, err := sarama.NewClient("clientId", brokers, sarama.NewClientConfig())
            if err != nil {
                log.Error(err)
                return
            }
            defer client.Close()
            sarama.NewConsumerConfig()
            consumerConfig.OffsetMethod = sarama.OffsetMethodManual
            consumerConfig.OffsetValue = int64(lastgoodoff)
            consumer, err := sarama.NewConsumer(client, topic, partition, "consumerId", consumerConfig)
            if err != nil {
                log.Error(err)
                return
            }
            defer consumer.Close()
            for {
                select {
                case event := <-consumer.Events():
                    if event.Err != nil {
                        log.Error(event.Err)
                        return
                    }
                    msgContent := &Message{}
                    err = json.Unmarshal(message.Value, msgContent)
                    if err != nil {
                        log.Error(err)
                        continue //continue to skip this message or return to stop without updating the offset.
                    }
                    // Send the message on to be processed.
                    out <- msgContent 
    
                    lastgoodoff = event.Offset
                }
            }
        }()
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题