Skip to content

Commit 7e4ee44

Browse files
github-actions[bot]csun5285claude
authored
branch-4.1: [refactor](scan) remove SchemaCache to fix concurrent crash in OlapScanner::prepare #62327 (#62414)
Cherry-picked from #62327 Co-authored-by: Chenyang Sun <sunchenyang@selectdb.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6e4e3d5 commit 7e4ee44

12 files changed

Lines changed: 17 additions & 242 deletions

File tree

be/src/common/config.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,6 @@ DEFINE_mInt64(hdfs_write_batch_buffer_size_mb, "1"); // 1MB
12761276

12771277
//disable shrink memory by default
12781278
DEFINE_mBool(enable_shrink_memory, "false");
1279-
DEFINE_mInt32(schema_cache_capacity, "1024");
1280-
DEFINE_mInt32(schema_cache_sweep_time_sec, "100");
12811279

12821280
// max number of segment cache, default -1 for backward compatibility fd_number*2/5
12831281
DEFINE_Int32(segment_cache_capacity, "-1");

be/src/common/config.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,9 +1320,6 @@ DECLARE_mInt64(file_cache_max_file_reader_cache_size);
13201320
DECLARE_mInt64(hdfs_write_batch_buffer_size_mb);
13211321
//enable shrink memory
13221322
DECLARE_mBool(enable_shrink_memory);
1323-
// enable cache for high concurrent point query work load
1324-
DECLARE_mInt32(schema_cache_capacity);
1325-
DECLARE_mInt32(schema_cache_sweep_time_sec);
13261323

13271324
// max number of segment cache
13281325
DECLARE_Int32(segment_cache_capacity);

