use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.
the class EventMeshTcp2Client method serverGoodby2Client.
public static InetSocketAddress serverGoodby2Client(EventMeshTCPServer eventMeshTCPServer, Session session, ClientSessionGroupMapping mapping) {
logger.info("serverGoodby2Client client[{}]", session.getClient());
try {
long startTime = System.currentTimeMillis();
Package msg = new Package();
msg.setHeader(new Header(SERVER_GOODBYE_REQUEST, OPStatus.SUCCESS.getCode(), "graceful normal quit from eventmesh", null));
eventMeshTCPServer.getScheduler().submit(new Runnable() {
@Override
public void run() {
long taskExecuteTime = System.currentTimeMillis();
Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
}
});
InetSocketAddress address = (InetSocketAddress) session.getContext().channel().remoteAddress();
closeSessionIfTimeout(eventMeshTCPServer, session, mapping);
return address;
} catch (Exception e) {
logger.error("exception occur while serverGoodby2Client", e);
return null;
}
}
use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.
the class EventMeshTcpMessageDispatcher method writeToClient.
private void writeToClient(Command cmd, Package pkg, ChannelHandlerContext ctx, Exception e) {
try {
Package res = new Package();
res.setHeader(new Header(getReplyCommand(cmd), OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq()));
ctx.writeAndFlush(res);
} catch (Exception ex) {
logger.warn("writeToClient failed", ex);
}
}
use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.
the class HelloTask method run.
@Override
public void run() {
long taskExecuteTime = System.currentTimeMillis();
Package res = new Package();
Session session = null;
UserAgent user = (UserAgent) pkg.getBody();
try {
// do acl check in connect
if (eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable) {
String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
Acl.doAclCheckInTcpConnect(remoteAddr, user, HELLO_REQUEST.value());
}
if (eventMeshTCPServer.getEventMeshServer().getServiceState() != ServiceState.RUNNING) {
logger.error("server state is not running:{}", eventMeshTCPServer.getEventMeshServer().getServiceState());
throw new Exception("server state is not running, maybe deploying...");
}
validateUserAgent(user);
session = eventMeshTCPServer.getClientSessionGroupMapping().createSession(user, ctx);
res.setHeader(new Header(HELLO_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq()));
Utils.writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session);
} catch (Throwable e) {
messageLogger.error("HelloTask failed|address={},errMsg={}", ctx.channel().remoteAddress(), e);
res.setHeader(new Header(HELLO_RESPONSE, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg.getHeader().getSeq()));
ctx.writeAndFlush(res).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
Utils.logFailedMessageFlow(future, res, user, startTime, taskExecuteTime);
} else {
Utils.logSucceedMessageFlow(res, user, startTime, taskExecuteTime);
}
logger.warn("HelloTask failed,close session,addr:{}", ctx.channel().remoteAddress());
eventMeshTCPServer.getClientSessionGroupMapping().closeSession(ctx);
}
});
}
}
use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.
the class MessageTransferTask method run.
@Override
public void run() {
long taskExecuteTime = System.currentTimeMillis();
Command cmd = pkg.getHeader().getCmd();
Command replyCmd = getReplyCmd(cmd);
String protocolType = "EventMeshMessage";
if (pkg.getHeader().getProperties() != null && pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE) != null) {
protocolType = (String) pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE);
}
ProtocolAdaptor<ProtocolTransportObject> protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
Package msg = new Package();
// EventMeshMessage eventMeshMessage = (EventMeshMessage) pkg.getBody();
int retCode = 0;
EventMeshTcpSendResult sendStatus;
CloudEvent event = null;
try {
event = protocolAdaptor.toCloudEvent(pkg);
if (event == null) {
throw new Exception("event is null");
}
String content = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
if (content.length() > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshEventSize) {
throw new Exception("event size exceeds the limit: " + eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshEventSize);
}
// do acl check in sending msg
if (eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable) {
String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
Acl.doAclCheckInTcpSend(remoteAddr, session.getClient(), event.getSubject(), cmd.value());
}
if (eventMeshTCPServer.getRateLimiter().tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) {
synchronized (session) {
long sendTime = System.currentTimeMillis();
event = addTimestamp(event, cmd, sendTime);
sendStatus = session.upstreamMsg(pkg.getHeader(), event, createSendCallback(replyCmd, taskExecuteTime, event), startTime, taskExecuteTime);
if (StringUtils.equals(EventMeshTcpSendStatus.SUCCESS.name(), sendStatus.getSendStatus().name())) {
messageLogger.info("pkg|eventMesh2mq|cmd={}|Msg={}|user={}|wait={}ms|cost={}ms", cmd, event, session.getClient(), taskExecuteTime - startTime, sendTime - startTime);
} else {
throw new Exception(sendStatus.getDetail());
}
}
} else {
msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), "Tps overload, global flow control", pkg.getHeader().getSeq()));
ctx.writeAndFlush(msg).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
Utils.logSucceedMessageFlow(msg, session.getClient(), startTime, taskExecuteTime);
}
});
logger.warn("======Tps overload, global flow control, rate:{}! PLEASE CHECK!========", eventMeshTCPServer.getRateLimiter().getRate());
return;
}
} catch (Exception e) {
logger.error("MessageTransferTask failed|cmd={}|event={}|user={}|errMsg={}", cmd, event, session.getClient(), e);
if (!cmd.equals(RESPONSE_TO_SERVER)) {
msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg.getHeader().getSeq()));
Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
}
}
}
use of org.apache.eventmesh.common.protocol.tcp.Package in project incubator-eventmesh by apache.
the class RecommendTask method run.
@Override
public void run() {
long taskExecuteTime = System.currentTimeMillis();
Package res = new Package();
try {
if (!eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerRegistryEnable) {
throw new Exception("registry enable config is false, not support");
}
UserAgent user = (UserAgent) pkg.getBody();
validateUserAgent(user);
String group = getGroupOfClient(user);
EventMeshRecommendStrategy eventMeshRecommendStrategy = new EventMeshRecommendImpl(eventMeshTCPServer);
String eventMeshRecommendResult = eventMeshRecommendStrategy.calculateRecommendEventMesh(group, user.getPurpose());
res.setHeader(new Header(RECOMMEND_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq()));
res.setBody(eventMeshRecommendResult);
} catch (Exception e) {
messageLogger.error("RecommendTask failed|address={}|errMsg={}", ctx.channel().remoteAddress(), e);
res.setHeader(new Header(RECOMMEND_RESPONSE, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq()));
} finally {
writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session);
// session.write2Client(res);
}
}
Aggregations