Search in sources :

Example 11 with RPCHook

use of org.apache.rocketmq.remoting.RPCHook in project rocketmq-externals by apache.

the class ConnectUtil method startMQAdminTool.

public static DefaultMQAdminExt startMQAdminTool(ConnectConfig connectConfig) throws MQClientException {
    RPCHook rpcHook = null;
    if (connectConfig.getAclEnable()) {
        rpcHook = new AclClientRPCHook(new SessionCredentials(connectConfig.getAccessKey(), connectConfig.getSecretKey()));
    }
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setNamesrvAddr(connectConfig.getNamesrvAddr());
    defaultMQAdminExt.setAdminExtGroup(connectConfig.getAdminExtGroup());
    defaultMQAdminExt.setInstanceName(ConnectUtil.createUniqInstance(connectConfig.getNamesrvAddr()));
    defaultMQAdminExt.start();
    return defaultMQAdminExt;
}
Also used : SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook)

Example 12 with RPCHook

use of org.apache.rocketmq.remoting.RPCHook in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class NettyRemotingAbstract method processRequestCommand.

/**
 * Process incoming request command issued by remote peer.
 * @param ctx channel handler context.
 * @param cmd request command.
 */
public void processRequestCommand(final ChannelHandlerContext ctx, final RemotingCommand cmd) {
    final Pair<NettyRequestProcessor, ExecutorService> matched = this.processorTable.get(cmd.getCode());
    final Pair<NettyRequestProcessor, ExecutorService> pair = null == matched ? this.defaultRequestProcessor : matched;
    final int opaque = cmd.getOpaque();
    if (pair != null) {
        Runnable run = new Runnable() {

            @Override
            public void run() {
                try {
                    RPCHook rpcHook = NettyRemotingAbstract.this.getRPCHook();
                    if (rpcHook != null) {
                        // 处理前
                        rpcHook.doBeforeRequest(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd);
                    }
                    final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd);
                    if (rpcHook != null) {
                        rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd, response);
                    }
                    if (!cmd.isOnewayRPC()) {
                        if (response != null) {
                            response.setOpaque(opaque);
                            response.markResponseType();
                            try {
                                ctx.writeAndFlush(response);
                            } catch (Throwable e) {
                                PLOG.error("process request over, but response failed", e);
                                PLOG.error(cmd.toString());
                                PLOG.error(response.toString());
                            }
                        } else {
                        // 收到请求,但是没有返回应答,可能是processRequest中进行了应答,忽略这种情况
                        }
                    }
                } catch (Throwable e) {
                    PLOG.error("process request exception", e);
                    PLOG.error(cmd.toString());
                    if (!cmd.isOnewayRPC()) {
                        final RemotingCommand response = // 
                        RemotingCommand.createResponseCommand(// 
                        RemotingSysResponseCode.SYSTEM_ERROR, RemotingHelper.exceptionSimpleDesc(e));
                        response.setOpaque(opaque);
                        ctx.writeAndFlush(response);
                    }
                }
            }
        };
        if (pair.getObject1().rejectRequest()) {
            final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY, "[REJECTREQUEST]system busy, start flow control for a while");
            response.setOpaque(opaque);
            ctx.writeAndFlush(response);
            return;
        }
        try {
            final RequestTask requestTask = new RequestTask(run, ctx.channel(), cmd);
            // 这里需要做流控,要求线程池对应的队列必须是有大小限制的
            pair.getObject2().submit(requestTask);
        } catch (RejectedExecutionException e) {
            // 每个线程10s打印一次
            if ((System.currentTimeMillis() % 10000) == 0) {
                PLOG.warn(// 
                RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + // 
                ", too many requests and system thread pool busy, RejectedExecutionException " + // 
                pair.getObject2().toString() + " request code: " + cmd.getCode());
            }
            if (!cmd.isOnewayRPC()) {
                final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY, "[OVERLOAD]system busy, start flow control for a while");
                response.setOpaque(opaque);
                ctx.writeAndFlush(response);
            }
        }
    } else {
        String error = " request type " + cmd.getCode() + " not supported";
        final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error);
        response.setOpaque(opaque);
        ctx.writeAndFlush(response);
        PLOG.error(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + error);
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ExecutorService(java.util.concurrent.ExecutorService) RPCHook(org.apache.rocketmq.remoting.RPCHook) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 13 with RPCHook

use of org.apache.rocketmq.remoting.RPCHook in project rocketmq by apache.

the class NettyRemotingAbstract method processRequestCommand.

/**
 * Process incoming request command issued by remote peer.
 *
 * @param ctx channel handler context.
 * @param cmd request command.
 */
public void processRequestCommand(final ChannelHandlerContext ctx, final RemotingCommand cmd) {
    final Pair<NettyRequestProcessor, ExecutorService> matched = this.processorTable.get(cmd.getCode());
    final Pair<NettyRequestProcessor, ExecutorService> pair = null == matched ? this.defaultRequestProcessor : matched;
    final int opaque = cmd.getOpaque();
    if (pair != null) {
        Runnable run = new Runnable() {

            @Override
            public void run() {
                try {
                    RPCHook rpcHook = NettyRemotingAbstract.this.getRPCHook();
                    if (rpcHook != null) {
                        rpcHook.doBeforeRequest(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd);
                    }
                    final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd);
                    if (rpcHook != null) {
                        rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd, response);
                    }
                    if (!cmd.isOnewayRPC()) {
                        if (response != null) {
                            response.setOpaque(opaque);
                            response.markResponseType();
                            try {
                                ctx.writeAndFlush(response);
                            } catch (Throwable e) {
                                log.error("process request over, but response failed", e);
                                log.error(cmd.toString());
                                log.error(response.toString());
                            }
                        } else {
                        }
                    }
                } catch (Throwable e) {
                    log.error("process request exception", e);
                    log.error(cmd.toString());
                    if (!cmd.isOnewayRPC()) {
                        final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_ERROR, RemotingHelper.exceptionSimpleDesc(e));
                        response.setOpaque(opaque);
                        ctx.writeAndFlush(response);
                    }
                }
            }
        };
        if (pair.getObject1().rejectRequest()) {
            final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY, "[REJECTREQUEST]system busy, start flow control for a while");
            response.setOpaque(opaque);
            ctx.writeAndFlush(response);
            return;
        }
        try {
            final RequestTask requestTask = new RequestTask(run, ctx.channel(), cmd);
            pair.getObject2().submit(requestTask);
        } catch (RejectedExecutionException e) {
            if ((System.currentTimeMillis() % 10000) == 0) {
                log.warn(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + ", too many requests and system thread pool busy, RejectedExecutionException " + pair.getObject2().toString() + " request code: " + cmd.getCode());
            }
            if (!cmd.isOnewayRPC()) {
                final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY, "[OVERLOAD]system busy, start flow control for a while");
                response.setOpaque(opaque);
                ctx.writeAndFlush(response);
            }
        }
    } else {
        String error = " request type " + cmd.getCode() + " not supported";
        final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error);
        response.setOpaque(opaque);
        ctx.writeAndFlush(response);
        log.error(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + error);
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ExecutorService(java.util.concurrent.ExecutorService) RPCHook(org.apache.rocketmq.remoting.RPCHook) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 14 with RPCHook

use of org.apache.rocketmq.remoting.RPCHook in project canal by alibaba.

the class CanalRocketMQConsumer method connect.

@Override
public void connect() {
    RPCHook rpcHook = null;
    if (null != accessKey && accessKey.length() > 0 && null != secretKey && secretKey.length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(accessKey);
        sessionCredentials.setSecretKey(secretKey);
        rpcHook = new AclClientRPCHook(sessionCredentials);
    }
    rocketMQConsumer = new DefaultMQPushConsumer(groupName, rpcHook, new AllocateMessageQueueAveragely(), enableMessageTrace, customizedTraceTopic);
    rocketMQConsumer.setVipChannelEnabled(false);
    if (CLOUD_ACCESS_CHANNEL.equals(this.accessChannel)) {
        rocketMQConsumer.setAccessChannel(AccessChannel.CLOUD);
    }
    if (!StringUtils.isEmpty(this.namespace)) {
        rocketMQConsumer.setNamespace(this.namespace);
    }
    if (!StringUtils.isBlank(nameServer)) {
        rocketMQConsumer.setNamesrvAddr(nameServer);
    }
    if (batchSize != -1) {
        rocketMQConsumer.setConsumeMessageBatchMaxSize(batchSize);
    }
    try {
        if (rocketMQConsumer == null) {
            this.connect();
        }
        rocketMQConsumer.subscribe(this.topic, this.filter);
        rocketMQConsumer.registerMessageListener((MessageListenerOrderly) (messageExts, context) -> {
            context.setAutoCommit(true);
            boolean isSuccess = process(messageExts);
            if (isSuccess) {
                return ConsumeOrderlyStatus.SUCCESS;
            } else {
                return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }
        });
        rocketMQConsumer.start();
    } catch (MQClientException ex) {
        logger.error("Start RocketMQ consumer error", ex);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) LoggerFactory(org.slf4j.LoggerFactory) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageUtil(com.alibaba.otter.canal.connector.core.util.MessageUtil) CanalMessageSerializerUtil(com.alibaba.otter.canal.connector.core.util.CanalMessageSerializerUtil) ArrayList(java.util.ArrayList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalConstants(com.alibaba.otter.canal.connector.core.config.CanalConstants) RocketMQConstants(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQConstants) CanalMsgConsumer(com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer) Properties(java.util.Properties) Logger(org.slf4j.Logger) Message(com.alibaba.otter.canal.protocol.Message) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) BlockingQueue(java.util.concurrent.BlockingQueue) SPI(com.alibaba.otter.canal.connector.core.spi.SPI) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) TimeUnit(java.util.concurrent.TimeUnit) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) List(java.util.List) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) JSON(com.alibaba.fastjson.JSON) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) AccessChannel(org.apache.rocketmq.client.AccessChannel) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 15 with RPCHook

