Skip to content

Commit e235289

Browse files
committed
[Feature] support column zero copy read from page cache
Signed-off-by: stdpain <drfeng08@gmail.com>
1 parent cd764ae commit e235289

File tree

210 files changed

+1729
-1129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+1729
-1129
lines changed

be/src/agent/publish_version.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "fmt/format.h"
2222
#include "gen_cpp/AgentService_types.h"
2323
#include "gutil/strings/join.h"
24+
#include "runtime/exec_env.h"
2425
#include "storage/data_dir.h"
2526
#include "storage/replication_txn_manager.h"
2627
#include "storage/storage_engine.h"

be/src/cache/datacache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "common/status.h"
2323
#include "gutil/strings/split.h"
2424
#include "gutil/strings/strip.h"
25+
#include "runtime/exec_env.h"
2526
#include "storage/options.h"
2627
#include "util/parse_util.h"
2728

be/src/cache/lrucache_engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace starrocks {
2323
class LRUCacheEngine final : public LocalCacheEngine {
2424
public:
2525
LRUCacheEngine() = default;
26-
virtual ~LRUCacheEngine() override = default;
26+
~LRUCacheEngine() override = default;
2727

2828
Status init(const CacheOptions& options) override;
2929
bool is_initialized() const override { return _initialized.load(std::memory_order_relaxed); }

be/src/cache/object_cache/page_cache.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@
4444

4545
namespace starrocks {
4646

47+
std::atomic<size_t> StoragePageCacheMetrics::returned_page_handle_count{};
48+
std::atomic<size_t> StoragePageCacheMetrics::released_page_handle_count{};
49+
4750
METRIC_DEFINE_UINT_GAUGE(page_cache_lookup_count, MetricUnit::OPERATIONS);
4851
METRIC_DEFINE_UINT_GAUGE(page_cache_hit_count, MetricUnit::OPERATIONS);
4952
METRIC_DEFINE_UINT_GAUGE(page_cache_capacity, MetricUnit::BYTES);
53+
METRIC_DEFINE_UINT_GAUGE(page_cache_pinned_count, MetricUnit::BYTES);
5054

5155
void StoragePageCache::init_metrics() {
5256
StarRocksMetrics::instance()->metrics()->register_metric("page_cache_lookup_count", &page_cache_lookup_count);
@@ -60,6 +64,10 @@ void StoragePageCache::init_metrics() {
6064
StarRocksMetrics::instance()->metrics()->register_metric("page_cache_capacity", &page_cache_capacity);
6165
StarRocksMetrics::instance()->metrics()->register_hook("page_cache_capacity",
6266
[this]() { page_cache_capacity.set_value(get_capacity()); });
67+
68+
StarRocksMetrics::instance()->metrics()->register_metric("page_cache_pinned_count", &page_cache_pinned_count);
69+
StarRocksMetrics::instance()->metrics()->register_hook(
70+
"page_cache_pinned_count", [this]() { page_cache_pinned_count.set_value(get_pinned_count()); });
6371
}
6472

6573
void StoragePageCache::prune() {
@@ -92,12 +100,17 @@ bool StoragePageCache::adjust_capacity(int64_t delta, size_t min_capacity) {
92100
return true;
93101
}
94102

103+
size_t StoragePageCache::get_pinned_count() const {
104+
return StoragePageCacheMetrics::returned_page_handle_count - StoragePageCacheMetrics::released_page_handle_count;
105+
}
106+
95107
bool StoragePageCache::lookup(const std::string& key, PageCacheHandle* handle) {
96108
ObjectCacheHandle* obj_handle = nullptr;
97109
Status st = _cache->lookup(key, &obj_handle);
98110
if (!st.ok()) {
99111
return false;
100112
}
113+
StoragePageCacheMetrics::returned_page_handle_count++;
101114
*handle = PageCacheHandle(_cache, obj_handle);
102115
return true;
103116
}
@@ -124,6 +137,7 @@ Status StoragePageCache::insert(const std::string& key, std::vector<uint8_t>* da
124137
Status st = _cache->insert(key, (void*)data, mem_size, deleter, &obj_handle, opts);
125138
if (st.ok()) {
126139
*handle = PageCacheHandle(_cache, obj_handle);
140+
StoragePageCacheMetrics::returned_page_handle_count++;
127141
}
128142
return st;
129143
}
@@ -134,6 +148,7 @@ Status StoragePageCache::insert(const std::string& key, void* data, int64_t size
134148
Status st = _cache->insert(key, data, size, deleter, &obj_handle, opts);
135149
if (st.ok()) {
136150
*handle = PageCacheHandle(_cache, obj_handle);
151+
StoragePageCacheMetrics::returned_page_handle_count++;
137152
}
138153
return st;
139154
}

be/src/cache/object_cache/page_cache.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@
3434

3535
#pragma once
3636

37-
#include <memory>
3837
#include <string>
3938
#include <utility>
4039

4140
#include "cache/datacache.h"
42-
#include "gutil/macros.h" // for DISALLOW_COPY
43-
#include "runtime/current_thread.h"
44-
#include "runtime/exec_env.h"
45-
#include "util/defer_op.h"
4641

4742
namespace starrocks {
4843

4944
class PageCacheHandle;
5045
class MemTracker;
51-
class ObjectCacheWriteOptions;
46+
struct ObjectCacheWriteOptions;
5247

5348
// Page cache min size is 256MB
5449
static constexpr int64_t kcacheMinSize = 268435456;
5550

51+
struct StoragePageCacheMetrics {
52+
static std::atomic<size_t> returned_page_handle_count;
53+
static std::atomic<size_t> released_page_handle_count;
54+
};
55+
5656
// Wrapper around Cache, and used for cache page of column datas in Segment.
5757
// TODO(zc): We should add some metric to see cache hit/miss rate.
5858
class StoragePageCache {
@@ -110,6 +110,10 @@ class StoragePageCache {
110110
bool is_initialized() const { return _initialized.load(std::memory_order_relaxed); }
111111
bool available() const { return is_initialized() && _cache->mem_cache_available(); }
112112

113+
// get the number of pinned pages in the page cache
114+
// used for metrics
115+
size_t get_pinned_count() const;
116+
113117
private:
114118
LocalCacheEngine* _cache = nullptr;
115119
std::atomic<bool> _initialized = false;
@@ -124,6 +128,7 @@ class PageCacheHandle {
124128
PageCacheHandle(LocalCacheEngine* cache, ObjectCacheHandle* handle) : _cache(cache), _handle(handle) {}
125129
~PageCacheHandle() {
126130
if (_handle != nullptr) {
131+
StoragePageCacheMetrics::released_page_handle_count++;
127132
_cache->release(_handle);
128133
}
129134
}

be/src/column/adaptive_nullable_column.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ size_t AdaptiveNullableColumn::null_count() const {
3535
if (!_has_null) {
3636
return 0;
3737
}
38-
return SIMD::count_nonzero(_null_column->get_data());
38+
return SIMD::count_nonzero(_null_column->immutable_data());
3939
}
4040
}
4141
}
@@ -53,7 +53,7 @@ size_t AdaptiveNullableColumn::null_count(size_t offset, size_t count) const {
5353
if (!_has_null) {
5454
return 0;
5555
}
56-
return SIMD::count_nonzero(_null_column->get_data());
56+
return SIMD::count_nonzero(_null_column->immutable_data());
5757
}
5858
}
5959
}
@@ -271,7 +271,7 @@ void AdaptiveNullableColumn::serialize_batch(uint8_t* dst, Buffer<uint32_t>& sli
271271
uint32_t max_one_row_size) const {
272272
materialized_nullable();
273273
_data_column->serialize_batch_with_null_masks(dst, slice_sizes, chunk_size, max_one_row_size,
274-
_null_column->get_data().data(), _has_null);
274+
_null_column->immutable_data().data(), _has_null);
275275
}
276276

277277
const uint8_t* AdaptiveNullableColumn::deserialize_and_append(const uint8_t* pos) {

be/src/column/adaptive_nullable_column.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ class AdaptiveNullableColumn final
8686

8787
AdaptiveNullableColumn(const AdaptiveNullableColumn& rhs) { CHECK(false) << "unimplemented"; }
8888

89-
AdaptiveNullableColumn(AdaptiveNullableColumn&& rhs) { CHECK(false) << "unimplemented"; }
89+
AdaptiveNullableColumn(AdaptiveNullableColumn&& rhs) noexcept { CHECK(false) << "unimplemented"; }
9090

9191
AdaptiveNullableColumn& operator=(const AdaptiveNullableColumn& rhs) {
9292
AdaptiveNullableColumn tmp(rhs);
9393
this->swap_column(tmp);
9494
return *this;
9595
}
9696

97-
AdaptiveNullableColumn& operator=(AdaptiveNullableColumn&& rhs) {
97+
AdaptiveNullableColumn& operator=(AdaptiveNullableColumn&& rhs) noexcept {
9898
AdaptiveNullableColumn tmp(std::move(rhs));
9999
this->swap_column(tmp);
100100
return *this;
@@ -160,7 +160,7 @@ class AdaptiveNullableColumn final
160160
return false;
161161
}
162162
case State::kMaterialized: {
163-
return _has_null && _null_column->get_data()[index];
163+
return _has_null && _null_column->immutable_data()[index];
164164
}
165165
default: {
166166
__builtin_unreachable();
@@ -346,7 +346,7 @@ class AdaptiveNullableColumn final
346346

347347
uint32_t serialize_size(size_t idx) const override {
348348
materialized_nullable();
349-
if (_null_column->get_data()[idx]) {
349+
if (_null_column->immutable_data()[idx]) {
350350
return sizeof(uint8_t);
351351
}
352352
return sizeof(uint8_t) + _data_column->serialize_size(idx);
@@ -458,10 +458,10 @@ class AdaptiveNullableColumn final
458458
// however, this may is not user want because once adaptive nullable column materialized,
459459
// its performance will be degraded to nullable column. Due to the following reason, we add
460460
// DCHECK(false) here and disable the behaviour.
461-
const NullData& immutable_null_column_data() const {
461+
const ImmutableNullData immutable_null_column_data() const {
462462
DCHECK(false);
463463
materialized_nullable();
464-
return _null_column->get_data();
464+
return _null_column->immutable_data();
465465
}
466466

467467
Column* mutable_data_column() {

0 commit comments

Comments
 (0)