Java Code Examples for org.apache.rocketmq.common.sysflag.MessageSysFlag#getTransactionValue()

The following examples show how to use org.apache.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 rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public void doDispatch(DispatchRequest req) {
    final int tranType = MessageSysFlag.getTransactionValue(req.getSysFlag());
    switch (tranType) {
        case MessageSysFlag.TRANSACTION_NOT_TYPE:
        case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            DefaultMessageStore.this.putMessagePositionInfo(req.getTopic(), req.getQueueId(), req.getCommitLogOffset(), req.getMsgSize(),
                    req.getTagsCode(), req.getStoreTimestamp(), req.getConsumeQueueOffset());
            break;
        case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
            break;
    }
    this.handingTransactionState(req);
    if (DefaultMessageStore.this.getMessageStoreConfig().isMessageIndexEnable()) {
        DefaultMessageStore.this.indexService.buildIndex(req);
    }
}
 
Example 2
Source File: DefaultMessageStore.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * 执行调度请求
 * 1. 非事务消息 或 事务提交消息 建立 消息位置信息 到 ConsumeQueue
 * 2. 建立 索引信息 到 IndexFile
 *
 * @param req 调度请求
 */
public void doDispatch(DispatchRequest req) {
    // 非事务消息 或 事务提交消息 建立 消息位置信息 到 ConsumeQueue
    final int tranType = MessageSysFlag.getTransactionValue(req.getSysFlag());
    switch (tranType) {
        case MessageSysFlag.TRANSACTION_NOT_TYPE: // 非事务消息
        case MessageSysFlag.TRANSACTION_COMMIT_TYPE: // 事务消息COMMIT
            DefaultMessageStore.this.putMessagePositionInfo(req.getTopic(), req.getQueueId(), req.getCommitLogOffset(), req.getMsgSize(),
                req.getTagsCode(), req.getStoreTimestamp(), req.getConsumeQueueOffset());
            break;
        case MessageSysFlag.TRANSACTION_PREPARED_TYPE: // 事务消息PREPARED
        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE: // 事务消息ROLLBACK
            break;
    }
    // 建立 索引信息 到 IndexFile
    if (DefaultMessageStore.this.getMessageStoreConfig().isMessageIndexEnable()) {
        DefaultMessageStore.this.indexService.buildIndex(req);
    }
}
 
Example 3
Source File: DefaultMessageStore.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public void dispatch(DispatchRequest request) {
    final int tranType = MessageSysFlag.getTransactionValue(request.getSysFlag());
    switch (tranType) {
        case MessageSysFlag.TRANSACTION_NOT_TYPE:
        case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            DefaultMessageStore.this.putMessagePositionInfo(request);
            break;
        case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
            break;
    }
}
 
Example 4
Source File: DefaultMessageStore.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
/**
 * 记录事务状态
 *
 * @param req
 */
private void handingTransactionState(DispatchRequest req) {
    final int tranType = MessageSysFlag.getTransactionValue(req.getSysFlag());
    boolean persistInDB = DefaultMessageStore.this.getMessageStoreConfig().isTransactionLogInDB();
    if (req.getProducerGroup() != null) {
        switch (tranType) {
            case MessageSysFlag.TRANSACTION_NOT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
                getTransactionStateService().appendPreparedTransaction(req);
                break;
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                getTransactionStateService().updateTransactionState(req);
                break;
        }
    }
    if (!persistInDB) {
        switch (tranType) {
            case MessageSysFlag.TRANSACTION_NOT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
                DefaultMessageStore.this.getTransactionStateService().getTranRedoLog().putMessagePositionInfoWrapper(
                        req.getCommitLogOffset(), req.getMsgSize(), TransactionStateFileService.PREPARED_MESSAGE_TAGS_CODE,
                        req.getStoreTimestamp(), 0L);
                break;
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                DefaultMessageStore.this.getTransactionStateService().getTranRedoLog().putMessagePositionInfoWrapper(
                        req.getCommitLogOffset(), req.getMsgSize(), req.getPreparedTransactionOffset(),
                        req.getStoreTimestamp(), 0L);
                break;
        }
    }
}
 
