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

jianliangqi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 83ab29fd56 [Fix](inverted index) fix compound directory unlock problem 
(#16861)
83ab29fd56 is described below

commit 83ab29fd56a0fbe82234f59a2e49c1b15fba92f5
Author: airborne12 <[email protected]>
AuthorDate: Mon Feb 20 18:29:39 2023 +0800

    [Fix](inverted index) fix compound directory unlock problem (#16861)
    
    In DorisCompoundDirectory::FSIndexInput::close, use lock_guard to automatic 
unlock, or it may cause lock leak.
---
 .../inverted_index_compound_directory.cpp          | 30 ++++++++++++----------
 .../segment_v2/inverted_index_compound_directory.h |  7 ++---
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git 
a/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.cpp 
b/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.cpp
index 12bffffb00..4f966bf44d 100644
--- a/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.cpp
+++ b/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.cpp
@@ -271,7 +271,7 @@ DorisCompoundDirectory::FSIndexInput::FSIndexInput(const 
FSIndexInput& other)
         _CLTHROWA(CL_ERR_NullPointer, "other handle is null");
     }
 
-    std::lock_guard<std::mutex> wlock(other._handle->_shared_lock);
+    std::lock_guard<doris::Mutex> wlock(*other._handle->_shared_lock);
     _handle = _CL_POINTER(other._handle);
     _pos = other._handle->_fpos; //note where we are currently...
 }
@@ -280,6 +280,7 @@ 
DorisCompoundDirectory::FSIndexInput::SharedHandle::SharedHandle(const char* pat
     _length = 0;
     _fpos = 0;
     strcpy(this->path, path);
+    _shared_lock = new doris::Mutex();
 }
 
 DorisCompoundDirectory::FSIndexInput::SharedHandle::~SharedHandle() {
@@ -300,16 +301,19 @@ lucene::store::IndexInput* 
DorisCompoundDirectory::FSIndexInput::clone() const {
 void DorisCompoundDirectory::FSIndexInput::close() {
     BufferedIndexInput::close();
     if (_handle != nullptr) {
-        _handle->_shared_lock.lock();
-
-        //determine if we are about to delete the handle...
-        bool dounlock = (_LUCENE_ATOMIC_INT_GET(_handle->__cl_refcount) > 1);
-
-        //decdelete (deletes if refcount is down to 0
-        _CLDECDELETE(_handle)
+        doris::Mutex* lock = _handle->_shared_lock;
+        bool ref = false;
+        {
+            std::lock_guard<doris::Mutex> wlock(*lock);
+            //determine if we are about to delete the handle...
+            ref = (_LUCENE_ATOMIC_INT_GET(_handle->__cl_refcount) > 1);
+            //decdelete (deletes if refcount is down to 0
+            _CLDECDELETE(_handle);
+        }
 
-        if (dounlock) {
-            _handle->_shared_lock.unlock();
+        //if _handle is not ref by other FSIndexInput, try to release mutex 
lock, or it will be leaked.
+        if (!ref) {
+            delete lock;
         }
     }
 }
@@ -323,7 +327,7 @@ void 
DorisCompoundDirectory::FSIndexInput::seekInternal(const int64_t position)
 void DorisCompoundDirectory::FSIndexInput::readInternal(uint8_t* b, const 
int32_t len) {
     CND_PRECONDITION(_handle != nullptr, "shared file handle has closed");
     CND_PRECONDITION(_handle->_reader != nullptr, "file is not open");
-    std::lock_guard<std::mutex> wlock(_handle->_shared_lock);
+    std::lock_guard<doris::Mutex> wlock(*_handle->_shared_lock);
 
     if (_handle->_fpos != _pos) {
         _handle->_fpos = _pos;
@@ -471,7 +475,7 @@ void DorisCompoundDirectory::init(const io::FileSystemSPtr& 
_fs, const char* _pa
 }
 
 void DorisCompoundDirectory::create() {
-    std::lock_guard<std::mutex> wlock(_this_lock);
+    std::lock_guard<doris::Mutex> wlock(_this_lock);
 
     //clear old files
     std::vector<std::string> files;
@@ -642,7 +646,7 @@ bool DorisCompoundDirectory::deleteDirectory() {
 
 void DorisCompoundDirectory::renameFile(const char* from, const char* to) {
     CND_PRECONDITION(directory[0] != 0, "directory is not open");
-    std::lock_guard<std::mutex> wlock(_this_lock);
+    std::lock_guard<doris::Mutex> wlock(_this_lock);
     char old[CL_MAX_DIR];
     priv_getFN(old, from);
 
diff --git a/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.h 
b/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.h
index 1fbdc9d592..b87498a34e 100644
--- a/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.h
+++ b/be/src/olap/rowset/segment_v2/inverted_index_compound_directory.h
@@ -26,6 +26,7 @@
 #include <vector>
 
 #include "io/fs/file_system.h"
+#include "util/lock.h"
 
 namespace doris {
 
@@ -49,7 +50,7 @@ class CLUCENE_EXPORT DorisCompoundDirectory : public 
lucene::store::Directory {
 private:
     int filemode;
 
-    std::mutex _this_lock;
+    doris::Mutex _this_lock;
 
 protected:
     DorisCompoundDirectory();
@@ -115,7 +116,7 @@ class DorisCompoundDirectory::FSIndexInput : public 
lucene::store::BufferedIndex
         io::FileReaderSPtr _reader;
         uint64_t _length;
         int64_t _fpos;
-        std::mutex _shared_lock;
+        doris::Mutex* _shared_lock;
         char path[4096];
         SharedHandle(const char* path);
         ~SharedHandle() override;
@@ -145,7 +146,7 @@ public:
     const char* getObjectName() const override { return getClassName(); }
     static const char* getClassName() { return "FSIndexInput"; }
 
-    std::mutex _this_lock;
+    doris::Mutex _this_lock;
 
 protected:
     // Random-access methods


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to