Skip to content

Commit

Permalink
chore: expose StoreOption to c API
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zhang <[email protected]>
  • Loading branch information
zz-jason committed Aug 17, 2024
1 parent d69d7dc commit 158925f
Show file tree
Hide file tree
Showing 50 changed files with 494 additions and 364 deletions.
12 changes: 6 additions & 6 deletions benchmarks/micro-benchmarks/InsertUpdateBench.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "leanstore/LeanStore.hpp"
#include "leanstore/StoreOption.hpp"
#include "leanstore/btree/BasicKV.hpp"
#include "leanstore/btree/TransactionKV.hpp"
#include "leanstore/buffer-manager/BufferManager.hpp"
#include "leanstore/c/StoreOption.h"
#include "leanstore/concurrency/CRManager.hpp"
#include "leanstore/utils/RandomGenerator.hpp"

Expand All @@ -21,11 +21,11 @@ static void BenchUpdateInsert(benchmark::State& state) {
std::filesystem::path dirPath = "/tmp/InsertUpdateBench";
std::filesystem::remove_all(dirPath);
std::filesystem::create_directories(dirPath);
auto sLeanStore = std::make_unique<leanstore::LeanStore>(StoreOption{
.mCreateFromScratch = true,
.mStoreDir = "/tmp/InsertUpdateBench",
.mWorkerThreads = 4,
});

StoreOption* option = CreateStoreOption("/tmp/InsertUpdateBench");
option->mCreateFromScratch = true;
option->mWorkerThreads = 4;
auto sLeanStore = std::make_unique<leanstore::LeanStore>(option);

storage::btree::TransactionKV* btree;

Expand Down
30 changes: 16 additions & 14 deletions benchmarks/ycsb/YcsbLeanStore.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "Ycsb.hpp"
#include "leanstore/KVInterface.hpp"
#include "leanstore/LeanStore.hpp"
#include "leanstore/StoreOption.hpp"
#include "leanstore/btree/BasicKV.hpp"
#include "leanstore/btree/TransactionKV.hpp"
#include "leanstore/c/StoreOption.h"
#include "leanstore/concurrency/CRManager.hpp"
#include "leanstore/concurrency/Worker.hpp"
#include "leanstore/utils/Defer.hpp"
Expand Down Expand Up @@ -38,16 +38,18 @@ class YcsbLeanStore : public YcsbExecutor {
public:
YcsbLeanStore(bool benchTransactionKv, bool createFromScratch)
: mBenchTransactionKv(benchTransactionKv) {
auto res = LeanStore::Open(StoreOption{
.mCreateFromScratch = createFromScratch,
.mStoreDir = FLAGS_ycsb_data_dir + "/leanstore",
.mWorkerThreads = FLAGS_ycsb_threads,
.mBufferPoolSize = FLAGS_ycsb_mem_kb * 1024,
.mEnableMetrics = true,
.mMetricsPort = 8080,
});
auto dataDirStr = FLAGS_ycsb_data_dir + std::string("/leanstore");
StoreOption* option = CreateStoreOption(dataDirStr.c_str());
option->mCreateFromScratch = createFromScratch;
option->mWorkerThreads = FLAGS_ycsb_threads;
option->mBufferPoolSize = FLAGS_ycsb_mem_kb * 1024;
option->mEnableMetrics = true;
option->mMetricsPort = 8080;

auto res = LeanStore::Open(option);
if (!res) {
std::cerr << "Failed to open leanstore: " << res.error().ToString() << std::endl;
DestroyStoreOption(option);
exit(res.error().Code());
}

Expand Down Expand Up @@ -112,7 +114,7 @@ class YcsbLeanStore : public YcsbExecutor {
std::cout << summary << std::endl;
});

auto numWorkers = mStore->mStoreOption.mWorkerThreads;
auto numWorkers = mStore->mStoreOption->mWorkerThreads;
auto avg = FLAGS_ycsb_record_count / numWorkers;
auto rem = FLAGS_ycsb_record_count % numWorkers;
for (auto workerId = 0u, begin = 0u; workerId < numWorkers;) {
Expand Down Expand Up @@ -151,8 +153,8 @@ class YcsbLeanStore : public YcsbExecutor {
auto zipfRandom =
utils::ScrambledZipfGenerator(0, FLAGS_ycsb_record_count, FLAGS_ycsb_zipf_factor);
std::atomic<bool> keepRunning = true;
std::vector<std::atomic<uint64_t>> threadCommitted(mStore->mStoreOption.mWorkerThreads);
std::vector<std::atomic<uint64_t>> threadAborted(mStore->mStoreOption.mWorkerThreads);
std::vector<std::atomic<uint64_t>> threadCommitted(mStore->mStoreOption->mWorkerThreads);
std::vector<std::atomic<uint64_t>> threadAborted(mStore->mStoreOption->mWorkerThreads);
// init counters
for (auto& c : threadCommitted) {
c = 0;
Expand All @@ -161,7 +163,7 @@ class YcsbLeanStore : public YcsbExecutor {
a = 0;
}

for (uint64_t workerId = 0; workerId < mStore->mStoreOption.mWorkerThreads; workerId++) {
for (uint64_t workerId = 0; workerId < mStore->mStoreOption->mWorkerThreads; workerId++) {
mStore->ExecAsync(workerId, [&]() {
uint8_t key[FLAGS_ycsb_key_size];
std::string valRead;
Expand Down Expand Up @@ -236,7 +238,7 @@ class YcsbLeanStore : public YcsbExecutor {
a = 0;
}

printTpsSummary(1, FLAGS_ycsb_run_for_seconds, mStore->mStoreOption.mWorkerThreads,
printTpsSummary(1, FLAGS_ycsb_run_for_seconds, mStore->mStoreOption->mWorkerThreads,
threadCommitted, threadAborted);

// Shutdown threads
Expand Down
20 changes: 16 additions & 4 deletions examples/c/BasicKvExample.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
#include "leanstore/c/StoreOption.h"
#include "leanstore/c/leanstore-c.h"

#include <cstddef>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "leanstore/leanstore-c.h"

int main() {
LeanStoreHandle* storeHandle =
CreateLeanStore(1, "/tmp/leanstore/examples/BasicKvExample", 2, 0, 1);
struct StoreOption* option = CreateStoreOption("/tmp/leanstore/examples/BasicKvExample");
option->mCreateFromScratch = 1;
option->mWorkerThreads = 2;
option->mEnableBulkInsert = 0;
option->mEnableEagerGc = 1;
LeanStoreHandle* storeHandle = CreateLeanStore(option);
BasicKvHandle* kvHandle = CreateBasicKV(storeHandle, 0, "testTree1");
if (kvHandle == NULL) {
DestroyStoreOption(option);
printf("create basic kv failed\n");
return -1;
}

// key-value pair 1
StringSlice keySlice;
Expand Down
18 changes: 10 additions & 8 deletions examples/cpp/BasicKvExample.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#include <leanstore/LeanStore.hpp>
#include <leanstore/btree/BasicKV.hpp>
#include <leanstore/c/StoreOption.h>
#include <leanstore/concurrency/Worker.hpp>

#include <iostream>
#include <memory>

using leanstore::LeanStore;
using leanstore::StoreOption;

int main() {
// create store option
StoreOption* option = CreateStoreOption("/tmp/leanstore/examples/BasicKvExample");
option->mCreateFromScratch = true;
option->mWorkerThreads = 2;
option->mEnableBulkInsert = false;
option->mEnableEagerGc = true;

// create store
auto res = LeanStore::Open(StoreOption{
.mCreateFromScratch = true,
.mStoreDir = "/tmp/leanstore/examples/BasicKvExample",
.mWorkerThreads = 2,
.mEnableBulkInsert = false,
.mEnableEagerGc = true,
});
auto res = LeanStore::Open(option);
if (!res) {
DestroyStoreOption(option);
std::cerr << "open store failed: " << res.error().ToString() << std::endl;
return 1;
}
Expand Down
38 changes: 28 additions & 10 deletions include/leanstore/LeanStore.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "leanstore/StoreOption.hpp"
#include "leanstore/Units.hpp"
#include "leanstore/c/StoreOption.h"
#include "leanstore/utils/DebugFlags.hpp"
#include "leanstore/utils/Result.hpp"

Expand All @@ -10,6 +10,7 @@
#include <expected>
#include <functional>
#include <memory>
#include <string>

//! forward declarations
namespace leanstore::telemetry {
Expand Down Expand Up @@ -47,10 +48,13 @@ namespace leanstore {

class LeanStore {
public:
static Result<std::unique_ptr<LeanStore>> Open(StoreOption option = StoreOption{});
//! Opens a LeanStore instance with the provided options.
//! NOTE: The option is created by LeanStore user, its ownership is transferred to the LeanStore
//! instance after the call, it will be destroyed when the LeanStore instance is destroyed.
static Result<std::unique_ptr<LeanStore>> Open(StoreOption* option);

//! The storage option for leanstore
StoreOption mStoreOption;
const StoreOption* mStoreOption;

//! The file descriptor for pages
int32_t mPageFd;
Expand Down Expand Up @@ -79,14 +83,17 @@ class LeanStore {
utils::DebugFlagsRegistry mDebugFlagsRegistry;
#endif

LeanStore(StoreOption option = StoreOption{});
//! The LeanStore constructor
//! NOTE: The option is created by LeanStore user, its ownership is transferred to the LeanStore
//! instance after the call, it will be destroyed when the LeanStore instance is destroyed.
LeanStore(StoreOption* option);

//! The LeanStore destructor
~LeanStore();

//! Create a BasicKV
Result<storage::btree::BasicKV*> CreateBasicKV(const std::string& name,
BTreeConfig config = BTreeConfig{
.mEnableWal = true, .mUseBulkInsert = false});
Result<leanstore::storage::btree::BasicKV*> CreateBasicKV(
const std::string& name, BTreeConfig config = kDefaultBTreeConfig);

//! Get a registered BasicKV
void GetBasicKV(const std::string& name, storage::btree::BasicKV** btree);
Expand All @@ -95,9 +102,8 @@ class LeanStore {
void DropBasicKV(const std::string& name);

//! Register a TransactionKV
Result<storage::btree::TransactionKV*> CreateTransactionKV(
const std::string& name,
BTreeConfig config = BTreeConfig{.mEnableWal = true, .mUseBulkInsert = false});
Result<leanstore::storage::btree::TransactionKV*> CreateTransactionKV(
const std::string& name, BTreeConfig config = kDefaultBTreeConfig);

//! Get a registered TransactionKV
void GetTransactionKV(const std::string& name, storage::btree::TransactionKV** btree);
Expand Down Expand Up @@ -126,6 +132,18 @@ class LeanStore {
//! Waits for all Workers to complete.
void WaitAll();

std::string GetMetaFilePath() const {
return std::string(mStoreOption->mStoreDir) + "/db.meta.json";
}

std::string GetDbFilePath() const {
return std::string(mStoreOption->mStoreDir) + "/db.pages";
}

std::string GetWalFilePath() const {
return std::string(mStoreOption->mStoreDir) + "/db.wal";
}

private:
//! serializeMeta serializes all the metadata about concurrent resources,
//! buffer manager, btrees, and flags
Expand Down
2 changes: 1 addition & 1 deletion include/leanstore/btree/ChainedTuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class __attribute__((packed)) ChainedTuple : public Tuple {
bool commandValid = mCommandId != kInvalidCommandid;
bool hasLongRunningOLAP =
cr::Worker::My().mStore->mCRManager->mGlobalWmkInfo.HasActiveLongRunningTx();
bool frequentlyUpdated = mTotalUpdates > cr::Worker::My().mStore->mStoreOption.mWorkerThreads;
bool frequentlyUpdated = mTotalUpdates > cr::Worker::My().mStore->mStoreOption->mWorkerThreads;
bool recentUpdatedByOthers =
mWorkerId != cr::Worker::My().mWorkerId || mTxId != cr::ActiveTx().mStartTs;
return commandValid && hasLongRunningOLAP && recentUpdatedByOthers && frequentlyUpdated;
Expand Down
2 changes: 1 addition & 1 deletion include/leanstore/btree/TransactionKV.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class TransactionKV : public BasicKV {
}

inline static uint64_t ConvertToFatTupleThreshold() {
return cr::Worker::My().mStore->mStoreOption.mWorkerThreads;
return cr::Worker::My().mStore->mStoreOption->mWorkerThreads;
}

//! Updates the value stored in FatTuple. The former newest version value is
Expand Down
4 changes: 2 additions & 2 deletions include/leanstore/btree/core/BTreeGeneric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ inline void BTreeGeneric::IterateChildSwips(BufferFrame& bf, std::function<bool(
}

inline SpaceCheckResult BTreeGeneric::CheckSpaceUtilization(BufferFrame& bf) {
if (!mStore->mStoreOption.mEnableXMerge) {
if (!mStore->mStoreOption->mEnableXMerge) {
return SpaceCheckResult::kNothing;
}

Expand All @@ -262,7 +262,7 @@ inline SpaceCheckResult BTreeGeneric::CheckSpaceUtilization(BufferFrame& bf) {
}

inline void BTreeGeneric::Checkpoint(BufferFrame& bf, void* dest) {
std::memcpy(dest, &bf.mPage, mStore->mStoreOption.mPageSize);
std::memcpy(dest, &bf.mPage, mStore->mStoreOption->mPageSize);
auto* destPage = reinterpret_cast<Page*>(dest);
auto* destNode = reinterpret_cast<BTreeNode*>(destPage->mPayload);

Expand Down
4 changes: 2 additions & 2 deletions include/leanstore/btree/core/BTreeNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class BTreeNode : public BTreeNodeHeader {
}

static uint16_t Size() {
return static_cast<uint16_t>(utils::tlsStore->mStoreOption.mPageSize - sizeof(Page));
return static_cast<uint16_t>(utils::tlsStore->mStoreOption->mPageSize - sizeof(Page));
}

static uint16_t UnderFullSize() {
Expand Down Expand Up @@ -528,7 +528,7 @@ int16_t BTreeNode::LowerBound(Slice key, bool* isEqual) {
SearchHint(keyHead, lower, upper);
while (lower < upper) {
bool foundEqual(false);
if (utils::tlsStore->mStoreOption.mEnableHeadOptimization) {
if (utils::tlsStore->mStoreOption->mEnableHeadOptimization) {
foundEqual = shrinkSearchRangeWithHead(lower, upper, key, keyHead);
} else {
foundEqual = shrinkSearchRange(lower, upper, key);
Expand Down
8 changes: 4 additions & 4 deletions include/leanstore/btree/core/PessimisticExclusiveIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ class PessimisticExclusiveIterator : public PessimisticIterator {

//! Updates contention statistics after each slot modification on the page.
virtual void UpdateContentionStats() {
if (!utils::tlsStore->mStoreOption.mEnableContentionSplit) {
if (!utils::tlsStore->mStoreOption->mEnableContentionSplit) {
return;
}
const uint64_t randomNumber = utils::RandomGenerator::RandU64();

// haven't met the contention stats update probability
if ((randomNumber &
((1ull << utils::tlsStore->mStoreOption.mContentionSplitSampleProbability) - 1)) != 0) {
((1ull << utils::tlsStore->mStoreOption->mContentionSplitSampleProbability) - 1)) != 0) {
return;
}
auto& contentionStats = mGuardedLeaf.mBf->mHeader.mContentionStats;
Expand All @@ -148,14 +148,14 @@ class PessimisticExclusiveIterator : public PessimisticIterator {
mGuardedLeaf.mBf->mHeader.mPageId, mSlotId, mGuardedLeaf.EncounteredContention());

// haven't met the contention split validation probability
if ((randomNumber & ((1ull << utils::tlsStore->mStoreOption.mContentionSplitProbility) - 1)) !=
if ((randomNumber & ((1ull << utils::tlsStore->mStoreOption->mContentionSplitProbility) - 1)) !=
0) {
return;
}
auto contentionPct = contentionStats.ContentionPercentage();
contentionStats.Reset();
if (lastUpdatedSlot != mSlotId &&
contentionPct >= utils::tlsStore->mStoreOption.mContentionSplitThresholdPct &&
contentionPct >= utils::tlsStore->mStoreOption->mContentionSplitThresholdPct &&
mGuardedLeaf->mNumSeps > 2) {
int16_t splitSlot = std::min<int16_t>(lastUpdatedSlot, mSlotId);
mGuardedLeaf.unlock();
Expand Down
4 changes: 2 additions & 2 deletions include/leanstore/btree/core/PessimisticIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ inline void PessimisticIterator::Next() {
mFuncCleanUp = nullptr;
}

if (utils::tlsStore->mStoreOption.mEnableOptimisticScan && mLeafPosInParent != -1) {
if (utils::tlsStore->mStoreOption->mEnableOptimisticScan && mLeafPosInParent != -1) {
JUMPMU_TRY() {
if ((mLeafPosInParent + 1) <= mGuardedParent->mNumSeps) {
int32_t nextLeafPos = mLeafPosInParent + 1;
Expand Down Expand Up @@ -463,7 +463,7 @@ inline void PessimisticIterator::Prev() {
mFuncCleanUp = nullptr;
}

if (utils::tlsStore->mStoreOption.mEnableOptimisticScan && mLeafPosInParent != -1) {
if (utils::tlsStore->mStoreOption->mEnableOptimisticScan && mLeafPosInParent != -1) {
JUMPMU_TRY() {
if ((mLeafPosInParent - 1) >= 0) {
int32_t nextLeafPos = mLeafPosInParent - 1;
Expand Down
2 changes: 1 addition & 1 deletion include/leanstore/buffer-manager/BufferFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Page {

public:
uint64_t CRC() {
return utils::CRC(mPayload, utils::tlsStore->mStoreOption.mPageSize - sizeof(Page));
return utils::CRC(mPayload, utils::tlsStore->mStoreOption->mPageSize - sizeof(Page));
}
};

Expand Down
4 changes: 2 additions & 2 deletions include/leanstore/buffer-manager/BufferManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class BufferManager {
//! Randomly pick a buffer frame.
BufferFrame& RandomBufferFrame() {
auto bfId = utils::RandomGenerator::Rand<uint64_t>(0, mNumBfs);
auto* bfAddr = &mBufferPool[bfId * mStore->mStoreOption.mBufferFrameSize];
auto* bfAddr = &mBufferPool[bfId * mStore->mStoreOption->mBufferFrameSize];
return *reinterpret_cast<BufferFrame*>(bfAddr);
}

Expand Down Expand Up @@ -155,7 +155,7 @@ class BufferManager {
private:
Result<void> writePage(PID pageId, void* buffer) {
utils::AsyncIo aio(1);
const auto pageSize = mStore->mStoreOption.mPageSize;
const auto pageSize = mStore->mStoreOption->mPageSize;
DEBUG_BLOCK() {
auto* page [[maybe_unused]] = reinterpret_cast<Page*>(buffer);
LS_DLOG("page write, pageId={}, btreeId={}", pageId, page->mBTreeId);
Expand Down
Loading

0 comments on commit 158925f

Please sign in to comment.