|
| 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 | +#include "olap/tablet_schema_cache.h" |
| 19 | + |
| 20 | +#include <gen_cpp/olap_file.pb.h> |
| 21 | + |
| 22 | +#include "common/sync_point.h" |
| 23 | + |
| 24 | +namespace doris { |
| 25 | + |
| 26 | +void TabletSchemaCache::create_global_schema_cache() { |
| 27 | + DCHECK(_s_instance == nullptr); |
| 28 | + static TabletSchemaCache instance; |
| 29 | + _s_instance = &instance; |
| 30 | + std::thread t(&TabletSchemaCache::_recycle, _s_instance); |
| 31 | + t.detach(); |
| 32 | +} |
| 33 | + |
| 34 | +TabletSchemaSPtr TabletSchemaCache::insert(int64_t index_id, const TabletSchemaPB& schema) { |
| 35 | + TEST_SYNC_POINT_RETURN_WITH_VALUE("TabletSchemaCache::insert1", |
| 36 | + std::make_shared<TabletSchema>()); |
| 37 | + DCHECK(_s_instance != nullptr); |
| 38 | + DCHECK(index_id > 0 && schema.has_schema_version()); |
| 39 | + std::lock_guard guard(_mtx); |
| 40 | + auto iter = _cache.find({index_id, schema.schema_version()}); |
| 41 | + if (iter == _cache.end()) { |
| 42 | + TabletSchemaSPtr tablet_schema_ptr = std::make_shared<TabletSchema>(); |
| 43 | + tablet_schema_ptr->init_from_pb(schema); |
| 44 | + _cache.insert({{index_id, schema.schema_version()}, tablet_schema_ptr}); |
| 45 | + return tablet_schema_ptr; |
| 46 | + } |
| 47 | + return iter->second; |
| 48 | +} |
| 49 | + |
| 50 | +TabletSchemaSPtr TabletSchemaCache::insert(int64_t index_id, const TabletSchemaSPtr& schema) { |
| 51 | + TEST_SYNC_POINT_RETURN_WITH_VALUE("TabletSchemaCache::insert2", |
| 52 | + std::make_shared<TabletSchema>()); |
| 53 | + DCHECK(_s_instance != nullptr); |
| 54 | + DCHECK(index_id > 0 && schema->schema_version() > 0); |
| 55 | + std::lock_guard guard(_mtx); |
| 56 | + auto [it, _] = _cache.insert({{index_id, schema->schema_version()}, schema}); |
| 57 | + return it->second; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief recycle when TabletSchemaSPtr use_count equals 1. |
| 62 | + */ |
| 63 | +void TabletSchemaCache::_recycle() { |
| 64 | + int64_t tablet_schema_cache_recycle_interval = 86400; // s, one day |
| 65 | + for (;;) { |
| 66 | + std::this_thread::sleep_for(std::chrono::seconds(tablet_schema_cache_recycle_interval)); |
| 67 | + std::lock_guard guard(_mtx); |
| 68 | + for (auto iter = _cache.begin(), last = _cache.end(); iter != last;) { |
| 69 | + if (iter->second.unique()) { |
| 70 | + iter = _cache.erase(iter); |
| 71 | + } else { |
| 72 | + ++iter; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace doris |
0 commit comments