apache / apache/rocketmq

[Bug] TransactionalMessageService may check unrelated producer in the same producer group

Open
#9,791 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
22.6k
Forks
12k
Avg merge
2d 20h
Merged PRs (30d)
26

Description

### Before Creating the Bug Report

- [x] I found a bug, not just asking a question, which should be created in [GitHub Discussions](https://github.com/apache/rocketmq/discussions).

- [x] I have searched the [GitHub Issues](https://github.com/apache/rocketmq/issues) and [GitHub Discussions](https://github.com/apache/rocketmq/discussions) of this repository and believe that this is not a duplicate.

- [x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.

### Runtime platform environment

any

### RocketMQ version

develop

### JDK Version

any

### Describe the Bug

Theoretically producers that belongs to the same producer group should have the same behavior, but rocketmq didn't define this constraint in code, which means users could run several producers that pub messages to different topics in only one producer group.
In transaction message check process, broker would select a active producer channel and send check request to get local transaction state, by calling `getAvailableChannel`. As shown, the selection may not differentiate producers, for broker think they are the same, even though there is no such mechanism to guarantee this.
The above description may lead to the following results: users may misuse producer group, and the check state request may be sent to the wrong producer, and get wrong result, which happened in our production.
```
private final ConcurrentMap> groupChannelTable =
new ConcurrentHashMap<>();
...
public Channel getAvailableChannel(String groupId) {
if (groupId == null) {
return null;
}
List channelList;
ConcurrentMap channelClientChannelInfoHashMap = groupChannelTable.get(groupId);
if (channelClientChannelInfoHashMap != null) {
channelList = new ArrayList<>(channelClientChannelInfoHashMap.keySet());
} else {
log.warn("Check transaction failed, channel table is empty. groupId={}", groupId);
return null;
}

int size = channelList.size();
if (0 == size) {
log.warn("Channel list is empty. groupId={}", groupId);
return null;
}

Channel lastActiveChannel = null;

int index = positiveAtomicCounter.incrementAndGet() % size;
Channel channel = channelList.get(index);
int count = 0;
boolean isOk = channel.isActive() && channel.isWritable();
while (count++ < GET_AVAILABLE_CHANNEL_RETRY_COUNT) {
if (isOk) {
return channel;
}
if (channel.isActive()) {
lastActiveChannel = channel;
}
index = (++index) % size;
channel = channelList.get(index);
isOk = channel.isActive() && channel.isWritable();
}

return lastActiveChannel;
}
```

### Steps to Reproduce

Run multiple producers, produce some trans msgs to different topics.

### What Did You Expect to See?

Trans msgs should get committed/rollbacked correctly.

### What Did You See Instead?

Trans msgs may be committed/rollbacked wrongly.

### Additional Context

Solution is not clear for now, maybe we can build a lineage mechanism to connect producers with topics?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.