use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext in project incubator-eventmesh by apache.
the class ClientGroupWrapper method initClientGroupPersistentConsumer.
public synchronized void initClientGroupPersistentConsumer() throws Exception {
if (inited4Persistent.get()) {
return;
}
Properties keyValue = new Properties();
keyValue.put("isBroadcast", "false");
keyValue.put("consumerGroup", group);
keyValue.put("eventMeshIDC", eventMeshTCPConfiguration.eventMeshIDC);
keyValue.put("instanceName", EventMeshUtil.buildMeshTcpClientID(sysId, "SUB", eventMeshTCPConfiguration.eventMeshCluster));
persistentMsgConsumer.init(keyValue);
EventListener listener = (event, context) -> {
eventMeshTcpMonitor.getTcpSummaryMetrics().getMq2eventMeshMsgNum().incrementAndGet();
event = CloudEventBuilder.from(event).withExtension(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())).withExtension(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, eventMeshTCPConfiguration.eventMeshServerIp).build();
String topic = event.getSubject();
EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext) context;
Session session = downstreamDispatchStrategy.select(group, topic, groupConsumerSessions);
String bizSeqNo = EventMeshUtil.getMessageBizSeq(event);
if (session == null) {
try {
Integer sendBackTimes = 0;
String sendBackFromEventMeshIp = "";
if (StringUtils.isNotBlank(Objects.requireNonNull(event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES)).toString())) {
sendBackTimes = (Integer) event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES);
}
if (StringUtils.isNotBlank(Objects.requireNonNull(event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_IP)).toString())) {
sendBackFromEventMeshIp = (String) event.getExtension(EventMeshConstants.EVENTMESH_SEND_BACK_IP);
}
logger.error("found no session to downstream msg,groupName:{}, topic:{}, " + "bizSeqNo:{}, sendBackTimes:{}, sendBackFromEventMeshIp:{}", group, topic, bizSeqNo, sendBackTimes, sendBackFromEventMeshIp);
if (sendBackTimes >= eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSendBackMaxTimes) {
logger.error("sendBack to broker over max times:{}, groupName:{}, topic:{}, " + "bizSeqNo:{}", eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpSendBackMaxTimes, group, topic, bizSeqNo);
} else {
sendBackTimes++;
event = CloudEventBuilder.from(event).withExtension(EventMeshConstants.EVENTMESH_SEND_BACK_TIMES, sendBackTimes.toString()).withExtension(EventMeshConstants.EVENTMESH_SEND_BACK_IP, eventMeshTCPConfiguration.eventMeshServerIp).build();
sendMsgBackToBroker(event, bizSeqNo);
}
} catch (Exception e) {
logger.warn("handle msg exception when no session found", e);
}
eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage);
return;
}
SubscriptionItem subscriptionItem = subscriptions.get(topic);
DownStreamMsgContext downStreamMsgContext = new DownStreamMsgContext(event, session, persistentMsgConsumer, eventMeshAsyncConsumeContext.getAbstractContext(), false, subscriptionItem);
// msg put in eventmesh,waiting client ack
session.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext);
session.downstreamMsg(downStreamMsgContext);
eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck);
};
persistentMsgConsumer.registerEventListener(listener);
inited4Persistent.compareAndSet(false, true);
logger.info("init persistentMsgConsumer success, group:{}", group);
}
use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext in project incubator-eventmesh by apache.
the class ClientSessionGroupMapping method handleUnackMsgsInSession.
/**
* handle unAck msgs in this session
*
* @param session
*/
private void handleUnackMsgsInSession(Session session) {
ConcurrentHashMap<String, DownStreamMsgContext> /**
* seq
*/
unAckMsg = session.getPusher().getUnAckMsg();
if (unAckMsg.size() > 0 && session.getClientGroupWrapper().get().getGroupConsumerSessions().size() > 0) {
for (Map.Entry<String, DownStreamMsgContext> entry : unAckMsg.entrySet()) {
DownStreamMsgContext downStreamMsgContext = entry.getValue();
if (SubscriptionMode.BROADCASTING.equals(downStreamMsgContext.subscriptionItem.getMode())) {
logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}", downStreamMsgContext.seq, EventMeshUtil.getMessageBizSeq(downStreamMsgContext.event), session.getClient());
continue;
}
Session reChooseSession = session.getClientGroupWrapper().get().getDownstreamDispatchStrategy().select(session.getClientGroupWrapper().get().getGroup(), downStreamMsgContext.event.getSubject(), Objects.requireNonNull(session.getClientGroupWrapper().get()).groupConsumerSessions);
if (reChooseSession != null) {
downStreamMsgContext.session = reChooseSession;
reChooseSession.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext);
reChooseSession.downstreamMsg(downStreamMsgContext);
logger.info("rePush msg form unAckMsgs,seq:{},rePushClient:{}", entry.getKey(), downStreamMsgContext.session.getClient());
} else {
logger.warn("select session fail in handleUnackMsgsInSession,seq:{},topic:{}", entry.getKey(), downStreamMsgContext.event.getSubject());
}
}
}
}
use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext in project incubator-eventmesh by apache.
the class ClientGroupWrapper method initClientGroupBroadcastConsumer.
public synchronized void initClientGroupBroadcastConsumer() throws Exception {
if (inited4Broadcast.get()) {
return;
}
Properties keyValue = new Properties();
keyValue.put("isBroadcast", "true");
keyValue.put("consumerGroup", group);
keyValue.put("eventMeshIDC", eventMeshTCPConfiguration.eventMeshIDC);
keyValue.put("instanceName", EventMeshUtil.buildMeshTcpClientID(sysId, "SUB", eventMeshTCPConfiguration.eventMeshCluster));
broadCastMsgConsumer.init(keyValue);
EventListener listener = (event, context) -> {
eventMeshTcpMonitor.getTcpSummaryMetrics().getMq2eventMeshMsgNum().incrementAndGet();
event = CloudEventBuilder.from(event).withExtension(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP, String.valueOf(System.currentTimeMillis())).withExtension(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP, eventMeshTCPConfiguration.eventMeshServerIp).build();
String topic = event.getSubject();
// message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION);
// message.getSystemProperties().put(EventMeshConstants.REQ_MQ2EVENTMESH_TIMESTAMP,
// String.valueOf(System.currentTimeMillis()));
// message.getSystemProperties().put(EventMeshConstants.REQ_RECEIVE_EVENTMESH_IP,
// eventMeshTCPConfiguration.eventMeshServerIp);
EventMeshAsyncConsumeContext eventMeshAsyncConsumeContext = (EventMeshAsyncConsumeContext) context;
if (CollectionUtils.isEmpty(groupConsumerSessions)) {
logger.warn("found no session to downstream broadcast msg");
eventMeshAsyncConsumeContext.commit(EventMeshAction.CommitMessage);
return;
}
Iterator<Session> sessionsItr = groupConsumerSessions.iterator();
SubscriptionItem subscriptionItem = subscriptions.get(topic);
DownStreamMsgContext downStreamMsgContext = new DownStreamMsgContext(event, null, broadCastMsgConsumer, eventMeshAsyncConsumeContext.getAbstractContext(), false, subscriptionItem);
while (sessionsItr.hasNext()) {
Session session = sessionsItr.next();
if (!session.isAvailable(topic)) {
logger.warn("downstream broadcast msg,session is not available,client:{}", session.getClient());
continue;
}
downStreamMsgContext.session = session;
// downstream broadcast msg asynchronously
eventMeshTCPServer.getBroadcastMsgDownstreamExecutorService().submit(new Runnable() {
@Override
public void run() {
// msg put in eventmesh,waiting client ack
session.getPusher().unAckMsg(downStreamMsgContext.seq, downStreamMsgContext);
session.downstreamMsg(downStreamMsgContext);
}
});
}
eventMeshAsyncConsumeContext.commit(EventMeshAction.ManualAck);
};
broadCastMsgConsumer.registerEventListener(listener);
inited4Broadcast.compareAndSet(false, true);
logger.info("init broadCastMsgConsumer success, group:{}", group);
}
use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext in project incubator-eventmesh by apache.
the class MessageAckTask method run.
@Override
public void run() {
long taskExecuteTime = System.currentTimeMillis();
String seq = pkg.getHeader().getSeq();
Command cmd = pkg.getHeader().getCmd();
if (seq == null) {
logger.error("MessageAckTask failed, seq cannot be null|user={}", session.getClient());
return;
}
DownStreamMsgContext downStreamMsgContext = session.getPusher().getUnAckMsg().get(seq);
// ack non-broadcast msg
if (downStreamMsgContext != null) {
downStreamMsgContext.ackMsg();
session.getPusher().getUnAckMsg().remove(seq);
} else {
if (!cmd.equals(Command.RESPONSE_TO_CLIENT_ACK)) {
logger.warn("MessageAckTask, seq:{}, downStreamMsgContext not in downStreamMap,client:{}", seq, session.getClient());
}
}
messageLogger.info("pkg|c2eventMesh|cmd={}|seq=[{}]|user={}|wait={}ms|cost={}ms", cmd, seq, session.getClient(), taskExecuteTime - startTime, System.currentTimeMillis() - startTime);
}
use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext in project incubator-eventmesh by apache.
the class EventMeshTcpRetryer method pushRetry.
public void pushRetry(RetryContext retryContext) {
if (retrys.size() >= eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryQueueSize) {
logger.error("pushRetry fail,retrys is too much,allow max retryQueueSize:{}, retryTimes:{}, seq:{}, bizSeq:{}", eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgRetryQueueSize, retryContext.retryTimes, retryContext.seq, EventMeshUtil.getMessageBizSeq(retryContext.event));
return;
}
int maxRetryTimes = eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgAsyncRetryTimes;
if (retryContext instanceof DownStreamMsgContext) {
DownStreamMsgContext downStreamMsgContext = (DownStreamMsgContext) retryContext;
maxRetryTimes = SubscriptionType.SYNC.equals(downStreamMsgContext.subscriptionItem.getType()) ? eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgSyncRetryTimes : eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshTcpMsgAsyncRetryTimes;
}
if (retryContext.retryTimes >= maxRetryTimes) {
logger.warn("pushRetry fail,retry over maxRetryTimes:{}, retryTimes:{}, seq:{}, bizSeq:{}", maxRetryTimes, retryContext.retryTimes, retryContext.seq, EventMeshUtil.getMessageBizSeq(retryContext.event));
return;
}
retrys.offer(retryContext);
logger.info("pushRetry success,seq:{}, retryTimes:{}, bizSeq:{}", retryContext.seq, retryContext.retryTimes, EventMeshUtil.getMessageBizSeq(retryContext.event));
}
Aggregations