lizhimins commented on code in PR #937: URL: https://github.com/apache/rocketmq-externals/pull/937#discussion_r1872964950
########## rocketmq-tieredstore-s3/src/main/java/org/apache/rocketmq/tieredstore/s3/S3FileSegment.java: ########## @@ -0,0 +1,568 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.tieredstore.s3; + +import com.alibaba.fastjson.JSON; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.CompositeByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.util.ReferenceCountUtil; +import io.opentelemetry.api.common.Attributes; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.rocketmq.tieredstore.MessageStoreConfig; +import org.apache.rocketmq.tieredstore.common.FileSegmentType; +import org.apache.rocketmq.tieredstore.exception.TieredStoreErrorCode; +import org.apache.rocketmq.tieredstore.exception.TieredStoreException; +import org.apache.rocketmq.tieredstore.metrics.TieredStoreMetricsManager; +import org.apache.rocketmq.tieredstore.provider.FileSegment; +import org.apache.rocketmq.tieredstore.s3.metadata.S3ChunkMetadata; +import org.apache.rocketmq.tieredstore.s3.metadata.S3FileSegmentMetadata; +import org.apache.rocketmq.tieredstore.s3.object.AbstractS3Storage; +import org.apache.rocketmq.tieredstore.s3.object.S3Storage; +import org.apache.rocketmq.tieredstore.s3.object.bytebuf.ByteBufAlloc; +import org.apache.rocketmq.tieredstore.s3.util.S3PathUtils; +import org.apache.rocketmq.tieredstore.stream.FileSegmentInputStream; +import org.apache.rocketmq.tieredstore.util.MessageStoreUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.rocketmq.tieredstore.metrics.TieredStoreMetricsConstant.LABEL_FILE_TYPE; +import static org.apache.rocketmq.tieredstore.metrics.TieredStoreMetricsConstant.LABEL_PATH; +import static org.apache.rocketmq.tieredstore.s3.constants.S3Constants.MIN_PART_SIZE; +import static org.apache.rocketmq.tieredstore.util.MessageFormatUtil.CONSUME_QUEUE_UNIT_SIZE; + +public class S3FileSegment extends FileSegment { + + private static final Logger log = LoggerFactory.getLogger(MessageStoreUtil.TIERED_STORE_LOGGER_NAME); + + private final String basePath; + + /** + * The path of the chunk file in S3. Format: + * <pre> + * {@link #filePath}/chunk + * </pre> + */ + private final String chunkPath; + + /** + * The path of the segment file in S3. Format: + * <pre> + * {@link #filePath}/segment + * </pre> + */ + private final String segmentPath; + + private final TieredS3Storage s3Storage; + + private final S3FileSegmentMetadata metadata; + + private final boolean isObjectMergeEnable = false; + + private final AtomicBoolean compactStatus = new AtomicBoolean(false); + + private final Attributes attributes = TieredStoreMetricsManager.newAttributesBuilder().put(LABEL_PATH, filePath) + .put(LABEL_FILE_TYPE, this.fileType.name().toLowerCase()).build(); + + public S3FileSegment(MessageStoreConfig storeConfig, + FileSegmentType fileType, String filePath, long baseOffset) { + super(storeConfig, fileType, filePath, baseOffset); + + // fullPath: clusterBasePath/broker/topic/queueId/fileType/seg-${baseOffset} + String clusterName = storeConfig.getBrokerClusterName(); + String clusterBasePath = String.format("%s_%s", MessageStoreUtil.getHash(clusterName), clusterName); + this.basePath = clusterBasePath + S3PathUtils.FILE_SEPARATOR + filePath + S3PathUtils.FILE_SEPARATOR + + fileType.toString() + S3PathUtils.FILE_SEPARATOR + "seg-" + baseOffset; + this.chunkPath = S3PathUtils.getBaseChunkPath(basePath); + this.segmentPath = S3PathUtils.getBaseSegmentPath(basePath); + this.s3Storage = TieredS3Storage.getInstance(storeConfig); + this.metadata = new S3FileSegmentMetadata(); + this.initialize(); + } + + private void initialize() { + + S3Storage.ObjectInfo segmentObjectHeader = s3Storage.readHeader(S3PathUtils.getSegmentPath(segmentPath)).join(); + if (segmentObjectHeader != null) { + this.metadata.setSegment( + new S3ChunkMetadata(0, (int) segmentObjectHeader.size(), true)); + return; + } + + S3Storage.ObjectInfo chunkZeroObjectHeader = s3Storage.readHeader(S3PathUtils.getChunkPathByPosition(chunkPath, 0)).join(); + // if chunk start position equal 0, means new file + if (chunkZeroObjectHeader == null) { + log.info("create new S3FileSegment {}", basePath); + return; + } + + CompletableFuture<List<S3ChunkMetadata>> listChunks = this.s3Storage.listChunks(this.chunkPath); + List<S3ChunkMetadata> chunks = listChunks.join(); + // add all chunks into metadata + checkAndLoadChunks(chunks); + if (log.isDebugEnabled()) { + log.debug("init file segment metadata successfully. path: {} meta: {}", basePath, JSON.toJSONString(this.metadata)); + } + + log.info("init file segment metadata successfully. path: {}", basePath); + + } + + private void checkAndLoadChunks(List<S3ChunkMetadata> chunks) { + if (chunks.size() == 0) { + return; + } + for (S3ChunkMetadata chunk : chunks) { + S3ChunkMetadata newChunk = + new S3ChunkMetadata(chunk.getStartPosition(), + chunk.getChunkSize(), false); + if (!this.metadata.addChunk(newChunk)) { + // the chunk is not valid + log.error("Check and load chunks failed, the path {} the chunk: {} is not valid, now chunks last end position: {}, please check it.", + basePath, newChunk, + this.metadata.getEndPosition()); + throw new RuntimeException( + "The chunk: " + chunk + " is not valid, now chunks last end position: " + this.metadata.getEndPosition() + ", please check it."); + } + } + } + + @Override + public String getPath() { + return this.filePath; + } + + @Override + public long getSize() { + return this.metadata.getSize(); + } + + @Override + public boolean exists() { + return this.metadata.getSize() > 0; + } + + @Override + public void createFile() { Review Comment: 留这个函数当时是考虑到两阶段写的问题,会先写一个 length=0 的 meta,然后 append 成功更新这个 meta -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@rocketmq.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org