Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -327,21 +327,21 @@ public ConcurrentMap<String, ConcurrentMap<Integer, Long>> getPullOffsetTable()
public Map<Integer, Long> queryMinOffsetInAllGroup(final String topic, final String filterGroups) {

Map<Integer, Long> queueMinOffset = new HashMap<>();
Set<String> topicGroups = this.offsetTable.keySet();
// Work on a snapshot of the keys: offsetTable must never be mutated by a query.
Set<String> topicGroups = new HashSet<>(this.offsetTable.keySet());
if (!UtilAll.isBlank(filterGroups)) {
for (String group : filterGroups.split(",")) {
Iterator<String> it = topicGroups.iterator();
while (it.hasNext()) {
String topicAtGroup = it.next();
if (group.equals(topicAtGroup.split(TOPIC_GROUP_SEPARATOR)[1])) {
it.remove();
removeConsumerOffset(topicAtGroup);
}
}
topicGroups.removeIf(topicAtGroup -> {
String[] arrays = topicAtGroup.split(TOPIC_GROUP_SEPARATOR);
return arrays.length == 2 && group.equals(arrays[1]);
});
}
}

for (Map.Entry<String, ConcurrentMap<Integer, Long>> offSetEntry : this.offsetTable.entrySet()) {
if (!topicGroups.contains(offSetEntry.getKey())) {
continue;
}
String topicGroup = offSetEntry.getKey();
String[] topicGroupArr = topicGroup.split(TOPIC_GROUP_SEPARATOR);
if (topic.equals(topicGroupArr[0])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import org.apache.rocketmq.broker.BrokerController;
import org.apache.rocketmq.common.BrokerConfig;
import org.apache.rocketmq.store.MessageStore;
import org.apache.rocketmq.store.config.MessageStoreConfig;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.mockito.Mockito;
Expand Down Expand Up @@ -121,4 +123,54 @@ public void testEraseResetOffset() {
Assert.assertFalse(consumerOffsetManager.hasOffsetReset(topic, group, 1));
Assert.assertFalse(consumerOffsetManager.resetOffsetTable.containsKey(key));
}

@Test
public void testQueryMinOffsetInAllGroupDoesNotDeleteOffsets() {
Mockito.when(brokerController.getBrokerConfig()).thenReturn(new BrokerConfig());
MessageStore messageStore = Mockito.mock(MessageStore.class);
Mockito.when(brokerController.getMessageStore()).thenReturn(messageStore);
Mockito.when(messageStore.getMinOffsetInQueue(Mockito.anyString(), Mockito.anyInt())).thenReturn(0L);

String topic = "Topic";
String group1 = "G1";
String group2 = "G2";
ConcurrentHashMap<Integer, Long> offsets1 = new ConcurrentHashMap<>();
offsets1.put(0, 50L);
ConcurrentHashMap<Integer, Long> offsets2 = new ConcurrentHashMap<>();
offsets2.put(0, 30L);
ConcurrentHashMap<String, ConcurrentMap<Integer, Long>> offsetTable = new ConcurrentHashMap<>();
offsetTable.put(topic + TOPIC_GROUP_SEPARATOR + group1, offsets1);
offsetTable.put(topic + TOPIC_GROUP_SEPARATOR + group2, offsets2);
consumerOffsetManager.setOffsetTable(offsetTable);

// filtering out G2 must exclude its offsets from the min computation
Map<Integer, Long> result = consumerOffsetManager.queryMinOffsetInAllGroup(topic, group2);
assertThat(result).containsEntry(0, 50L);

// but the query must not destroy the filtered group's offsets
assertThat(offsetTable).containsKey(topic + TOPIC_GROUP_SEPARATOR + group2);
assertThat(consumerOffsetManager.queryOffset(group2, topic, 0)).isEqualTo(30L);

// without filter, the min across all groups is returned
result = consumerOffsetManager.queryMinOffsetInAllGroup(topic, "");
assertThat(result).containsEntry(0, 30L);
}

@Test
public void testQueryMinOffsetInAllGroupToleratesMalformedKeys() {
Mockito.when(brokerController.getBrokerConfig()).thenReturn(new BrokerConfig());
MessageStore messageStore = Mockito.mock(MessageStore.class);
Mockito.when(brokerController.getMessageStore()).thenReturn(messageStore);
Mockito.when(messageStore.getMinOffsetInQueue(Mockito.anyString(), Mockito.anyInt())).thenReturn(0L);

String topic = "Topic";
ConcurrentHashMap<String, ConcurrentMap<Integer, Long>> offsetTable = new ConcurrentHashMap<>();
offsetTable.put(topic + TOPIC_GROUP_SEPARATOR + "G1", new ConcurrentHashMap<>());
// malformed key without '@' must not break the query
offsetTable.put("MalformedKey", new ConcurrentHashMap<>());
consumerOffsetManager.setOffsetTable(offsetTable);

assertThat(consumerOffsetManager.queryMinOffsetInAllGroup(topic, "G1")).isEmpty();
assertThat(consumerOffsetManager.queryMinOffsetInAllGroup(topic, "")).isEmpty();
}
}
Loading