RocketMQ源码系列(五)RocketMQ消息存储,broker源码篇,消息存储流程

消息存储流程

默认方法为org.apache.rocketmq.store.DefaultMessageStore中的putMessage(MessageExtBrokerInner msg)方法
1、检查是否可写入消息
2、检查topic和消息属性长度是否合法
3、写入消息
4、设置写入时长
5、判断写入结果
6、返回结果

    @Override
    public PutMessageResult putMessage(MessageExtBrokerInner msg) {
    
    
        //检查是否可写入消息
        PutMessageStatus checkStoreStatus = this.checkStoreStatus();
        if (checkStoreStatus != PutMessageStatus.PUT_OK) {
    
    
            return new PutMessageResult(checkStoreStatus, null);
        }

        //检查topic和消息属性长度是否合法
        PutMessageStatus msgCheckStatus = this.checkMessage(msg);
        if (msgCheckStatus == PutMessageStatus.MESSAGE_ILLEGAL) {
    
    
            return new PutMessageResult(msgCheckStatus, null);
        }

        //写入消息
        long beginTime = this.getSystemClock().now();
        PutMessageResult result = this.commitLog.putMessage(msg);
        long elapsedTime = this.getSystemClock().now() - beginTime;
        //判断是否超时
        if (elapsedTime > 500) {
    
    
            log.warn("not in lock elapsed time(ms)={}, bodyLength={}", elapsedTime, msg.getBody().length);
        }

        //设置写入时长
        this.storeStatsService.setPutMessageEntireTimeMax(elapsedTime);

        //判断写入结果
        if (null == result || !result.isOk()) {
    
    
            this.storeStatsService.getPutMessageFailedTimes().incrementAndGet();
        }
		//返回结果
        return result;
    }

检查是否可写入消息

    private PutMessageStatus checkStoreStatus() {
    
    
        //判断broker是否停止工作
        if (this.shutdown) {
    
    
            log.warn("message store has shutdown, so putMessage is forbidden");
            return PutMessageStatus.SERVICE_NOT_AVAILABLE;
        }

        //判断是否是slave节点
        if (BrokerRole.SLAVE == this.messageStoreConfig.getBrokerRole()) {
    
    
            long value = this.printTimes.getAndIncrement();
            if ((value % 50000) == 0) {
    
    
                log.warn("message store has shutdown, so putMessage is forbidden");
            }
            return PutMessageStatus.SERVICE_NOT_AVAILABLE;
        }

        //判断是否拥有写权限
        if (!this.runningFlags.isWriteable()) {
    
    
            long value = this.printTimes.getAndIncrement();
            if ((value % 50000) == 0) {
    
    
                log.warn("message store has shutdown, so putMessage is forbidden");
            }
            return PutMessageStatus.SERVICE_NOT_AVAILABLE;
        } else {
    
    
            this.printTimes.set(0);
        }

        if (this.isOSPageCacheBusy()) {
    
    
            return PutMessageStatus.OS_PAGECACHE_BUSY;
        }
        return PutMessageStatus.PUT_OK;
    }

检查topic和消息属性长度是否合法

    private PutMessageStatus checkMessage(MessageExtBrokerInner msg) {
    
    
        //检查topic长度不能超过127
        if (msg.getTopic().length() > Byte.MAX_VALUE) {
    
    
            log.warn("putMessage message topic length too long " + msg.getTopic().length());
            return PutMessageStatus.MESSAGE_ILLEGAL;
        }

        //消息属性长度不能超过32767个字符
        if (msg.getPropertiesString() != null && msg.getPropertiesString().length() > Short.MAX_VALUE) {
    
    
            log.warn("putMessage message properties length too long " + msg.getPropertiesString().length());
            return PutMessageStatus.MESSAGE_ILLEGAL;
        }
        return PutMessageStatus.PUT_OK;
    }

