This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dup): implement ship_mutation stage and mutation_batch (#312)
- Loading branch information
Showing
10 changed files
with
440 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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
119 changes: 119 additions & 0 deletions
119
src/dist/replication/lib/duplication/mutation_batch.cpp
This file contains 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,119 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Microsoft Corporation | ||
* | ||
* -=- Robust Distributed System Nucleus (rDSN) -=- | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <dsn/dist/fmt_logging.h> | ||
#include <dsn/cpp/message_utils.h> | ||
|
||
#include "replica_duplicator.h" | ||
#include "mutation_batch.h" | ||
#include "dist/replication/lib/prepare_list.h" | ||
|
||
namespace dsn { | ||
namespace replication { | ||
|
||
/*static*/ constexpr int64_t mutation_batch::PREPARE_LIST_NUM_ENTRIES; | ||
|
||
error_s mutation_batch::add(mutation_ptr mu) | ||
{ | ||
if (mu->get_decree() <= _mutation_buffer->last_committed_decree()) { | ||
// ignore | ||
return error_s::ok(); | ||
} | ||
|
||
auto old = _mutation_buffer->get_mutation_by_decree(mu->get_decree()); | ||
if (old != nullptr && old->data.header.ballot >= mu->data.header.ballot) { | ||
// ignore | ||
return error_s::ok(); | ||
} | ||
|
||
error_code ec = _mutation_buffer->prepare(mu, partition_status::PS_INACTIVE); | ||
if (ec != ERR_OK) { | ||
return FMT_ERR( | ||
ERR_INVALID_DATA, | ||
"failed to add mutation [err:{}, logged:{}, decree:{}, committed:{}, start_decree:{}]", | ||
ec.to_string(), | ||
mu->is_logged(), | ||
mu->get_decree(), | ||
mu->data.header.last_committed_decree, | ||
_start_decree); | ||
} | ||
|
||
return error_s::ok(); | ||
} | ||
|
||
decree mutation_batch::last_decree() const { return _mutation_buffer->last_committed_decree(); } | ||
|
||
void mutation_batch::set_start_decree(decree d) { _start_decree = d; } | ||
|
||
mutation_tuple_set mutation_batch::move_all_mutations() | ||
{ | ||
// free the internal space | ||
_mutation_buffer->truncate(last_decree()); | ||
return std::move(_loaded_mutations); | ||
} | ||
|
||
mutation_batch::mutation_batch(replica_duplicator *r) : replica_base(r) | ||
{ | ||
// Prepend a special tag identifying this is a mutation_batch, | ||
// so `dxxx_replica` logging in prepare_list will print along with its real caller. | ||
// This helps for debugging. | ||
replica_base base(r->get_gpid(), std::string("mutation_batch@") + r->replica_name()); | ||
_mutation_buffer = | ||
make_unique<prepare_list>(&base, 0, PREPARE_LIST_NUM_ENTRIES, [this](mutation_ptr &mu) { | ||
// committer | ||
add_mutation_if_valid(mu, _loaded_mutations, _start_decree); | ||
}); | ||
|
||
// start duplication from confirmed_decree | ||
_mutation_buffer->reset(r->progress().confirmed_decree); | ||
} | ||
|
||
/*extern*/ void | ||
add_mutation_if_valid(mutation_ptr &mu, mutation_tuple_set &mutations, decree start_decree) | ||
{ | ||
if (mu->get_decree() < start_decree) { | ||
// ignore | ||
return; | ||
} | ||
for (mutation_update &update : mu->data.updates) { | ||
// ignore WRITE_EMPTY | ||
if (update.code == RPC_REPLICATION_WRITE_EMPTY) { | ||
continue; | ||
} | ||
|
||
blob bb; | ||
if (update.data.buffer() != nullptr) { | ||
bb = std::move(update.data); | ||
} else { | ||
bb = blob::create_from_bytes(update.data.data(), update.data.length()); | ||
} | ||
|
||
mutations.emplace(std::make_tuple(mu->data.header.timestamp, update.code, std::move(bb))); | ||
} | ||
} | ||
|
||
} // namespace replication | ||
} // namespace dsn |
This file contains 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,73 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Microsoft Corporation | ||
* | ||
* -=- Robust Distributed System Nucleus (rDSN) -=- | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <dsn/dist/replication/mutation_duplicator.h> | ||
|
||
#include "dist/replication/lib/mutation.h" | ||
|
||
namespace dsn { | ||
namespace replication { | ||
|
||
class replica_duplicator; | ||
class prepare_list; | ||
|
||
// A sorted array of committed mutations that are ready for duplication. | ||
// Not thread-safe. | ||
class mutation_batch : replica_base | ||
{ | ||
public: | ||
static constexpr int64_t PREPARE_LIST_NUM_ENTRIES{200}; | ||
|
||
explicit mutation_batch(replica_duplicator *r); | ||
|
||
error_s add(mutation_ptr mu); | ||
|
||
mutation_tuple_set move_all_mutations(); | ||
|
||
decree last_decree() const; | ||
|
||
// mutations with decree < d will be ignored. | ||
void set_start_decree(decree d); | ||
|
||
size_t size() const { return _loaded_mutations.size(); } | ||
|
||
private: | ||
friend class replica_duplicator_test; | ||
|
||
std::unique_ptr<prepare_list> _mutation_buffer; | ||
mutation_tuple_set _loaded_mutations; | ||
decree _start_decree{invalid_decree}; | ||
}; | ||
|
||
using mutation_batch_u_ptr = std::unique_ptr<mutation_batch>; | ||
|
||
/// Extract mutations into mutation_tuple_set if they are not WRITE_EMPTY. | ||
extern void add_mutation_if_valid(mutation_ptr &, mutation_tuple_set &, decree start_decree); | ||
|
||
} // namespace replication | ||
} // namespace dsn |
This file contains 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 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
63 changes: 63 additions & 0 deletions
63
src/dist/replication/lib/duplication/test/mutation_batch_test.cpp
This file contains 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,63 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Microsoft Corporation | ||
* | ||
* -=- Robust Distributed System Nucleus (rDSN) -=- | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include "dist/replication/test/replica_test/unit_test/replica_test_base.h" | ||
#include "dist/replication/lib/duplication/mutation_batch.h" | ||
|
||
namespace dsn { | ||
namespace replication { | ||
|
||
class mutation_batch_test : public replica_test_base | ||
{ | ||
}; | ||
|
||
TEST_F(mutation_batch_test, add_mutation_if_valid) | ||
{ | ||
mutation_tuple_set result; | ||
|
||
std::string s = "hello"; | ||
mutation_ptr mu1 = create_test_mutation(1, s); | ||
add_mutation_if_valid(mu1, result, 0); | ||
mutation_tuple mt1 = *result.begin(); | ||
|
||
result.clear(); | ||
|
||
s = "world"; | ||
mutation_ptr mu2 = create_test_mutation(2, s); | ||
add_mutation_if_valid(mu2, result, 0); | ||
mutation_tuple mt2 = *result.begin(); | ||
|
||
ASSERT_EQ(std::get<2>(mt1).to_string(), "hello"); | ||
ASSERT_EQ(std::get<2>(mt2).to_string(), "world"); | ||
|
||
// decree 1 should be ignored | ||
mutation_ptr mu3 = create_test_mutation(1, s); | ||
add_mutation_if_valid(mu2, result, 2); | ||
ASSERT_EQ(result.size(), 2); | ||
} | ||
|
||
} // namespace replication | ||
} // namespace dsn |
Oops, something went wrong.