Skip to content

Commit

Permalink
[Producer] Change the time units from ns to ms (apache#13057)
Browse files Browse the repository at this point in the history
### Motivation
The time unit in this exception message is ns, which is not very readable. We can change it from ns to ms.
```
org.apache.pulsar.client.api.PulsarClientException$TimeoutException:
The producer xxx can not send message to the topic xxx within given timeout : createdAt 461913074 ns ago, firstSentAt 29545553038276935 ns ago, lastSentAt 29545553038276935 ns ago, retryCount 0 at org.apache.pulsar.client.api.PulsarClientException.unwrap(PulsarClientException.java:916)
at org.apache.pulsar.client.impl.TypedMessageBuilderImpl.send(TypedMessageBuilderImpl.java:93)
at org.apache.pulsar.client.impl.ProducerBase.send(ProducerBase.java:63)
at com.yum.boh.oh.service.impl.StoreOrderPostServiceImpl.generalProcessing(StoreOrderPostServiceImpl.java:272)
at com.yum.boh.oh.service.impl.StoreOrderPostServiceImpl.saveThirdOrder(StoreOrderPostServiceImpl.java:72)
at com.yum.boh.oh.controller.StoreOrderController.postOrderInfo$original$T8425mfx(StoreOrderController.java:39)
at com.yum.boh.oh.controller.StoreOrderController.postOrderInfo$original$T8425mfx$accessor$vJljNzML(StoreOrderController.java)
at com.yum.boh.oh.controller.StoreOrderController$auxiliary$nysalhgy.call(Unknown Source)
at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86)
```

### Modifications
Change the time units from ns to ms for ProducerImpl#OpSendMsg.
  • Loading branch information
tomscut authored Dec 23, 2021
1 parent 3934dc0 commit 891660e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.apache.pulsar.common.schema.SchemaType;
import org.apache.pulsar.common.util.DateFormatter;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.common.util.RelativeTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -1260,9 +1261,9 @@ void sendComplete(final Exception e) {
String errMsg = String.format(
"%s : createdAt %s ns ago, firstSentAt %s ns ago, lastSentAt %s ns ago, retryCount %s",
te.getMessage(),
ns - this.createdAt,
this.firstSentAt <= 0 ? ns - this.lastSentAt : ns - this.firstSentAt,
ns - this.lastSentAt,
RelativeTimeUtil.nsToSeconds(ns - this.createdAt),
RelativeTimeUtil.nsToSeconds(this.firstSentAt <= 0 ? ns - this.lastSentAt : ns - this.firstSentAt),
RelativeTimeUtil.nsToSeconds(ns - this.lastSentAt),
retryCount
);

Expand Down Expand Up @@ -1325,6 +1326,7 @@ protected OpSendMsg newObject(Handle<OpSendMsg> handle) {
};
}


/**
* Queue implementation that is used as the pending messages queue.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.common.util;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;
import lombok.experimental.UtilityClass;

Expand Down Expand Up @@ -63,4 +64,15 @@ public static long parseRelativeTimeInSeconds(String relativeTime) {
throw new IllegalArgumentException("Invalid time unit '" + lastChar + "'");
}
}

/**
* Convert nanoseconds to seconds and keep three decimal places.
* @param ns
* @return seconds
*/
public static double nsToSeconds(long ns) {
double seconds = (double) ns / 1_000_000_000;
BigDecimal bd = new BigDecimal(seconds);
return bd.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}

0 comments on commit 891660e

Please sign in to comment.