Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 22 additions & 120 deletions cloud/src/meta-store/txn_kv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,136 +1040,38 @@ TxnErrorCode Transaction::get_versionstamp(Versionstamp* versionstamp) {
return TxnErrorCode::TXN_OK;
}

static TxnErrorCode get_conflict_range(Transaction* txn, std::string_view prefix,
std::vector<std::pair<std::string, std::string>>* values) {
std::string end(prefix);
end.push_back('\xff');
FullRangeGetOptions opts;
opts.batch_limit = 0;
TEST_SYNC_POINT_CALLBACK("transaction:get_conflict_range:batch_limit", &opts.batch_limit);

// The iterator uses the full last key to read the next page in the same transaction.
auto iter = txn->full_range_get(prefix, end, opts);
for (auto kv = iter->next(); kv.has_value(); kv = iter->next()) {
auto [key, value] = *kv;
// Drop the prefix only from the key copied to the result.
key.remove_prefix(prefix.size());
values->emplace_back(key, value);
}
return iter->error_code();
}

TxnErrorCode Transaction::get_conflicting_range(
std::vector<std::pair<std::string, std::string>>* values) {
constexpr std::string_view start = "\xff\xff/transaction/conflicting_keys/";
constexpr std::string_view end = "\xff\xff/transaction/conflicting_keys/\xff";

int limit = 0;
int target_bytes = 0;
FDBStreamingMode mode = FDB_STREAMING_MODE_WANT_ALL;
int iteration = 0;
fdb_bool_t snapshot = 0;
fdb_bool_t reverse = 0;
FDBFuture* future = fdb_transaction_get_range(
txn_, FDB_KEYSEL_FIRST_GREATER_OR_EQUAL((uint8_t*)start.data(), start.size()),
FDB_KEYSEL_FIRST_GREATER_OR_EQUAL((uint8_t*)end.data(), end.size()), limit,
target_bytes, mode, iteration, snapshot, reverse);

DORIS_CLOUD_DEFER {
fdb_future_destroy(future);
};

RETURN_IF_ERROR(await_future(future));

FDBKeyValue const* out_kvs;
int out_kvs_count;
fdb_bool_t out_more = false;
do {
fdb_error_t err =
fdb_future_get_keyvalue_array(future, &out_kvs, &out_kvs_count, &out_more);
if (err) {
LOG(WARNING) << "get_conflicting_range get keyvalue array error: "
<< fdb_get_error(err);
return cast_as_txn_code(err);
}
for (int i = 0; i < out_kvs_count; i++) {
std::string_view key((char*)out_kvs[i].key, out_kvs[i].key_length);
std::string_view value((char*)out_kvs[i].value, out_kvs[i].value_length);
key.remove_prefix(start.size());
values->emplace_back(key, value);
}
} while (out_more);

return TxnErrorCode::TXN_OK;
return get_conflict_range(this, "\xff\xff/transaction/conflicting_keys/", values);
}

TxnErrorCode Transaction::get_read_conflict_range(
std::vector<std::pair<std::string, std::string>>* values) {
constexpr std::string_view start = "\xff\xff/transaction/read_conflict_range/";
constexpr std::string_view end = "\xff\xff/transaction/read_conflict_range/\xff";

int limit = 0;
int target_bytes = 0;
FDBStreamingMode mode = FDB_STREAMING_MODE_WANT_ALL;
int iteration = 0;
fdb_bool_t snapshot = 0;
fdb_bool_t reverse = 0;
FDBFuture* future = fdb_transaction_get_range(
txn_, FDB_KEYSEL_FIRST_GREATER_OR_EQUAL((uint8_t*)start.data(), start.size()),
FDB_KEYSEL_FIRST_GREATER_OR_EQUAL((uint8_t*)end.data(), end.size()), limit,
target_bytes, mode, iteration, snapshot, reverse);

DORIS_CLOUD_DEFER {
fdb_future_destroy(future);
};

RETURN_IF_ERROR(await_future(future));

FDBKeyValue const* out_kvs;
int out_kvs_count;
fdb_bool_t out_more;
do {
fdb_error_t err =
fdb_future_get_keyvalue_array(future, &out_kvs, &out_kvs_count, &out_more);
if (err) {
LOG(WARNING) << "get_conflicting_range get keyvalue array error: "
<< fdb_get_error(err);
return cast_as_txn_code(err);
}
for (int i = 0; i < out_kvs_count; i++) {
std::string_view key((char*)out_kvs[i].key, out_kvs[i].key_length);
std::string_view value((char*)out_kvs[i].value, out_kvs[i].value_length);
key.remove_prefix(start.size());
values->emplace_back(key, value);
}
} while (out_more);

return TxnErrorCode::TXN_OK;
return get_conflict_range(this, "\xff\xff/transaction/read_conflict_range/", values);
}

TxnErrorCode Transaction::get_write_conflict_range(
std::vector<std::pair<std::string, std::string>>* values) {
constexpr std::string_view start = "\xff\xff/transaction/write_conflict_range/";
constexpr std::string_view end = "\xff\xff/transaction/write_conflict_range/\xff";

int limit = 0;
int target_bytes = 0;
FDBStreamingMode mode = FDB_STREAMING_MODE_WANT_ALL;
int iteration = 0;
fdb_bool_t snapshot = 0;
fdb_bool_t reverse = 0;
FDBFuture* future = fdb_transaction_get_range(
txn_, FDB_KEYSEL_FIRST_GREATER_OR_EQUAL((uint8_t*)start.data(), start.size()),
FDB_KEYSEL_FIRST_GREATER_OR_EQUAL((uint8_t*)end.data(), end.size()), limit,
target_bytes, mode, iteration, snapshot, reverse);

DORIS_CLOUD_DEFER {
fdb_future_destroy(future);
};

RETURN_IF_ERROR(await_future(future));

FDBKeyValue const* out_kvs;
int out_kvs_count;
fdb_bool_t out_more;
do {
fdb_error_t err =
fdb_future_get_keyvalue_array(future, &out_kvs, &out_kvs_count, &out_more);
if (err) {
LOG(WARNING) << "get_conflicting_range get keyvalue array error: "
<< fdb_get_error(err);
return cast_as_txn_code(err);
}
for (int i = 0; i < out_kvs_count; i++) {
std::string_view key((char*)out_kvs[i].key, out_kvs[i].key_length);
std::string_view value((char*)out_kvs[i].value, out_kvs[i].value_length);
key.remove_prefix(start.size());
values->emplace_back(key, value);
}
} while (out_more);

return TxnErrorCode::TXN_OK;
return get_conflict_range(this, "\xff\xff/transaction/write_conflict_range/", values);
}

TxnErrorCode Transaction::report_conflicting_range() {
Expand Down
60 changes: 58 additions & 2 deletions cloud/test/txn_kv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,10 +1547,16 @@ TEST(TxnKvTest, BatchScan) {
}

TEST(TxnKvTest, ReportConflictingRange) {
const bool old_enable_logging_conflict_keys = config::enable_logging_conflict_keys;
DORIS_CLOUD_DEFER {
config::enable_logging_conflict_keys = old_enable_logging_conflict_keys;
};
config::enable_logging_conflict_keys = true;

constexpr std::string_view key_prefix = "txn_kv_test__report_conflicting_range";
std::string key = std::string(key_prefix) + std::to_string(time(nullptr));
key.push_back('\0');
key.append("suffix");

{
// 1. write a common key
Expand All @@ -1576,13 +1582,63 @@ TEST(TxnKvTest, ReportConflictingRange) {
ASSERT_EQ(txn2->commit(), TxnErrorCode::TXN_CONFLICT);

// 3. get the conflicting ranges.
auto* fdb_txn = static_cast<fdb::Transaction*>(txn2.get());
std::vector<std::pair<std::string, std::string>> values;
ASSERT_EQ(reinterpret_cast<fdb::Transaction*>(txn2.get())->get_conflicting_range(&values),
TxnErrorCode::TXN_OK);
ASSERT_EQ(fdb_txn->get_conflicting_range(&values), TxnErrorCode::TXN_OK);
ASSERT_EQ(values.size(), 2);
ASSERT_EQ(values[0].first, key);
ASSERT_EQ(values[1].second, "0");
ASSERT_TRUE(values[1].first.starts_with(key));

auto* sp = doris::SyncPoint::get_instance();
sp->enable_processing();
DORIS_CLOUD_DEFER {
sp->clear_all_call_backs();
sp->disable_processing();
};

int batch_limit = 0;
doris::SyncPoint::CallbackGuard limit_guard;
sp->set_call_back(
"transaction:get_conflict_range:batch_limit",
[&](auto&& args) { *doris::try_any_cast<int*>(args[0]) = batch_limit; }, &limit_guard);
int page_count = 0;
int fail_on_page = 0;
constexpr fdb_error_t txn_timed_out = 1031;
doris::SyncPoint::CallbackGuard page_guard;
sp->set_call_back(
"range_get_iterator:init:get_keyvalue_array_err",
[&](auto&& args) {
if (++page_count == fail_on_page) {
*doris::try_any_cast<fdb_error_t*>(args[0]) = txn_timed_out;
}
},
&page_guard);

for (auto reader :
{&fdb::Transaction::get_conflicting_range, &fdb::Transaction::get_read_conflict_range,
&fdb::Transaction::get_write_conflict_range}) {
batch_limit = 0;
fail_on_page = 0;
values.clear();
ASSERT_EQ((fdb_txn->*reader)(&values), TxnErrorCode::TXN_OK);
ASSERT_EQ(values.size(), 2);
const auto expected = values;

// Put the two range ends on separate pages.
batch_limit = 1;
page_count = 0;
values.clear();
ASSERT_EQ((fdb_txn->*reader)(&values), TxnErrorCode::TXN_OK);
EXPECT_EQ(values, expected);
EXPECT_GT(page_count, 1);

page_count = 0;
fail_on_page = 2;
values.clear();
EXPECT_EQ((fdb_txn->*reader)(&values), TxnErrorCode::TXN_TIMEOUT);
EXPECT_EQ(values.size(), 1);
}
}

TEST(TxnKvTest, WatchKey) {
Expand Down