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 #528] enhance Retry support for HTTP and TCP processors. #529

Merged
merged 24 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ba67c5c
Merge pull request #1 from apache/develop
jinrongluo May 10, 2021
d638ec4
[Issue #337] Fix HttpSubscriber startup issue
May 10, 2021
5ebfb54
[Issue #337] test commit
jinrongluo May 10, 2021
a3afff3
[Issue #337] revert test commit
jinrongluo May 10, 2021
50f959d
[Issue #337] Enhance Http Demo Subscriber by using ExecutorService, C…
jinrongluo May 11, 2021
7adc322
Merge remote-tracking branch 'origin/develop' into develop
jinrongluo May 11, 2021
d48ead5
[Issue #337] Enhance Http Demo Subscriber by using ExecutorService, C…
jinrongluo May 11, 2021
c9021fe
[Issue #337] Address code review comment for Subscriber Demo App
jinrongluo May 12, 2021
c6d732e
Merge branch 'apache:develop' into develop
jinrongluo May 14, 2021
c613be8
Merge branch 'apache:develop' into develop
jinrongluo May 18, 2021
66ac95e
Merge branch 'apache:develop' into develop
jinrongluo May 19, 2021
9e636c0
Merge branch 'apache:develop' into develop
jinrongluo May 26, 2021
a0f44b0
Merge branch 'apache:develop' into develop
jinrongluo Jun 4, 2021
37f5d7a
Merge branch 'apache:develop' into develop
jinrongluo Jun 12, 2021
93e711c
Merge branch 'apache:develop' into develop
jinrongluo Jun 15, 2021
e75d67a
Merge branch 'apache:develop' into develop
jinrongluo Jun 21, 2021
2fb485b
Merge branch 'apache:develop' into develop
jinrongluo Jul 5, 2021
80ab9af
Merge branch 'apache:develop' into develop
jinrongluo Sep 14, 2021
87d1747
Merge branch 'apache:develop' into develop
jinrongluo Sep 17, 2021
2cfd6ca
[Issue #528] enhance Retry support for HTTP and TCP processors.
jinrongluo Sep 20, 2021
6fe7b41
[Issue #528] fixing checkstyle issue
jinrongluo Sep 20, 2021
e4b9a1f
Merge branch 'apache:develop' into develop-retry
jinrongluo Sep 26, 2021
c188948
Merge branch 'apache:develop' into develop-retry
jinrongluo Sep 27, 2021
19b50d7
[Issue #528] Fix taskExecuteTime in TCP ClientGroupWrapper UpStreamMs…
jinrongluo Sep 27, 2021
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 @@ -44,7 +44,7 @@
import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.rebalance.EventMeshRebalanceService;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.rebalance.EventmeshRebalanceImpl;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.retry.EventMeshTcpRetryer;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.retry.EventMeshTcpRetryer;
import org.apache.eventmesh.runtime.metrics.tcp.EventMeshTcpMonitor;
import org.apache.eventmesh.runtime.registry.Registry;
import org.apache.eventmesh.runtime.util.EventMeshThreadFactoryImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ public void onException(OnExceptionContext context) {
SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getRetCode(),
EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(context.getException(), 2)));
asyncContext.onComplete(err, handler);

eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
long endTime = System.currentTimeMillis();
eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed();
eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime);
Expand All @@ -248,6 +250,8 @@ public void onException(OnExceptionContext context) {
SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getRetCode(),
EventMeshRetCode.EVENTMESH_SEND_ASYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(ex, 2)));
asyncContext.onComplete(err);

eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
long endTime = System.currentTimeMillis();
messageLogger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}",
endTime - startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
package org.apache.eventmesh.runtime.core.protocol.http.processor;

import com.alibaba.fastjson.JSON;

import io.netty.channel.ChannelHandlerContext;
import io.openmessaging.api.Message;
import io.openmessaging.api.OnExceptionContext;
import io.openmessaging.api.SendCallback;
import io.openmessaging.api.SendResult;

import org.apache.commons.lang3.StringUtils;
import org.apache.eventmesh.api.RRCallback;
import org.apache.eventmesh.common.Constants;
Expand Down Expand Up @@ -245,6 +240,8 @@ public void onException(Throwable e) {
SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_WAITING_RR_MSG_ERR.getRetCode(),
EventMeshRetCode.EVENTMESH_WAITING_RR_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(e, 2)));
asyncContext.onComplete(err, handler);

eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
messageLogger.error("message|mq2eventMesh|RSP|SYNC|rrCost={}ms|topic={}|bizSeqNo={}|uniqueId={}",
System.currentTimeMillis() - startTime,
sendMessageRequestBody.getTopic(),
Expand All @@ -258,6 +255,8 @@ public void onException(Throwable e) {
SendMessageResponseBody.buildBody(EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getRetCode(),
EventMeshRetCode.EVENTMESH_SEND_SYNC_MSG_ERR.getErrMsg() + EventMeshUtil.stackTrace(ex, 2)));
asyncContext.onComplete(err);

eventMeshHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000));
long endTime = System.currentTimeMillis();
eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgFailed();
eventMeshHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,8 @@

package org.apache.eventmesh.runtime.core.protocol.tcp.client.group;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.alibaba.fastjson.JSON;

import io.openmessaging.api.AsyncConsumeContext;
import io.openmessaging.api.AsyncMessageListener;
import io.openmessaging.api.Message;
import io.openmessaging.api.OnExceptionContext;
import io.openmessaging.api.SendCallback;
import io.openmessaging.api.SendResult;

import io.openmessaging.api.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.eventmesh.api.EventMeshAction;
Expand All @@ -53,14 +35,20 @@
import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.retry.EventMeshTcpRetryer;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.retry.EventMeshTcpRetryer;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext;
import org.apache.eventmesh.runtime.metrics.tcp.EventMeshTcpMonitor;
import org.apache.eventmesh.runtime.util.EventMeshUtil;
import org.apache.eventmesh.runtime.util.HttpTinyClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ClientGroupWrapper {

public static Logger logger = LoggerFactory.getLogger(ClientGroupWrapper.class);
Expand Down Expand Up @@ -752,7 +740,8 @@ private void sendMsgBackToBroker(Message msg, String bizSeqNo) throws Exception
String topic = msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION);
logger.warn("send msg back to broker, bizSeqno:{}, topic:{}", bizSeqNo, topic);

send(new UpStreamMsgContext(null, null, msg), new SendCallback() {
long startTime = System.currentTimeMillis();
send(new UpStreamMsgContext(null, msg, null, startTime, startTime), new SendCallback() {
jinrongluo marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void onSuccess(SendResult sendResult) {
logger.info("consumerGroup:{} consume fail, sendMessageBack success, bizSeqno:{}, topic:{}", consumerGroup, bizSeqNo, topic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,36 @@

package org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import io.openmessaging.api.Message;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.eventmesh.api.AbstractContext;
import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.common.protocol.SubscriptionItem;
import org.apache.eventmesh.common.protocol.SubscriptionMode;
import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import org.apache.eventmesh.runtime.core.plugin.MQConsumerWrapper;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.retry.RetryContext;
import org.apache.eventmesh.runtime.util.EventMeshUtil;
import org.apache.eventmesh.runtime.util.ServerGlobal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;

public class DownStreamMsgContext implements Delayed {
public class DownStreamMsgContext extends RetryContext {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

public String seq;

public Message msgExt;

public Session session;

public AbstractContext consumeConcurrentlyContext;

public MQConsumerWrapper consumer;

public int retryTimes;

public SubscriptionItem subscriptionItem;

private long executeTime;

public long lastPushTime;

private long createTime;
Expand All @@ -68,11 +59,9 @@ public DownStreamMsgContext(Message msgExt, Session session, MQConsumerWrapper c
this.seq = String.valueOf(ServerGlobal.getInstance().getMsgCounter().incrementAndGet());
this.msgExt = msgExt;
this.session = session;
this.retryTimes = 0;
this.consumer = consumer;
this.consumeConcurrentlyContext = consumeConcurrentlyContext;
this.lastPushTime = System.currentTimeMillis();
this.executeTime = System.currentTimeMillis();
this.createTime = System.currentTimeMillis();
this.subscriptionItem = subscriptionItem;
String ttlStr = msgExt.getUserProperties("TTL");
Expand All @@ -99,10 +88,6 @@ public void ackMsg() {
}
}

public void delay(long delay) {
this.executeTime = System.currentTimeMillis() + (retryTimes + 1) * delay;
}

@Override
public String toString() {
return "DownStreamMsgContext{" +
Expand All @@ -119,19 +104,71 @@ public String toString() {
}

@Override
public int compareTo(Delayed delayed) {
DownStreamMsgContext context = (DownStreamMsgContext) delayed;
if (this.executeTime > context.executeTime) {
return 1;
} else if (this.executeTime == context.executeTime) {
return 0;
} else {
return -1;
public void retry() {
try {
logger.info("retry downStream msg start,seq:{},retryTimes:{},bizSeq:{}", this.seq, this.retryTimes, EventMeshUtil.getMessageBizSeq(this.msgExt));

if (isRetryMsgTimeout(this)) {
return;
}
this.retryTimes++;
this.lastPushTime = System.currentTimeMillis();

Session rechoosen = null;
String topic = this.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION);
if (!SubscriptionMode.BROADCASTING.equals(this.subscriptionItem.getMode())) {
rechoosen = this.session.getClientGroupWrapper()
.get().getDownstreamDispatchStrategy().select(this.session.getClientGroupWrapper().get().getSysId()
, topic
, this.session.getClientGroupWrapper().get().getGroupConsumerSessions());
} else {
rechoosen = this.session;
}

if (rechoosen == null) {
logger.warn("retry, found no session to downstream msg,seq:{}, retryTimes:{}, bizSeq:{}", this.seq, this.retryTimes, EventMeshUtil.getMessageBizSeq(this.msgExt));
} else {
this.session = rechoosen;
rechoosen.downstreamMsg(this);
logger.info("retry downStream msg end,seq:{},retryTimes:{},bizSeq:{}", this.seq, this.retryTimes, EventMeshUtil.getMessageBizSeq(this.msgExt));
}
} catch (Exception e) {
logger.error("retry-dispatcher error!", e);
}
}

@Override
public long getDelay(TimeUnit unit) {
return unit.convert(this.executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
private boolean isRetryMsgTimeout(DownStreamMsgContext downStreamMsgContext) {
boolean flag = false;
String ttlStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.PROPERTY_MESSAGE_TTL);
long ttl = StringUtils.isNumeric(ttlStr)? Long.parseLong(ttlStr) : EventMeshConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS;;

String storeTimeStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.STORE_TIME);
long storeTimestamp = StringUtils.isNumeric(storeTimeStr)? Long.parseLong(storeTimeStr) : 0;
String leaveTimeStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.LEAVE_TIME);
long brokerCost = StringUtils.isNumeric(leaveTimeStr) ? Long.parseLong(leaveTimeStr) - storeTimestamp : 0;

String arriveTimeStr = downStreamMsgContext.msgExt.getUserProperties(EventMeshConstants.ARRIVE_TIME);
long accessCost = StringUtils.isNumeric(arriveTimeStr) ? System.currentTimeMillis() - Long.parseLong(arriveTimeStr) : 0;
double elapseTime = brokerCost + accessCost;
if (elapseTime >= ttl) {
logger.warn("discard the retry because timeout, seq:{}, retryTimes:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.retryTimes, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.msgExt));
flag = true;
eventMeshAckMsg(downStreamMsgContext);
}
return flag;
}

/**
* eventMesh ack msg
*
* @param downStreamMsgContext
*/
private void eventMeshAckMsg(DownStreamMsgContext downStreamMsgContext) {
List<Message> msgExts = new ArrayList<Message>();
msgExts.add(downStreamMsgContext.msgExt);
logger.warn("eventMeshAckMsg topic:{}, seq:{}, bizSeq:{}", downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION),
downStreamMsgContext.seq, downStreamMsgContext.msgExt.getSystemProperties(EventMeshConstants.PROPERTY_MESSAGE_KEYS));
downStreamMsgContext.consumer.updateOffset(msgExts, downStreamMsgContext.consumeConcurrentlyContext);
}

}
Loading