Java Code Examples for com.alibaba.rocketmq.common.sysflag.MessageSysFlag#getTransactionValue()

The following examples show how to use com.alibaba.rocketmq.common.sysflag.MessageSysFlag#getTransactionValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultMessageStore.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 构建消费队列项和msgkey的索引项。
 * @param req
 */
public void doDispatch(DispatchRequest req) {
    final int tranType = MessageSysFlag.getTransactionValue(req.getSysFlag());
    switch (tranType) {
    case MessageSysFlag.TransactionNotType:
    case MessageSysFlag.TransactionCommitType:
        //把从commitlog中读取到的消息与consumeQueue关联起来,把该条消息的offset len taghash写入到 consumeQueue
        DefaultMessageStore.this.putMessagePostionInfo(req.getTopic(), req.getQueueId(), req.getCommitLogOffset(), req.getMsgSize(),
            req.getTagsCode(), req.getStoreTimestamp(), req.getConsumeQueueOffset());
        break;
    case MessageSysFlag.TransactionPreparedType: //如果是事务prepared或者rollback类型的消息,则不用把commitlog消息对应的索引dispatch到consumeQueue
    case MessageSysFlag.TransactionRollbackType:
        break;
    }

    if (DefaultMessageStore.this.getMessageStoreConfig().isMessageIndexEnable()) {
        DefaultMessageStore.this.indexService.buildIndex(req);
    }
}
 
Example 2
Source File: DefaultMessageStore.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
private void doDispatch() {
    if (!this.requestsRead.isEmpty()) {
        for (DispatchRequest req : this.requestsRead) {

            final int tranType = MessageSysFlag.getTransactionValue(req.getSysFlag());
            // 1、分发消息位置信息到ConsumeQueue
            switch (tranType) {
            case MessageSysFlag.TransactionNotType:
            case MessageSysFlag.TransactionCommitType:
                // 将请求发到具体的Consume Queue
                DefaultMessageStore.this.putMessagePostionInfo(req.getTopic(), req.getQueueId(),
                    req.getCommitLogOffset(), req.getMsgSize(), req.getTagsCode(),
                    req.getStoreTimestamp(), req.getConsumeQueueOffset());
                break;
            case MessageSysFlag.TransactionPreparedType:
            case MessageSysFlag.TransactionRollbackType:
                break;
            }
        }

        if (DefaultMessageStore.this.getMessageStoreConfig().isMessageIndexEnable()) {
            DefaultMessageStore.this.indexService.putRequest(this.requestsRead.toArray());
        }

        this.requestsRead.clear();
    }
}
 
Example 3
Source File: IndexService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public void buildIndex(DispatchRequest req) {
    boolean breakdown = false;
    IndexFile indexFile = retryGetAndCreateIndexFile();
    if (indexFile != null) {
        long endPhyOffset = indexFile.getEndPhyOffset();
        DispatchRequest msg = req;
        String topic = msg.getTopic();
        String keys = msg.getKeys();
        if (msg.getCommitLogOffset() < endPhyOffset) {
            return;
        }

        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
        switch (tranType) {
        case MessageSysFlag.TransactionNotType:
        case MessageSysFlag.TransactionPreparedType:
            break;
        case MessageSysFlag.TransactionCommitType:
        case MessageSysFlag.TransactionRollbackType:
            return;
        }

        if (keys != null && keys.length() > 0) {
            String[] keyset = keys.split(MessageConst.KEY_SEPARATOR);
            for (String key : keyset) {
                if (key.length() > 0) {
                    for (boolean ok =
                            indexFile.putKey(buildKey(topic, key), msg.getCommitLogOffset(),
                                msg.getStoreTimestamp()); !ok;) {
                        log.warn("index file full, so create another one, " + indexFile.getFileName());
                        indexFile = retryGetAndCreateIndexFile();
                        if (null == indexFile) {
                            breakdown = true;
                            return;
                        }

                        ok =
                                indexFile.putKey(buildKey(topic, key), msg.getCommitLogOffset(),
                                    msg.getStoreTimestamp());
                    }
                }
            }
        }
    }
    else {
        log.error("build index error, stop building index");
    }
}
 
Example 4
Source File: IndexService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public void buildIndex(DispatchRequest req) {
    IndexFile indexFile = retryGetAndCreateIndexFile();
    if (indexFile != null) {
        long endPhyOffset = indexFile.getEndPhyOffset();
        DispatchRequest msg = req;
        String topic = msg.getTopic();
        String keys = msg.getKeys();
        if (msg.getCommitLogOffset() < endPhyOffset) {
            return;
        }

        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
        switch (tranType) {
        case MessageSysFlag.TransactionNotType:
        case MessageSysFlag.TransactionPreparedType:
        case MessageSysFlag.TransactionCommitType:
                break;
        case MessageSysFlag.TransactionRollbackType:
            return;
        }

        if (req.getUniqKey() != null) {
            indexFile = putKey(indexFile, msg, buildKey(topic, req.getUniqKey()));
            if (indexFile == null) {
                log.error("putKey error commitlog {} uniqkey {}", req.getCommitLogOffset(), req.getUniqKey());
                return;
            }
        }

        if ((keys != null && keys.length() > 0)) {
            String[] keyset = keys.split(MessageConst.KEY_SEPARATOR);
            for (int i = 0; i <  keyset.length; i++) {
                String key = keyset[i];
                if (key.length() > 0) {
                        indexFile = putKey(indexFile, msg, buildKey(topic, key));
                        if (indexFile == null) {
                            log.error("putKey error commitlog {} uniqkey {}", req.getCommitLogOffset(), req.getUniqKey());
                            return;
                        }
                    }
             }
        }
    }

    else {
        log.error("build index error, stop building index");
    }
}
 
