Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #832] Client may submit wrong offset when network instability #833

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/apache/rocketmq-client-go/v2/errors"

jsoniter "github.com/json-iterator/go"
"github.com/tidwall/gjson"

Expand Down Expand Up @@ -699,8 +700,9 @@ func (dc *defaultConsumer) updateProcessQueueTable(topic string, mqs []*primitiv
continue
}
dc.storage.remove(&mq)
nextOffset := dc.computePullFromWhere(&mq)
if nextOffset >= 0 {
nextOffset, err := dc.computePullFromWhereWithException(&mq)

if nextOffset >= 0 && err == nil {
_, exist := dc.processQueueTable.Load(mq)
if exist {
rlog.Debug("do defaultConsumer, mq already exist", map[string]interface{}{
Expand Down Expand Up @@ -741,12 +743,23 @@ func (dc *defaultConsumer) removeUnnecessaryMessageQueue(mq *primitive.MessageQu
return true
}

// Deprecated: Use computePullFromWhereWithException instead.
func (dc *defaultConsumer) computePullFromWhere(mq *primitive.MessageQueue) int64 {
result, _ := dc.computePullFromWhereWithException(mq)
return result
}

func (dc *defaultConsumer) computePullFromWhereWithException(mq *primitive.MessageQueue) (int64, error) {
if dc.cType == _PullConsume {
return 0
return 0, nil
}
var result = int64(-1)
lastOffset := dc.storage.read(mq, _ReadFromStore)
result := int64(-1)
lastOffset, err := dc.storage.readWithException(mq, _ReadFromStore)
if err != nil {
// 这里 lastOffset = -1
return lastOffset, err
}

if lastOffset >= 0 {
result = lastOffset
} else {
Expand Down Expand Up @@ -803,7 +816,7 @@ func (dc *defaultConsumer) computePullFromWhere(mq *primitive.MessageQueue) int6
default:
}
}
return result
return result, nil
}

func (dc *defaultConsumer) pullInner(ctx context.Context, queue *primitive.MessageQueue, data *internal.SubscriptionData,
Expand Down Expand Up @@ -950,7 +963,8 @@ func (dc *defaultConsumer) queryMaxOffset(mq *primitive.MessageQueue) (int64, er
}

func (dc *defaultConsumer) queryOffset(mq *primitive.MessageQueue) int64 {
return dc.storage.read(mq, _ReadMemoryThenStore)
result, _ := dc.storage.readWithException(mq, _ReadMemoryThenStore)
return result
}

// SearchOffsetByTimestamp with specific queueId and topic
Expand Down
24 changes: 16 additions & 8 deletions consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/apache/rocketmq-client-go/v2/errors"
"github.com/apache/rocketmq-client-go/v2/internal"
"github.com/apache/rocketmq-client-go/v2/internal/remote"
"github.com/apache/rocketmq-client-go/v2/primitive"
Expand Down Expand Up @@ -116,13 +117,20 @@ func TestComputePullFromWhere(t *testing.T) {
rmqCli.SetNameSrv(namesrvCli)

Convey("get effective offset", func() {
offsetStore.EXPECT().read(gomock.Any(), gomock.Any()).Return(int64(10))
res := dc.computePullFromWhere(mq)
offsetStore.EXPECT().readWithException(gomock.Any(), gomock.Any()).Return(int64(10), nil)
res, _ := dc.computePullFromWhereWithException(mq)
assert.Equal(t, int64(10), res)
})

Convey("get offset error", func() {
offsetStore.EXPECT().readWithException(gomock.Any(), gomock.Any()).Return(int64(-1), errors.ErrRequestTimeout)

_, err := dc.computePullFromWhereWithException(mq)
assert.Equal(t, err, errors.ErrRequestTimeout)
})

Convey("ConsumeFromLastOffset for normal topic", func() {
offsetStore.EXPECT().read(gomock.Any(), gomock.Any()).Return(int64(-1))
offsetStore.EXPECT().readWithException(gomock.Any(), gomock.Any()).Return(int64(-1), nil)
dc.option.FromWhere = ConsumeFromLastOffset

broker := "a"
Expand All @@ -135,20 +143,20 @@ func TestComputePullFromWhere(t *testing.T) {
},
}, nil)

res := dc.computePullFromWhere(mq)
res, _ := dc.computePullFromWhereWithException(mq)
assert.Equal(t, int64(20), res)
})

Convey("ConsumeFromFirstOffset for normal topic", func() {
offsetStore.EXPECT().read(gomock.Any(), gomock.Any()).Return(int64(-1))
offsetStore.EXPECT().readWithException(gomock.Any(), gomock.Any()).Return(int64(-1), nil)
dc.option.FromWhere = ConsumeFromFirstOffset

res := dc.computePullFromWhere(mq)
res, _ := dc.computePullFromWhereWithException(mq)
assert.Equal(t, int64(0), res)
})

Convey("ConsumeFromTimestamp for normal topic", func() {
offsetStore.EXPECT().read(gomock.Any(), gomock.Any()).Return(int64(-1))
offsetStore.EXPECT().readWithException(gomock.Any(), gomock.Any()).Return(int64(-1), nil)
dc.option.FromWhere = ConsumeFromTimestamp

dc.option.ConsumeTimestamp = "20060102150405"
Expand All @@ -163,7 +171,7 @@ func TestComputePullFromWhere(t *testing.T) {
},
}, nil)

res := dc.computePullFromWhere(mq)
res, _ := dc.computePullFromWhereWithException(mq)
assert.Equal(t, int64(30), res)
})

Expand Down
63 changes: 43 additions & 20 deletions consumer/mock_offset_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions consumer/offset_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type OffsetStore interface {
persist(mqs []*primitive.MessageQueue)
remove(mq *primitive.MessageQueue)
read(mq *primitive.MessageQueue, t readType) int64
readWithException(mq *primitive.MessageQueue, t readType) (int64, error)
update(mq *primitive.MessageQueue, offset int64, increaseOnly bool)
}

Expand Down Expand Up @@ -156,21 +157,27 @@ func (local *localFileOffsetStore) load() {
}
}

// Deprecated: Use readWithException instead.
func (local *localFileOffsetStore) read(mq *primitive.MessageQueue, t readType) int64 {
result, _ := local.readWithException(mq, t)
return result
}

func (local *localFileOffsetStore) readWithException(mq *primitive.MessageQueue, t readType) (int64, error) {
switch t {
case _ReadFromMemory, _ReadMemoryThenStore:
off := readFromMemory(local.OffsetTable, mq)
if off >= 0 || (off == -1 && t == _ReadFromMemory) {
return off
return off, nil
}
fallthrough
case _ReadFromStore:
local.load()
return readFromMemory(local.OffsetTable, mq)
return readFromMemory(local.OffsetTable, mq), nil
default:

}
return -1
return -1, nil
}

func (local *localFileOffsetStore) update(mq *primitive.MessageQueue, offset int64, increaseOnly bool) {
Expand Down Expand Up @@ -284,18 +291,24 @@ func (r *remoteBrokerOffsetStore) remove(mq *primitive.MessageQueue) {
})
}

