Nói về Cơ chế Lưu trữ Tin nhắn trong RocketMQ - nohu tải game nổ hũ club

Cấu trúc IndexFile sử dụng cơ chế hash để có thể tìm kiếm vị trí tương ứng trong CommitLog thông qua key.

Việc cwin666 xây dựng IndexFile được phân công cho một thành phần xử lý cụ thể. Dưới đây là đoạn mã minh họa cách mà quá trình keo da banh này diễn ra:

 1class CommitLogDispatcherBuildIndex implements CommitLogDispatcher {
 2    @Override
 3    public void dispatch(DispatchRequest request) {
 4        if (DefaultMessageStore.this.messageStoreConfig.isMessageIndexEnable()) {
 5            DefaultMessageStore.this.indexService.buildIndex(request);
 6        }
 7    }
 8}
 9
10public void buildIndex(DispatchRequest req) {
11    IndexFile indexFile = retryGetAndCreateIndexFile();
12    if (indexFile != null) {
13        long endPhyOffset = indexFile.getEndPhyOffset();
14        DispatchRequest msg = req;
15        String topic = msg.getTopic();
16        String keys = msg.getKeys();
17
18        if (msg.getCommitLogOffset() < endPhyOffset) {
19            return;
20        }
21
22        final int tranType = MessageSysFlag.getTransactionValue(msg.getSysFlag());
23        switch (tranType) {
24            case MessageSysFlag.TRANSACTION_NOT_TYPE:
25            case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
26            case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
27                break;
28            case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
29                return;
30        }
31
32        if (req.getUniqKey() != null) {
33            indexFile = putKey(indexFile, msg, buildKey(topic, req.getUniqKey()));
34            if (indexFile == null) {
35                log.error("putKey error commitlog {} uniqkey {}", req.getCommitLogOffset(), req.getUniqKey());
36                return;
37            }
38        }
39
40        if (keys != null && keys.length() > 0) {
41            String[] keyset = keys.split(MessageConst.KEY_SEPARATOR);
42            for (int i = 0; i < keyset.length; i++) {
43                String key = keyset[i];
44                if (key.length() > 0) {
45                    indexFile = putKey(indexFile, msg, buildKey(topic, key));
46                    if (indexFile == null) {
47                        log.error("putKey error commitlog {} uniqkey {}", req.getCommitLogOffset(), req.getUniqKey());
48                        return;
49                    }
50                }
51            }
52        }
53    } else {
54        log.error("build index error, stop building index");
55    }
56}