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 @@ -20,6 +20,7 @@
import apache.rocketmq.v2.ChangeInvisibleDurationResponse;
import apache.rocketmq.v2.Code;
import com.google.protobuf.util.Durations;
import io.netty.channel.Channel;
import java.util.concurrent.CompletableFuture;
import org.apache.rocketmq.client.consumer.AckResult;
import org.apache.rocketmq.client.consumer.AckStatus;
Expand Down Expand Up @@ -49,8 +50,9 @@ public CompletableFuture<ChangeInvisibleDurationResponse> changeInvisibleDuratio

ReceiptHandle receiptHandle = ReceiptHandle.decode(request.getReceiptHandle());
String group = request.getGroup().getName();
Channel channel = grpcChannelManager.getChannel(ctx.getClientID());

MessageReceiptHandle messageReceiptHandle = messagingProcessor.removeReceiptHandle(ctx, grpcChannelManager.getChannel(ctx.getClientID()), group, request.getMessageId(), receiptHandle.getReceiptHandle());
MessageReceiptHandle messageReceiptHandle = messagingProcessor.removeReceiptHandle(ctx, channel, group, request.getMessageId(), receiptHandle.getReceiptHandle());
if (messageReceiptHandle != null) {
receiptHandle = ReceiptHandle.decode(messageReceiptHandle.getReceiptHandleStr());
}
Expand All @@ -65,23 +67,48 @@ public CompletableFuture<ChangeInvisibleDurationResponse> changeInvisibleDuratio
MessagingProcessor.DEFAULT_TIMEOUT_MILLS,
request.getSuspend()
).thenApply(
ackResult -> convertToChangeInvisibleDurationResponse(ctx, request, ackResult));
ackResult -> convertToChangeInvisibleDurationResponse(ctx, request, ackResult, messageReceiptHandle, channel))
.whenComplete((response, throwable) -> {
// The broker call threw (or the callback did): re-register the old managed handle
// so that later renew/ack can still find it, then let the failure propagate.
if (throwable != null) {
restoreReceiptHandle(ctx, request, messageReceiptHandle, channel);
}
});
} catch (Throwable t) {
future.completeExceptionally(t);
}
return future;
}

protected ChangeInvisibleDurationResponse convertToChangeInvisibleDurationResponse(ProxyContext ctx,
ChangeInvisibleDurationRequest request, AckResult ackResult) {
ChangeInvisibleDurationRequest request, AckResult ackResult, MessageReceiptHandle messageReceiptHandle,
Channel channel) {
if (AckStatus.OK.equals(ackResult.getStatus())) {
String receiptHandle = ackResult.getExtraInfo();
if (messageReceiptHandle != null && channel != null) {
messageReceiptHandle.updateReceiptHandle(receiptHandle);
messagingProcessor.addReceiptHandle(ctx, channel,
request.getGroup().getName(), request.getMessageId(), messageReceiptHandle);
}
return ChangeInvisibleDurationResponse.newBuilder()
.setStatus(ResponseBuilder.getInstance().buildStatus(Code.OK, Code.OK.name()))
.setReceiptHandle(ackResult.getExtraInfo())
.setReceiptHandle(receiptHandle)
.build();
}
// Broker returned a non-OK status: re-register the old managed handle so that later
// renew/ack can still find it instead of losing it after the upfront removal.
restoreReceiptHandle(ctx, request, messageReceiptHandle, channel);
return ChangeInvisibleDurationResponse.newBuilder()
.setStatus(ResponseBuilder.getInstance().buildStatus(Code.INTERNAL_SERVER_ERROR, "changeInvisibleDuration failed: status is abnormal"))
.build();
}

