Search in sources :

Example 21 with RemotingCommandException

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

the class AdminBrokerProcessor method updateAndCreateTopic.

private RemotingCommand updateAndCreateTopic(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class);
    log.info("updateAndCreateTopic called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
    if (requestHeader.getTopic().equals(this.brokerController.getBrokerConfig().getBrokerClusterName())) {
        String errorMsg = "the topic[" + requestHeader.getTopic() + "] is conflict with system reserved words.";
        log.warn(errorMsg);
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(errorMsg);
        return response;
    }
    try {
        response.setCode(ResponseCode.SUCCESS);
        response.setOpaque(request.getOpaque());
        response.markResponseType();
        response.setRemark(null);
        ctx.writeAndFlush(response);
    } catch (Exception e) {
        log.error("Failed to produce a proper response", e);
    }
    TopicConfig topicConfig = new TopicConfig(requestHeader.getTopic());
    topicConfig.setReadQueueNums(requestHeader.getReadQueueNums());
    topicConfig.setWriteQueueNums(requestHeader.getWriteQueueNums());
    topicConfig.setTopicFilterType(requestHeader.getTopicFilterTypeEnum());
    topicConfig.setPerm(requestHeader.getPerm());
    topicConfig.setTopicSysFlag(requestHeader.getTopicSysFlag() == null ? 0 : requestHeader.getTopicSysFlag());
    this.brokerController.getTopicConfigManager().updateTopicConfig(topicConfig);
    this.brokerController.registerBrokerAll(false, true);
    return null;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) CreateTopicRequestHeader(org.apache.rocketmq.common.protocol.header.CreateTopicRequestHeader) TopicConfig(org.apache.rocketmq.common.TopicConfig) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 22 with RemotingCommandException

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

the class ClientManageProcessor method checkClientConfig.

public RemotingCommand checkClientConfig(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    CheckClientRequestBody requestBody = CheckClientRequestBody.decode(request.getBody(), CheckClientRequestBody.class);
    if (requestBody != null && requestBody.getSubscriptionData() != null) {
        SubscriptionData subscriptionData = requestBody.getSubscriptionData();
        if (ExpressionType.isTagType(subscriptionData.getExpressionType())) {
            response.setCode(ResponseCode.SUCCESS);
            response.setRemark(null);
            return response;
        }
        if (!this.brokerController.getBrokerConfig().isEnablePropertyFilter()) {
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("The broker does not support consumer to filter message by " + subscriptionData.getExpressionType());
            return response;
        }
        try {
            FilterFactory.INSTANCE.get(subscriptionData.getExpressionType()).compile(subscriptionData.getSubString());
        } catch (Exception e) {
            log.warn("Client {}@{} filter message, but failed to compile expression! sub={}, error={}", requestBody.getClientId(), requestBody.getGroup(), requestBody.getSubscriptionData(), e.getMessage());
            response.setCode(ResponseCode.SUBSCRIPTION_PARSE_FAILED);
            response.setRemark(e.getMessage());
            return response;
        }
    }
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) CheckClientRequestBody(org.apache.rocketmq.common.protocol.body.CheckClientRequestBody) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 23 with RemotingCommandException

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

the class QueryMessageProcessor method queryMessage.

// 查询消息
public RemotingCommand queryMessage(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(QueryMessageResponseHeader.class);
    final QueryMessageResponseHeader responseHeader = (QueryMessageResponseHeader) response.readCustomHeader();
    final QueryMessageRequestHeader requestHeader = (QueryMessageRequestHeader) request.decodeCommandCustomHeader(QueryMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    String isUniqueKey = request.getExtFields().get(MixAll.UNIQUE_MSG_QUERY_FLAG);
    if (isUniqueKey != null && isUniqueKey.equals("true")) {
        requestHeader.setMaxNum(this.brokerController.getMessageStoreConfig().getDefaultQueryMaxNum());
    }
    final QueryMessageResult queryMessageResult = this.brokerController.getMessageStore().queryMessage(requestHeader.getTopic(), requestHeader.getKey(), requestHeader.getMaxNum(), requestHeader.getBeginTimestamp(), requestHeader.getEndTimestamp());
    assert queryMessageResult != null;
    responseHeader.setIndexLastUpdatePhyoffset(queryMessageResult.getIndexLastUpdatePhyoffset());
    responseHeader.setIndexLastUpdateTimestamp(queryMessageResult.getIndexLastUpdateTimestamp());
    // 说明找到消息
    if (queryMessageResult.getBufferTotalSize() > 0) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new QueryMessageTransfer(response.encodeHeader(queryMessageResult.getBufferTotalSize()), queryMessageResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    queryMessageResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer query message by page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            queryMessageResult.release();
        }
        return null;
    }
    response.setCode(ResponseCode.QUERY_NOT_FOUND);
    response.setRemark("can not find message, maybe time range not correct");
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ChannelFuture(io.netty.channel.ChannelFuture) QueryMessageTransfer(org.apache.rocketmq.broker.pagecache.QueryMessageTransfer) QueryMessageResponseHeader(org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader) QueryMessageRequestHeader(org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader) QueryMessageResult(org.apache.rocketmq.store.QueryMessageResult) FileRegion(io.netty.channel.FileRegion) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 24 with RemotingCommandException

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

the class DefaultRequestProcessor method registerMessageFilterClass.

private RemotingCommand registerMessageFilterClass(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final RegisterMessageFilterClassRequestHeader requestHeader = (RegisterMessageFilterClassRequestHeader) request.decodeCommandCustomHeader(RegisterMessageFilterClassRequestHeader.class);
    try {
        boolean ok = this.filtersrvController.getFilterClassManager().registerFilterClass(requestHeader.getConsumerGroup(), requestHeader.getTopic(), requestHeader.getClassName(), requestHeader.getClassCRC(), request.getBody());
        if (!ok) {
            throw new Exception("registerFilterClass error");
        }
    } catch (Exception e) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(RemotingHelper.exceptionSimpleDesc(e));
        return response;
    }
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) RegisterMessageFilterClassRequestHeader(org.apache.rocketmq.common.protocol.header.filtersrv.RegisterMessageFilterClassRequestHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) IOException(java.io.IOException)

