yujun777 commented on code in PR #27693:
URL: https://github.com/apache/doris/pull/27693#discussion_r1408705127


##########
be/src/olap/tablet_manager.cpp:
##########
@@ -1040,119 +1028,155 @@ Status 
TabletManager::build_all_report_tablets_info(std::map<TTabletId, TTablet>
 }
 
 Status TabletManager::start_trash_sweep() {
+    std::unique_lock<std::mutex> lock(_gc_tablets_lock, std::defer_lock);
+    if (!lock.try_lock()) {
+        return Status::OK();
+    }
+
     SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
+    for_each_tablet([](const TabletSharedPtr& tablet) { 
tablet->delete_expired_stale_rowset(); },
+                    filter_all_tablets);
+
+    std::list<TabletSharedPtr>::iterator last_it;
     {
-        for_each_tablet(
-                [](const TabletSharedPtr& tablet) { 
tablet->delete_expired_stale_rowset(); },
-                filter_all_tablets);
+        std::shared_lock rdlock(_shutdown_tablets_lock);
+        last_it = _shutdown_tablets.begin();
+        if (last_it == _shutdown_tablets.end()) {
+            return Status::OK();
+        }
     }
 
-    int32_t clean_num = 0;
-    do {
-#ifndef BE_TEST
-        sleep(1);
-#endif
-        clean_num = 0;
-        // should get write lock here, because it will remove tablet from 
shut_down_tablets
-        // and get tablet will access shut_down_tablets
-        {
-            std::lock_guard<std::shared_mutex> wrlock1(_shutdown_tablets_lock);
-            std::lock_guard<std::shared_mutex> 
wrlock2(_shutdown_deleting_tablets_lock);
-            for (const auto& tablet : _shutdown_tablets) {
-                _shutdown_deleting_tablets.push_back(tablet);
+    auto get_batch_tablets = [this, &last_it](int limit) {
+        std::vector<TabletSharedPtr> batch_tablets;
+        std::lock_guard<std::shared_mutex> wrdlock(_shutdown_tablets_lock);
+        while (last_it != _shutdown_tablets.end() && batch_tablets.size() < 
limit) {
+            // it means current tablet is referenced by other thread
+            if (last_it->use_count() > 1) {

Review Comment:
   如果tablet被其他线程拿住,不会回收这个tablet;
   
如果tablet被_move_tablet_to_trash成功了,其他线程不会拿到这个tablet(因为其他线程拿tablet需要获得shutdown_tablets_lock
 的读锁)



##########
be/src/olap/tablet_manager.cpp:
##########
@@ -1040,119 +1028,155 @@ Status 
TabletManager::build_all_report_tablets_info(std::map<TTabletId, TTablet>
 }
 
 Status TabletManager::start_trash_sweep() {
+    std::unique_lock<std::mutex> lock(_gc_tablets_lock, std::defer_lock);
+    if (!lock.try_lock()) {
+        return Status::OK();
+    }
+
     SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
+    for_each_tablet([](const TabletSharedPtr& tablet) { 
tablet->delete_expired_stale_rowset(); },
+                    filter_all_tablets);
+
+    std::list<TabletSharedPtr>::iterator last_it;
     {
-        for_each_tablet(
-                [](const TabletSharedPtr& tablet) { 
tablet->delete_expired_stale_rowset(); },
-                filter_all_tablets);
+        std::shared_lock rdlock(_shutdown_tablets_lock);
+        last_it = _shutdown_tablets.begin();
+        if (last_it == _shutdown_tablets.end()) {
+            return Status::OK();
+        }
     }
 
-    int32_t clean_num = 0;
-    do {
-#ifndef BE_TEST
-        sleep(1);
-#endif
-        clean_num = 0;
-        // should get write lock here, because it will remove tablet from 
shut_down_tablets
-        // and get tablet will access shut_down_tablets
-        {
-            std::lock_guard<std::shared_mutex> wrlock1(_shutdown_tablets_lock);
-            std::lock_guard<std::shared_mutex> 
wrlock2(_shutdown_deleting_tablets_lock);
-            for (const auto& tablet : _shutdown_tablets) {
-                _shutdown_deleting_tablets.push_back(tablet);
+    auto get_batch_tablets = [this, &last_it](int limit) {
+        std::vector<TabletSharedPtr> batch_tablets;
+        std::lock_guard<std::shared_mutex> wrdlock(_shutdown_tablets_lock);
+        while (last_it != _shutdown_tablets.end() && batch_tablets.size() < 
limit) {
+            // it means current tablet is referenced by other thread
+            if (last_it->use_count() > 1) {

Review Comment:
   如果tablet被其他线程拿住,不会回收这个tablet;
   
如果tablet被_move_tablet_to_trash成功了,其他线程不会拿到这个tablet(因为其他线程拿tablet需要获得shutdown_tablets_lock
 的读锁)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to