use of org.apache.rocketmq.remoting.RPCHook in project canal by alibaba.

the class CanalRocketMQProducer method init.

@Override
public void init(Properties properties) {
    RocketMQProducerConfig rocketMQProperties = new RocketMQProducerConfig();
    this.mqProperties = rocketMQProperties;
    super.init(properties);
    loadRocketMQProperties(properties);
    RPCHook rpcHook = null;
    if (mqProperties.getAliyunAccessKey().length() > 0 && mqProperties.getAliyunSecretKey().length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(mqProperties.getAliyunAccessKey());
        sessionCredentials.setSecretKey(mqProperties.getAliyunSecretKey());
        rpcHook = new AclClientRPCHook(sessionCredentials);
    }
    defaultMQProducer = new DefaultMQProducer(rocketMQProperties.getProducerGroup(), rpcHook, rocketMQProperties.isEnableMessageTrace(), rocketMQProperties.getCustomizedTraceTopic());
    if (CLOUD_ACCESS_CHANNEL.equals(rocketMQProperties.getAccessChannel())) {
        defaultMQProducer.setAccessChannel(AccessChannel.CLOUD);
    }
    if (!StringUtils.isEmpty(rocketMQProperties.getNamespace())) {
        defaultMQProducer.setNamespace(rocketMQProperties.getNamespace());
    }
    defaultMQProducer.setNamesrvAddr(rocketMQProperties.getNamesrvAddr());
    defaultMQProducer.setRetryTimesWhenSendFailed(rocketMQProperties.getRetryTimesWhenSendFailed());
    defaultMQProducer.setVipChannelEnabled(rocketMQProperties.isVipChannelEnabled());
    logger.info("##Start RocketMQ producer##");
    try {
        defaultMQProducer.start();
    } catch (MQClientException ex) {
        throw new CanalException("Start RocketMQ producer error", ex);
    }
    int parallelPartitionSendThreadSize = mqProperties.getParallelSendThreadSize();
    sendPartitionExecutor = new ThreadPoolExecutor(parallelPartitionSendThreadSize, parallelPartitionSendThreadSize, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(parallelPartitionSendThreadSize * 2), new NamedThreadFactory("MQ-Parallel-Sender-Partition"), new ThreadPoolExecutor.CallerRunsPolicy());
}
Also used : SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NamedThreadFactory(com.alibaba.otter.canal.common.utils.NamedThreadFactory) RocketMQProducerConfig(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalException(com.alibaba.otter.canal.common.CanalException)

Aggregations

RPCHook (org.apache.rocketmq.remoting.RPCHook)16 AclClientRPCHook (org.apache.rocketmq.acl.common.AclClientRPCHook)11 SessionCredentials (org.apache.rocketmq.acl.common.SessionCredentials)11 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)7 MQClientException (org.apache.rocketmq.client.exception.MQClientException)5 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)3 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)2 AllocateMessageQueueAveragely (org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2 JSON (com.alibaba.fastjson.JSON)1 CanalException (com.alibaba.otter.canal.common.CanalException)1 NamedThreadFactory (com.alibaba.otter.canal.common.utils.NamedThreadFactory)1 CanalConstants (com.alibaba.otter.canal.connector.core.config.CanalConstants)1 CommonMessage (com.alibaba.otter.canal.connector.core.consumer.CommonMessage)1 CanalMsgConsumer (com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer)1 SPI (com.alibaba.otter.canal.connector.core.spi.SPI)1