Skip to content

Commit

Permalink
Lift tests so they can be reused by homestore implementation once wor…
Browse files Browse the repository at this point in the history
…king.
  • Loading branch information
szmyd committed Sep 26, 2023
1 parent c262887 commit b001c94
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 516 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ ignore:
- "**/*_generated.h"
- "src/mocks/**"
- "src/lib/homestore/tests/**"
- "src/lib/memory/tests/**"
- "src/lib/tests/**"
3 changes: 3 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ target_link_libraries(${PROJECT_NAME}_core
${COMMON_DEPS}
)

if(BUILD_TESTING)
add_subdirectory(tests)
endif()

add_subdirectory(homestore)
add_subdirectory(memory)
24 changes: 23 additions & 1 deletion src/lib/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,27 @@ target_link_libraries("${PROJECT_NAME}_memory"
)

if(BUILD_TESTING)
add_subdirectory(tests)
add_executable (pg_memory_test)
target_sources(pg_memory_test PRIVATE $<TARGET_OBJECTS:pg_test>)
target_link_libraries(pg_memory_test
homeobject_memory
${COMMON_TEST_DEPS}
)
add_test(NAME PGMemoryTest COMMAND pg_memory_test -csv error)

add_executable (shard_memory_test)
target_sources(shard_memory_test PRIVATE $<TARGET_OBJECTS:shard_test>)
target_link_libraries(shard_memory_test
homeobject_memory
${COMMON_TEST_DEPS}
)
add_test(NAME ShardMemoryTest COMMAND shard_memory_test -csv error)

add_executable (blob_memory_test)
target_sources(blob_memory_test PRIVATE $<TARGET_OBJECTS:blob_test>)
target_link_libraries(blob_memory_test
homeobject_memory
${COMMON_TEST_DEPS}
)
add_test(NAME BlobMemoryTest COMMAND blob_memory_test -csv error)
endif()
90 changes: 34 additions & 56 deletions src/lib/memory/blob_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,49 @@