private void restoreReceiptHandle(ProxyContext ctx, ChangeInvisibleDurationRequest request,
MessageReceiptHandle messageReceiptHandle, Channel channel) {
if (messageReceiptHandle != null && channel != null) {
messagingProcessor.addReceiptHandle(ctx, channel,
request.getGroup().getName(), request.getMessageId(), messageReceiptHandle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import apache.rocketmq.v2.Code;
import apache.rocketmq.v2.Resource;
import com.google.protobuf.util.Durations;
import io.netty.channel.Channel;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand All @@ -40,6 +41,9 @@
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ChangeInvisibleDurationActivityTest extends BaseActivityTest {
Expand Down Expand Up @@ -162,6 +166,218 @@ public void testChangeInvisibleDurationActivityFailed() throws Throwable {
assertEquals(TimeUnit.SECONDS.toMillis(3), invisibleTimeArgumentCaptor.getValue().longValue());
}

@Test
public void testChangeInvisibleDurationReRegistersNewHandleWhenOldHandleIsManaged() throws Throwable {
String oldHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 3000);
String newHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 5000);
String msgId = "msgId";
grpcChannelManager.createChannel(createContext(), CLIENT_ID);
MessageReceiptHandle messageReceiptHandle =
new MessageReceiptHandle(CONSUMER_GROUP, TOPIC, 0, oldHandle, msgId, 1, 2);
AckResult ackResult = new AckResult();
ackResult.setExtraInfo(newHandle);
ackResult.setStatus(AckStatus.OK);
when(this.messagingProcessor.changeInvisibleTime(
any(),
any(),
anyString(),
anyString(),
anyString(),
anyLong(),
anyString(),
anyLong(),
anyBoolean()
)).thenReturn(CompletableFuture.completedFuture(ackResult));
when(messagingProcessor.removeReceiptHandle(any(), any(), anyString(), anyString(), anyString()))
.thenReturn(messageReceiptHandle);

ChangeInvisibleDurationResponse response = this.changeInvisibleDurationActivity.changeInvisibleDuration(
createContext(),
ChangeInvisibleDurationRequest.newBuilder()
.setInvisibleDuration(Durations.fromSeconds(5))
.setTopic(Resource.newBuilder().setName(TOPIC).build())
.setGroup(Resource.newBuilder().setName(CONSUMER_GROUP).build())
.setMessageId(msgId)
.setReceiptHandle(oldHandle)
.build()
).get();

assertEquals(Code.OK, response.getStatus().getCode());
assertEquals(newHandle, response.getReceiptHandle());
assertEquals(newHandle, messageReceiptHandle.getReceiptHandleStr());
verify(messagingProcessor).addReceiptHandle(any(), any(), eq(CONSUMER_GROUP), eq(msgId),
eq(messageReceiptHandle));
}

@Test
public void testChangeInvisibleDurationReusesOriginalChannelWhenClientDisconnectsBeforeCallback() throws Throwable {
String oldHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 3000);
String newHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 5000);
String msgId = "msgId";
Channel channel = grpcChannelManager.createChannel(createContext(), CLIENT_ID);
MessageReceiptHandle messageReceiptHandle =
new MessageReceiptHandle(CONSUMER_GROUP, TOPIC, 0, oldHandle, msgId, 1, 2);
CompletableFuture<AckResult> ackResultFuture = new CompletableFuture<>();
when(this.messagingProcessor.changeInvisibleTime(
any(),
any(),
anyString(),
anyString(),
anyString(),
anyLong(),
anyString(),
anyLong(),
anyBoolean()
)).thenReturn(ackResultFuture);
when(messagingProcessor.removeReceiptHandle(any(), eq(channel), anyString(), anyString(), anyString()))
.thenReturn(messageReceiptHandle);

CompletableFuture<ChangeInvisibleDurationResponse> responseFuture =
this.changeInvisibleDurationActivity.changeInvisibleDuration(
createContext(),
ChangeInvisibleDurationRequest.newBuilder()
.setInvisibleDuration(Durations.fromSeconds(5))
.setTopic(Resource.newBuilder().setName(TOPIC).build())
.setGroup(Resource.newBuilder().setName(CONSUMER_GROUP).build())
.setMessageId(msgId)
.setReceiptHandle(oldHandle)
.build()
);

grpcChannelManager.removeChannel(CLIENT_ID);
AckResult ackResult = new AckResult();
ackResult.setExtraInfo(newHandle);
ackResult.setStatus(AckStatus.OK);
ackResultFuture.complete(ackResult);

assertEquals(Code.OK, responseFuture.get().getStatus().getCode());
verify(messagingProcessor).addReceiptHandle(any(), eq(channel), eq(CONSUMER_GROUP), eq(msgId),
eq(messageReceiptHandle));
}

@Test
public void testChangeInvisibleDurationDoesNotRegisterNewHandleWhenOldHandleIsNotManaged() throws Throwable {
String newHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 5000);
AckResult ackResult = new AckResult();
ackResult.setExtraInfo(newHandle);
ackResult.setStatus(AckStatus.OK);
when(this.messagingProcessor.changeInvisibleTime(
any(),
any(),
anyString(),
anyString(),
anyString(),
anyLong(),
anyString(),
anyLong(),
anyBoolean()
)).thenReturn(CompletableFuture.completedFuture(ackResult));
when(messagingProcessor.removeReceiptHandle(any(), any(), anyString(), anyString(), anyString()))
.thenReturn(null);

ChangeInvisibleDurationResponse response = this.changeInvisibleDurationActivity.changeInvisibleDuration(
createContext(),
ChangeInvisibleDurationRequest.newBuilder()
.setInvisibleDuration(Durations.fromSeconds(5))
.setTopic(Resource.newBuilder().setName(TOPIC).build())
.setGroup(Resource.newBuilder().setName(CONSUMER_GROUP).build())
.setMessageId("msgId")
.setReceiptHandle(buildReceiptHandle(TOPIC, System.currentTimeMillis(), 3000))
.build()
).get();

