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

使用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
                }
            }
        }()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器