写入消息

    public PutMessageResult putMessage(final MessageExtBrokerInner msg) {
    
    
        // Set the storage time
        msg.setStoreTimestamp(System.currentTimeMillis());
        // Set the message body BODY CRC (consider the most appropriate setting
        // on the client)
        msg.setBodyCRC(UtilAll.crc32(msg.getBody()));
        // Back to Results
        AppendMessageResult result = null;

        StoreStatsService storeStatsService = this.defaultMessageStore.getStoreStatsService();

        String topic = msg.getTopic();
        int queueId = msg.getQueueId();

        //判断事务类型
        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
        if (tranType == MessageSysFlag.TRANSACTION_NOT_TYPE
            || tranType == MessageSysFlag.TRANSACTION_COMMIT_TYPE) {
    
    
            // Delay Delivery,如果消息延迟级别大于0
            if (msg.getDelayTimeLevel() > 0) {
    
    
                if (msg.getDelayTimeLevel() > this.defaultMessageStore.getScheduleMessageService().getMaxDelayLevel()) {
    
    
                    msg.setDelayTimeLevel(this.defaultMessageStore.getScheduleMessageService().getMaxDelayLevel());
                }

                //使用延迟消息主题RMQ_SYS_SCHEDULE_TOPIC
                topic = TopicValidator.RMQ_SYS_SCHEDULE_TOPIC;
                queueId = ScheduleMessageService.delayLevel2QueueId(msg.getDelayTimeLevel());

                // Backup real topic, queueId
                MessageAccessor.putProperty(msg, MessageConst.PROPERTY_REAL_TOPIC, msg.getTopic());
                MessageAccessor.putProperty(msg, MessageConst.PROPERTY_REAL_QUEUE_ID, String.valueOf(msg.getQueueId()));
                msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

                //将消息的原topic名称和原队列ID存入属性中
                msg.setTopic(topic);
                msg.setQueueId(queueId);
            }
        }

        InetSocketAddress bornSocketAddress = (InetSocketAddress) msg.getBornHost();
        if (bornSocketAddress.getAddress() instanceof Inet6Address) {
    
    
            msg.setBornHostV6Flag();
        }

        InetSocketAddress storeSocketAddress = (InetSocketAddress) msg.getStoreHost();
        if (storeSocketAddress.getAddress() instanceof Inet6Address) {
    
    
            msg.setStoreHostAddressV6Flag();
        }

        long elapsedTimeInLock = 0;

        MappedFile unlockMappedFile = null;
        //mappedFileQueue相当于CommitLog文件夹,MappedFile则是文件夹下的单个文件
        //获取当前的commitlog文件
        MappedFile mappedFile = this.mappedFileQueue.getLastMappedFile();

        //在写入commitlog前,先获取写入锁,说明消息存储是串行的
        putMessageLock.lock(); //spin or ReentrantLock ,depending on store config
        try {
    
    
            long beginLockTimestamp = this.defaultMessageStore.getSystemClock().now();
            this.beginTimeInLock = beginLockTimestamp;

            // Here settings are stored timestamp, in order to ensure an orderly
            // global,设置消息存储的开始时间
            msg.setStoreTimestamp(beginLockTimestamp);

            if (null == mappedFile || mappedFile.isFull()) {
    
    
                //如果文件不存在或者文件已满,则新创建一个文件
                mappedFile = this.mappedFileQueue.getLastMappedFile(0); // Mark: NewFile may be cause noise
            }
            if (null == mappedFile) {
    
    
                //判断文件是否创建成功
                log.error("create mapped file1 error, topic: " + msg.getTopic() + " clientAddr: " + msg.getBornHostString());
                beginTimeInLock = 0;
                return new PutMessageResult(PutMessageStatus.CREATE_MAPEDFILE_FAILED, null);
            }

            //写入消息
            result = mappedFile.appendMessage(msg, this.appendMessageCallback);
            //判断消息存储结果
            switch (result.getStatus()) {
    
    
                case PUT_OK:
                    break;
                case END_OF_FILE:
                    unlockMappedFile = mappedFile;
                    // Create a new file, re-write the message
                    mappedFile = this.mappedFileQueue.getLastMappedFile(0);
                    if (null == mappedFile) {
    
    
                        // XXX: warn and notify me
                        log.error("create mapped file2 error, topic: " + msg.getTopic() + " clientAddr: " + msg.getBornHostString());
                        beginTimeInLock = 0;
                        return new PutMessageResult(PutMessageStatus.CREATE_MAPEDFILE_FAILED, result);
                    }
                    result = mappedFile.appendMessage(msg, this.appendMessageCallback);
                    break;
                case MESSAGE_SIZE_EXCEEDED:
                case PROPERTIES_SIZE_EXCEEDED:
                    beginTimeInLock = 0;
                    return new PutMessageResult(PutMessageStatus.MESSAGE_ILLEGAL, result);
                case UNKNOWN_ERROR:
                    beginTimeInLock = 0;
                    return new PutMessageResult(PutMessageStatus.UNKNOWN_ERROR, result);
                default:
                    beginTimeInLock = 0;
                    return new PutMessageResult(PutMessageStatus.UNKNOWN_ERROR, result);
            }

            elapsedTimeInLock = this.defaultMessageStore.getSystemClock().now() - beginLockTimestamp;
            beginTimeInLock = 0;
        } finally {
    
    
            putMessageLock.unlock();
        }

        if (elapsedTimeInLock > 500) {
    
    
            log.warn("[NOTIFYME]putMessage in lock cost time(ms)={}, bodyLength={} AppendMessageResult={}", elapsedTimeInLock, msg.getBody().length, result);
        }

        if (null != unlockMappedFile && this.defaultMessageStore.getMessageStoreConfig().isWarmMapedFileEnable()) {
    
    
            this.defaultMessageStore.unlockMappedFile(unlockMappedFile);
        }

        PutMessageResult putMessageResult = new PutMessageResult(PutMessageStatus.PUT_OK, result);

        // Statistics
        storeStatsService.getSinglePutMessageTopicTimesTotal(msg.getTopic()).incrementAndGet();
        storeStatsService.getSinglePutMessageTopicSizeTotal(topic).addAndGet(result.getWroteBytes());

        handleDiskFlush(result, putMessageResult, msg);
        handleHA(result, putMessageResult, msg);

        return putMessageResult;
    }
    public AppendMessageResult appendMessagesInner(final MessageExt messageExt, final AppendMessageCallback cb) {
    
    
        assert messageExt != null;
        assert cb != null;

        //获取当前写指针
        int currentPos = this.wrotePosition.get();

        //检查当前写指针是不是小于文件最大容量大小
        if (currentPos < this.fileSize) {
    
    
            //通过slice方法创建一个与原ByteBuffer共享的内存区
            ByteBuffer byteBuffer = writeBuffer != null ? writeBuffer.slice() : this.mappedByteBuffer.slice();
            byteBuffer.position(currentPos);//设置position为当前指针
            AppendMessageResult result;
            if (messageExt instanceof MessageExtBrokerInner) {
    
    
                //把消息内容存储到ByteBuffer
                result = cb.doAppend(this.getFileFromOffset(), byteBuffer, this.fileSize - currentPos, (MessageExtBrokerInner) messageExt);
            } else if (messageExt instanceof MessageExtBatch) {
    
    
                result = cb.doAppend(this.getFileFromOffset(), byteBuffer, this.fileSize - currentPos, (MessageExtBatch) messageExt);
            } else {
    
    
                return new AppendMessageResult(AppendMessageStatus.UNKNOWN_ERROR);
            }
            this.wrotePosition.addAndGet(result.getWroteBytes());
            this.storeTimestamp = result.getStoreTimestamp();
            return result;
        }
        log.error("MappedFile.appendMessage return null, wrotePosition: {} fileSize: {}", currentPos, this.fileSize);
        return new AppendMessageResult(AppendMessageStatus.UNKNOWN_ERROR);
    }

猜你喜欢

转载自blog.csdn.net/weixin_43073775/article/details/109705410