Example 5
Source File: DefaultMessageStore.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public void dispatch(DispatchRequest request) {
    final int tranType = MessageSysFlag.getTransactionValue(request.getSysFlag());
    switch (tranType) {
        // 1、分发消息位置信息到ConsumeQueue
        case MessageSysFlag.TRANSACTION_NOT_TYPE:
        case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            DefaultMessageStore.this.putMessagePositionInfo(request);  // 将请求发到具体的Consume Queue
            break;
        case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
            break;
    }
}
 
Example 6
Source File: DefaultMessageStore.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public void dispatch(DispatchRequest request) {
    final int tranType = MessageSysFlag.getTransactionValue(request.getSysFlag());
    switch (tranType) {
        case MessageSysFlag.TRANSACTION_NOT_TYPE:
        case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            DefaultMessageStore.this.putMessagePositionInfo(request);
            break;
        case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
            break;
    }
}
 
Example 7
Source File: DefaultMessageStore.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Override
public void dispatch(DispatchRequest request) {
    final int tranType = MessageSysFlag.getTransactionValue(request.getSysFlag());
    switch (tranType) {
        case MessageSysFlag.TRANSACTION_NOT_TYPE:
        case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
            DefaultMessageStore.this.putMessagePositionInfo(request);
            break;
        case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
            break;
        default:
            break;
    }
}
 
Example 8
Source File: DefaultMessageStore.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Override
        public void dispatch(DispatchRequest request) {
//            获取事务类型=》
            final int tranType = MessageSysFlag.getTransactionValue(request.getSysFlag());
            switch (tranType) {
                case MessageSysFlag.TRANSACTION_NOT_TYPE:
                case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
//                    构建存放消息位置信息=》
                    DefaultMessageStore.this.putMessagePositionInfo(request);
                    break;
                case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
                case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                    break;
            }
        }
 
Example 9
Source File: IndexService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
/**
 * 只有在消息中指定了keys或者uniqueKey,才会构建索引
 * {@linkplain MessageSysFlag#TRANSACTION_ROLLBACK_TYPE}类型消息不会构建索引
 *
 * @param req
 */
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.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            // 事务类型为ROLLBACK_TYPE的消息不会进IndexFile
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                return;
        }
        //若消息体中指定了uniqueKey属性(Producer创建时Client都会生成)
        if (req.getUniqKey() != null) {
            // key = topic#uniqueKey
            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) {   //若消息中未指定keys及uniqueKey,则不会构建索引
            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));  //key = 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 10
