Coverage Report

Created: 2026-07-13 15:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/condition_cache.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <butil/macros.h>
21
#include <glog/logging.h>
22
#include <stddef.h>
23
#include <stdint.h>
24
25
#include <atomic>
26
#include <memory>
27
#include <roaring/roaring.hh>
28
#include <string>
29
#include <vector>
30
31
#include "common/config.h"
32
#include "common/status.h"
33
#include "io/fs/file_system.h"
34
#include "io/fs/path.h"
35
#include "runtime/exec_env.h"
36
#include "runtime/memory/lru_cache_policy.h"
37
#include "runtime/memory/mem_tracker.h"
38
#include "util/lru_cache.h"
39
#include "util/slice.h"
40
#include "util/time.h"
41
42
namespace doris {
43
44
// Context passed from scan/table-reader layers to physical readers for condition cache
45
// integration. On MISS, readers set filter_result[granule] to true when row-level predicates keep
46
// at least one row in that granule. On HIT, readers skip granules whose cached bit is false.
47
struct ConditionCacheContext {
48
    bool is_hit = false;
49
    std::shared_ptr<std::vector<bool>> filter_result; // per-granule: true = has surviving rows
50
    int64_t base_granule = 0;                         // global granule index of filter_result[0]
51
    size_t num_granules = 0; // authoritative bitmap length; excludes allocation-only guard bits
52
    static constexpr int GRANULE_SIZE = 2048;
53
};
54
55
namespace segment_v2 {
56
57
class ConditionCacheHandle;
58
59
class ConditionCache : public LRUCachePolicy {
60
public:
61
    using LRUCachePolicy::insert;
62
63
    // The cache key or segment lru cache
64
    struct CacheKey {
65
        CacheKey(RowsetId rowset_id_, int64_t segment_id_, uint64_t digest_)
66
0
                : rowset_id(rowset_id_), segment_id(segment_id_), digest(digest_) {}
67
        RowsetId rowset_id;
68
        int64_t segment_id;
69
        uint64_t digest;
70
71
        // Encode to a flat binary which can be used as LRUCache's key
72
0
        [[nodiscard]] std::string encode() const {
73
0
            char buf[16];
74
0
            memcpy(buf, &segment_id, 8);
75
0
            memcpy(buf + 8, &digest, 8);
76
77
0
            return rowset_id.to_string() + std::string(buf, 16);
78
0
        }
79
    };
80
81
    class CacheValue : public LRUCacheValueBase {
82
    public:
83
        std::shared_ptr<std::vector<bool>> filter_result;
84
        // The bitmap coordinate system is part of the cached result. A later scan may prune a
85
        // different first row group, so it must not derive this origin from its current plan.
86
        int64_t base_granule = 0;
87
    };
88
89
    // Cache key for external tables (Hive ORC/Parquet)
90
    struct ExternalCacheKey {
91
        static constexpr uint8_t BASE_GRANULE_AWARE_VERSION = 1;
92
93
142
        ExternalCacheKey() = default;
94
        ExternalCacheKey(const std::string& path_, int64_t modification_time_, int64_t file_size_,
95
                         uint64_t digest_, int64_t start_offset_, int64_t size_,
96
                         uint8_t format_version_ = 0)
97
12
                : path(path_),
98
12
                  modification_time(modification_time_),
99
12
                  file_size(file_size_),
100
12
                  digest(digest_),
101
12
                  start_offset(start_offset_),
102
12
                  size(size_),
103
12
                  format_version(format_version_) {}
104
        std::string path;
105
        int64_t modification_time = 0;
106
        int64_t file_size = 0;
107
        uint64_t digest = 0;
108
        int64_t start_offset = 0;
109
        int64_t size = 0;
110
        uint8_t format_version = 0;
111
112
13
        [[nodiscard]] std::string encode() const {
113
13
            std::string key = path;
114
13
            char buf[41];
115
13
            memcpy(buf, &modification_time, 8);
116
13
            memcpy(buf + 8, &file_size, 8);
117
13
            memcpy(buf + 16, &digest, 8);
118
13
            memcpy(buf + 24, &start_offset, 8);
119
13
            memcpy(buf + 32, &size, 8);
120
13
            buf[40] = static_cast<char>(format_version);
121
13
            key.append(buf, 41);
122
13
            return key;
123
13
        }
124
    };
125
126
    // Create global instance of this class
127
12
    static ConditionCache* create_global_cache(size_t capacity, uint32_t num_shards = 16) {
128
12
        auto* res = new ConditionCache(capacity, num_shards);
129
12
        return res;
130
12
    }
131
132
    // Return global instance.
133
    // Client should call create_global_cache before.
134
4
    static ConditionCache* instance() { return ExecEnv::GetInstance()->get_condition_cache(); }
135
136
    ConditionCache() = delete;
137
138
    ConditionCache(size_t capacity, uint32_t num_shards)
139
12
            : LRUCachePolicy(CachePolicy::CacheType::CONDITION_CACHE, capacity, LRUCacheType::SIZE,
140
12
                             config::inverted_index_cache_stale_sweep_time_sec, num_shards,
141
12
                             /*element_count_capacity*/ 0, /*enable_prune*/ true,
142
12
                             /*is_lru_k*/ true) {}
143
144
    template <typename KeyType>
145
    bool lookup(const KeyType& key, ConditionCacheHandle* handle);
146
147
    template <typename KeyType>
148
    void insert(const KeyType& key, std::shared_ptr<std::vector<bool>> filter_result,
149
                int64_t base_granule = 0);
150
};
151
152
class ConditionCacheHandle {
153
public:
154
10
    ConditionCacheHandle() = default;
155
156
    ConditionCacheHandle(LRUCachePolicy* cache, Cache::Handle* handle)
157
4
            : _cache(cache), _handle(handle) {}
158
159
14
    ~ConditionCacheHandle() {
160
14
        if (_handle != nullptr) {
161
4
            _cache->release(_handle);
162
4
        }
163
14
    }
164
165
0
    ConditionCacheHandle(ConditionCacheHandle&& other) noexcept {
166
0
        // we can use std::exchange if we switch c++14 on
167
0
        std::swap(_cache, other._cache);
168
0
        std::swap(_handle, other._handle);
169
0
    }
170
171
2
    ConditionCacheHandle& operator=(ConditionCacheHandle&& other) noexcept {
172
2
        std::swap(_cache, other._cache);
173
2
        std::swap(_handle, other._handle);
174
2
        return *this;
175
2
    }
176
177
0
    LRUCachePolicy* cache() const { return _cache; }
178
179
2
    std::shared_ptr<std::vector<bool>> get_filter_result() const {
180
2
        if (!_cache) {
181
0
            return nullptr;
182
0
        }
183
2
        return ((ConditionCache::CacheValue*)_cache->value(_handle))->filter_result;
184
2
    }
185
186
1
    int64_t get_base_granule() const {
187
1
        DORIS_CHECK(_cache != nullptr);
188
1
        return ((ConditionCache::CacheValue*)_cache->value(_handle))->base_granule;
189
1
    }
190
191
private:
192
    LRUCachePolicy* _cache = nullptr;
193
    Cache::Handle* _handle = nullptr;
194
195
    // Don't allow copy and assign
196
    DISALLOW_COPY_AND_ASSIGN(ConditionCacheHandle);
197
};
198
199
} // namespace segment_v2
200
} // namespace doris