Example 5
Source File: DefaultMessageStore.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public void doDispatch(DispatchRequest req) {
    final int tranType = MessageSysFlag.getTransactionValue(req.getSysFlag());
    switch (tranType) {
        case MessageSysFlag.TransactionNotType:
        case MessageSysFlag.TransactionCommitType:
            DefaultMessageStore.this.putMessagePostionInfo(req.getTopic(), req.getQueueId(), req.getCommitLogOffset(), req.getMsgSize(),
                    req.getTagsCode(), req.getStoreTimestamp(), req.getConsumeQueueOffset());
            break;
        case MessageSysFlag.TransactionPreparedType:
        case MessageSysFlag.TransactionRollbackType:
            break;
    }

    if (req.getProducerGroup() != null) {
        switch (tranType) {
            case MessageSysFlag.TransactionNotType:
                break;
            case MessageSysFlag.TransactionPreparedType:
                 DefaultMessageStore.this.getTransactionStateService().appendPreparedTransaction(//
                        req.getCommitLogOffset(),//
                        req.getMsgSize(),//
                        (int) (req.getStoreTimestamp() / 1000),//
                        req.getProducerGroup().hashCode());
                break;
            case MessageSysFlag.TransactionCommitType:
            case MessageSysFlag.TransactionRollbackType:
                DefaultMessageStore.this.getTransactionStateService().updateTransactionState(//
                        req.getTranStateTableOffset(),//
                        req.getPreparedTransactionOffset(),//
                        req.getProducerGroup().hashCode(),//
                        tranType//
                );
                break;
        }
    }

    switch (tranType) {
        case MessageSysFlag.TransactionNotType:
            break;
        case MessageSysFlag.TransactionPreparedType:
             DefaultMessageStore.this.getTransactionStateService().getTranRedoLog()
                    .putMessagePostionInfoWrapper(//
                            req.getCommitLogOffset(),//
                            req.getMsgSize(),//
                            TransactionStateService.PreparedMessageTagsCode,//
                            req.getStoreTimestamp(),//
                            0L//
                    );
            break;
        case MessageSysFlag.TransactionCommitType:
        case MessageSysFlag.TransactionRollbackType:
             DefaultMessageStore.this.getTransactionStateService().getTranRedoLog()
                    .putMessagePostionInfoWrapper(//
                            req.getCommitLogOffset(),//
                            req.getMsgSize(),//
                            req.getPreparedTransactionOffset(),//
                            req.getStoreTimestamp(),//
                            0L//
                    );
            break;
    }

    if (DefaultMessageStore.this.getMessageStoreConfig().isMessageIndexEnable()) {
        DefaultMessageStore.this.indexService.buildIndex(req);
    }
}
 
Example 6
Source File: IndexService.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void buildIndex(Object[] req) {
    boolean breakdown = false;
    IndexFile indexFile = retryGetAndCreateIndexFile();
    if (indexFile != null) {
        long endPhyOffset = indexFile.getEndPhyOffset();
        MSG_WHILE: for (Object o : req) {
            DispatchRequest msg = (DispatchRequest) o;
            String topic = msg.getTopic();
            String keys = msg.getKeys();
            if (msg.getCommitLogOffset() < endPhyOffset) {
                continue;
            }

            final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
            switch (tranType) {
            case MessageSysFlag.TransactionNotType:
            case MessageSysFlag.TransactionPreparedType:
                break;
            case MessageSysFlag.TransactionCommitType:
            case MessageSysFlag.TransactionRollbackType:
                continue;
            }

            if (keys != null && keys.length() > 0) {
                String[] keyset = keys.split(MessageConst.KEY_SEPARATOR);
                for (String key : keyset) {
                    // TODO 是否需要TRIM
                    if (key.length() > 0) {
                        for (boolean ok = indexFile.putKey(buildKey(topic, key), msg.getCommitLogOffset(),
                            msg.getStoreTimestamp()); !ok;) {
                            log.warn(
                                "index file full, so create another one, " + indexFile.getFileName());
                            indexFile = retryGetAndCreateIndexFile();
                            if (null == indexFile) {
                                breakdown = true;
                                break MSG_WHILE;
                            }

                            ok = indexFile.putKey(buildKey(topic, key), msg.getCommitLogOffset(),
                                msg.getStoreTimestamp());
                        }
                    }
                }
            }
        }
    }
    // IO发生故障,build索引过程中断,需要人工参与处理
    else {
        breakdown = true;
    }

    if (breakdown) {
        log.error("build index error, stop building index");
    }
}