Example 25 with RemotingCommandException

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

the class RemotingCommand method decodeCommandCustomHeader.

/**
 *解码过程
 */
public CommandCustomHeader decodeCommandCustomHeader(Class<? extends CommandCustomHeader> classHeader) throws RemotingCommandException {
    CommandCustomHeader objectHeader;
    try {
        objectHeader = classHeader.newInstance();
    } catch (InstantiationException e) {
        return null;
    } catch (IllegalAccessException e) {
        return null;
    }
    if (this.extFields != null) {
        Field[] fields = getClazzFields(classHeader);
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers())) {
                String fieldName = field.getName();
                if (!fieldName.startsWith("this")) {
                    try {
                        // 字段不允许为空
                        // 从扩展字段里面根据key取值,如果值为空,看看是否标记注解不为空,如果标记了那么表示有问题了
                        String value = this.extFields.get(fieldName);
                        if (null == value) {
                            // 进行了缓存 设计模式享元模式 欢迎参考【匠心零度】公众号文章,http://mp.weixin.qq.com/s/aMTVoawP_795kSio5ysh7Q
                            Annotation annotation = getNotNullAnnotation(field);
                            if (annotation != null) {
                                throw new RemotingCommandException("the custom field <" + fieldName + "> is null");
                            }
                            continue;
                        }
                        field.setAccessible(true);
                        String type = getCanonicalName(field.getType());
                        Object valueParsed;
                        if (type.equals(STRING_CANONICAL_NAME)) {
                            valueParsed = value;
                        } else if (type.equals(INTEGER_CANONICAL_NAME_1) || type.equals(INTEGER_CANONICAL_NAME_2)) {
                            valueParsed = Integer.parseInt(value);
                        } else if (type.equals(LONG_CANONICAL_NAME_1) || type.equals(LONG_CANONICAL_NAME_2)) {
                            valueParsed = Long.parseLong(value);
                        } else if (type.equals(BOOLEAN_CANONICAL_NAME_1) || type.equals(BOOLEAN_CANONICAL_NAME_2)) {
                            valueParsed = Boolean.parseBoolean(value);
                        } else if (type.equals(DOUBLE_CANONICAL_NAME_1) || type.equals(DOUBLE_CANONICAL_NAME_2)) {
                            valueParsed = Double.parseDouble(value);
                        } else {
                            throw new RemotingCommandException("the custom field <" + fieldName + "> type is not supported");
                        }
                        field.set(objectHeader, valueParsed);
                    } catch (Throwable e) {
                        log.error("Failed field [{}] decoding", fieldName, e);
                    }
                }
            }
        }
        objectHeader.checkFields();
    }
    return objectHeader;
}
Also used : JSONField(com.alibaba.fastjson.annotation.JSONField) Field(java.lang.reflect.Field) CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) Annotation(java.lang.annotation.Annotation)

Aggregations

RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)26 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)22 ChannelFuture (io.netty.channel.ChannelFuture)8 ChannelFutureListener (io.netty.channel.ChannelFutureListener)8 FileRegion (io.netty.channel.FileRegion)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 UnknownHostException (java.net.UnknownHostException)4 MQClientException (org.apache.rocketmq.client.exception.MQClientException)4 TopicConfig (org.apache.rocketmq.common.TopicConfig)4 QueryMessageRequestHeader (org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader)4 QueryMessageResponseHeader (org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader)4 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)4 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)4 CommandCustomHeader (org.apache.rocketmq.remoting.CommandCustomHeader)4 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)4 JSONField (com.alibaba.fastjson.annotation.JSONField)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 LinkedList (java.util.LinkedList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2