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 @@ -434,6 +434,16 @@ public void removeOffset(final String group) {
"offsetTable={}, resetOffsetTable={}, pullOffsetTable={}", group, clearOffset, clearReset, clearPull);
}

public void removeOffset(final String group, final String topic) {
String key = topic + TOPIC_GROUP_SEPARATOR + group;
ConcurrentMap<Integer, Long> removedOffset = this.offsetTable.remove(key);
this.resetOffsetTable.remove(key);
this.pullOffsetTable.remove(key);
removeConsumerOffset(key);
LOG.info("Remove consumer offset, group={}, topic={}, key={}, removedOffset={}",
group, topic, key, removedOffset);
}

public void assignResetOffset(String topic, String group, int queueId, long offset) {
if (Strings.isNullOrEmpty(topic) || Strings.isNullOrEmpty(group) || queueId < 0 || offset < 0) {
LOG.warn("Illegal arguments when assigning reset offset. Topic={}, group={}, queueId={}, offset={}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
import org.apache.rocketmq.remoting.protocol.header.CreateTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.CreateUserRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteAclRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteConsumerOffsetRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteSubscriptionGroupRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteUserRequestHeader;
Expand Down Expand Up @@ -310,6 +311,8 @@ public RemotingCommand processRequest(ChannelHandlerContext ctx,
return this.getAllSubscriptionGroup(ctx, request);
case RequestCode.DELETE_SUBSCRIPTIONGROUP:
return this.deleteSubscriptionGroup(ctx, request);
case RequestCode.DELETE_CONSUMER_OFFSET:
return this.deleteConsumerOffset(ctx, request);
case RequestCode.GET_TOPIC_STATS_INFO:
return this.getTopicStatsInfo(ctx, request);
case RequestCode.GET_CONSUMER_CONNECTION_LIST:
Expand Down Expand Up @@ -1714,6 +1717,22 @@ private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx,
return response;
}

private RemotingCommand deleteConsumerOffset(ChannelHandlerContext ctx,
RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
DeleteConsumerOffsetRequestHeader requestHeader =
request.decodeCommandCustomHeader(DeleteConsumerOffsetRequestHeader.class);

LOGGER.info("deleteConsumerOffset, caller={}, group={}, topic={}",
RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getConsumerGroup(), requestHeader.getTopic());

this.brokerController.getConsumerOffsetManager().removeOffset(requestHeader.getConsumerGroup(), requestHeader.getTopic());

response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
return response;
}

private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx,
RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ public void removeOffsetByGroupTest() {
Assert.assertEquals(-1L, consumerOffsetManager.queryPullOffset(group, topic, 0));
}

@Test
public void removeOffsetByGroupAndTopic_NotExist() {
consumerOffsetManager.removeOffset("NonExistGroup", "NonExistTopic");
assertThat(consumerOffsetManager.getOffsetTable().containsKey(KEY)).isTrue();
}

@Test
public void removeOffsetByGroupAndTopic_Exist() {
String topic = "TestTopic";
String group = "TestGroup";
String key = topic + TOPIC_GROUP_SEPARATOR + group;
Mockito.when(brokerController.getBrokerConfig()).thenReturn(new BrokerConfig());
consumerOffsetManager.commitOffset("Commit", group, topic, 0, 100);
consumerOffsetManager.assignResetOffset(topic, group, 0, 100);
consumerOffsetManager.commitPullOffset("Pull", group, topic, 0, 100);
assertThat(consumerOffsetManager.getOffsetTable().containsKey(key)).isTrue();

consumerOffsetManager.removeOffset(group, topic);
assertThat(consumerOffsetManager.getOffsetTable().containsKey(key)).isFalse();
Assert.assertEquals(-1L, consumerOffsetManager.queryOffset(group, topic, 0));
Assert.assertEquals(-1L, consumerOffsetManager.queryPullOffset(group, topic, 0));
}

@Test
public void testOffsetPersistInMemory() {
ConcurrentMap<String, ConcurrentMap<Integer, Long>> offsetTable = consumerOffsetManager.getOffsetTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import org.apache.rocketmq.remoting.protocol.header.CreateTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.CreateUserRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteAclRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteConsumerOffsetRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteUserRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.ExchangeHAInfoResponseHeader;
Expand Down Expand Up @@ -1722,6 +1723,17 @@ private RemotingCommand createUpdateBrokerConfigCommand() {
return request;
}

@Test
public void testDeleteConsumerOffset() throws RemotingCommandException {
DeleteConsumerOffsetRequestHeader requestHeader = new DeleteConsumerOffsetRequestHeader();
requestHeader.setConsumerGroup("testGroup");
requestHeader.setTopic("testTopic");
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.DELETE_CONSUMER_OFFSET, requestHeader);
request.makeCustomHeaderToNet();
RemotingCommand response = adminBrokerProcessor.processRequest(handlerContext, request);
assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
}

private boolean notToBeExecuted() {
return MixAll.isMac();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
import org.apache.rocketmq.remoting.protocol.header.CreateTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.CreateUserRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteAclRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteConsumerOffsetRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteSubscriptionGroupRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.DeleteUserRequestHeader;
Expand Down Expand Up @@ -2257,6 +2258,28 @@ public void deleteSubscriptionGroup(final String addr, final String groupName, f
throw new MQClientException(response.getCode(), response.getRemark());
}

public void deleteConsumerOffset(final String addr, final String group, final String topic,
final long timeoutMillis)
throws RemotingException, InterruptedException, MQClientException {
DeleteConsumerOffsetRequestHeader requestHeader = new DeleteConsumerOffsetRequestHeader();
requestHeader.setConsumerGroup(group);
requestHeader.setTopic(topic);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.DELETE_CONSUMER_OFFSET, requestHeader);

RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
request, timeoutMillis);
assert response != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using assert to check for null values ​​is not ideal. Users must add the JVM startup parameter -ea during configuration for it to take effect; otherwise, there is a risk of NPE errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kris20030907 There are many similar issues in MQClientAPIImpl. We may need to open a separate issue for discussion.

Copy link
Contributor

@Kris20030907 Kris20030907 Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think this is necessary; other projects have encountered similar issues in production environments.
cc.@RongtongJin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kris20030907 From your perspective, i'd like to ask whether this features is necessary for users. we have encountered many users with similar demands in the production environment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an optimization point. Without adding the -ea parameter, the assert logic here will not take effect. People who are unaware of this feature might be misled when troubleshooting, because IDEA automatically adds this parameter, which may differ from the actual environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kris20030907 The feature I'm referring to specifically is delete consumer offset.😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, No users have raised this request yet, but I think it could be a good option.

switch (response.getCode()) {
case ResponseCode.SUCCESS: {
return;
}
default:
break;
}

throw new MQClientException(response.getCode(), response.getRemark());
}

public String getKVConfigValue(final String namespace, final String key, final long timeoutMillis)
throws RemotingException, MQClientException, InterruptedException {
GetKVConfigRequestHeader requestHeader = new GetKVConfigRequestHeader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public class RequestCode {
public static final int LITE_PULL_MESSAGE = 361;
public static final int RECALL_MESSAGE = 370;

public static final int DELETE_CONSUMER_OFFSET = 380;

public static final int QUERY_ASSIGNMENT = 400;
public static final int SET_MESSAGE_REQUEST_MODE = 401;
public static final int GET_ALL_MESSAGE_REQUEST_MODE = 402;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.rocketmq.remoting.protocol.header;

import org.apache.rocketmq.common.action.Action;
import org.apache.rocketmq.common.action.RocketMQAction;
import org.apache.rocketmq.common.resource.ResourceType;
import org.apache.rocketmq.common.resource.RocketMQResource;
import org.apache.rocketmq.remoting.annotation.CFNotNull;
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
import org.apache.rocketmq.remoting.protocol.RequestCode;
import org.apache.rocketmq.remoting.rpc.RpcRequestHeader;

@RocketMQAction(value = RequestCode.DELETE_CONSUMER_OFFSET, action = Action.DELETE)
public class DeleteConsumerOffsetRequestHeader extends RpcRequestHeader {
@CFNotNull
@RocketMQResource(ResourceType.GROUP)
private String consumerGroup;

@CFNotNull
@RocketMQResource(ResourceType.TOPIC)
private String topic;

@Override
public void checkFields() throws RemotingCommandException {
}

public String getConsumerGroup() {
return consumerGroup;
}

public void setConsumerGroup(String consumerGroup) {
this.consumerGroup = consumerGroup;
}

public String getTopic() {
return topic;
}

public void setTopic(String topic) {
this.topic = topic;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ public void deleteSubscriptionGroup(String addr,
defaultMQAdminExtImpl.deleteSubscriptionGroup(addr, groupName, removeOffset);
}

@Override
public void deleteConsumerOffset(String addr, String group,
String topic) throws RemotingException, MQBrokerException, InterruptedException,
MQClientException {
defaultMQAdminExtImpl.deleteConsumerOffset(addr, group, topic);
}

@Override
public void createAndUpdateKvConfig(String namespace, String key,
String value) throws RemotingException, MQBrokerException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,12 @@ public void deleteSubscriptionGroup(String addr, String groupName,
this.mqClientInstance.getMQClientAPIImpl().deleteSubscriptionGroup(addr, groupName, removeOffset, timeoutMillis);
}

@Override
public void deleteConsumerOffset(String addr, String group,
String topic) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
this.mqClientInstance.getMQClientAPIImpl().deleteConsumerOffset(addr, group, topic, timeoutMillis);
}

@Override
public void createAndUpdateKvConfig(String namespace, String key,
String value) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ void deleteSubscriptionGroup(final String addr, String groupName,
boolean removeOffset) throws RemotingException, MQBrokerException,
InterruptedException, MQClientException;

void deleteConsumerOffset(final String addr, String group,
String topic) throws RemotingException, MQBrokerException,
InterruptedException, MQClientException;

void createAndUpdateKvConfig(String namespace, String key,
String value) throws RemotingException, MQBrokerException,
InterruptedException, MQClientException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import org.apache.rocketmq.tools.command.namesrv.UpdateNamesrvConfigCommand;
import org.apache.rocketmq.tools.command.namesrv.WipeWritePermSubCommand;
import org.apache.rocketmq.tools.command.offset.CloneGroupOffsetCommand;
import org.apache.rocketmq.tools.command.offset.DeleteConsumerOffsetCommand;
import org.apache.rocketmq.tools.command.offset.ResetOffsetByTimeCommand;
import org.apache.rocketmq.tools.command.offset.SkipAccumulationSubCommand;
import org.apache.rocketmq.tools.command.producer.ProducerSubCommand;
Expand Down Expand Up @@ -240,6 +241,7 @@ public static void initCommand() {
initCommand(new AddWritePermSubCommand());
initCommand(new ResetOffsetByTimeCommand());
initCommand(new SkipAccumulationSubCommand());
initCommand(new DeleteConsumerOffsetCommand());

initCommand(new UpdateOrderConfCommand());
initCommand(new CleanExpiredCQSubCommand());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.tools.command.offset;

import java.util.Set;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.rocketmq.remoting.RPCHook;
import org.apache.rocketmq.srvutil.ServerUtil;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.apache.rocketmq.tools.command.CommandUtil;
import org.apache.rocketmq.tools.command.SubCommand;
import org.apache.rocketmq.tools.command.SubCommandException;

public class DeleteConsumerOffsetCommand implements SubCommand {

@Override
public String commandName() {
return "deleteConsumerOffset";
}

@Override
public String commandDesc() {
return "Delete consumer offset for specified consumer group and topic.";
}

@Override
public Options buildCommandlineOptions(Options options) {
Option opt = new Option("g", "consumerGroup", true, "consumer group name (required)");
opt.setRequired(true);
options.addOption(opt);

opt = new Option("t", "topic", true, "topic name (required)");
opt.setRequired(true);
options.addOption(opt);

opt = new Option("b", "brokerAddr", true, "delete consumer offset from which broker");
opt.setRequired(false);
options.addOption(opt);

opt = new Option("c", "clusterName", true, "delete consumer offset from which cluster");
opt.setRequired(false);
options.addOption(opt);

return options;
}

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

if (commandLine.hasOption('n')) {
defaultMQAdminExt.setNamesrvAddr(commandLine.getOptionValue('n').trim());
}
try {
String group = commandLine.getOptionValue('g').trim();
String topic = commandLine.getOptionValue('t').trim();
defaultMQAdminExt.start();

if (commandLine.hasOption('b')) {
String addr = commandLine.getOptionValue('b').trim();

defaultMQAdminExt.deleteConsumerOffset(addr, group, topic);
System.out.printf("delete consumer offset for group [%s] topic [%s] from broker [%s] success.%n",
group, topic, addr);
return;
} else if (commandLine.hasOption('c')) {
String clusterName = commandLine.getOptionValue('c').trim();

Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
for (String master : masterSet) {
defaultMQAdminExt.deleteConsumerOffset(master, group, topic);
System.out.printf(
"delete consumer offset for group [%s] topic [%s] from broker [%s] in cluster [%s] success.%n",
group, topic, master, clusterName);
}
return;
}
ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
} catch (Exception e) {
throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
} finally {
defaultMQAdminExt.shutdown();
}
}
}

Loading
Loading