Source File: CommitLog.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
public PutMessageResult putMessages(final MessageExtBatch messageExtBatch) {
    messageExtBatch.setStoreTimestamp(System.currentTimeMillis());
    AppendMessageResult result;

    StoreStatsService storeStatsService = this.defaultMessageStore.getStoreStatsService();

    final int tranType = MessageSysFlag.getTransactionValue(messageExtBatch.getSysFlag());

    if (tranType != MessageSysFlag.TRANSACTION_NOT_TYPE) {
        return new PutMessageResult(PutMessageStatus.MESSAGE_ILLEGAL, null);
    }
    if (messageExtBatch.getDelayTimeLevel() > 0) {
        return new PutMessageResult(PutMessageStatus.MESSAGE_ILLEGAL, null);
    }

    long eclipseTimeInLock = 0;
    MappedFile unlockMappedFile = null;
    MappedFile mappedFile = this.mappedFileQueue.getLastMappedFile();

    //fine-grained lock instead of the coarse-grained
    MessageExtBatchEncoder batchEncoder = batchEncoderThreadLocal.get();

    messageExtBatch.setEncodedBuff(batchEncoder.encode(messageExtBatch));

    putMessageLock.lock();
    try {
        long beginLockTimestamp = this.defaultMessageStore.getSystemClock().now();
        this.beginTimeInLock = beginLockTimestamp;

        // Here settings are stored timestamp, in order to ensure an orderly
        // global
        messageExtBatch.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: {} clientAddr: {}", messageExtBatch.getTopic(), messageExtBatch.getBornHostString());
            beginTimeInLock = 0;
            return new PutMessageResult(PutMessageStatus.CREATE_MAPEDFILE_FAILED, null);
        }

        result = mappedFile.appendMessages(messageExtBatch, 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: {} clientAddr: {}", messageExtBatch.getTopic(), messageExtBatch.getBornHostString());
                    beginTimeInLock = 0;
                    return new PutMessageResult(PutMessageStatus.CREATE_MAPEDFILE_FAILED, result);
                }
                result = mappedFile.appendMessages(messageExtBatch, 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);
        }

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

    if (eclipseTimeInLock > 500) {
        log.warn("[NOTIFYME]putMessages in lock cost time(ms)={}, bodyLength={} AppendMessageResult={}", eclipseTimeInLock, messageExtBatch.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(messageExtBatch.getTopic()).addAndGet(result.getMsgNum());
    storeStatsService.getSinglePutMessageTopicSizeTotal(messageExtBatch.getTopic()).addAndGet(result.getWroteBytes());

    handleDiskFlush(result, putMessageResult, messageExtBatch);

    handleHA(result, putMessageResult, messageExtBatch);

    return putMessageResult;
}
 
Example 11
Source File: IndexService.java    From DDMQ 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();
        Map<String, String> properties = msg.getPropertiesMap();
        if (msg.getCommitLogOffset() < endPhyOffset) {
            return;
        }

        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
        switch (tranType) {
            case MessageSysFlag.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                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 {} key {}", req.getCommitLogOffset(), key);
                        return;
                    }
                }
            }
        }

        if (properties != null & properties.containsKey(MessageConst.PROPERTY_USER_TRACE_ID)) {
            String traceId = properties.get(MessageConst.PROPERTY_USER_TRACE_ID);
            if (traceId != null) {
                indexFile = putKey(indexFile, msg, buildKey(topic, traceId));
                if (indexFile == null) {
                    log.error("putKey error commitlog {} traceId {}", req.getCommitLogOffset(), traceId);
                    return;
                }
            }
        }
    } else {
        log.error("build index error, stop building index");
    }
}
 
Example 12
Source File: IndexService.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * 构造索引。当commitlog添加成功后构造索引
 * 构造keys的每一个key的索引文件和uniquekeys的文件
 * @param req req
 */
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();

        //已经提交过偏移量了,直接return
        if (msg.getCommitLogOffset() < endPhyOffset) {
            return;
        }

        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
        switch (tranType) {
            case MessageSysFlag.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                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;
            }
        }

        //遍历keys循环添加索引文件
        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 13
