-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Opt](TabletSchema) reuse TabletColumn info to reduce mem #42448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,23 @@ const int32_t MAX_LEAF_COUNT = 1024; | |
| const float MAXMBSortInHeap = 512.0 * 8; | ||
| const int DIMS = 1; | ||
|
|
||
| bool InvertedIndexColumnWriter::check_support_inverted_index(const TabletColumn& column) { | ||
| // bellow types are not supported in inverted index for extracted columns | ||
| static std::set<FieldType> invalid_types = { | ||
| FieldType::OLAP_FIELD_TYPE_DOUBLE, | ||
| FieldType::OLAP_FIELD_TYPE_JSONB, | ||
| FieldType::OLAP_FIELD_TYPE_ARRAY, | ||
| FieldType::OLAP_FIELD_TYPE_FLOAT, | ||
| }; | ||
| if (column.is_extracted_column() && (invalid_types.contains(column.type()))) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why check is_extracted_column? |
||
| return false; | ||
| } | ||
| if (column.is_variant_type()) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| template <FieldType field_type> | ||
| class InvertedIndexColumnWriterImpl : public InvertedIndexColumnWriter { | ||
| public: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include "olap/tablet_column_object_pool.h" | ||
|
|
||
| #include <gen_cpp/AgentService_types.h> | ||
| #include <gen_cpp/olap_file.pb.h> | ||
|
|
||
| #include "olap/tablet_schema.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| bvar::Adder<int64_t> g_tablet_column_cache_count("tablet_column_cache_count"); | ||
| bvar::Adder<int64_t> g_tablet_column_cache_hit_count("tablet_column_cache_hit_count"); | ||
|
|
||
| std::pair<Cache::Handle*, TabletColumnPtr> TabletColumnObjectPool::insert(const std::string& key) { | ||
| auto* lru_handle = lookup(key); | ||
| TabletColumnPtr tablet_column_ptr; | ||
| if (lru_handle) { | ||
| auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle); | ||
| tablet_column_ptr = value->tablet_column; | ||
| VLOG_DEBUG << "reuse column "; | ||
| g_tablet_column_cache_hit_count << 1; | ||
| } else { | ||
| auto* value = new CacheValue; | ||
| tablet_column_ptr = std::make_shared<TabletColumn>(); | ||
| ColumnPB pb; | ||
| pb.ParseFromString(key); | ||
| tablet_column_ptr->init_from_pb(pb); | ||
| VLOG_DEBUG << "create column "; | ||
| value->tablet_column = tablet_column_ptr; | ||
| lru_handle = LRUCachePolicy::insert(key, value, 1, 0, CachePriority::NORMAL); | ||
| g_tablet_column_cache_count << 1; | ||
| } | ||
| DCHECK(lru_handle != nullptr); | ||
| return {lru_handle, tablet_column_ptr}; | ||
| } | ||
|
|
||
| TabletColumnObjectPool::CacheValue::~CacheValue() { | ||
| g_tablet_column_cache_count << -1; | ||
| } | ||
|
|
||
| } // namespace doris |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "olap/tablet_fwd.h" | ||
| #include "olap/tablet_schema.h" | ||
| #include "runtime/exec_env.h" | ||
| #include "runtime/memory/lru_cache_policy.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| // TabletColumnObjectPool is a cache for TabletColumn objects. It is used to reduce memory consumption | ||
| // when there are a large number of identical TabletColumns in the cluster, which usually occurs | ||
| // when VARIANT type columns are modified and added, each Rowset has an individual TabletSchema. | ||
| // Excessive TabletSchemas can lead to significant memory overhead. Reusing memory for identical | ||
| // TabletColumns would greatly reduce this memory consumption. | ||
|
|
||
| class TabletColumnObjectPool : public LRUCachePolicy { | ||
| public: | ||
| TabletColumnObjectPool(size_t capacity) | ||
| : LRUCachePolicy(CachePolicy::CacheType::TABLET_COLUMN_OBJECT_POOL, capacity, | ||
| LRUCacheType::NUMBER, config::tablet_schema_cache_recycle_interval) {} | ||
|
|
||
| static TabletColumnObjectPool* create_global_column_cache(size_t capacity) { | ||
| auto* res = new TabletColumnObjectPool(capacity); | ||
| return res; | ||
| } | ||
|
|
||
| static TabletColumnObjectPool* instance() { | ||
| return ExecEnv::GetInstance()->get_tablet_column_object_pool(); | ||
| } | ||
|
|
||
| std::pair<Cache::Handle*, TabletColumnPtr> insert(const std::string& key); | ||
|
|
||
| private: | ||
| class CacheValue : public LRUCacheValueBase { | ||
| public: | ||
| ~CacheValue() override; | ||
| TabletColumnPtr tablet_column; | ||
| }; | ||
| }; | ||
|
|
||
| } // namespace doris |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,8 +38,10 @@ | |
| #include "exec/tablet_info.h" | ||
| #include "olap/inverted_index_parser.h" | ||
| #include "olap/olap_define.h" | ||
| #include "olap/tablet_column_object_pool.h" | ||
| #include "olap/types.h" | ||
| #include "olap/utils.h" | ||
| #include "runtime/memory/lru_cache_policy.h" | ||
| #include "runtime/thread_context.h" | ||
| #include "tablet_meta.h" | ||
| #include "vec/aggregate_functions/aggregate_function_simple_factory.h" | ||
|
|
@@ -853,7 +855,9 @@ void TabletIndex::to_schema_pb(TabletIndexPB* index) const { | |
|
|
||
| TabletSchema::TabletSchema() = default; | ||
|
|
||
| TabletSchema::~TabletSchema() = default; | ||
| TabletSchema::~TabletSchema() { | ||
| clear_column_cache_handlers(); | ||
| } | ||
|
|
||
| int64_t TabletSchema::get_metadata_size() const { | ||
| return sizeof(TabletSchema) + _vl_field_mem_size; | ||
|
|
@@ -947,9 +951,18 @@ void TabletSchema::clear_columns() { | |
| _num_null_columns = 0; | ||
| _num_key_columns = 0; | ||
| _cols.clear(); | ||
| clear_column_cache_handlers(); | ||
| } | ||
|
|
||
| void TabletSchema::clear_column_cache_handlers() { | ||
| for (auto* cache_handle : _column_cache_handlers) { | ||
| TabletColumnObjectPool::instance()->release(cache_handle); | ||
| } | ||
| _column_cache_handlers.clear(); | ||
| } | ||
|
|
||
| void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns) { | ||
| void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: function 'init_from_pb' exceeds recommended size/complexity thresholds [readability-function-size] void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
^Additional contextbe/src/olap/tablet_schema.cpp:945: 85 lines including whitespace and comments (threshold 80) void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: function 'init_from_pb' exceeds recommended size/complexity thresholds [readability-function-size] void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
^Additional contextbe/src/olap/tablet_schema.cpp:956: 88 lines including whitespace and comments (threshold 80) void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: function 'init_from_pb' exceeds recommended size/complexity thresholds [readability-function-size] void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
^Additional contextbe/src/olap/tablet_schema.cpp:963: 88 lines including whitespace and comments (threshold 80) void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
^ |
||
| bool reuse_cache_column) { | ||
| _keys_type = schema.keys_type(); | ||
| _num_columns = 0; | ||
| _num_variant_columns = 0; | ||
|
|
@@ -960,25 +973,34 @@ void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extrac | |
| _field_name_to_index.clear(); | ||
| _field_id_to_index.clear(); | ||
| _cluster_key_idxes.clear(); | ||
| clear_column_cache_handlers(); | ||
| for (const auto& i : schema.cluster_key_idxes()) { | ||
| _cluster_key_idxes.push_back(i); | ||
| } | ||
| for (auto& column_pb : schema.column()) { | ||
| TabletColumn column; | ||
| column.init_from_pb(column_pb); | ||
| if (ignore_extracted_columns && column.is_extracted_column()) { | ||
| TabletColumnPtr column; | ||
| if (reuse_cache_column) { | ||
| auto pair = TabletColumnObjectPool::instance()->insert( | ||
| deterministic_string_serialize(column_pb)); | ||
| column = pair.second; | ||
| _column_cache_handlers.push_back(pair.first); | ||
| } else { | ||
| column = std::make_shared<TabletColumn>(); | ||
| column->init_from_pb(column_pb); | ||
| } | ||
| if (ignore_extracted_columns && column->is_extracted_column()) { | ||
| continue; | ||
| } | ||
| if (column.is_key()) { | ||
| if (column->is_key()) { | ||
| _num_key_columns++; | ||
| } | ||
| if (column.is_nullable()) { | ||
| if (column->is_nullable()) { | ||
| _num_null_columns++; | ||
| } | ||
| if (column.is_variant_type()) { | ||
| if (column->is_variant_type()) { | ||
| ++_num_variant_columns; | ||
| } | ||
| _cols.emplace_back(std::make_shared<TabletColumn>(std::move(column))); | ||
| _cols.emplace_back(std::move(column)); | ||
| _vl_field_mem_size += | ||
| sizeof(StringRef) + sizeof(char) * _cols.back()->name().size() + sizeof(size_t); | ||
| _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns); | ||
|
|
@@ -1097,6 +1119,7 @@ void TabletSchema::build_current_tablet_schema(int64_t index_id, int32_t version | |
| _version_col_idx = -1; | ||
| _skip_bitmap_col_idx = -1; | ||
| _cluster_key_idxes.clear(); | ||
| clear_column_cache_handlers(); | ||
| for (const auto& i : ori_tablet_schema._cluster_key_idxes) { | ||
| _cluster_key_idxes.push_back(i); | ||
| } | ||
|
|
@@ -1514,13 +1537,4 @@ bool operator!=(const TabletSchema& a, const TabletSchema& b) { | |
| return !(a == b); | ||
| } | ||
|
|
||
| std::string TabletSchema::deterministic_string_serialize(const TabletSchemaPB& schema_pb) { | ||
| std::string output; | ||
| google::protobuf::io::StringOutputStream string_output_stream(&output); | ||
| google::protobuf::io::CodedOutputStream output_stream(&string_output_stream); | ||
| output_stream.SetSerializationDeterministic(true); | ||
| schema_pb.SerializeToCodedStream(&output_stream); | ||
| return output; | ||
| } | ||
|
|
||
| } // namespace doris | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array is supported by inverted index