be/src/exec/scan/olap_scanner.cpp

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include "runtime/runtime_profile.h"
5252
#include "runtime/runtime_state.h"
5353
#include "service/backend_options.h"
54-
#include "storage/cache/schema_cache.h"
5554
#include "storage/id_manager.h"
5655
#include "storage/index/inverted/inverted_index_profile.h"
5756
#include "storage/iterator/block_reader.h"
@@ -164,68 +163,24 @@ Status OlapScanner::prepare() {
164163
// value (e.g. select a from t where a .. and b ... limit 1),
165164
// it will be very slow when reading data in segment iterator
166165
_tablet_reader->set_batch_size(_state->batch_size());
167-
TabletSchemaSPtr cached_schema;
168-
std::string schema_key;
169166
{
170167
TOlapScanNode& olap_scan_node = local_state->olap_scan_node();
171168

172-
const auto check_can_use_cache = [&]() {
173-
if (!(olap_scan_node.__isset.schema_version && olap_scan_node.__isset.columns_desc &&
174-
!olap_scan_node.columns_desc.empty() &&
175-
olap_scan_node.columns_desc[0].col_unique_id >= 0 && // Why check first column?
176-
tablet->tablet_schema()->num_variant_columns() == 0 &&
177-
tablet->tablet_schema()->num_virtual_columns() == 0)) {
178-
return false;
169+
// Each scanner builds its own TabletSchema to avoid concurrent modification.
170+
tablet_schema = std::make_shared<TabletSchema>();
171+
tablet_schema->copy_from(*tablet->tablet_schema());
172+
if (olap_scan_node.__isset.columns_desc && !olap_scan_node.columns_desc.empty() &&
173+
olap_scan_node.columns_desc[0].col_unique_id >= 0) {
174+
tablet_schema->clear_columns();
175+
for (const auto& column_desc : olap_scan_node.columns_desc) {
176+
tablet_schema->append_column(TabletColumn(column_desc));
179177
}
180-
181-
// If `delete_predicates` is not empty, will merge the columns in delete predicate into current tablet schema
182-
if (!_tablet_reader_params.delete_predicates.empty()) {
183-
return false;
178+
if (olap_scan_node.__isset.schema_version) {
179+
tablet_schema->set_schema_version(olap_scan_node.schema_version);
184180
}
185-
186-
const bool has_pruned_column =
187-
std::ranges::any_of(_output_tuple_desc->slots(), [](const auto& slot) {
188-
if ((slot->type()->get_primitive_type() == PrimitiveType::TYPE_STRUCT ||
189-
slot->type()->get_primitive_type() == PrimitiveType::TYPE_MAP ||
190-
slot->type()->get_primitive_type() == PrimitiveType::TYPE_ARRAY) &&
191-
!slot->all_access_paths().empty()) {
192-
return true;
193-
}
194-
return false;
195-
});
196-
return !has_pruned_column;
197-
}();
198-
199-
if (check_can_use_cache) {
200-
schema_key =
201-
SchemaCache::get_schema_key(tablet->tablet_id(), olap_scan_node.columns_desc,
202-
olap_scan_node.schema_version);
203-
cached_schema = SchemaCache::instance()->get_schema(schema_key);
204181
}
205-
if (cached_schema && cached_schema->num_virtual_columns() == 0) {
206-
tablet_schema = cached_schema;
207-
} else {
208-
// If schema is not cached or cached schema has virtual columns,
209-
// we need to create a new TabletSchema.
210-
tablet_schema = std::make_shared<TabletSchema>();
211-
tablet_schema->copy_from(*tablet->tablet_schema());
212-
if (olap_scan_node.__isset.columns_desc && !olap_scan_node.columns_desc.empty() &&
213-
olap_scan_node.columns_desc[0].col_unique_id >= 0) {
214-
// Originally scanner get TabletSchema from tablet object in BE.
215-
// To support lightweight schema change for adding / dropping columns,
216-
// tabletschema is bounded to rowset and tablet's schema maybe outdated,
217-
// so we have to use schema from a query plan witch FE puts it in query plans.
218-
tablet_schema->clear_columns();
219-
for (const auto& column_desc : olap_scan_node.columns_desc) {
220-
tablet_schema->append_column(TabletColumn(column_desc));
221-
}
222-
if (olap_scan_node.__isset.schema_version) {
223-
tablet_schema->set_schema_version(olap_scan_node.schema_version);
224-
}
225-
}
226-
if (olap_scan_node.__isset.indexes_desc) {
227-
tablet_schema->update_indexes_from_thrift(olap_scan_node.indexes_desc);
228-
}
182+
if (olap_scan_node.__isset.indexes_desc) {
183+
tablet_schema->update_indexes_from_thrift(olap_scan_node.indexes_desc);
229184
}
230185

231186
if (_tablet_reader_params.rs_splits.empty()) {
@@ -283,12 +238,6 @@ Status OlapScanner::prepare() {
283238
read_columns_to_string(tablet_schema, _return_columns));
284239
}
285240

286-
// Add newly created tablet schema to schema cache if it does not have virtual columns.
287-
if (cached_schema == nullptr && !schema_key.empty() &&
288-
tablet_schema->num_virtual_columns() == 0 && !tablet_schema->has_pruned_columns()) {
289-
SchemaCache::instance()->insert_schema(schema_key, tablet_schema);
290-
}
291-
292241
if (_tablet_reader_params.score_runtime) {
293242
SCOPED_TIMER(local_state->_statistics_collect_timer);
294243
_tablet_reader_params.collection_statistics = std::make_shared<CollectionStatistics>();

be/src/runtime/exec_env.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class CdcClientMgr;
119119
class TabletSchemaCache;
120120
class TabletColumnObjectPool;
121121
class UserFunctionCache;
122-
class SchemaCache;
123122
class StoragePageCache;
124123
class AnnIndexIVFListCache;
125124
class SegmentLoader;
@@ -379,7 +378,6 @@ class ExecEnv {
379378

380379
TabletSchemaCache* get_tablet_schema_cache() { return _tablet_schema_cache; }
381380
TabletColumnObjectPool* get_tablet_column_object_pool() { return _tablet_column_object_pool; }
382-
SchemaCache* schema_cache() { return _schema_cache; }
383381
StoragePageCache* get_storage_page_cache() { return _storage_page_cache; }
384382
AnnIndexIVFListCache* get_ann_index_ivf_list_cache() { return _ann_index_ivf_list_cache; }
385383
SegmentLoader* segment_loader() { return _segment_loader; }
@@ -539,7 +537,6 @@ class ExecEnv {
539537
TabletSchemaCache* _tablet_schema_cache = nullptr;
540538
TabletColumnObjectPool* _tablet_column_object_pool = nullptr;
541539
std::unique_ptr<BaseStorageEngine> _storage_engine;
542-
SchemaCache* _schema_cache = nullptr;
543540
StoragePageCache* _storage_page_cache = nullptr;
544541
AnnIndexIVFListCache* _ann_index_ivf_list_cache = nullptr;
545542
SegmentLoader* _segment_loader = nullptr;

be/src/runtime/exec_env_init.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
#include "service/point_query_executor.h"
100100
#include "storage/cache/ann_index_ivf_list_cache.h"
101101
#include "storage/cache/page_cache.h"
102-
#include "storage/cache/schema_cache.h"
103102
#include "storage/id_manager.h"
104103
#include "storage/index/inverted/inverted_index_cache.h"
105104
#include "storage/olap_define.h"
@@ -641,8 +640,6 @@ Status ExecEnv::init_mem_env() {
641640
<< " segment_cache_capacity: " << segment_cache_capacity
642641
<< " min_segment_cache_mem_limit " << segment_cache_mem_limit;
643642

644-
_schema_cache = new SchemaCache(config::schema_cache_capacity);
645-
646643
size_t block_file_cache_fd_cache_size =
647644
std::min((uint64_t)config::file_cache_max_file_reader_cache_size, fd_number / 3);
648645
LOG(INFO) << "max file reader cache size is: " << block_file_cache_fd_cache_size
@@ -725,7 +722,7 @@ void ExecEnv::init_mem_tracker() {
725722
_segcompaction_mem_tracker =
726723
MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::COMPACTION, "SegCompaction");
727724
_tablets_no_cache_mem_tracker = MemTrackerLimiter::create_shared(
728-
MemTrackerLimiter::Type::METADATA, "Tablets(not in SchemaCache, TabletSchemaCache)");
725+
MemTrackerLimiter::Type::METADATA, "Tablets(not in TabletSchemaCache)");
729726
_segments_no_cache_mem_tracker = MemTrackerLimiter::create_shared(
730727
MemTrackerLimiter::Type::METADATA, "Segments(not in SegmentCache)");
731728
_rowsets_no_cache_mem_tracker =
@@ -885,7 +882,6 @@ void ExecEnv::destroy() {
885882
SAFE_DELETE(_encoding_info_resolver);
886883
SAFE_DELETE(_condition_cache);
887884
SAFE_DELETE(_lookup_connection_cache);
888-
SAFE_DELETE(_schema_cache);
889885
SAFE_DELETE(_segment_loader);
890886
SAFE_DELETE(_row_cache);
891887
SAFE_DELETE(_query_cache);

be/src/runtime/memory/cache_policy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ class CachePolicy {
149149
}
150150
}
151151

152-
inline static std::vector<CacheType> MetadataCache {
153-
CacheType::SEGMENT_CACHE, CacheType::SCHEMA_CACHE, CacheType::TABLET_SCHEMA_CACHE};
152+
inline static std::vector<CacheType> MetadataCache {CacheType::SEGMENT_CACHE,
153+
CacheType::TABLET_SCHEMA_CACHE};
154154

155155
CachePolicy(CacheType type, size_t capacity, uint32_t stale_sweep_time_s, bool enable_prune);
156156
virtual ~CachePolicy();

be/src/runtime/memory/memory_profile.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "runtime/memory/jemalloc_control.h"
2424
#include "runtime/memory/mem_tracker_limiter.h"
2525
#include "runtime/runtime_profile.h"
26-
#include "storage/cache/schema_cache.h"
2726
#include "storage/metadata_adder.h"
27+
#include "storage/segment/segment_loader.h"
2828
#include "storage/tablet/tablet_schema_cache.h"
2929
#include "util/mem_info.h"
3030

@@ -162,8 +162,7 @@ void MemoryProfile::refresh_memory_overview_profile() {
162162
// 2 refresh metadata memory tracker
163163
ExecEnv::GetInstance()->tablets_no_cache_mem_tracker()->set_consumption(
164164
MetadataAdder<TabletMeta>::get_all_tablets_size() -
165-
TabletSchemaCache::instance()->value_mem_consumption() -
166-
SchemaCache::instance()->value_mem_consumption());
165+
TabletSchemaCache::instance()->value_mem_consumption());
167166
ExecEnv::GetInstance()->rowsets_no_cache_mem_tracker()->set_consumption(
168167
MetadataAdder<RowsetMeta>::get_all_rowsets_size());
169168
ExecEnv::GetInstance()->segments_no_cache_mem_tracker()->set_consumption(

be/src/storage/cache/schema_cache.cpp

Lines changed: 0 additions & 60 deletions
This file was deleted.

be/src/storage/cache/schema_cache.h

Lines changed: 0 additions & 98 deletions
This file was deleted.

be/src/storage/iterator/vgeneric_iterators.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "core/block/column_with_type_and_name.h"
2727
#include "core/column/column.h"
2828
#include "core/data_type/data_type.h"
29-
#include "storage/cache/schema_cache.h"
3029
#include "storage/field.h"
3130
#include "storage/iterators.h"
3231
#include "storage/olap_common.h"

0 commit comments

Comments
 (0)