namespace homeobject {

ShardIndex& MemoryHomeObject::_find_index(shard_id shard) const {
auto index_it = _in_memory_index.find(shard);
RELEASE_ASSERT(_in_memory_index.end() != index_it, "Missing BTree!!");
return *index_it->second;
}

BlobManager::Result< blob_id > MemoryHomeObject::_put_blob(ShardInfo const& shard, Blob&& blob) {
// Lookup Shard Index (BTree and SSN)
auto& our_shard = _find_index(shard.id);

// Generate BlobID (with RAFT this will happen implicitly) and Route
auto const route = BlobRoute{shard.id, our_shard._shard_seq_num++};
LOGTRACEMOD(homeobject, "Writing Blob {}", route);

// Write (move) Blob to Heap
auto new_blob = BlobExt();
new_blob._state = BlobState::ALIVE;
new_blob._blob = new Blob(std::move(blob));
LOGDEBUGMOD(homeobject, "Wrote BLOB {} to: BlkId:[{}]", route, fmt::ptr(new_blob._blob));

// Insert BlobExt to Index
auto [_, happened] = our_shard._btree.try_emplace(route, std::move(new_blob));
#define WITH_SHARD \
auto index_it = index_.find(_shard.id); \
RELEASE_ASSERT(index_.end() != index_it, "Missing BTree!!"); \
auto& shard = *index_it->second;

#define WITH_ROUTE(blob) \
auto const route = BlobRoute{_shard.id, (blob)}; \
LOGTRACEMOD(homeobject, "[route={}]", route);

#define IF_BLOB_ALIVE \
if (auto blob_it = shard.btree_.find(route); shard.btree_.end() == blob_it || !blob_it->second) { \
LOGWARNMOD(homeobject, "[route={}] missing", route); \
return folly::makeUnexpected(BlobError::UNKNOWN_BLOB); \
} else

// Write (move) Blob to new BlobExt on heap and Insert BlobExt to Index
BlobManager::Result< blob_id > MemoryHomeObject::_put_blob(ShardInfo const& _shard, Blob&& _blob) {
WITH_SHARD
WITH_ROUTE(shard.shard_seq_num_++)

auto [_, happened] =
shard.btree_.try_emplace(route, BlobExt{.state_ = BlobState::ALIVE, .blob_ = new Blob(std::move(_blob))});
RELEASE_ASSERT(happened, "Generated duplicate BlobRoute!");
return route.blob;
}

BlobManager::Result< Blob > MemoryHomeObject::_get_blob(ShardInfo const& shard, blob_id blob) const {
// Lookup Shard Index (BTree and SSN)
auto& our_shard = _find_index(shard.id);

// Find BlobExt by BlobRoute
auto const route = BlobRoute(shard.id, blob);

// Calculate BlobRoute from ShardInfo (use ordinal?)
LOGTRACEMOD(homeobject, "Looking up Blob {}", route);
auto blob_it = our_shard._btree.find(route);
if (our_shard._btree.end() == blob_it || !blob_it->second) {
LOGWARNMOD(homeobject, "Blob [{}] missing during get", route);
return folly::makeUnexpected(BlobError::UNKNOWN_BLOB);
}

// Duplicate underyling Blob for user
// This is only *safe* because we defer GC.
auto& ext_blob = blob_it->second;
RELEASE_ASSERT(ext_blob._blob != nullptr, "Blob Deleted!");
return ext_blob._blob->clone();
// Lookup BlobExt and duplicate underyling Blob for user; only *safe* because we defer GC.
BlobManager::Result< Blob > MemoryHomeObject::_get_blob(ShardInfo const& _shard, blob_id _blob) const {
WITH_SHARD
WITH_ROUTE(_blob)
IF_BLOB_ALIVE { return blob_it->second.blob_->clone(); }
}

BlobManager::NullResult MemoryHomeObject::_del_blob(ShardInfo const& shard, blob_id id) {
// Lookup Shard Index (BTree and SSN)
auto& our_shard = _find_index(shard.id);

// Calculate BlobRoute from ShardInfo (use ordinal?)
auto const route = BlobRoute(shard.id, id);

// Tombstone BlobExt entry
LOGTRACEMOD(homeobject, "Tombstoning Blob {}", route);
if (auto blob_it = our_shard._btree.find(route); our_shard._btree.end() != blob_it && blob_it->second) {
// Tombstone BlobExt entry
BlobManager::NullResult MemoryHomeObject::_del_blob(ShardInfo const& _shard, blob_id _blob) {
WITH_SHARD
WITH_ROUTE(_blob)
IF_BLOB_ALIVE {
auto del_blob = BlobExt();
del_blob._blob = blob_it->second._blob;
our_shard._btree.assign_if_equal(route, blob_it->second, std::move(del_blob));
del_blob.blob_ = blob_it->second.blob_;
shard.btree_.assign_if_equal(route, blob_it->second, std::move(del_blob));
return folly::Unit();
}
LOGWARNMOD(homeobject, "Blob [{}] missing during del", route);
return folly::makeUnexpected(BlobError::UNKNOWN_BLOB);
}

} // namespace homeobject
4 changes: 2 additions & 2 deletions src/lib/memory/homeobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void HomeObjectImpl::init_repl_svc() {
}

ShardIndex::~ShardIndex() {
for (auto it = _btree.begin(); it != _btree.end(); ++it) {
delete it->second._blob;
for (auto it = btree_.begin(); it != btree_.end(); ++it) {
delete it->second.blob_;
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/lib/memory/homeobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ namespace homeobject {
ENUM(BlobState, uint8_t, ALIVE = 0, DELETED);

struct BlobExt {
BlobState _state{BlobState::DELETED};
Blob* _blob;
BlobState state_{BlobState::DELETED};
Blob* blob_;

explicit operator bool() const { return _state == BlobState::ALIVE; }
bool operator==(const BlobExt& rhs) const { return _blob == rhs._blob; }
explicit operator bool() const { return state_ == BlobState::ALIVE; }
bool operator==(const BlobExt& rhs) const { return blob_ == rhs.blob_; }
};

struct ShardIndex {
folly::ConcurrentHashMap< BlobRoute, BlobExt > _btree;
std::atomic< blob_id > _shard_seq_num{0ull};
folly::ConcurrentHashMap< BlobRoute, BlobExt > btree_;
std::atomic< blob_id > shard_seq_num_{0ull};
~ShardIndex();
};

class MemoryHomeObject : public HomeObjectImpl {
/// Simulates the Shard=>Chunk mapping in IndexSvc
using index_svc = folly::ConcurrentHashMap< shard_id, std::unique_ptr< ShardIndex > >;
index_svc _in_memory_index;
index_svc index_;
///

/// Helpers
Expand All @@ -46,8 +46,6 @@ class MemoryHomeObject : public HomeObjectImpl {
BlobManager::NullResult _del_blob(ShardInfo const&, blob_id) override;
///

ShardIndex& _find_index(shard_id) const;

public:
using HomeObjectImpl::HomeObjectImpl;
~MemoryHomeObject() override = default;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/memory/shard_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ShardManager::Result< ShardInfo > MemoryHomeObject::_create_shard(pg_id pg_owner
auto [_, s_happened] = _shard_map.emplace(info.id, iter);
RELEASE_ASSERT(s_happened, "Duplicate Shard insertion!");
}
auto [it, happened] = _in_memory_index.try_emplace(info.id, std::make_unique< ShardIndex >());
auto [it, happened] = index_.try_emplace(info.id, std::make_unique< ShardIndex >());
RELEASE_ASSERT(happened, "Could not create BTree!");
return info;
}
Expand Down
157 changes: 0 additions & 157 deletions src/lib/memory/tests/BlobManagerTest.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/lib/memory/tests/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit b001c94

Please sign in to comment.