Java Code Examples for org.apache.rocketmq.common.sysflag.MessageSysFlag#TRANSACTION_PREPARED_TYPE

The following examples show how to use org.apache.rocketmq.common.sysflag.MessageSysFlag#TRANSACTION_PREPARED_TYPE . 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: SendMessageProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private RemotingCommand createSendTransactionMsgCommand(int requestCode) {
    SendMessageRequestHeader header = createSendMsgRequestHeader();
    int sysFlag = header.getSysFlag();
    Map<String, String> oriProps = MessageDecoder.string2messageProperties(header.getProperties());
    oriProps.put(MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    header.setProperties(MessageDecoder.messageProperties2String(oriProps));
    sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
    header.setSysFlag(sysFlag);
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, header);
    request.setBody(new byte[] {'a'});
    request.makeCustomHeaderToNet();
    return request;
}
 
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: SendMessageProcessorTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private RemotingCommand createSendTransactionMsgCommand(int requestCode) {
    SendMessageRequestHeader header = createSendMsgRequestHeader();
    int sysFlag = header.getSysFlag();
    Map<String, String> oriProps = MessageDecoder.string2messageProperties(header.getProperties());
    oriProps.put(MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    header.setProperties(MessageDecoder.messageProperties2String(oriProps));
    sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
    header.setSysFlag(sysFlag);
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, header);
    request.setBody(new byte[] {'a'});
    request.makeCustomHeaderToNet();
    return request;
}
 
Example 9
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 10
Source File: DefaultMessageStore.java    From rocketmq 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 11
Source File: SendMessageProcessorTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private RemotingCommand createSendTransactionMsgCommand(int requestCode) {
    SendMessageRequestHeader header = createSendMsgRequestHeader();
    int sysFlag = header.getSysFlag();
    Map<String, String> oriProps = MessageDecoder.string2messageProperties(header.getProperties());
    oriProps.put(MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    header.setProperties(MessageDecoder.messageProperties2String(oriProps));
    sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
    header.setSysFlag(sysFlag);
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, header);
    request.setBody(new byte[] {'a'});
    request.makeCustomHeaderToNet();
    return request;
}
 
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 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 14
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 15
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 16
Source File: TransactionStateFileService.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
/**
 * 加载(解析)TranStateTable 的 MappedFile
 * 1. 清理多余 MappedFile,设置最后一个 MappedFile的写入位置(position
 * 2. 设置 TanStateTable 最大物理位置(可写入位置)
 */
private void recoverStateTableNormal() {
    final List<MappedFile> mapedFiles = this.tranStateTable.getMappedFiles();
    if (!mapedFiles.isEmpty()) {
        // 从倒数第三个文件开始恢复
        int index = mapedFiles.size() - 3;
        if (index < 0) {
            index = 0;
        }

        int mapedFileSizeLogics = this.tranStateTable.getMappedFileSize();
        MappedFile mapedFile = mapedFiles.get(index);
        ByteBuffer byteBuffer = mapedFile.sliceByteBuffer();
        long processOffset = mapedFile.getFileFromOffset();
        long mapedFileOffset = 0;
        while (true) {
            for (int i = 0; i < mapedFileSizeLogics; i += TSS_STORE_UNIT_SIZE) {

                final long clOffset_read = byteBuffer.getLong();
                final int size_read = byteBuffer.getInt();
                final int timestamp_read = byteBuffer.getInt();
                final int groupHashCode_read = byteBuffer.getInt();
                final int state_read = byteBuffer.getInt();

                boolean stateOK = false;
                switch (state_read) {
                    case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
                    case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
                    case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
                        stateOK = true;
                        break;
                    default:
                        break;
                }

                // 说明当前存储单元有效
                if (clOffset_read >= 0 && size_read > 0 && stateOK) {
                    mapedFileOffset = i + TSS_STORE_UNIT_SIZE;
                } else {
                    log.info("recover current transaction state table file over,  " + mapedFile.getFileName() + " "
                            + clOffset_read + " " + size_read + " " + timestamp_read);
                    break;
                }
            }

            // 走到文件末尾,切换至下一个文件
            if (mapedFileOffset == mapedFileSizeLogics) {
                index++;
                if (index >= mapedFiles.size()) { // 循环while结束
                    log.info("recover last transaction state table file over, last maped file " + mapedFile.getFileName());
                    break;
                } else { // 切换下一个文件
                    mapedFile = mapedFiles.get(index);
                    byteBuffer = mapedFile.sliceByteBuffer();
                    processOffset = mapedFile.getFileFromOffset();
                    mapedFileOffset = 0;
                    log.info("recover next transaction state table file, " + mapedFile.getFileName());
                }
            } else {
                log.info("recover current transaction state table queue over " + mapedFile.getFileName() + " " + (processOffset + mapedFileOffset));
                break;
            }
        }

        // 清理多余 MappedFile,设置最后一个 MappedFile的写入位置(position
        processOffset += mapedFileOffset;
        this.tranStateTable.truncateDirtyFiles(processOffset);

        // 设置 TanStateTable 最大物理位置(可写入位置)
        this.tranStateTableOffset.set(this.tranStateTable.getMaxOffset() / TSS_STORE_UNIT_SIZE);
        log.info("recover normal over, transaction state table max offset: {}", this.tranStateTableOffset.get());
    }
}
 
Example 17
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 18
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 19
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 20
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");
    }
}