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 #8365] add remoting client non-oneway updateConsumerOffset function #8368

Merged
merged 2 commits into from
Jul 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,33 @@ public CompletableFuture<Void> updateConsumerOffsetOneWay(
return future;
}

public CompletableFuture<Void> updateConsumerOffsetAsync(
String brokerAddr,
UpdateConsumerOffsetRequestHeader header,
long timeoutMillis
) {
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, header);
CompletableFuture<Void> future = new CompletableFuture<>();
invoke(brokerAddr, request, timeoutMillis).whenComplete((response, t) -> {
drpmma marked this conversation as resolved.
Show resolved Hide resolved
if (t != null) {
log.error("updateConsumerOffsetAsync failed, brokerAddr={}, requestHeader={}", brokerAddr, header, t);
future.completeExceptionally(t);
return;
}
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
future.complete(null);
}
case ResponseCode.SYSTEM_ERROR:
case ResponseCode.SUBSCRIPTION_GROUP_NOT_EXIST:
case ResponseCode.TOPIC_NOT_EXIST: {
future.completeExceptionally(new MQBrokerException(response.getCode(), response.getRemark()));
}
}
});
return future;
}

public CompletableFuture<List<String>> getConsumerListByGroupAsync(
String brokerAddr,
GetConsumerListByGroupRequestHeader requestHeader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
package org.apache.rocketmq.client.impl.mqclient;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.apache.rocketmq.client.ClientConfig;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.utils.FutureUtils;
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
import org.apache.rocketmq.remoting.netty.NettyRemotingClient;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.protocol.ResponseCode;
import org.apache.rocketmq.remoting.protocol.header.SendMessageRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.UpdateConsumerOffsetRequestHeader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -34,9 +39,12 @@
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;

@RunWith(MockitoJUnitRunner.class)
public class MQClientAPIExtTest {
Expand Down Expand Up @@ -71,4 +79,33 @@ public void sendMessageAsync() {
CompletableFuture<SendResult> future = mqClientAPIExt.sendMessageAsync("127.0.0.1:10911", "test", msg, requestHeader, 10);
assertThatThrownBy(future::get).getCause().isInstanceOf(RemotingTimeoutException.class);
}

@Test
public void testUpdateConsumerOffsetAsync_Success() throws ExecutionException, InterruptedException {
CompletableFuture<RemotingCommand> remotingFuture = new CompletableFuture<>();
remotingFuture.complete(RemotingCommand.createResponseCommand(ResponseCode.SUCCESS, ""));
doReturn(remotingFuture).when(remotingClientMock).invoke(anyString(), any(RemotingCommand.class), anyLong());

CompletableFuture<Void> future = mqClientAPIExt.updateConsumerOffsetAsync("brokerAddr", new UpdateConsumerOffsetRequestHeader(), 3000L);

assertNull("Future should be completed without exception", future.get());
}

@Test
public void testUpdateConsumerOffsetAsync_Fail() throws InterruptedException {

CompletableFuture<RemotingCommand> remotingFuture = new CompletableFuture<>();
remotingFuture.complete(RemotingCommand.createResponseCommand(ResponseCode.SYSTEM_ERROR, "QueueId is null, topic is testTopic"));
doReturn(remotingFuture).when(remotingClientMock).invoke(anyString(), any(RemotingCommand.class), anyLong());

CompletableFuture<Void> future = mqClientAPIExt.updateConsumerOffsetAsync("brokerAddr", new UpdateConsumerOffsetRequestHeader(), 3000L);

try {
future.get();
} catch (ExecutionException e) {
MQBrokerException customEx = (MQBrokerException) e.getCause();
assertEquals(customEx.getResponseCode(), ResponseCode.SYSTEM_ERROR);
assertEquals(customEx.getErrorMessage(), "QueueId is null, topic is testTopic");
}
}
}
Loading