// Deprecated: Use readWithException instead.
func (r *remoteBrokerOffsetStore) read(mq *primitive.MessageQueue, t readType) int64 {
result, _ := r.readWithException(mq, t)
return result
}

func (r *remoteBrokerOffsetStore) readWithException(mq *primitive.MessageQueue, t readType) (int64, error) {
r.mutex.RLock()
switch t {
case _ReadFromMemory, _ReadMemoryThenStore:
off, exist := r.OffsetTable[*mq]
if exist {
r.mutex.RUnlock()
return off
return off, nil
}
if t == _ReadFromMemory {
r.mutex.RUnlock()
return -1
return -1, nil
}
fallthrough
case _ReadFromStore:
Expand All @@ -307,7 +320,7 @@ func (r *remoteBrokerOffsetStore) read(mq *primitive.MessageQueue, t readType) i
rlog.LogKeyUnderlayError: err,
})
r.mutex.RUnlock()
return -1
return -1, err
}
rlog.Warning("fetch offset of mq from broker success", map[string]interface{}{
rlog.LogKeyConsumerGroup: r.group,
Expand All @@ -316,11 +329,11 @@ func (r *remoteBrokerOffsetStore) read(mq *primitive.MessageQueue, t readType) i
})
r.mutex.RUnlock()
r.update(mq, off, true)
return off
return off, nil
default:
}

return -1
return -1, nil
}

func (r *remoteBrokerOffsetStore) update(mq *primitive.MessageQueue, offset int64, increaseOnly bool) {
Expand Down
Loading