assertEquals(Code.OK, response.getStatus().getCode());
assertEquals(newHandle, response.getReceiptHandle());
verify(messagingProcessor, never()).addReceiptHandle(any(), any(), anyString(), anyString(),
any(MessageReceiptHandle.class));
}

@Test
public void testChangeInvisibleDurationRestoresOldHandleWhenBrokerReturnsNonOk() throws Throwable {
String oldHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 3000);
String msgId = "msgId";
grpcChannelManager.createChannel(createContext(), CLIENT_ID);
MessageReceiptHandle messageReceiptHandle =
new MessageReceiptHandle(CONSUMER_GROUP, TOPIC, 0, oldHandle, msgId, 1, 2);
AckResult ackResult = new AckResult();
ackResult.setStatus(AckStatus.NO_EXIST);
when(this.messagingProcessor.changeInvisibleTime(
any(),
any(),
anyString(),
anyString(),
anyString(),
anyLong(),
anyString(),
anyLong(),
anyBoolean()
)).thenReturn(CompletableFuture.completedFuture(ackResult));
when(messagingProcessor.removeReceiptHandle(any(), any(), anyString(), anyString(), anyString()))
.thenReturn(messageReceiptHandle);

ChangeInvisibleDurationResponse response = this.changeInvisibleDurationActivity.changeInvisibleDuration(
createContext(),
ChangeInvisibleDurationRequest.newBuilder()
.setInvisibleDuration(Durations.fromSeconds(5))
.setTopic(Resource.newBuilder().setName(TOPIC).build())
.setGroup(Resource.newBuilder().setName(CONSUMER_GROUP).build())
.setMessageId(msgId)
.setReceiptHandle(oldHandle)
.build()
).get();

assertEquals(Code.INTERNAL_SERVER_ERROR, response.getStatus().getCode());
// The old handle was re-registered (restored) and left unmodified.
assertEquals(oldHandle, messageReceiptHandle.getReceiptHandleStr());
verify(messagingProcessor).addReceiptHandle(any(), any(), eq(CONSUMER_GROUP), eq(msgId),
eq(messageReceiptHandle));
}

@Test
public void testChangeInvisibleDurationRestoresOldHandleWhenBrokerFutureFails() throws Throwable {
String oldHandle = buildReceiptHandle(TOPIC, System.currentTimeMillis(), 3000);
String msgId = "msgId";
grpcChannelManager.createChannel(createContext(), CLIENT_ID);
MessageReceiptHandle messageReceiptHandle =
new MessageReceiptHandle(CONSUMER_GROUP, TOPIC, 0, oldHandle, msgId, 1, 2);
CompletableFuture<AckResult> ackResultFuture = new CompletableFuture<>();
ackResultFuture.completeExceptionally(new RuntimeException("broker boom"));
when(this.messagingProcessor.changeInvisibleTime(
any(),
any(),
anyString(),
anyString(),
anyString(),
anyLong(),
anyString(),
anyLong(),
anyBoolean()
)).thenReturn(ackResultFuture);
when(messagingProcessor.removeReceiptHandle(any(), any(), anyString(), anyString(), anyString()))
.thenReturn(messageReceiptHandle);

try {
this.changeInvisibleDurationActivity.changeInvisibleDuration(
createContext(),
ChangeInvisibleDurationRequest.newBuilder()
.setInvisibleDuration(Durations.fromSeconds(5))
.setTopic(Resource.newBuilder().setName(TOPIC).build())
.setGroup(Resource.newBuilder().setName(CONSUMER_GROUP).build())
.setMessageId(msgId)
.setReceiptHandle(oldHandle)
.build()
).get();
org.junit.Assert.fail("expected ExecutionException");
} catch (ExecutionException executionException) {
// expected: the failure surfaces to the caller
}

// The old handle was re-registered (restored) and left unmodified.
assertEquals(oldHandle, messageReceiptHandle.getReceiptHandleStr());
verify(messagingProcessor).addReceiptHandle(any(), any(), eq(CONSUMER_GROUP), eq(msgId),
eq(messageReceiptHandle));
}

@Test
public void testChangeInvisibleDurationInvisibleTimeTooSmall() throws Throwable {
try {
Expand Down Expand Up @@ -199,4 +415,4 @@ public void testChangeInvisibleDurationInvisibleTimeTooLarge() throws Throwable
assertEquals(Code.ILLEGAL_INVISIBLE_TIME, exception.getCode());
}
}
}
}
Loading