-
Notifications
You must be signed in to change notification settings - Fork 12
CraftReplDev write path #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbinmalek
wants to merge
4
commits into
eBay:dev/v6.x
Choose a base branch
from
sbinmalek:craft-write-path
base: dev/v6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
44410e4
Add STALE_TERM error and HomeStoreCraftJournalBackend stub
sbinmalek 09365b1
Implement CraftReplDev::write() — journal append at client-assigned LSN
sbinmalek 719fc60
Add unit tests for CraftReplDev write path (S2 chunk 4)
sbinmalek b51b3fc
Address PR comments
sbinmalek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| cmake_minimum_required (VERSION 3.11) | ||
|
|
||
| add_executable(test_craft_write) | ||
| target_sources(test_craft_write PRIVATE | ||
| test_craft_write.cpp | ||
| ) | ||
| target_link_libraries(test_craft_write | ||
| ${PROJECT_NAME} | ||
| ${COMMON_TEST_DEPS} | ||
| -rdynamic | ||
| ) | ||
|
|
||
| add_test(NAME CraftWriteTest COMMAND test_craft_write) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /********************************************************************************* | ||
| * Modifications Copyright 2026 eBay Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed | ||
| * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| * CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| *********************************************************************************/ | ||
|
|
||
| #include <map> | ||
| #include <gtest/gtest.h> | ||
| #include <boost/uuid/random_generator.hpp> | ||
| #include <sisl/options/options.h> | ||
| #include <sisl/logging/logging.h> | ||
|
|
||
| #include "../craft_repl_dev.hpp" | ||
| #include "../../coro_helpers.hpp" | ||
|
|
||
| SISL_LOGGING_INIT(HOMEBLOCKS_LOG_MODS) | ||
| SISL_OPTIONS_ENABLE(logging) | ||
|
|
||
| using namespace homeblocks; | ||
| using namespace homeblocks::detail; | ||
|
|
||
| // ─── MockCraftJournalBackend ────────────────────────────────────────────────── | ||
| // | ||
| // In-memory journal for unit tests. Stores slots in a map keyed by LSN; | ||
| // does not copy data buffers (test writes pass empty sg_lists). | ||
|
|
||
| class MockCraftJournalBackend : public CraftJournalBackend { | ||
| public: | ||
| async_status write_slot(int64_t lsn, lba_t lba, lba_count_t len, | ||
| sisl::sg_list /* data */) override { | ||
| slots_[lsn] = JournalSlot{lsn, false, lba, len, {}}; | ||
| co_return homestore::ok(); | ||
| } | ||
|
|
||
| async_result< JournalSlot > read_slot(int64_t lsn) override { | ||
| auto it = slots_.find(lsn); | ||
| if (it == slots_.end()) { co_return JournalSlot{.lsn = lsn, .is_empty = true}; } | ||
| co_return it->second; | ||
| } | ||
|
|
||
| async_status truncate_to(int64_t lsn) override { | ||
| std::erase_if(slots_, [lsn](auto& kv) { return kv.first > lsn; }); | ||
| co_return homestore::ok(); | ||
| } | ||
|
|
||
| bool has_slot(int64_t lsn) const { return slots_.contains(lsn); } | ||
| size_t slot_count() const { return slots_.size(); } | ||
|
|
||
| private: | ||
| std::map< int64_t, JournalSlot > slots_; | ||
| }; | ||
|
|
||
| // ─── test fixture ───────────────────────────────────────────────────────────── | ||
|
|
||
| class CraftWriteTest : public ::testing::Test { | ||
| protected: | ||
| void SetUp() override { | ||
| vol_id_ = boost::uuids::random_generator()(); | ||
| auto mock_owned = std::make_unique< MockCraftJournalBackend >(); | ||
| mock_ = mock_owned.get(); | ||
| dev_ = std::make_unique< CraftReplDev >(vol_id_, std::move(mock_owned)); | ||
| } | ||
|
|
||
| // Convenience: drive a write coroutine synchronously. | ||
| // term=0 matches initial state_.term; lba and glsn equal lsn for simplicity. | ||
| status do_write(uint64_t term, int64_t lsn) { | ||
| return sync_get(dev_->write(term, lsn, lsn, static_cast< lba_t >(lsn), | ||
| 1 /* len */, sisl::sg_list{})); | ||
| } | ||
|
|
||
| volume_id_t vol_id_; | ||
| MockCraftJournalBackend* mock_{nullptr}; | ||
| std::unique_ptr< CraftReplDev > dev_; | ||
| }; | ||
|
|
||
| // ─── tests ──────────────────────────────────────────────────────────────────── | ||
|
|
||
| // In-order writes: lsns 0,1,2 arrive sequentially. | ||
| // Missing set stays empty; last_append_lsn advances to 2; all slots land in journal. | ||
| TEST_F(CraftWriteTest, InOrderWrites) { | ||
| for (int64_t lsn = 0; lsn < 3; ++lsn) { | ||
| auto res = do_write(0 /* term */, lsn); | ||
| ASSERT_TRUE(res.has_value()) << "write lsn=" << lsn << " failed: " << res.error().message(); | ||
| } | ||
|
|
||
| EXPECT_EQ(mock_->slot_count(), 3u); | ||
| EXPECT_EQ(dev_->missing_count(), 0u); | ||
|
|
||
| auto lsns = sync_get(dev_->get_lsns(vol_id_)); | ||
| ASSERT_TRUE(lsns.has_value()); | ||
| EXPECT_EQ(lsns->last_append_lsn, 2); | ||
| EXPECT_EQ(lsns->commit_lsn, -1); // commit() is S3; nothing committed yet | ||
| } | ||
|
|
||
| // Out-of-order writes: lsn=4 arrives before lsn=1,2,3. | ||
| // Missing set should contain {1,2,3}; filling them in clears the set. | ||
| TEST_F(CraftWriteTest, OutOfOrderWrites) { | ||
| // First write establishes baseline. | ||
| ASSERT_TRUE(do_write(0, 0).has_value()); | ||
| EXPECT_EQ(dev_->missing_count(), 0u); | ||
|
|
||
| // Jump ahead — creates gaps {1, 2, 3}. | ||
| ASSERT_TRUE(do_write(0, 4).has_value()); | ||
| EXPECT_EQ(dev_->missing_count(), 3u); | ||
| EXPECT_TRUE(dev_->is_missing(1)); | ||
| EXPECT_TRUE(dev_->is_missing(2)); | ||
| EXPECT_TRUE(dev_->is_missing(3)); | ||
| EXPECT_FALSE(dev_->is_missing(0)); | ||
| EXPECT_FALSE(dev_->is_missing(4)); | ||
|
|
||
| // Fill in the gaps in reverse order. | ||
| ASSERT_TRUE(do_write(0, 3).has_value()); | ||
| ASSERT_TRUE(do_write(0, 2).has_value()); | ||
| ASSERT_TRUE(do_write(0, 1).has_value()); | ||
| EXPECT_EQ(dev_->missing_count(), 0u); | ||
|
|
||
| EXPECT_EQ(mock_->slot_count(), 5u); | ||
|
|
||
| auto lsns = sync_get(dev_->get_lsns(vol_id_)); | ||
| ASSERT_TRUE(lsns.has_value()); | ||
| EXPECT_EQ(lsns->last_append_lsn, 4); | ||
| } | ||
|
|
||
| // Term rejection: write with term != state_.term returns STALE_TERM. | ||
| // Journal must not be touched. | ||
| TEST_F(CraftWriteTest, TermRejection) { | ||
| // state_.term starts at 0; term=1 does not match and should be rejected. | ||
| auto res = do_write(1 /* wrong term */, 0); | ||
| ASSERT_FALSE(res.has_value()); | ||
| EXPECT_EQ(res.error(), make_error_condition(volume_error::STALE_TERM)); | ||
|
|
||
| // Journal untouched; missing set empty. | ||
| EXPECT_EQ(mock_->slot_count(), 0u); | ||
| EXPECT_EQ(dev_->missing_count(), 0u); | ||
| } | ||
|
|
||
| // ─── main ───────────────────────────────────────────────────────────────────── | ||
|
|
||
| int main(int argc, char** argv) { | ||
| SISL_OPTIONS_LOAD(argc, argv, logging); | ||
| sisl::logging::SetLogger("test_craft_write"); | ||
| ::testing::InitGoogleTest(&argc, argv); | ||
| return RUN_ALL_TESTS(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed