You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/04/23 16:20:41 UTC

[doris] 08/08: [Fix](inverted index) fix memory leak when create bkd reader (#18914)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch branch-2.0-alpha
in repository https://gitbox.apache.org/repos/asf/doris.git

commit ca2123cbf66ff3aa3bdfd8b282b03bcf20b38dd6
Author: airborne12 <ai...@gmail.com>
AuthorDate: Sun Apr 23 23:21:44 2023 +0800

    [Fix](inverted index) fix memory leak when create bkd reader (#18914)
    
    The function compoundReader->openInput is called three times, and if any of these calls fail,
    an error is logged, and the function returns early. If one or two of the calls succeed, but the others fail,
    there might be a situation where the allocated memory for the IndexInput objects is not freed.
    
    To fix this, you could use std::unique_ptr to manage the memory for IndexInput objects.
    This would automatically clean up the memory when the function goes out of scope.
---
 .../rowset/segment_v2/inverted_index_compound_reader.cpp     | 11 +++++++++++
 .../olap/rowset/segment_v2/inverted_index_compound_reader.h  |  2 ++
 be/src/olap/rowset/segment_v2/inverted_index_reader.cpp      | 12 ++++++------
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp
index 9ce20703b4..7748d105cc 100644
--- a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp
+++ b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.cpp
@@ -224,6 +224,17 @@ int64_t DorisCompoundReader::fileLength(const char* name) const {
     return e->length;
 }
 
+bool DorisCompoundReader::openInput(const char* name,
+                                    std::unique_ptr<lucene::store::IndexInput>& ret,
+                                    CLuceneError& error, int32_t bufferSize) {
+    lucene::store::IndexInput* tmp;
+    bool success = openInput(name, tmp, error, bufferSize);
+    if (success) {
+        ret.reset(tmp);
+    }
+    return success;
+}
+
 bool DorisCompoundReader::openInput(const char* name, lucene::store::IndexInput*& ret,
                                     CLuceneError& error, int32_t bufferSize) {
     if (stream == nullptr) {
diff --git a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h
index d6e95d9d50..a400737b34 100644
--- a/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h
+++ b/be/src/olap/rowset/segment_v2/inverted_index_compound_reader.h
@@ -67,6 +67,8 @@ public:
     int64_t fileLength(const char* name) const override;
     bool openInput(const char* name, lucene::store::IndexInput*& ret, CLuceneError& err,
                    int32_t bufferSize = -1) override;
+    bool openInput(const char* name, std::unique_ptr<lucene::store::IndexInput>& ret,
+                   CLuceneError& err, int32_t bufferSize = -1);
     void renameFile(const char* from, const char* to) override;
     void touchFile(const char* name) override;
     lucene::store::IndexOutput* createOutput(const char* name) override;
diff --git a/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp b/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp
index 9a7520dc57..68508274c3 100644
--- a/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp
+++ b/be/src/olap/rowset/segment_v2/inverted_index_reader.cpp
@@ -557,9 +557,9 @@ Status BkdIndexReader::get_bkd_reader(std::shared_ptr<lucene::util::bkd::bkd_rea
         return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>();
     }
     CLuceneError err;
-    lucene::store::IndexInput* data_in;
-    lucene::store::IndexInput* meta_in;
-    lucene::store::IndexInput* index_in;
+    std::unique_ptr<lucene::store::IndexInput> data_in;
+    std::unique_ptr<lucene::store::IndexInput> meta_in;
+    std::unique_ptr<lucene::store::IndexInput> index_in;
 
     if (!compoundReader->openInput(
                 InvertedIndexDescriptor::get_temporary_bkd_index_data_file_name().c_str(), data_in,
@@ -574,12 +574,12 @@ Status BkdIndexReader::get_bkd_reader(std::shared_ptr<lucene::util::bkd::bkd_rea
         return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>();
     }
 
-    bkdReader = std::make_shared<lucene::util::bkd::bkd_reader>(data_in);
-    if (0 == bkdReader->read_meta(meta_in)) {
+    bkdReader = std::make_shared<lucene::util::bkd::bkd_reader>(data_in.release());
+    if (0 == bkdReader->read_meta(meta_in.get())) {
         return Status::EndOfFile("bkd index file is empty");
     }
 
-    bkdReader->read_index(index_in);
+    bkdReader->read_index(index_in.get());
 
     _type_info = get_scalar_type_info((FieldType)bkdReader->type);
     if (_type_info == nullptr) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org