Coverage Report

Created: 2026-08-20 23:07

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 "storage/rowset_id.h"
39
#include "util/lru_cache.h"
40
#include "util/slice.h"
41
#include "util/time.h"
42
43
namespace doris {
44
45
// Context passed from scan/table-reader layers to physical readers for condition cache
46
// integration. On MISS, readers set filter_result[granule] to true when row-level predicates keep
47
// at least one row in that granule. On HIT, readers skip granules whose cached bit is false.
48
struct ConditionCacheContext {
49
    bool is_hit = false;
50
    std::shared_ptr<std::vector<bool>> filter_result; // per-granule: true = has surviving rows
51
    int64_t base_granule = 0;                         // global granule index of filter_result[0]
52
    size_t num_granules = 0; // authoritative bitmap length; excludes allocation-only guard bits
53
    static constexpr int GRANULE_SIZE = 2048;
54
};
55
56
namespace segment_v2 {
57
58
class ConditionCacheHandle;
59
60
class ConditionCache : public LRUCachePolicy {
61
public:
62
    using LRUCachePolicy::insert;
63
64
    // The cache key or segment lru cache
65
    struct CacheKey {
66
        CacheKey(RowsetId rowset_id_, int64_t segment_id_, uint64_t digest_)
67
0
                : rowset_id(rowset_id_), segment_id(segment_id_), digest(digest_) {}
68
        RowsetId rowset_id;
69
        int64_t segment_id;
70
        uint64_t digest;
71
72
        // Encode to a flat binary which can be used as LRUCache's key
73
0
        [[nodiscard]] std::string encode() const {
74
0
            char buf[16];
75
0
            memcpy(buf, &segment_id, 8);
76
0
            memcpy(buf + 8, &digest, 8);
77
78
0
            return rowset_id.to_string() + std::string(buf, 16);
79
0
        }
80
    };
81
82
    class CacheValue : public LRUCacheValueBase {
83
    public:
84
        std::shared_ptr<std::vector<bool>> filter_result;
85
        // The bitmap coordinate system is part of the cached result. A later scan may prune a
86
        // different first row group, so it must not derive this origin from its current plan.
87
        int64_t base_granule = 0;
88
    };
89
90
    // Cache key for external tables (Hive ORC/Parquet)
91
    struct ExternalCacheKey {
92
        static constexpr uint8_t BASE_GRANULE_AWARE_VERSION = 1;
93
94
336
        ExternalCacheKey() = default;
95
        ExternalCacheKey(const std::string& path_, int64_t modification_time_, int64_t file_size_,
96
                         uint64_t digest_, int64_t start_offset_, int64_t size_,
97
                         uint8_t format_version_ = 0)
98
15
                : path(path_),
99
15
                  modification_time(modification_time_),
100
15
                  file_size(file_size_),
101
15
                  digest(digest_),
102
15
                  start_offset(start_offset_),
103
15
                  size(size_),
104
15
                  format_version(format_version_) {}
105
        std::string path;
106
        int64_t modification_time = 0;
107
        int64_t file_size = 0;
108
        uint64_t digest = 0;
109
        int64_t start_offset = 0;
110
        int64_t size = 0;
111
        uint8_t format_version = 0;
112
113
17
        [[nodiscard]] std::string encode() const {
114
17
            std::string key = path;
115
17
            char buf[41];
116
17
            memcpy(buf, &modification_time, 8);
117
17
            memcpy(buf + 8, &file_size, 8);
118
17
            memcpy(buf + 16, &digest, 8);
119
17
            memcpy(buf + 24, &start_offset, 8);
120
17
            memcpy(buf + 32, &size, 8);
121
17
            buf[40] = static_cast<char>(format_version);
122
17
            key.append(buf, 41);
123
17
            return key;
124
17
        }
125
    };
126
127
    // Create global instance of this class
128
13
    static ConditionCache* create_global_cache(size_t capacity, uint32_t num_shards = 16) {
129
13
        auto* res = new ConditionCache(capacity, num_shards);
130
13
        return res;
131
13
    }
132
133
    // Return global instance.
134
    // Client should call create_global_cache before.
135
6
    static ConditionCache* instance() { return ExecEnv::GetInstance()->get_condition_cache(); }
136
137
    ConditionCache() = delete;
138
139
    ConditionCache(size_t capacity, uint32_t num_shards)
140
13
            : LRUCachePolicy(CachePolicy::CacheType::CONDITION_CACHE, capacity, LRUCacheType::SIZE,
141
13
                             config::inverted_index_cache_stale_sweep_time_sec, num_shards,
142
13
                             /*element_count_capacity*/ 0, /*enable_prune*/ true,
143
13
                             /*is_lru_k*/ true) {}
144
145
    template <typename KeyType>
146
    bool lookup(const KeyType& key, ConditionCacheHandle* handle);
147
148
    template <typename KeyType>
149
    void insert(const KeyType& key, std::shared_ptr<std::vector<bool>> filter_result,
150
                int64_t base_granule = 0);
151
};
152
153
class ConditionCacheHandle {
154
public:
155
12
    ConditionCacheHandle() = default;
156
157
    ConditionCacheHandle(LRUCachePolicy* cache, Cache::Handle* handle)
158
6
            : _cache(cache), _handle(handle) {}
159
160
18
    ~ConditionCacheHandle() {
161
18
        if (_handle != nullptr) {
162
6
            _cache->release(_handle);
163
6
        }
164
18
    }
165
166
0
    ConditionCacheHandle(ConditionCacheHandle&& other) noexcept {
167
0
        // we can use std::exchange if we switch c++14 on
168
0
        std::swap(_cache, other._cache);
169
0
        std::swap(_handle, other._handle);
170
0
    }
171
172
3
    ConditionCacheHandle& operator=(ConditionCacheHandle&& other) noexcept {
173
3
        std::swap(_cache, other._cache);
174
3
        std::swap(_handle, other._handle);
175
3
        return *this;
176
3
    }
177
178
0
    LRUCachePolicy* cache() const { return _cache; }
179
180
2
    std::shared_ptr<std::vector<bool>> get_filter_result() const {
181
2
        if (!_cache) {
182
0
            return nullptr;
183
0
        }
184
2
        return ((ConditionCache::CacheValue*)_cache->value(_handle))->filter_result;
185
2
    }
186
187
1
    int64_t get_base_granule() const {
188
1
        DORIS_CHECK(_cache != nullptr);
189
1
        return ((ConditionCache::CacheValue*)_cache->value(_handle))->base_granule;
190
1
    }
191
192
private:
193
    LRUCachePolicy* _cache = nullptr;
194
    Cache::Handle* _handle = nullptr;
195
196
    // Don't allow copy and assign
197
    DISALLOW_COPY_AND_ASSIGN(ConditionCacheHandle);
198
};
199
200
} // namespace segment_v2
201
} // namespace doris