|
31 | 31 | import java.util.concurrent.TimeUnit;
|
32 | 32 | import javax.annotation.Nullable;
|
33 | 33 | import org.apache.commons.lang3.tuple.Pair;
|
| 34 | +import org.apache.rocketmq.common.BoundaryType; |
34 | 35 | import org.apache.rocketmq.common.message.MessageQueue;
|
35 | 36 | import org.apache.rocketmq.logging.org.slf4j.Logger;
|
36 | 37 | import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
|
|
50 | 51 | import org.apache.rocketmq.tieredstore.file.CompositeQueueFlatFile;
|
51 | 52 | import org.apache.rocketmq.tieredstore.file.TieredConsumeQueue;
|
52 | 53 | import org.apache.rocketmq.tieredstore.file.TieredFlatFileManager;
|
53 |
| -import org.apache.rocketmq.tieredstore.file.TieredIndexFile; |
| 54 | +import org.apache.rocketmq.tieredstore.index.IndexItem; |
| 55 | +import org.apache.rocketmq.tieredstore.index.IndexService; |
54 | 56 | import org.apache.rocketmq.tieredstore.metadata.TieredMetadataStore;
|
55 | 57 | import org.apache.rocketmq.tieredstore.metadata.TopicMetadata;
|
56 | 58 | import org.apache.rocketmq.tieredstore.metrics.TieredStoreMetricsConstant;
|
57 | 59 | import org.apache.rocketmq.tieredstore.metrics.TieredStoreMetricsManager;
|
58 | 60 | import org.apache.rocketmq.tieredstore.util.CQItemBufferUtil;
|
59 | 61 | import org.apache.rocketmq.tieredstore.util.MessageBufferUtil;
|
60 | 62 | import org.apache.rocketmq.tieredstore.util.TieredStoreUtil;
|
61 |
| -import org.apache.rocketmq.common.BoundaryType; |
62 | 63 |
|
63 | 64 | public class TieredMessageFetcher implements MessageStoreFetcher {
|
64 | 65 |
|
@@ -555,85 +556,51 @@ public long getOffsetInQueueByTime(String topic, int queueId, long timestamp, Bo
|
555 | 556 | public CompletableFuture<QueryMessageResult> queryMessageAsync(
|
556 | 557 | String topic, String key, int maxCount, long begin, long end) {
|
557 | 558 |
|
558 |
| - TieredIndexFile indexFile = TieredFlatFileManager.getIndexFile(storeConfig); |
| 559 | + IndexService indexStoreService = TieredFlatFileManager.getTieredIndexService(storeConfig); |
559 | 560 |
|
560 |
| - int hashCode = TieredIndexFile.indexKeyHashMethod(TieredIndexFile.buildKey(topic, key)); |
561 | 561 | long topicId;
|
562 | 562 | try {
|
563 | 563 | TopicMetadata topicMetadata = metadataStore.getTopic(topic);
|
564 | 564 | if (topicMetadata == null) {
|
565 |
| - LOGGER.info("TieredMessageFetcher#queryMessageAsync, topic metadata not found, topic: {}", topic); |
| 565 | + LOGGER.info("MessageFetcher#queryMessageAsync, topic metadata not found, topic: {}", topic); |
566 | 566 | return CompletableFuture.completedFuture(new QueryMessageResult());
|
567 | 567 | }
|
568 | 568 | topicId = topicMetadata.getTopicId();
|
569 | 569 | } catch (Exception e) {
|
570 |
| - LOGGER.error("TieredMessageFetcher#queryMessageAsync, get topic id failed, topic: {}", topic, e); |
| 570 | + LOGGER.error("MessageFetcher#queryMessageAsync, get topic id failed, topic: {}", topic, e); |
571 | 571 | return CompletableFuture.completedFuture(new QueryMessageResult());
|
572 | 572 | }
|
573 | 573 |
|
574 |
| - return indexFile.queryAsync(topic, key, begin, end) |
575 |
| - .thenCompose(indexBufferList -> { |
576 |
| - QueryMessageResult result = new QueryMessageResult(); |
577 |
| - int resultCount = 0; |
578 |
| - List<CompletableFuture<Void>> futureList = new ArrayList<>(maxCount); |
579 |
| - for (Pair<Long, ByteBuffer> pair : indexBufferList) { |
580 |
| - Long fileBeginTimestamp = pair.getKey(); |
581 |
| - ByteBuffer indexBuffer = pair.getValue(); |
582 |
| - |
583 |
| - if (indexBuffer.remaining() % TieredIndexFile.INDEX_FILE_HASH_COMPACT_INDEX_SIZE != 0) { |
584 |
| - LOGGER.error("[Bug] TieredMessageFetcher#queryMessageAsync: " + |
585 |
| - "index buffer size {} is not multiple of index item size {}", |
586 |
| - indexBuffer.remaining(), TieredIndexFile.INDEX_FILE_HASH_COMPACT_INDEX_SIZE); |
587 |
| - continue; |
588 |
| - } |
589 |
| - |
590 |
| - for (int indexOffset = indexBuffer.position(); |
591 |
| - indexOffset < indexBuffer.limit(); |
592 |
| - indexOffset += TieredIndexFile.INDEX_FILE_HASH_COMPACT_INDEX_SIZE) { |
593 |
| - |
594 |
| - int indexItemHashCode = indexBuffer.getInt(indexOffset); |
595 |
| - if (indexItemHashCode != hashCode) { |
596 |
| - continue; |
597 |
| - } |
598 |
| - |
599 |
| - int indexItemTopicId = indexBuffer.getInt(indexOffset + 4); |
600 |
| - if (indexItemTopicId != topicId) { |
601 |
| - continue; |
602 |
| - } |
603 |
| - |
604 |
| - int queueId = indexBuffer.getInt(indexOffset + 4 + 4); |
605 |
| - CompositeFlatFile flatFile = |
606 |
| - flatFileManager.getFlatFile(new MessageQueue(topic, brokerName, queueId)); |
607 |
| - if (flatFile == null) { |
608 |
| - continue; |
609 |
| - } |
610 |
| - |
611 |
| - // decode index item |
612 |
| - long offset = indexBuffer.getLong(indexOffset + 4 + 4 + 4); |
613 |
| - int size = indexBuffer.getInt(indexOffset + 4 + 4 + 4 + 8); |
614 |
| - int timeDiff = indexBuffer.getInt(indexOffset + 4 + 4 + 4 + 8 + 4); |
615 |
| - long indexTimestamp = fileBeginTimestamp + timeDiff; |
616 |
| - if (indexTimestamp < begin || indexTimestamp > end) { |
617 |
| - continue; |
618 |
| - } |
| 574 | + CompletableFuture<List<IndexItem>> future = indexStoreService.queryAsync(topic, key, maxCount, begin, end); |
619 | 575 |
|
620 |
| - CompletableFuture<Void> getMessageFuture = flatFile.getCommitLogAsync(offset, size) |
621 |
| - .thenAccept(messageBuffer -> result.addMessage( |
622 |
| - new SelectMappedBufferResult(0, messageBuffer, size, null))); |
623 |
| - futureList.add(getMessageFuture); |
624 |
| - |
625 |
| - resultCount++; |
626 |
| - if (resultCount >= maxCount) { |
627 |
| - break; |
628 |
| - } |
629 |
| - } |
630 |
| - |
631 |
| - if (resultCount >= maxCount) { |
632 |
| - break; |
633 |
| - } |
| 576 | + return future.thenCompose(indexItemList -> { |
| 577 | + QueryMessageResult result = new QueryMessageResult(); |
| 578 | + List<CompletableFuture<Void>> futureList = new ArrayList<>(maxCount); |
| 579 | + for (IndexItem indexItem : indexItemList) { |
| 580 | + if (topicId != indexItem.getTopicId()) { |
| 581 | + continue; |
634 | 582 | }
|
635 |
| - return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])) |
636 |
| - .thenApply(v -> result); |
637 |
| - }); |
| 583 | + CompositeFlatFile flatFile = |
| 584 | + flatFileManager.getFlatFile(new MessageQueue(topic, brokerName, indexItem.getQueueId())); |
| 585 | + if (flatFile == null) { |
| 586 | + continue; |
| 587 | + } |
| 588 | + CompletableFuture<Void> getMessageFuture = flatFile |
| 589 | + .getCommitLogAsync(indexItem.getOffset(), indexItem.getSize()) |
| 590 | + .thenAccept(messageBuffer -> result.addMessage( |
| 591 | + new SelectMappedBufferResult( |
| 592 | + indexItem.getOffset(), messageBuffer, indexItem.getSize(), null))); |
| 593 | + futureList.add(getMessageFuture); |
| 594 | + if (futureList.size() >= maxCount) { |
| 595 | + break; |
| 596 | + } |
| 597 | + } |
| 598 | + return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).thenApply(v -> result); |
| 599 | + }).whenComplete((result, throwable) -> { |
| 600 | + if (result != null) { |
| 601 | + LOGGER.info("MessageFetcher#queryMessageAsync, query result: {}, topic: {}, topicId: {}, key: {}, maxCount: {}, timestamp: {}-{}", |
| 602 | + result.getMessageBufferList().size(), topic, topicId, key, maxCount, begin, end); |
| 603 | + } |
| 604 | + }); |
638 | 605 | }
|
639 | 606 | }
|
0 commit comments