Skip to content

Commit 92fa470

Browse files
committed
Abstract KV layer options to eliminate dynamic casts to FDBTransaction
1 parent 49e0ad5 commit 92fa470

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

src/common/kv/ITransaction.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ static constexpr std::string_view kMetadataVersionKey = "\xff/metadataVersion";
3131
using Versionstamp = std::array<uint8_t, 10>;
3232
static_assert(sizeof(Versionstamp) == 10);
3333

34+
enum class Priority : uint8_t {
35+
MIN = 0,
36+
LOW = 1,
37+
HIGH = 2,
38+
MAX = 3,
39+
};
40+
3441
class IReadOnlyTransaction {
3542
public:
3643
virtual ~IReadOnlyTransaction() = default;
@@ -83,6 +90,8 @@ class IReadOnlyTransaction {
8390

8491
virtual CoTryTask<void> cancel() = 0;
8592
virtual void reset() = 0;
93+
94+
virtual Result<Void> enableStaleRead() = 0;
8695
};
8796

8897
class IReadWriteTransaction : public IReadOnlyTransaction {
@@ -109,6 +118,8 @@ class IReadWriteTransaction : public IReadOnlyTransaction {
109118

110119
virtual CoTryTask<void> commit() = 0;
111120
virtual int64_t getCommittedVersion() = 0;
121+
122+
virtual Result<Void> setPriority(Priority priority) = 0;
112123
};
113124

114125
struct TransactionHelper {

src/common/kv/mem/MemTransaction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <algorithm>
4+
#include <boost/core/ignore_unused.hpp>
45
#include <cassert>
56
#include <folly/Likely.h>
67
#include <folly/Synchronized.h>
@@ -245,6 +246,12 @@ class MemTransaction : public IReadWriteTransaction {
245246
writeConflicts_.clear();
246247
}
247248

249+
Result<Void> enableStaleRead() override { return Void{}; }
250+
Result<Void> setPriority(Priority priority) override {
251+
boost::ignore_unused(priority);
252+
return Void{};
253+
}
254+
248255
// check txn1 updated txn2's read set
249256
static bool checkConflict(MemTransaction &txn1, MemTransaction &txn2) {
250257
std::scoped_lock<std::mutex> guard1(txn1.mutex_);

src/fdb/FDBTransaction.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,22 @@ void FDBTransaction::reset() {
392392
tr_.reset();
393393
}
394394

395+
Result<Void> FDBTransaction::enableStaleRead() {
396+
return setOption(FDBTransactionOption::FDB_TR_OPTION_USE_GRV_CACHE, {});
397+
}
398+
399+
Result<Void> FDBTransaction::setPriority(Priority priority) {
400+
assert(priority > PRIORITY::MIN && priority < PRIORITY::MAX);
401+
switch (priority) {
402+
case Priority::LOW: {
403+
return setOption(FDBTransactionOption::FDB_TR_OPTION_PRIORITY_BATCH, {});
404+
} break;
405+
default: {
406+
return Void{};
407+
} break;
408+
}
409+
}
410+
395411
Result<Void> FDBTransaction::setOption(FDBTransactionOption option, std::string_view value) {
396412
fdb_error_t errcode = tr_.setOption(option, value);
397413
if (errcode != 0) {

src/fdb/FDBTransaction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class FDBTransaction : public IReadWriteTransaction {
4040
CoTryTask<void> commit() override;
4141
void reset() override;
4242

43+
Result<Void> enableStaleRead() override;
44+
Result<Void> setPriority(Priority priority) override;
4345
Result<Void> setOption(FDBTransactionOption option, std::string_view value = {});
4446
CoTask<bool> onError(fdb_error_t errcode);
4547

src/meta/components/GcManager.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,8 @@ CoTryTask<void> GcManager::GcTask::gcDirectory(GcManager &manager) {
306306
auto finished = false;
307307
auto checked = false;
308308
auto handler = [&](IReadWriteTransaction &txn) -> CoTryTask<void> {
309-
auto fdbTxn = dynamic_cast<kv::FDBTransaction *>(&txn);
310-
if (fdbTxn && manager.config_.gc().txn_low_priority()) {
311-
fdbTxn->setOption(FDBTransactionOption::FDB_TR_OPTION_PRIORITY_BATCH, {});
309+
if (manager.config_.gc().txn_low_priority()) {
310+
txn.setPriority(kv::Priority::LOW);
312311
}
313312
if (!checked) {
314313
auto loadInode = co_await Inode::load(txn, taskEntry.id);
@@ -375,9 +374,8 @@ CoTryTask<void> GcManager::GcTask::gcFile(GcManager &manager) {
375374
XLOGF(DBG, "Gc file {}", taskEntry.id);
376375

377376
auto load = co_await manager.runReadOnly([&](auto &txn) -> CoTryTask<std::optional<Inode>> {
378-
auto fdbTxn = dynamic_cast<kv::FDBTransaction *>(&txn);
379-
if (fdbTxn && manager.config_.gc().txn_low_priority()) {
380-
fdbTxn->setOption(FDBTransactionOption::FDB_TR_OPTION_PRIORITY_BATCH, {});
377+
if (manager.config_.gc().txn_low_priority()) {
378+
txn->setPriority(kv::Priority::LOW);
381379
}
382380
if (!manager.config_.gc().check_session()) {
383381
co_return co_await Inode::snapshotLoad(txn, taskEntry.id);

src/meta/store/Operation.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ class OperationDriver {
168168
}
169169

170170
auto grvCache = operation_.isReadOnly() && enableGrvCache;
171-
if (grvCache && dynamic_cast<kv::FDBTransaction *>(txn.get())) {
172-
auto fdbTxn = dynamic_cast<kv::FDBTransaction *>(txn.get());
173-
CO_RETURN_ON_ERROR(fdbTxn->setOption(FDBTransactionOption::FDB_TR_OPTION_USE_GRV_CACHE, {}));
171+
if (grvCache) {
172+
CO_RETURN_ON_ERROR(txn->enableStaleRead());
174173
}
175174

176175
Result<Rsp> result = makeError(MetaCode::kOperationTimeout);

tests/common/TestWithTransaction.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <boost/core/ignore_unused.hpp>
12
#include <folly/experimental/coro/BlockingWait.h>
23
#include <gtest/gtest.h>
34
#include <string_view>
@@ -65,6 +66,8 @@ class MockROTxn : public IReadOnlyTransaction {
6566
void reset() override {}
6667

6768
void setReadVersion(int64_t) override {}
69+
70+
Result<Void> enableStaleRead() override { return Void{}; }
6871
};
6972

7073
class MockRWTxn : public IReadWriteTransaction {
@@ -113,6 +116,12 @@ class MockRWTxn : public IReadWriteTransaction {
113116

114117
void reset() override {}
115118

119+
Result<Void> enableStaleRead() override { return Void{}; }
120+
Result<Void> setPriority(Priority priority) override {
121+
boost::ignore_unused(priority);
122+
return Void{};
123+
}
124+
116125
private:
117126
OpResultSeq &commitSeq_;
118127
};

0 commit comments

Comments
 (0)