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
1 change: 1 addition & 0 deletions proxy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ java_library(
"@maven//:io_netty_netty_all",
"@maven//:io_github_aliyunmq_rocketmq_grpc_netty_codec_haproxy",
"@maven//:org_apache_commons_commons_lang3",
"@maven//:io_opentelemetry_opentelemetry_api",
"@maven//:io_opentelemetry_opentelemetry_exporter_otlp",
"@maven//:io_opentelemetry_opentelemetry_exporter_prometheus",
"@maven//:io_opentelemetry_opentelemetry_sdk",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public CompletableFuture<AckResult> batchAckMessage(ProxyContext ctx, List<Recei
String consumerGroup, String topic, long timeoutMillis) {
SimpleChannel channel = channelManager.createChannel(ctx);
ChannelHandlerContext channelHandlerContext = channel.getChannelHandlerContext();
RemotingCommand command = LocalRemotingCommand.createRequestCommand(RequestCode.BATCH_ACK_MESSAGE, null);
RemotingCommand command = LocalRemotingCommand.createRequestCommand(RequestCode.BATCH_ACK_MESSAGE, null, ctx.getLanguage());

Map<String, BatchAck> batchAckMap = new HashMap<>();
for (ReceiptHandleMessage receiptHandleMessage : handleList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.rocketmq.proxy.service.message;

import com.google.common.base.Stopwatch;
import java.util.HashMap;
import org.apache.rocketmq.remoting.CommandCustomHeader;
import org.apache.rocketmq.remoting.protocol.LanguageCode;
Expand All @@ -31,6 +32,8 @@ public static LocalRemotingCommand createRequestCommand(int code, CommandCustomH
cmd.setExtFields(new HashMap<>());
setCmdVersion(cmd);
cmd.makeCustomHeaderToNet();
// Local requests bypass NettyDecoder, so initialize the processing timer explicitly.
cmd.setProcessTimer(Stopwatch.createStarted());
return cmd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.rocketmq.proxy.service.channel.SimpleChannelHandlerContext;
import org.apache.rocketmq.proxy.service.route.AddressableMessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
import org.apache.rocketmq.remoting.protocol.LanguageCode;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.protocol.RequestCode;
import org.apache.rocketmq.remoting.protocol.ResponseCode;
Expand Down Expand Up @@ -604,6 +605,28 @@ public void testAckMessage() throws Exception {
assertThat(ackResult.getStatus()).isEqualTo(AckStatus.OK);
}

@Test
public void testBatchAckMessage() throws Exception {
String messageId = "messageId";
long popTime = System.currentTimeMillis();
long invisibleTime = 3000L;
int reviveQueueId = 1;
String extraInfo = ExtraInfoUtil.buildExtraInfo(0L, popTime, invisibleTime, reviveQueueId,
topic, brokerName, queueId, queueOffset);
ReceiptHandle handle = ReceiptHandle.decode(extraInfo);
proxyContext.withVal(ContextVariable.LANGUAGE, LanguageCode.GO.name());
RemotingCommand remotingCommand = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS, null);
Mockito.when(ackMessageProcessorMock.processRequest(Mockito.any(SimpleChannelHandlerContext.class), Mockito.argThat(argument ->
argument instanceof LocalRemotingCommand
&& argument.getCode() == RequestCode.BATCH_ACK_MESSAGE
&& argument.getLanguage() == LanguageCode.GO
&& argument.getProcessTimer() != null))).thenReturn(remotingCommand);
CompletableFuture<AckResult> future = localMessageService.batchAckMessage(proxyContext,
Collections.singletonList(new ReceiptHandleMessage(handle, messageId)), "group", topic, 1000L);
AckResult ackResult = future.get();
assertThat(ackResult.getStatus()).isEqualTo(AckStatus.OK);
}

@Test
public void testRecallMessage_success() throws Exception {
RecallMessageResponseHeader responseHeader = new RecallMessageResponseHeader();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.proxy.service.message;

import io.netty.channel.ChannelFuture;
import io.netty.channel.DefaultChannelPromise;
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongHistogram;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.rocketmq.common.metrics.NopLongHistogram;
import org.apache.rocketmq.proxy.service.channel.SimpleChannel;
import org.apache.rocketmq.remoting.metrics.RemotingMetricsManager;
import org.apache.rocketmq.remoting.netty.NettyRemotingAbstract;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.protocol.RequestCode;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class LocalRemotingCommandTest {

@Test
public void testCreateRequestCommandShouldStartProcessTimer() {
LocalRemotingCommand command = LocalRemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, null, "JAVA");

assertNotNull(command.getProcessTimer());
assertTrue(command.getProcessTimer().isRunning());
}

@Test
public void testWriteResponseShouldRecordLatencyForLocalRemotingCommand() {
LocalRemotingCommand command = LocalRemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, null, "JAVA");
RemotingCommand response = RemotingCommand.createResponseCommand(0, null);
AtomicBoolean latencyRecorded = new AtomicBoolean(false);
LongHistogram rpcLatency = new NopLongHistogram() {
@Override
public void record(long value, Attributes attributes) {
latencyRecorded.set(true);
}
};
RemotingMetricsManager remotingMetricsManager = new RemotingMetricsManager() {
@Override
public LongHistogram getRpcLatency() {
return rpcLatency;
}
};
SimpleChannel channel = new SimpleChannel("127.0.0.1:8080", "127.0.0.1:8081") {
@Override
public ChannelFuture writeAndFlush(Object msg) {
return new DefaultChannelPromise(this, ImmediateEventExecutor.INSTANCE).setSuccess();
}
};

NettyRemotingAbstract.writeResponse(
channel,
command,
response,
null,
remotingMetricsManager
);

assertTrue(latencyRecorded.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.rocketmq.remoting.netty;

import com.google.common.base.Stopwatch;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -235,8 +236,9 @@ public static void writeResponse(Channel channel, RemotingCommand request, @Null
if (response == null) {
return;
}
final Stopwatch processTimer = request.getProcessTimer();
final AttributesBuilder attributesBuilder;
if (remotingMetricsManager != null) {
if (remotingMetricsManager != null && processTimer != null) {
attributesBuilder = remotingMetricsManager.newAttributesBuilder();
attributesBuilder.put(LABEL_IS_LONG_POLLING, request.isSuspended())
.put(LABEL_REQUEST_CODE, RemotingHelper.getRequestCodeDesc(request.getCode()))
Expand All @@ -247,7 +249,7 @@ public static void writeResponse(Channel channel, RemotingCommand request, @Null
if (request.isOnewayRPC()) {
if (attributesBuilder != null) {
attributesBuilder.put(LABEL_RESULT, RESULT_ONEWAY);
remotingMetricsManager.getRpcLatency().record(request.getProcessTimer().elapsed(TimeUnit.MILLISECONDS), attributesBuilder.build());
remotingMetricsManager.getRpcLatency().record(processTimer.elapsed(TimeUnit.MILLISECONDS), attributesBuilder.build());
}
return;
}
Expand All @@ -262,9 +264,9 @@ public static void writeResponse(Channel channel, RemotingCommand request, @Null
log.error("Failed to write response[request code: {}, response code: {}, opaque: {}] to channel{}",
request.getCode(), response.getCode(), response.getOpaque(), channel, future.cause());
}
if (remotingMetricsManager != null) {
if (attributesBuilder != null) {
attributesBuilder.put(LABEL_RESULT, remotingMetricsManager.getWriteAndFlushResult(future));
remotingMetricsManager.getRpcLatency().record(request.getProcessTimer().elapsed(TimeUnit.MILLISECONDS), attributesBuilder.build());
remotingMetricsManager.getRpcLatency().record(processTimer.elapsed(TimeUnit.MILLISECONDS), attributesBuilder.build());
}
if (callback != null) {
callback.accept(future);
Expand All @@ -274,9 +276,9 @@ public static void writeResponse(Channel channel, RemotingCommand request, @Null
log.error("process request over, but response failed", e);
log.error(request.toString());
log.error(response.toString());
if (remotingMetricsManager != null) {
if (attributesBuilder != null) {
attributesBuilder.put(LABEL_RESULT, RESULT_WRITE_CHANNEL_FAILED);
remotingMetricsManager.getRpcLatency().record(request.getProcessTimer().elapsed(TimeUnit.MILLISECONDS), attributesBuilder.build());
remotingMetricsManager.getRpcLatency().record(processTimer.elapsed(TimeUnit.MILLISECONDS), attributesBuilder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
*/
package org.apache.rocketmq.remoting.netty;

import io.netty.channel.Channel;
import io.netty.channel.DefaultChannelPromise;
import io.netty.util.concurrent.ImmediateEventExecutor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.rocketmq.remoting.InvokeCallback;
import org.apache.rocketmq.remoting.common.SemaphoreReleaseOnlyOnce;
import org.apache.rocketmq.remoting.metrics.RemotingMetricsManager;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -27,6 +32,11 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -168,4 +178,28 @@ public void operationFail(Throwable throwable) {
semaphore.acquire(1);
assertThat(semaphore.availablePermits()).isEqualTo(0);
}

@Test
public void testWriteResponseWithNullProcessTimer() {
RemotingMetricsManager remotingMetricsManager = spy(new RemotingMetricsManager());
Channel channel = mock(Channel.class);
when(channel.writeAndFlush(any())).thenAnswer(invocation ->
new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE).setSuccess());

RemotingCommand request = RemotingCommand.createRequestCommand(1, null);
RemotingCommand response = RemotingCommand.createResponseCommand(0, null);
AtomicBoolean callbackInvoked = new AtomicBoolean(false);
NettyRemotingAbstract.writeResponse(channel, request, response, future -> callbackInvoked.set(true), remotingMetricsManager);
assertTrue(callbackInvoked.get());

RemotingCommand onewayRequest = RemotingCommand.createRequestCommand(1, null);
onewayRequest.markOnewayRPC();
NettyRemotingAbstract.writeResponse(channel, onewayRequest, RemotingCommand.createResponseCommand(0, null), null, remotingMetricsManager);

Channel failingChannel = mock(Channel.class);
when(failingChannel.writeAndFlush(any())).thenThrow(new RuntimeException("write failed"));
NettyRemotingAbstract.writeResponse(failingChannel, request, RemotingCommand.createResponseCommand(0, null), null, remotingMetricsManager);

verifyNoInteractions(remotingMetricsManager);
}
}
Loading