Source File: IndexService.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public void buildIndex(DispatchRequest req) {
    IndexFile indexFile = retryGetAndCreateIndexFile();//如果indexFile有就获取没有就创建
    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.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                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 14
Source File: CommitLog.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public PutMessageResult putMessages(final MessageExtBatch messageExtBatch) {
    messageExtBatch.setStoreTimestamp(System.currentTimeMillis());
    AppendMessageResult result;

    StoreStatsService storeStatsService = this.defaultMessageStore.getStoreStatsService();

    final int tranType = MessageSysFlag.getTransactionValue(messageExtBatch.getSysFlag());

    if (tranType != MessageSysFlag.TRANSACTION_NOT_TYPE) {
        return new PutMessageResult(PutMessageStatus.MESSAGE_ILLEGAL, null);
    }
    if (messageExtBatch.getDelayTimeLevel() > 0) {
        return new PutMessageResult(PutMessageStatus.MESSAGE_ILLEGAL, null);
    }

    long eclipseTimeInLock = 0;
    MappedFile unlockMappedFile = null;
    MappedFile mappedFile = this.mappedFileQueue.getLastMappedFile();

    //fine-grained lock instead of the coarse-grained
    MessageExtBatchEncoder batchEncoder = batchEncoderThreadLocal.get();

    messageExtBatch.setEncodedBuff(batchEncoder.encode(messageExtBatch));

    putMessageLock.lock();
    try {
        long beginLockTimestamp = this.defaultMessageStore.getSystemClock().now();
        this.beginTimeInLock = beginLockTimestamp;

        // Here settings are stored timestamp, in order to ensure an orderly
        // global
        messageExtBatch.setStoreTimestamp(beginLockTimestamp);

        if (null == mappedFile || mappedFile.isFull()) {
            mappedFile = this.mappedFileQueue.getLastMappedFile(0); // Mark: NewFile may be cause noise
        }
        if (null == mappedFile) {
            log.error("Create maped file1 error, topic: {} clientAddr: {}", messageExtBatch.getTopic(), messageExtBatch.getBornHostString());
            beginTimeInLock = 0;
            return new PutMessageResult(PutMessageStatus.CREATE_MAPEDFILE_FAILED, null);
        }

        result = mappedFile.appendMessages(messageExtBatch, 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 maped file2 error, topic: {} clientAddr: {}", messageExtBatch.getTopic(), messageExtBatch.getBornHostString());
                    beginTimeInLock = 0;
                    return new PutMessageResult(PutMessageStatus.CREATE_MAPEDFILE_FAILED, result);
                }
                result = mappedFile.appendMessages(messageExtBatch, 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);
        }

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


    if (eclipseTimeInLock > 500) {
        log.warn("[NOTIFYME]putMessages in lock cost time(ms)={}, bodyLength={} AppendMessageResult={}", eclipseTimeInLock, messageExtBatch.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(messageExtBatch.getTopic()).addAndGet(result.getMsgNum());
    storeStatsService.getSinglePutMessageTopicSizeTotal(messageExtBatch.getTopic()).addAndGet(result.getWroteBytes());


    handleDiskFlush(result, putMessageResult, messageExtBatch);

    handleHA(result, putMessageResult, messageExtBatch);

    return putMessageResult;
}
 
Example 15
Source File: TransactionStateFileService.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
/**
 * 更新事务状态
 *
 * @return 是否成功
 */
@Override
public boolean updateTransactionState(DispatchRequest request) {
    final long tsOffset = request.getTranStateTableOffset();
    final long clOffset = request.getPreparedTransactionOffset();
    final int groupHashCode = request.getProducerGroup().hashCode();
    final int state = MessageSysFlag.getTransactionValue(request.getSysFlag());
    SelectMappedBufferResult selectMapedBufferResult = this.findTransactionBuffer(tsOffset);
    if (selectMapedBufferResult != null) {
        try {
            final long clOffset_read = selectMapedBufferResult.getByteBuffer().getLong();
            final int size_read = selectMapedBufferResult.getByteBuffer().getInt();
            final int timestamp_read = selectMapedBufferResult.getByteBuffer().getInt();
            final int groupHashCode_read = selectMapedBufferResult.getByteBuffer().getInt();
            final int state_read = selectMapedBufferResult.getByteBuffer().getInt();

            // 校验数据正确性
            if (clOffset != clOffset_read) {
                log.error("updateTransactionState error clOffset: {} clOffset_read: {}", clOffset,
                        clOffset_read);
                return false;
            }

            // 校验数据正确性
            if (groupHashCode != groupHashCode_read) {
                log.error("updateTransactionState error groupHashCode: {} groupHashCode_read: {}",
                        groupHashCode, groupHashCode_read);
                return false;
            }

            // 判断是否已经更新过
            if (MessageSysFlag.TRANSACTION_PREPARED_TYPE != state_read) {
                log.warn("updateTransactionState error, the transaction is updated before.");
                return true;
            }

            // 更新事务状态
            selectMapedBufferResult.getByteBuffer().putInt(TS_STATE_POS, state);
        } catch (Exception e) {
            log.error("updateTransactionState exception", e);
        } finally {
            selectMapedBufferResult.release();
        }
    }

    return false;
}
 
Example 16
Source File: TransactionRecordFlush2DBService.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
private void doFlushDB(boolean shutdown) {
    DispatchRequestCollections requests = dispatchRequestBufferQueue.poll();
    if (requests == null) {
        return;
    }
    if (!shutdown) {
        putEmptyRequestList();
    }
    boolean addSuccess = false, removeSuccess = false;
    LinkedHashMap<Long, TransactionRecord> prepareTrs = null;
    LinkedHashMap<Long, Void> confirmTrs = null;
    while (true) {
        if (requests.latch.get() != requests.requestlist.size() && requests.latch.get() > 0) {
            continue;
        }
        requests.latch.set(Integer.MIN_VALUE);

        if (requests.requestlist.size() == 0) {
            break;
        }
        try {
            long transactionOffset = -1L;
            //数据处理
            if (prepareTrs == null && confirmTrs == null) {
                prepareTrs = new LinkedHashMap<Long, TransactionRecord>();
                confirmTrs = new LinkedHashMap<Long, Void>();
                for (DispatchRequest request : requests.requestlist) {
                    final int tranType = MessageSysFlag.getTransactionValue(request.getSysFlag());
                    switch (tranType) {
                        case MessageSysFlag.TRANSACTION_NOT_TYPE:
                            break;
                        case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
                            if (this.maxTransOffset.get() < request.getCommitLogOffset()) {
                                prepareTrs.put(request.getCommitLogOffset(), new TransactionRecord(request.getCommitLogOffset(),
                                        request.getCheckImmunityTimeOutTimestamp(), request.getMsgSize(), request.getProducerGroup()));
                                this.maxTransOffset.set(request.getCommitLogOffset());
                            } else {
                                log.info("[PREPARED] request ignore offset =" + request.getCommitLogOffset());
                            }
                            if (request.getPreparedTransactionOffset() == 0L) {
                                break;
                            }
                        case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                        case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                            if (this.maxTransOffset.get() < request.getCommitLogOffset()) {
                                if (prepareTrs.containsKey(request.getPreparedTransactionOffset())) {
                                    prepareTrs.remove(request.getPreparedTransactionOffset());
                                } else {
                                    confirmTrs.put(request.getPreparedTransactionOffset(), null);
                                }
                            } else {
                                log.info("[COMMIT] request ignore offset =" + request.getCommitLogOffset()
                                        + ",isCommitMessge=" + (tranType == MessageSysFlag.TRANSACTION_COMMIT_TYPE));
                            }
                            break;
                    }
                }
                transactionOffset = requests.requestlist.get(requests.requestlist.size() - 1).getCommitLogOffset();
            }

            long startTime = System.currentTimeMillis();
            addSuccess = addSuccess || transactionStore.parpare(new ArrayList<>(prepareTrs.values()));
            if (addSuccess && (removeSuccess = transactionStore.confirm(new ArrayList<>(confirmTrs.keySet())))) {
                log.info("pull TransactionRecord consume {}ms ,size={},realParpareSize={},realConfirmSize:{}",
                        (System.currentTimeMillis() - startTime), requests.requestlist.size(), prepareTrs.size(), confirmTrs.size());
                //更新最新的offset
                if (transactionOffset > 0) {
                    transactionOffsetConifgService.putOffset(transactionOffset);
                }
                break;
            }
        } catch (Throwable e) {
            log.error("transactionStore error:", e);
            ThreadUtils.sleep(2000);
        } finally {
            if (addSuccess && removeSuccess) {
                flowController.release(requests.requestlist.size());
            }
        }
    }
}
 
Example 17
Source File: IndexService.java    From rocketmq_trans_message 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.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                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 18
Source File: IndexService.java    From rocketmq-4.3.0 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.TRANSACTION_NOT_TYPE:
                case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
                case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                    break;
                case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                    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 19
Source File: IndexService.java    From DDMQ 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();
        Map<String, String> properties = msg.getPropertiesMap();
        if (msg.getCommitLogOffset() < endPhyOffset) {
            return;
        }

        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
        switch (tranType) {
            case MessageSysFlag.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                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 {} key {}", req.getCommitLogOffset(), key);
                        return;
                    }
                }
            }
        }

        if (properties != null & properties.containsKey(MessageConst.PROPERTY_USER_TRACE_ID)) {
            String traceId = properties.get(MessageConst.PROPERTY_USER_TRACE_ID);
            if (traceId != null) {
                indexFile = putKey(indexFile, msg, buildKey(topic, traceId));
                if (indexFile == null) {
                    log.error("putKey error commitlog {} traceId {}", req.getCommitLogOffset(), traceId);
                    return;
                }
            }
        }
    } else {
        log.error("build index error, stop building index");
    }
}
 
Example 20
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.TRANSACTION_NOT_TYPE:
            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                break;
            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                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");
    }
}