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 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络