Skip to content

Commit 49af192

Browse files
authored
[fix](transaction) avoid aborting newer same-label transactions (#64939)
### What problem does this PR solve? In cloud mode, `CloudMetaMgr::abort_txn` currently prefers `db_id + label` over `txn_id` when building `AbortTxnRequest`. For stream load rollback, the context usually contains both `txn_id` and `label`. If an old stream load attempt is aborted externally but its BE-side execution is still cleaning up later, its rollback may abort by label instead of by the original txn id. When the upstream retries with the same label, this label-based rollback can match and abort a newer running transaction with the same label, causing the new attempt to fail with: ``` txn is not in 1 state, txn_status=4 ``` ### Solution Prefer txn_id when aborting a cloud stream load transaction. Fall back to db_id + label only when txn_id is not available. This makes rollback cleanup target the exact failed transaction, instead of affecting another transaction that happens to use the same label.
1 parent 9fc6513 commit 49af192

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎be/src/cloud/cloud_meta_mgr.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,16 +1669,17 @@ Status CloudMetaMgr::abort_txn(const StreamLoadContext& ctx) {
16691669
AbortTxnResponse res;
16701670
req.set_cloud_unique_id(config::cloud_unique_id);
16711671
req.set_reason(std::string(ctx.status.msg().substr(0, 1024)));
1672-
if (ctx.db_id > 0 && !ctx.label.empty()) {
1672+
if (ctx.txn_id > 0) {
1673+
req.set_txn_id(ctx.txn_id);
1674+
} else if (ctx.db_id > 0 && !ctx.label.empty()) {
16731675
req.set_db_id(ctx.db_id);
16741676
req.set_label(ctx.label);
1675-
} else if (ctx.txn_id > 0) {
1676-
req.set_txn_id(ctx.txn_id);
16771677
} else {
16781678
LOG(WARNING) << "failed abort txn, with illegal input, db_id=" << ctx.db_id
16791679
<< " txn_id=" << ctx.txn_id << " label=" << ctx.label;
16801680
return Status::InternalError<false>("failed to abort txn");
16811681
}
1682+
TEST_SYNC_POINT_RETURN_WITH_VALUE("CloudMetaMgr::abort_txn.before_rpc", Status::OK(), &req);
16821683
return retry_rpc(MetaServiceRPC::ABORT_TXN, req, &res, &MetaService_Stub::abort_txn,
16831684
{
16841685
.host_limiters = host_level_ms_rpc_rate_limiters_,

‎be/test/cloud/cloud_meta_mgr_test.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "cloud/cloud_storage_engine.h"
2929
#include "cloud/cloud_tablet.h"
3030
#include "cpp/sync_point.h"
31+
#include "load/stream_load/stream_load_context.h"
3132
#include "storage/olap_common.h"
3233
#include "storage/rowset/rowset_factory.h"
3334
#include "storage/rowset/rowset_meta.h"
@@ -43,6 +44,35 @@ class CloudMetaMgrTest : public testing::Test {
4344
void TearDown() override {}
4445
};
4546

47+
static AbortTxnRequest get_abort_txn_request(CloudMetaMgr* meta_mgr, const StreamLoadContext& ctx) {
48+
auto* sp = SyncPoint::get_instance();
49+
sp->clear_all_call_backs();
50+
sp->enable_processing();
51+
52+
bool called = false;
53+
AbortTxnRequest captured_req;
54+
SyncPoint::CallbackGuard guard;
55+
sp->set_call_back(
56+
"CloudMetaMgr::abort_txn.before_rpc",
57+
[&](auto&& args) {
58+
auto* req = try_any_cast<AbortTxnRequest*>(args[0]);
59+
captured_req.CopyFrom(*req);
60+
called = true;
61+
auto* ret = try_any_cast<std::pair<Status, bool>*>(args.back());
62+
ret->first = Status::OK();
63+
ret->second = true;
64+
},
65+
&guard);
66+
67+
Status status = meta_mgr->abort_txn(ctx);
68+
69+
EXPECT_TRUE(status.ok()) << "Status: " << status;
70+
EXPECT_TRUE(called);
71+
sp->disable_processing();
72+
sp->clear_all_call_backs();
73+
return captured_req;
74+
}
75+
4676
TEST_F(CloudMetaMgrTest, bthread_fork_join_test) {
4777
// clang-format off
4878
std::atomic<int> task_counter{0};
@@ -170,6 +200,38 @@ TEST_F(CloudMetaMgrTest, bthread_fork_join_test) {
170200
// clang-format on
171201
}
172202

203+
TEST_F(CloudMetaMgrTest, abort_txn_prefers_txn_id_when_label_is_also_present) {
204+
CloudMetaMgr meta_mgr;
205+
StreamLoadContext ctx(nullptr);
206+
ctx.db_id = 10001;
207+
ctx.txn_id = 20002;
208+
ctx.label = "same_label";
209+
ctx.status = Status::InternalError<false>("load failed");
210+
211+
AbortTxnRequest captured_req = get_abort_txn_request(&meta_mgr, ctx);
212+
213+
EXPECT_TRUE(captured_req.has_txn_id());
214+
EXPECT_EQ(captured_req.txn_id(), ctx.txn_id);
215+
EXPECT_FALSE(captured_req.has_db_id());
216+
EXPECT_FALSE(captured_req.has_label());
217+
}
218+
219+
TEST_F(CloudMetaMgrTest, abort_txn_uses_label_when_txn_id_is_missing) {
220+
CloudMetaMgr meta_mgr;
221+
StreamLoadContext ctx(nullptr);
222+
ctx.db_id = 10001;
223+
ctx.label = "same_label";
224+
ctx.status = Status::InternalError<false>("load failed");
225+
226+
AbortTxnRequest captured_req = get_abort_txn_request(&meta_mgr, ctx);
227+
228+
EXPECT_FALSE(captured_req.has_txn_id());
229+
EXPECT_TRUE(captured_req.has_db_id());
230+
EXPECT_EQ(captured_req.db_id(), ctx.db_id);
231+
EXPECT_TRUE(captured_req.has_label());
232+
EXPECT_EQ(captured_req.label(), ctx.label);
233+
}
234+
173235
TEST_F(CloudMetaMgrTest, test_fill_version_holes_no_holes) {
174236
CloudStorageEngine engine(EngineOptions {});
175237
CloudMetaMgr meta_mgr;

0 commit comments

Comments
 (0)