Skip to content

Commit cc7cbaa

Browse files
committed
add test for btree tojson
1 parent 097fe33 commit cc7cbaa

File tree

5 files changed

+72
-84
lines changed

5 files changed

+72
-84
lines changed

.clang-tidy

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ CheckOptions:
4343
- key: readability-identifier-naming.StaticConstantPrefix
4444
value: S_
4545
- key: readability-identifier-naming.StaticVariableCase
46-
value: UPPER_CASE
46+
value: camelBack
4747
- key: readability-identifier-naming.StaticVariablePrefix
48-
value: S_
48+
value: s
4949
- key: readability-redundant-member-init.IgnoreBaseInCopyConstructors
5050
value: 1
5151
- key: modernize-use-default-member-init.UseAssignment

source/concurrency-recovery/ConcurrencyControl.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void ConcurrencyControl::refreshGlobalState() {
9696
Worker::sAllLwm.store(global_all_lwm_buffer, std::memory_order_release);
9797
Worker::sOltpLwm.store(global_oltp_lwm_buffer, std::memory_order_release);
9898
}
99-
// -------------------------------------------------------------------------------------
99+
100100
Worker::sGlobalMutex.unlock();
101101
}
102102
}
@@ -110,7 +110,7 @@ void ConcurrencyControl::switchToSnapshotIsolationMode() {
110110
}
111111
refreshGlobalState();
112112
}
113-
// -------------------------------------------------------------------------------------
113+
114114
void ConcurrencyControl::switchToReadCommittedMode() {
115115
u64 workerId = Worker::my().mWorkerId;
116116
{
@@ -194,7 +194,7 @@ ConcurrencyControl::VISIBILITY ConcurrencyControl::isVisibleForIt(
194194
? VISIBILITY::VISIBLE_ALREADY
195195
: VISIBILITY::VISIBLE_NEXT_ROUND;
196196
}
197-
// -------------------------------------------------------------------------------------
197+
198198
// UNDETERMINED is not possible atm because we spin on startTs
199199
ConcurrencyControl::VISIBILITY ConcurrencyControl::isVisibleForIt(
200200
WORKERID whom_worker_id, WORKERID what_worker_id, TXID tx_ts) {
@@ -209,8 +209,7 @@ TXID ConcurrencyControl::getCommitTimestamp(WORKERID workerId, TXID tx_ts) {
209209
if (tx_ts & MSB) {
210210
return tx_ts & MSB_MASK;
211211
}
212-
assert((tx_ts & MSB) || isVisibleForMe(workerId, tx_ts));
213-
// -------------------------------------------------------------------------------------
212+
DCHECK((tx_ts & MSB) || isVisibleForMe(workerId, tx_ts));
214213
const TXID& startTs = tx_ts;
215214
TXID lcb = other(workerId).commit_tree.LCB(startTs);
216215
TXID commitTs =

source/concurrency-recovery/Worker.hpp

+19-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ namespace cr {
2828

2929
static constexpr u16 STATIC_MAX_WORKERS = std::numeric_limits<WORKERID>::max();
3030

31-
/// @brief WalFlushReq is used to sync wal flush request to group commit thread
32-
/// for each worker thread.
31+
/// Used to sync wal flush request to group commit thread for each worker
32+
/// thread.
3333
struct WalFlushReq {
3434
/// Used for optimistic locking.
3535
u64 mVersion = 0;
@@ -44,6 +44,7 @@ struct WalFlushReq {
4444
TXID mPrevTxCommitTs = 0;
4545
};
4646

47+
/// Helps to transaction concurrenct control and write-ahead logging.
4748
class Logging {
4849
public:
4950
/// The minimum flushed GSN among all worker threads. Transactions whose max
@@ -60,7 +61,7 @@ class Logging {
6061

6162
public:
6263
static void UpdateMinFlushedGsn(LID minGsn) {
63-
assert(sMinFlushedGsn.load() <= minGsn);
64+
DCHECK(sMinFlushedGsn.load() <= minGsn);
6465
sMinFlushedGsn.store(minGsn, std::memory_order_release);
6566
}
6667

@@ -84,11 +85,15 @@ class Logging {
8485

8586
WALEntryComplex* mActiveWALEntryComplex;
8687

87-
// Shared between Group Committer and Worker
88+
/// Shared between Group Committer and Worker
8889
std::mutex mPreCommittedQueueMutex;
8990

91+
/// The queue for each worker thread to store pending-to-commit transactions
92+
/// which have remote dependencies.
9093
std::vector<Transaction> mPreCommittedQueue;
9194

95+
/// The queue for each worker thread to store pending-to-commit transactions
96+
/// which doesn't have any remote dependencies.
9297
std::vector<Transaction> mPreCommittedQueueRfa;
9398

9499
/// Represents the commit TS of the last transaction in the current worker,
@@ -256,15 +261,25 @@ class ConcurrencyControl {
256261
// Object members
257262
//-------------------------------------------------------------------------
258263
atomic<TXID> local_lwm_latch = 0;
264+
259265
atomic<TXID> oltp_lwm_receiver;
266+
260267
atomic<TXID> all_lwm_receiver;
268+
261269
atomic<TXID> mLatestWriteTx = 0;
270+
262271
atomic<TXID> mLatestLwm4Tx = 0;
272+
263273
TXID local_all_lwm;
274+
264275
TXID local_oltp_lwm;
276+
265277
TXID local_global_all_lwm_cache = 0;
278+
266279
unique_ptr<TXID[]> mLocalSnapshotCache; // = Readview
280+
267281
unique_ptr<TXID[]> local_snapshot_cache_ts;
282+
268283
unique_ptr<TXID[]> local_workers_start_ts;
269284

270285
// LeanStore NoSteal, Nothing for now

tests/BTreeVITest.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <gtest/gtest.h>
99

1010
#include <filesystem>
11+
#include <iostream>
1112

1213
namespace leanstore {
1314

@@ -138,4 +139,50 @@ TEST_F(BTreeVITest, BTreeVIInsertAndLookup) {
138139
});
139140
}
140141

142+
/*
143+
// TODO(jian.z): this test is fail when executed after other tests because the
144+
// global static variables are changed after each test.
145+
TEST_F(BTreeVITest, BTreeVIToJSON) {
146+
FLAGS_data_dir = "/tmp/BTreeVITest/BTreeVIToJSON";
147+
std::filesystem::path dirPath = FLAGS_data_dir;
148+
std::filesystem::remove_all(dirPath);
149+
std::filesystem::create_directories(dirPath);
150+
FLAGS_worker_threads = 2;
151+
FLAGS_recover = false;
152+
mLeanStore = std::make_unique<leanstore::LeanStore>();
153+
storage::btree::BTreeVI* btree;
154+
155+
// prepare key-value pairs to insert
156+
size_t numKVs(10);
157+
std::vector<std::tuple<std::string, std::string>> kvToTest;
158+
for (size_t i = 0; i < numKVs; ++i) {
159+
std::string key("key_btree_VI_xxxxxxxxxxxx_" + std::to_string(i));
160+
std::string val("VAL_BTREE_VI_YYYYYYYYYYYY_" + std::to_string(i));
161+
kvToTest.push_back(std::make_tuple(key, val));
162+
}
163+
164+
// create leanstore btree for table records
165+
const auto* btreeName = "testTree1";
166+
auto btreeConfig = leanstore::storage::btree::BTreeGeneric::Config{
167+
.mEnableWal = FLAGS_wal,
168+
.mUseBulkInsert = FLAGS_bulk_insert,
169+
};
170+
cr::CRManager::sInstance->scheduleJobSync(0, [&]() {
171+
EXPECT_TRUE(mLeanStore->RegisterBTreeVI(btreeName, btreeConfig, &btree));
172+
EXPECT_NE(btree, nullptr);
173+
174+
// insert some values
175+
for (size_t i = 0; i < numKVs; ++i) {
176+
const auto& [key, val] = kvToTest[i];
177+
EXPECT_EQ(btree->insert(Slice((const u8*)key.data(), key.size()),
178+
Slice((const u8*)val.data(), val.size())),
179+
OP_RESULT::OK);
180+
}
181+
182+
auto doc = leanstore::storage::btree::BTreeGeneric::ToJSON(*btree);
183+
std::cout << leanstore::utils::JsonToStr(&doc);
184+
});
185+
}
186+
*/
187+
141188
} // namespace leanstore

tests/LeanStoreTest.cpp

-73
Original file line numberDiff line numberDiff line change
@@ -30,79 +30,6 @@ class LeanStoreTest : public ::testing::Test {
3030
}
3131
};
3232

33-
TEST_F(LeanStoreTest, Serde) {
34-
FLAGS_data_dir = "/tmp/LeanStoreTest/Serde";
35-
std::filesystem::path dirPath = FLAGS_data_dir;
36-
std::filesystem::remove_all(dirPath);
37-
std::filesystem::create_directories(dirPath);
38-
39-
dirPath = leanstore::GetLogDir();
40-
std::filesystem::remove_all(dirPath);
41-
std::filesystem::create_directories(dirPath);
42-
43-
FLAGS_worker_threads = 2;
44-
FLAGS_recover = false;
45-
mLeanStore = std::make_unique<leanstore::LeanStore>();
46-
storage::btree::BTreeVI* btree;
47-
48-
// prepare key-value pairs to insert
49-
size_t numKVs(1);
50-
std::vector<std::tuple<std::string, std::string>> kvToTest;
51-
for (size_t i = 0; i < numKVs; ++i) {
52-
std::string key("key_xxxxxxxxxxxx_" + std::to_string(i));
53-
std::string val("VAL_YYYYYYYYYYYY_" + std::to_string(i));
54-
kvToTest.push_back(std::make_tuple(key, val));
55-
}
56-
57-
// create btree for table records
58-
const auto* btreeName = "testTree1";
59-
auto btreeConfig = leanstore::storage::btree::BTreeGeneric::Config{
60-
.mEnableWal = FLAGS_wal,
61-
.mUseBulkInsert = FLAGS_bulk_insert,
62-
};
63-
64-
// TODO(jian.z): need to create btree within a transaction, otherwise
65-
// transactions depend on the btree creator worker may hang on commit.
66-
cr::CRManager::sInstance->scheduleJobSync(0, [&]() {
67-
EXPECT_TRUE(mLeanStore->RegisterBTreeVI(btreeName, btreeConfig, &btree));
68-
EXPECT_NE(btree, nullptr);
69-
});
70-
71-
// insert some values
72-
cr::CRManager::sInstance->scheduleJobSync(0, [&]() {
73-
for (size_t i = 0; i < numKVs; ++i) {
74-
const auto& [key, val] = kvToTest[i];
75-
EXPECT_EQ(btree->insert(Slice((const u8*)key.data(), key.size()),
76-
Slice((const u8*)val.data(), val.size())),
77-
OP_RESULT::OK);
78-
}
79-
});
80-
81-
// meta file should be serialized during destructor.
82-
mLeanStore.reset(nullptr);
83-
FLAGS_recover = true;
84-
85-
// recreate the store, it's expected that all the meta and pages are rebult.
86-
mLeanStore = std::make_unique<leanstore::LeanStore>();
87-
EXPECT_TRUE(mLeanStore->GetBTreeVI(btreeName, &btree));
88-
EXPECT_NE(btree, nullptr);
89-
90-
// lookup the restored btree
91-
cr::CRManager::sInstance->scheduleJobSync(0, [&]() {
92-
std::string copiedValue;
93-
auto copyValueOut = [&](Slice val) {
94-
copiedValue = std::string((const char*)val.data(), val.size());
95-
};
96-
for (size_t i = 0; i < numKVs; ++i) {
97-
const auto& [key, expectedVal] = kvToTest[i];
98-
EXPECT_EQ(
99-
btree->Lookup(Slice((const u8*)key.data(), key.size()), copyValueOut),
100-
OP_RESULT::OK);
101-
EXPECT_EQ(copiedValue, expectedVal);
102-
}
103-
});
104-
}
105-
10633
/*
10734
TEST_F(LeanStoreTest, RecoverAfterInsert) {
10835
FLAGS_data_dir = "/tmp/LeanStoreTest/RecoverAfterInsert";

0 commit comments

Comments
 (0)