Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion be/src/olap/rowset/beta_rowset_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ Status BetaRowsetReader::get_segment_iterators(RowsetReaderContext* read_context
_read_options.output_columns = read_context->output_columns;

// load segments
// use cache is true when do vertica compaction
bool should_use_cache = use_cache || read_context->reader_type == ReaderType::READER_QUERY;
RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(_rowset, &_segment_cache_handle,
should_use_cache));
Expand Down
13 changes: 7 additions & 6 deletions be/src/olap/segment_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle*
if (lru_handle == nullptr) {
return false;
}
*handle = SegmentCacheHandle(_cache.get(), lru_handle);
handle->init(_cache.get(), lru_handle);
return true;
}

Expand All @@ -56,7 +56,7 @@ void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::Cache

auto lru_handle = _cache->insert(key.encode(), &value, sizeof(SegmentCache::CacheValue),
deleter, CachePriority::NORMAL, meta_mem_usage);
*handle = SegmentCacheHandle(_cache.get(), lru_handle);
handle->init(_cache.get(), lru_handle);
}

void SegmentCache::erase(const SegmentCache::CacheKey& key) {
Expand All @@ -65,12 +65,14 @@ void SegmentCache::erase(const SegmentCache::CacheKey& key) {

Status SegmentLoader::load_segments(const BetaRowsetSharedPtr& rowset,
SegmentCacheHandle* cache_handle, bool use_cache) {
if (cache_handle->is_inited()) {
return Status::OK();
}

SegmentCache::CacheKey cache_key(rowset->rowset_id());
if (_segment_cache->lookup(cache_key, cache_handle)) {
cache_handle->owned = false;
return Status::OK();
}
cache_handle->owned = !use_cache;

std::vector<segment_v2::SegmentSharedPtr> segments;
RETURN_IF_ERROR(rowset->load_segments(&segments));
Expand All @@ -81,9 +83,8 @@ Status SegmentLoader::load_segments(const BetaRowsetSharedPtr& rowset,
cache_value->segments = std::move(segments);
_segment_cache->insert(cache_key, *cache_value, cache_handle);
} else {
cache_handle->segments = std::move(segments);
cache_handle->init(std::move(segments));
}

return Status::OK();
}

Expand Down
41 changes: 20 additions & 21 deletions be/src/olap/segment_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,49 +128,48 @@ class SegmentLoader {
class SegmentCacheHandle {
public:
SegmentCacheHandle() = default;
SegmentCacheHandle(Cache* cache, Cache::Handle* handle) : _cache(cache), _handle(handle) {}

~SegmentCacheHandle() {
if (_handle != nullptr) {
CHECK(_cache != nullptr);
CHECK(segments.empty()) << segments.size();
CHECK(!owned);
CHECK(_segments.empty()) << _segments.size();
CHECK(!_owned);
// last_visit_time is set when release.
// because it only be needed when pruning.
((SegmentCache::CacheValue*)_cache->value(_handle))->last_visit_time = UnixMillis();
_cache->release(_handle);
}
}

SegmentCacheHandle(SegmentCacheHandle&& other) noexcept {
std::swap(_cache, other._cache);
std::swap(_handle, other._handle);
this->owned = other.owned;
this->segments = std::move(other.segments);
[[nodiscard]] bool is_inited() const { return _init; }

void init(std::vector<segment_v2::SegmentSharedPtr> segments) {
DCHECK(!_init);
_owned = true;
_segments = std::move(segments);
_init = true;
}

SegmentCacheHandle& operator=(SegmentCacheHandle&& other) noexcept {
std::swap(_cache, other._cache);
std::swap(_handle, other._handle);
this->owned = other.owned;
this->segments = std::move(other.segments);
return *this;
void init(Cache* cache, Cache::Handle* handle) {
DCHECK(!_init);
_owned = false;
_cache = cache;
_handle = handle;
_init = true;
}

std::vector<segment_v2::SegmentSharedPtr>& get_segments() {
if (owned) {
return segments;
if (_owned) {
return _segments;
} else {
return ((SegmentCache::CacheValue*)_cache->value(_handle))->segments;
}
}

public:
// If set to true, the loaded segments will be saved in segments, not in lru cache;
bool owned = false;
std::vector<segment_v2::SegmentSharedPtr> segments;

private:
bool _init {false};
bool _owned {false};
std::vector<segment_v2::SegmentSharedPtr> _segments;
Cache* _cache = nullptr;
Cache::Handle* _handle = nullptr;

Expand Down