Skip to content

Commit

Permalink
add producer stats manager
Browse files Browse the repository at this point in the history
  • Loading branch information
HScarb committed Jan 17, 2025
1 parent a3afb05 commit 6cf29d7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ public SendResult sendMessage(
switch (communicationMode) {
case ONEWAY:
this.remotingClient.invokeOneway(addr, request, timeoutMillis);
instance.getProducerStatsManager().incSendTimes(msg.getTopic(), 1);
instance.getProducerStatsManager().incSendRT(msg.getTopic(), System.currentTimeMillis() - beginStartTime);
return null;
case ASYNC:
final AtomicInteger times = new AtomicInteger();
Expand All @@ -644,7 +646,15 @@ public SendResult sendMessage(
if (timeoutMillis < costTimeSync) {
throw new RemotingTooMuchRequestException("sendMessage call timeout");
}
return this.sendMessageSync(addr, brokerName, msg, timeoutMillis - costTimeSync, request);
try {
SendResult result = this.sendMessageSync(addr, brokerName, msg, timeoutMillis - costTimeSync, request);
instance.getProducerStatsManager().incSendTimes(msg.getTopic(), 1);
instance.getProducerStatsManager().incSendRT(msg.getTopic(), System.currentTimeMillis() - beginStartTime);
return result;
} catch (Exception e) {
instance.getProducerStatsManager().incSendFailedTimes(msg.getTopic(), 1);
throw e;
}
default:
assert false;
break;
Expand Down Expand Up @@ -709,6 +719,8 @@ public void operationSucceed(RemotingCommand response) {
}

producer.updateFaultItem(brokerName, System.currentTimeMillis() - beginStartTime, false, true);
instance.getProducerStatsManager().incSendTimes(msg.getTopic(), 1);
instance.getProducerStatsManager().incSendRT(msg.getTopic(), cost);
return;
}

Expand All @@ -726,6 +738,8 @@ public void operationSucceed(RemotingCommand response) {
}

producer.updateFaultItem(brokerName, System.currentTimeMillis() - beginStartTime, false, true);
instance.getProducerStatsManager().incSendTimes(msg.getTopic(), 1);
instance.getProducerStatsManager().incSendRT(msg.getTopic(), cost);
} catch (Exception e) {
producer.updateFaultItem(brokerName, System.currentTimeMillis() - beginStartTime, true, true);
onExceptionImpl(brokerName, msg, timeoutMillis - cost, request, sendCallback, topicPublishInfo, instance,
Expand All @@ -737,6 +751,7 @@ public void operationSucceed(RemotingCommand response) {
public void operationFail(Throwable throwable) {
producer.updateFaultItem(brokerName, System.currentTimeMillis() - beginStartTime, true, true);
long cost = System.currentTimeMillis() - beginStartTime;
instance.getProducerStatsManager().incSendFailedTimes(msg.getTopic(), 1);
if (throwable instanceof RemotingSendRequestException) {
MQClientException ex = new MQClientException("send request failed", throwable);
onExceptionImpl(brokerName, msg, timeoutMillis - cost, request, sendCallback, topicPublishInfo, instance,
Expand All @@ -756,6 +771,7 @@ public void operationFail(Throwable throwable) {
} catch (Exception ex) {
long cost = System.currentTimeMillis() - beginStartTime;
producer.updateFaultItem(brokerName, cost, true, false);
instance.getProducerStatsManager().incSendFailedTimes(msg.getTopic(), 1);
onExceptionImpl(brokerName, msg, timeoutMillis - cost, request, sendCallback, topicPublishInfo, instance,
retryTimesWhenSendFailed, times, ex, context, true, producer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.apache.rocketmq.client.impl.producer.TopicPublishInfo;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.stat.ConsumerStatsManager;
import org.apache.rocketmq.client.stat.ProducerStatsManager;
import org.apache.rocketmq.common.MQVersion;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.ServiceState;
Expand Down Expand Up @@ -137,6 +138,7 @@ public Thread newThread(Runnable r) {
private final PullMessageService pullMessageService;
private final RebalanceService rebalanceService;
private final DefaultMQProducer defaultMQProducer;
private final ProducerStatsManager producerStatsManager;
private final ConsumerStatsManager consumerStatsManager;
private final AtomicLong sendHeartbeatTimesTotal = new AtomicLong(0);
private ServiceState serviceState = ServiceState.CREATE_JUST;
Expand Down Expand Up @@ -214,6 +216,7 @@ public void onChannelActive(String remoteAddr, Channel channel) {
this.defaultMQProducer = new DefaultMQProducer(MixAll.CLIENT_INNER_PRODUCER_GROUP);
this.defaultMQProducer.resetClientConfig(clientConfig);

this.producerStatsManager = new ProducerStatsManager(this.scheduledExecutorService);
this.consumerStatsManager = new ConsumerStatsManager(this.scheduledExecutorService);

log.info("Created a new client Instance, InstanceIndex:{}, ClientID:{}, ClientConfig:{}, ClientVersion:{}, SerializerType:{}",
Expand Down Expand Up @@ -1387,6 +1390,10 @@ public ConsumerStatsManager getConsumerStatsManager() {
return consumerStatsManager;
}

public ProducerStatsManager getProducerStatsManager() {
return producerStatsManager;
}

public NettyClientConfig getNettyClientConfig() {
return nettyClientConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.client.stat.ProducerStatsManager;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.ServiceState;
import org.apache.rocketmq.common.ThreadFactoryImpl;
Expand Down Expand Up @@ -1061,6 +1062,10 @@ private SendResult sendKernelImpl(final Message msg,
requestHeader,
timeout - costTimeSync,
communicationMode,
null,
null,
this.mQClientFactory,
0,
context,
this);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.client.stat;

import java.util.concurrent.ScheduledExecutorService;
import org.apache.rocketmq.common.stats.StatsItemSet;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;

public class ProducerStatsManager {
private static final Logger log = LoggerFactory.getLogger(ProducerStatsManager.class);

private static final String TOPIC_SEND_OK_TPS = "SEND_OK_TPS";
private static final String TOPIC_SEND_FAILED_TPS = "SEND_FAILED_TPS";
private static final String TOPIC_SEND_RT = "SEND_RT";

private final StatsItemSet topicSendOKTPS;
private final StatsItemSet topicSendFailedTPS;
private final StatsItemSet topicSendRT;

public ProducerStatsManager(final ScheduledExecutorService scheduledExecutorService) {
this.topicSendOKTPS = new StatsItemSet(TOPIC_SEND_OK_TPS, scheduledExecutorService, log);
this.topicSendFailedTPS = new StatsItemSet(TOPIC_SEND_FAILED_TPS, scheduledExecutorService, log);
this.topicSendRT = new StatsItemSet(TOPIC_SEND_RT, scheduledExecutorService, log);
}

public void start() {
}

public void shutdown() {
}

public void incSendTimes(final String topic, final int times) {
this.topicSendOKTPS.addValue(topic, times, 1);
}

public void incSendFailedTimes(final String topic, final int times) {
this.topicSendFailedTPS.addValue(topic, times, 1);
}

public void incSendRT(final String topic, final long rt) {
this.topicSendRT.addValue(topic, (int)rt, 1);
}

public StatsItemSet getTopicSendOKTPS() {
return topicSendOKTPS;
}

public StatsItemSet getTopicSendFailedTPS() {
return topicSendFailedTPS;
}

public StatsItemSet getTopicSendRT() {
return topicSendRT;
}
}

0 comments on commit 6cf29d7

Please sign in to comment.