com.alibaba.rocketmq.store.MapedFile Java Examples

The following examples show how to use com.alibaba.rocketmq.store.MapedFile. 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: IndexFile.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public IndexFile(final String fileName, final int hashSlotNum, final int indexNum,
        final long endPhyOffset, final long endTimestamp) throws IOException {
    int fileTotalSize =
            IndexHeader.INDEX_HEADER_SIZE + (hashSlotNum * HASH_SLOT_SIZE) + (indexNum * INDEX_SIZE);
    this.mapedFile = new MapedFile(fileName, fileTotalSize);
    this.fileChannel = this.mapedFile.getFileChannel();
    this.mappedByteBuffer = this.mapedFile.getMappedByteBuffer();
    this.hashSlotNum = hashSlotNum;
    this.indexNum = indexNum;

    ByteBuffer byteBuffer = this.mappedByteBuffer.slice();
    this.indexHeader = new IndexHeader(byteBuffer);

    if (endPhyOffset > 0) {
        this.indexHeader.setBeginPhyOffset(endPhyOffset);
        this.indexHeader.setEndPhyOffset(endPhyOffset);
    }

    if (endTimestamp > 0) {
        this.indexHeader.setBeginTimestamp(endTimestamp);
        this.indexHeader.setEndTimestamp(endTimestamp);
    }
}
 
Example #2
Source File: IndexFile.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public IndexFile(final String fileName, final int hashSlotNum, final int indexNum,
                 final long endPhyOffset, final long endTimestamp) throws IOException {
    int fileTotalSize =
            IndexHeader.INDEX_HEADER_SIZE + (hashSlotNum * HASH_SLOT_SIZE) + (indexNum * INDEX_SIZE);
    this.mapedFile = new MapedFile(fileName, fileTotalSize);
    this.fileChannel = this.mapedFile.getFileChannel();
    this.mappedByteBuffer = this.mapedFile.getMappedByteBuffer();
    this.hashSlotNum = hashSlotNum;
    this.indexNum = indexNum;

    ByteBuffer byteBuffer = this.mappedByteBuffer.slice();
    this.indexHeader = new IndexHeader(byteBuffer);

    if (endPhyOffset > 0) {
        this.indexHeader.setBeginPhyOffset(endPhyOffset);
        this.indexHeader.setEndPhyOffset(endPhyOffset);
    }

    if (endTimestamp > 0) {
        this.indexHeader.setBeginTimestamp(endTimestamp);
        this.indexHeader.setEndTimestamp(endTimestamp);
    }
}
 
Example #3
Source File: IndexFile.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public IndexFile(final String fileName, final int hashSlotNum, final int indexNum,
        final long endPhyOffset, final long endTimestamp) throws IOException {
    int fileTotalSize =
            IndexHeader.INDEX_HEADER_SIZE + (hashSlotNum * HASH_SLOT_SIZE) + (indexNum * INDEX_SIZE);
    this.mapedFile = new MapedFile(fileName, fileTotalSize);
    this.fileChannel = this.mapedFile.getFileChannel();
    this.mappedByteBuffer = this.mapedFile.getMappedByteBuffer();
    this.hashSlotNum = hashSlotNum;
    this.indexNum = indexNum;

    ByteBuffer byteBuffer = this.mappedByteBuffer.slice();
    this.indexHeader = new IndexHeader(byteBuffer);

    if (endPhyOffset > 0) {
        this.indexHeader.setBeginPhyOffset(endPhyOffset);
        this.indexHeader.setEndPhyOffset(endPhyOffset);
    }

    if (endTimestamp > 0) {
        this.indexHeader.setBeginTimestamp(endTimestamp);
        this.indexHeader.setEndTimestamp(endTimestamp);
    }
}
 
Example #4
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public boolean appendPreparedTransaction(//
        final long clOffset,//
        final int size,//
        final int timestamp,//
        final int groupHashCode//
) {
    MapedFile mapedFile = this.tranStateTable.getLastMapedFile();
    if (null == mapedFile) {
        log.error("appendPreparedTransaction: create mapedfile error.");
        return false;
    }

    if (0 == mapedFile.getWrotePostion()) {
        this.addTimerTask(mapedFile);
    }

    this.byteBufferAppend.position(0);
    this.byteBufferAppend.limit(TSStoreUnitSize);

    // Commit Log Offset
    this.byteBufferAppend.putLong(clOffset);
    // Message Size
    this.byteBufferAppend.putInt(size);
    // Timestamp
    this.byteBufferAppend.putInt(timestamp);
    // Producer Group Hashcode
    this.byteBufferAppend.putInt(groupHashCode);
    // Transaction State
    this.byteBufferAppend.putInt(MessageSysFlag.TransactionPreparedType);

    return mapedFile.appendMessage(this.byteBufferAppend.array());
}
 
Example #5
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private SelectMapedBufferResult findTransactionBuffer(final long tsOffset) {
    final int mapedFileSize =
            this.defaultMessageStore.getMessageStoreConfig().getTranStateTableMapedFileSize();
    final long offset = tsOffset * TSStoreUnitSize;
    MapedFile mapedFile = this.tranStateTable.findMapedFileByOffset(offset);
    if (mapedFile != null) {
        SelectMapedBufferResult result = mapedFile.selectMapedBuffer((int) (offset % mapedFileSize));
        return result;
    }

    return null;
}
 
Example #6
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private void initTimerTask() {
    final List<MapedFile> mapedFiles = this.tranStateTable.getMapedFiles();
    for (MapedFile mf : mapedFiles) {
        this.addTimerTask(mf);
    }
}
 
Example #7
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private void recoverStateTableNormal() {

        final List<MapedFile> mapedFiles = this.tranStateTable.getMapedFiles();
        if (!mapedFiles.isEmpty()) {
            int index = mapedFiles.size() - 3;
            if (index < 0)
                index = 0;

            int mapedFileSizeLogics = this.tranStateTable.getMapedFileSize();
            MapedFile mapedFile = mapedFiles.get(index);
            ByteBuffer byteBuffer = mapedFile.sliceByteBuffer();
            /*
             * queue offset(global offset)
             */
            long processOffset = mapedFile.getFileFromOffset();
            /*
             * file offset(local offset)
             */
            long mapedFileOffset = 0;
            while (true) {
                for (int i = 0; i < mapedFileSizeLogics; i += TSStoreUnitSize) {

                    final long clOffset_read = byteBuffer.getLong();
                    final int size_read = byteBuffer.getInt();
                    final int timestamp_read = byteBuffer.getInt();
                    final int groupHashCode_read = byteBuffer.getInt();
                    /**
                     * prepared/commit/rollback
                     */
                    final int state_read = byteBuffer.getInt();

                    boolean stateOK = false;
                    switch (state_read) {
                    case MessageSysFlag.TransactionPreparedType:
                    case MessageSysFlag.TransactionCommitType:
                    case MessageSysFlag.TransactionRollbackType:
                        stateOK = true;
                        break;
                    default:
                        break;
                    }

                    if (clOffset_read >= 0 && size_read > 0 && stateOK) {
                        mapedFileOffset = i + TSStoreUnitSize;
                    }
                    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()) {
                        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;
                }
            }

            processOffset += mapedFileOffset;

            this.tranStateTable.truncateDirtyFiles(processOffset);

            this.tranStateTableOffset.set(this.tranStateTable.getMaxOffset() / TSStoreUnitSize);
            log.info("recover normal over, transaction state table max offset: {}",
                this.tranStateTableOffset.get());
        }
    }