前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊rocketmq-client-go的localFileOffsetStore

聊聊rocketmq-client-go的localFileOffsetStore

原创
作者头像
code4it
修改2020-07-13 09:58:32
4210
修改2020-07-13 09:58:32
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下rocketmq-client-go的localFileOffsetStore

OffsetStore

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
type OffsetStore interface {
    persist(mqs []*primitive.MessageQueue)
    remove(mq *primitive.MessageQueue)
    read(mq *primitive.MessageQueue, t readType) int64
    update(mq *primitive.MessageQueue, offset int64, increaseOnly bool)
}
  • OffsetStore定义了persist、remove、read、update方法

localFileOffsetStore

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
type localFileOffsetStore struct {
    group       string
    path        string
    OffsetTable map[MessageQueueKey]int64
    // mutex for offset file
    mutex sync.Mutex
}
  • localFileOffsetStore定义了group、path、OffsetTable、mutex属性

NewLocalFileOffsetStore

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
func NewLocalFileOffsetStore(clientID, group string) OffsetStore {
    store := &localFileOffsetStore{
        group:       group,
        path:        filepath.Join(_LocalOffsetStorePath, clientID, group, "offset.json"),
        OffsetTable: make(map[MessageQueueKey]int64),
    }
    store.load()
    return store
}
  • NewLocalFileOffsetStore创建localFileOffsetStore,然后执行store.load()

load

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
func (local *localFileOffsetStore) load() {
    local.mutex.Lock()
    defer local.mutex.Unlock()
    data, err := utils.FileReadAll(local.path)
    if os.IsNotExist(err) {
        return
    }
    if err != nil {
        rlog.Info("read from local store error, try to use bak file", map[string]interface{}{
            rlog.LogKeyUnderlayError: err,
        })
        data, err = utils.FileReadAll(filepath.Join(local.path, ".bak"))
    }
    if err != nil {
        rlog.Info("read from local store bak file error", map[string]interface{}{
            rlog.LogKeyUnderlayError: err,
        })
        return
    }
    datas := make(map[MessageQueueKey]int64)
​
    wrapper := OffsetSerializeWrapper{
        OffsetTable: datas,
    }
​
    err = jsoniter.Unmarshal(data, &wrapper)
    if err != nil {
        rlog.Warning("unmarshal local offset error", map[string]interface{}{
            "local_path":             local.path,
            rlog.LogKeyUnderlayError: err.Error(),
        })
        return
    }
​
    if datas != nil {
        local.OffsetTable = datas
    }
}
  • load方法通过utils.FileReadAll(local.path)读取data,然后通过jsoniter.Unmarshal(data, &wrapper)将数据组装到local.OffsetTable

read

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
func (local *localFileOffsetStore) read(mq *primitive.MessageQueue, t readType) int64 {
    switch t {
    case _ReadFromMemory, _ReadMemoryThenStore:
        off := readFromMemory(local.OffsetTable, mq)
        if off >= 0 || (off == -1 && t == _ReadFromMemory) {
            return off
        }
        fallthrough
    case _ReadFromStore:
        local.load()
        return readFromMemory(local.OffsetTable, mq)
    default:
​
    }
    return -1
}
  • read方法根据readType来执行是readFromMemory还是执行ReadFromStore

update

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
func (local *localFileOffsetStore) update(mq *primitive.MessageQueue, offset int64, increaseOnly bool) {
    local.mutex.Lock()
    defer local.mutex.Unlock()
    rlog.Debug("update offset", map[string]interface{}{
        rlog.LogKeyMessageQueue: mq,
        "new_offset":            offset,
    })
    key := MessageQueueKey(*mq)
    localOffset, exist := local.OffsetTable[key]
    if !exist {
        local.OffsetTable[key] = offset
        return
    }
    if increaseOnly {
        if localOffset < offset {
            local.OffsetTable[key] = offset
        }
    } else {
        local.OffsetTable[key] = offset
    }
}
  • update方法更新local.OffsetTable[key]

persist

rocketmq-client-go-v2.0.0/consumer/offset_store.go

代码语言:javascript
复制
func (local *localFileOffsetStore) persist(mqs []*primitive.MessageQueue) {
    if len(mqs) == 0 {
        return
    }
    local.mutex.Lock()
    defer local.mutex.Unlock()
​
    wrapper := OffsetSerializeWrapper{
        OffsetTable: local.OffsetTable,
    }
​
    data, _ := jsoniter.Marshal(wrapper)
    utils.CheckError(fmt.Sprintf("persist offset to %s", local.path), utils.WriteToFile(local.path, data))
}
  • persist方法执行utils.WriteToFile(local.path, data)

小结

OffsetStore定义了persist、remove、read、update方法;localFileOffsetStore定义了group、path、OffsetTable、mutex属性

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OffsetStore
  • localFileOffsetStore
  • NewLocalFileOffsetStore
  • load
  • read
  • update
  • persist
  • 小结
  • doc
相关产品与服务
消息队列 CMQ 版
消息队列 CMQ 版(TDMQ for CMQ,简称 TDMQ CMQ 版)是一款分布式高可用的消息队列服务,它能够提供可靠的,基于消息的异步通信机制,能够将分布式部署的不同应用(或同一应用的不同组件)中的信息传递,存储在可靠有效的 CMQ 队列中,防止消息丢失。TDMQ CMQ 版支持多进程同时读写,收发互不干扰,无需各应用或组件始终处于运行状态。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档