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

wyxxxcat 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 7777552efa3 [fix](cloud) Prevent repeated rowset dereferencing during 
tablet recycling (#68552)
7777552efa3 is described below

commit 7777552efa34951d69df171b2bd4d694e24a3cac
Author: Yixuan Wang <[email protected]>
AuthorDate: Tue Dec 1 11:11:55 2026 +0800

    [fix](cloud) Prevent repeated rowset dereferencing during tablet recycling 
(#68552)
    
    ### What problem does this PR solve?
    
    Problem Summary:
    When recycling a versioned tablet, the ref_count > 1 classification path
    decremented the rowset reference count without removing its versioned
    metadata. If subsequent cleanup failed, a later round could read the
    same metadata and release the reference again, potentially deleting data
    still used by another reference.
    
    Remove the versioned rowset metadata in the same transaction as the
    reference count decrement.
    
    Add a unit test covering shared load and compact rowsets, an injected
    deletion failure, and a retry with a fresh recycler.
    
    ### Release note
    
    Prevent premature deletion of shared rowset data when tablet recycling
    is retried after a failure.
    
    ### Check List (For Author)
    
    - Test: Unit test added; not run. Build and formatting checks not run.
    - Behavior changed: Yes. Shared rowset metadata is removed atomically
    with its reference count decrement to prevent repeated dereferencing.
---
 cloud/src/recycler/recycler.cpp            |   6 ++
 cloud/test/recycle_versioned_keys_test.cpp | 111 +++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+)

diff --git a/cloud/src/recycler/recycler.cpp b/cloud/src/recycler/recycler.cpp
index 017b4225ebc..0bbb8eea93e 100644
--- a/cloud/src/recycler/recycler.cpp
+++ b/cloud/src/recycler/recycler.cpp
@@ -8623,6 +8623,12 @@ int InstanceRecycler::classify_rowset_task_by_ref_count(
                     .tag("ref_count", ref_count - 1)
                     .tag("ref_count_key", hex(rowset_ref_count_key));
 
+            if (!task.versioned_rowset_key.empty()) {
+                versioned::document_remove<RowsetMetaCloudPB>(txn.get(), 
task.versioned_rowset_key,
+                                                              
task.versionstamp);
+                LOG_INFO("remove versioned meta rowset key in classification 
phase")
+                        .tag("key", hex(task.versioned_rowset_key));
+            }
             if (!task.recycle_rowset_key.empty()) {
                 txn->remove(task.recycle_rowset_key);
                 LOG_INFO("remove recycle rowset key in classification phase")
diff --git a/cloud/test/recycle_versioned_keys_test.cpp 
b/cloud/test/recycle_versioned_keys_test.cpp
index 2d5413f3f85..5865e0c88bb 100644
--- a/cloud/test/recycle_versioned_keys_test.cpp
+++ b/cloud/test/recycle_versioned_keys_test.cpp
@@ -2198,6 +2198,117 @@ TEST(RecycleVersionedKeysTest, 
BatchDeleteRefCountGreaterThanOne) {
     }
 }
 
+TEST(RecycleVersionedKeysTest, BatchDeleteSharedRowsetsRetryAfterFailure) {
+    auto meta_service = get_meta_service();
+    auto txn_kv = meta_service->txn_kv();
+    std::string instance_id = 
"batch_delete_shared_rowsets_retry_test_instance";
+    std::string cloud_unique_id = fmt::format("1:{}:0", instance_id);
+    ASSERT_NO_FATAL_FAILURE(create_and_refresh_instance(meta_service.get(), 
instance_id));
+
+    int64_t db_id = 1, table_id = 2, index_id = 3, partition_id = 4, tablet_id 
= 5;
+    ASSERT_NO_FATAL_FAILURE(prepare_and_commit_index(meta_service.get(), 
cloud_unique_id, db_id,
+                                                     table_id, index_id));
+    ASSERT_NO_FATAL_FAILURE(prepare_and_commit_partition(meta_service.get(), 
cloud_unique_id, db_id,
+                                                         table_id, 
partition_id, index_id));
+    ASSERT_NO_FATAL_FAILURE(create_tablet(meta_service.get(), cloud_unique_id, 
db_id, table_id,
+                                          index_id, partition_id, tablet_id));
+
+    // Shared load/compact rowsets release one reference before deleting the 
unique rowset.
+    std::vector<RowsetMetaCloudPB> rowsets = {create_rowset(1, tablet_id, 
partition_id, 2),
+                                              create_rowset(2, tablet_id, 
partition_id, 3),
+                                              create_rowset(3, tablet_id, 
partition_id, 4)};
+    std::vector<std::string> rowset_keys = {
+            versioned::meta_rowset_load_key({instance_id, tablet_id, 2}),
+            versioned::meta_rowset_compact_key({instance_id, tablet_id, 3}),
+            versioned::meta_rowset_load_key({instance_id, tablet_id, 4})};
+    auto accessor = std::make_shared<MockAccessor>();
+    {
+        std::unique_ptr<Transaction> txn;
+        ASSERT_EQ(txn_kv->create_txn(&txn), TxnErrorCode::TXN_OK);
+        for (size_t i = 0; i < rowsets.size(); ++i) {
+            auto rowset = rowsets[i];
+            txn->put(meta_rowset_key({instance_id, tablet_id, 
rowset.end_version()}),
+                     rowset.SerializeAsString());
+            txn->atomic_add(versioned::data_rowset_ref_count_key(
+                                    {instance_id, tablet_id, 
rowset.rowset_id_v2()}),
+                            i < 2 ? 2 : 1);
+            ASSERT_EQ(0, accessor->put_file(segment_path(tablet_id, 
rowset.rowset_id_v2(), 0),
+                                            "segment_data"));
+            ASSERT_TRUE(versioned::document_put(txn.get(), rowset_keys[i], 
std::move(rowset)));
+        }
+        ASSERT_EQ(txn->commit(), TxnErrorCode::TXN_OK);
+    }
+
+    InstanceInfoPB instance_info;
+    ASSERT_NO_FATAL_FAILURE(get_instance(meta_service.get(), cloud_unique_id, 
instance_info));
+    RecyclerMetricsContext ctx;
+    {
+        auto sp = SyncPoint::get_instance();
+        DORIS_CLOUD_DEFER {
+            sp->disable_processing();
+            sp->clear_call_back("MockAccessor::delete_files");
+        };
+        sp->set_call_back("MockAccessor::delete_files", [](auto&& args) {
+            auto* ret = try_any_cast_ret<int>(args);
+            ret->first = -1;
+            ret->second = true;
+        });
+        sp->enable_processing();
+        auto recycler = get_instance_recycler(meta_service.get(), 
instance_info, accessor);
+        ASSERT_NE(0, recycler->recycle_tablets(table_id, index_id, ctx, 
partition_id));
+    }
+
+    auto check_shared_rowsets = [&]() {
+        std::unique_ptr<Transaction> txn;
+        ASSERT_EQ(txn_kv->create_txn(&txn), TxnErrorCode::TXN_OK);
+        for (size_t i = 0; i < 2; ++i) {
+            SCOPED_TRACE(rowsets[i].rowset_id_v2());
+            std::string value;
+            ASSERT_EQ(txn->get(versioned::data_rowset_ref_count_key(
+                                       {instance_id, tablet_id, 
rowsets[i].rowset_id_v2()}),
+                               &value),
+                      TxnErrorCode::TXN_OK);
+            int64_t ref_count = 0;
+            ASSERT_TRUE(txn->decode_atomic_int(value, &ref_count));
+            EXPECT_EQ(1, ref_count);
+            RowsetMetaCloudPB rowset;
+            Versionstamp version;
+            EXPECT_EQ(versioned::document_get(txn.get(), rowset_keys[i], 
&rowset, &version),
+                      TxnErrorCode::TXN_KEY_NOT_FOUND);
+            EXPECT_EQ(0, accessor->exists(segment_path(tablet_id, 
rowsets[i].rowset_id_v2(), 0)));
+        }
+    };
+    ASSERT_NO_FATAL_FAILURE(check_shared_rowsets());
+
+    std::string tablet_key =
+            meta_tablet_key({instance_id, table_id, index_id, partition_id, 
tablet_id});
+    {
+        std::unique_ptr<Transaction> txn;
+        ASSERT_EQ(txn_kv->create_txn(&txn), TxnErrorCode::TXN_OK);
+        std::string value;
+        ASSERT_EQ(txn->get(tablet_key, &value), TxnErrorCode::TXN_OK);
+        RowsetMetaCloudPB rowset;
+        Versionstamp version;
+        ASSERT_EQ(versioned::document_get(txn.get(), rowset_keys[2], &rowset, 
&version),
+                  TxnErrorCode::TXN_OK);
+        EXPECT_EQ(0, accessor->exists(segment_path(tablet_id, 
rowsets[2].rowset_id_v2(), 0)));
+    }
+
+    // A fresh recycler must rescan the tablet without decrementing shared 
references again.
+    {
+        auto recycler = get_instance_recycler(meta_service.get(), 
instance_info, accessor);
+        ASSERT_EQ(0, recycler->recycle_tablets(table_id, index_id, ctx, 
partition_id));
+    }
+    ASSERT_NO_FATAL_FAILURE(check_shared_rowsets());
+    EXPECT_EQ(1, accessor->exists(segment_path(tablet_id, 
rowsets[2].rowset_id_v2(), 0)));
+    {
+        std::unique_ptr<Transaction> txn;
+        ASSERT_EQ(txn_kv->create_txn(&txn), TxnErrorCode::TXN_OK);
+        std::string value;
+        EXPECT_EQ(txn->get(tablet_key, &value), 
TxnErrorCode::TXN_KEY_NOT_FOUND);
+    }
+}
+
 // Test: Mixed ref_count scenario - some rowsets have ref_count==1, others 
have ref_count>1
 TEST(RecycleVersionedKeysTest, BatchDeleteMixedRefCount) {
     auto meta_service = get_meta_service();


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

Reply via email to