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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配