This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
Resending votes in consensus #2200
Open
muratovv
wants to merge
1
commit into
develop
Choose a base branch
from
feature/resending_votes
base: develop
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
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
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
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
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,8 +32,9 @@ namespace iroha { | |||||||||
handler_ = handler; | ||||||||||
} | ||||||||||
|
||||||||||
void NetworkImpl::sendState(const shared_model::interface::Peer &to, | ||||||||||
const std::vector<VoteMessage> &state) { | ||||||||||
YacNetworkWithFeedBack::SendStateReturnType | ||||||||||
NetworkImpl::sendState(const shared_model::interface::Peer &to, | ||||||||||
const std::vector<VoteMessage> &state) { | ||||||||||
createPeerConnection(to); | ||||||||||
|
||||||||||
proto::State request; | ||||||||||
|
@@ -42,12 +43,60 @@ namespace iroha { | |||||||||
*pb_vote = PbConverters::serializeVote(vote); | ||||||||||
} | ||||||||||
|
||||||||||
async_call_->Call([&](auto context, auto cq) { | ||||||||||
return peers_.at(to.address())->AsyncSendState(context, request, cq); | ||||||||||
}); | ||||||||||
|
||||||||||
log_->info( | ||||||||||
"Send votes bundle[size={}] to {}", state.size(), to.address()); | ||||||||||
|
||||||||||
auto log_outcome = [log = log_, destination_peer = to.toString()]( | ||||||||||
const grpc::Status &status) { | ||||||||||
log->info("Sent to {} with status details [{}]", | ||||||||||
destination_peer, | ||||||||||
status.ok() ? "OK" : status.error_details()); | ||||||||||
}; | ||||||||||
|
||||||||||
return async_call_ | ||||||||||
->Call([&](auto context, auto cq) { | ||||||||||
return peers_.at(to.address()) | ||||||||||
->AsyncSendState(context, request, cq); | ||||||||||
}) | ||||||||||
.tap(log_outcome) | ||||||||||
.map( | ||||||||||
[](const auto &status) { return makeSendStateStatus(status); }); | ||||||||||
} | ||||||||||
|
||||||||||
YacNetworkWithFeedBack::ValueStateReturnType | ||||||||||
NetworkImpl::makeSendStateStatus(const grpc::Status &status) { | ||||||||||
auto is_ok = [](const auto &code) { | ||||||||||
return code == grpc::StatusCode::OK; | ||||||||||
}; | ||||||||||
|
||||||||||
auto is_troubles_with_recipient = [](const auto &code) { | ||||||||||
using namespace grpc; | ||||||||||
std::vector<int> codes = {StatusCode::CANCELLED, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
performs better lookup There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems that set is more appropriate because it is a more reliable way to stay unique constants. |
||||||||||
StatusCode::INVALID_ARGUMENT, | ||||||||||
StatusCode::UNAUTHENTICATED, | ||||||||||
StatusCode::RESOURCE_EXHAUSTED, | ||||||||||
StatusCode::ABORTED, | ||||||||||
StatusCode::UNIMPLEMENTED, | ||||||||||
StatusCode::UNAVAILABLE, | ||||||||||
StatusCode::DATA_LOSS}; | ||||||||||
return std::any_of(codes.begin(), codes.end(), [code](auto val) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or, if using set,
Suggested change
which is faster |
||||||||||
return code == val; | ||||||||||
}); | ||||||||||
}; | ||||||||||
|
||||||||||
auto code = status.error_code(); | ||||||||||
|
||||||||||
using namespace iroha::consensus::yac::sending_statuses; | ||||||||||
|
||||||||||
if (is_ok(code)) { | ||||||||||
return SuccessfulSent(); | ||||||||||
} | ||||||||||
|
||||||||||
if (is_troubles_with_recipient(code)) { | ||||||||||
return UnavailableReceiver(); | ||||||||||
} | ||||||||||
|
||||||||||
return UnavailableNetwork(); | ||||||||||
} | ||||||||||
|
||||||||||
grpc::Status NetworkImpl::SendState( | ||||||||||
|
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
46 changes: 46 additions & 0 deletions
46
irohad/consensus/yac/transport/impl/yac_network_sender.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,46 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "consensus/yac/transport/impl/yac_network_sender.hpp" | ||
|
||
#include "common/visitor.hpp" | ||
#include "consensus/yac/vote_message.hpp" | ||
|
||
using namespace iroha::consensus::yac; | ||
|
||
YacNetworkSender::YacNetworkSender(std::shared_ptr<TransportType> transport) | ||
: transport_(std::move(transport)) {} | ||
|
||
void YacNetworkSender::subscribe( | ||
std::shared_ptr<YacNetworkNotifications> handler) { | ||
transport_->subscribe(std::move(handler)); | ||
} | ||
|
||
void YacNetworkSender::sendState(PeerType to, StateType state) { | ||
sendStateViaTransport( | ||
to, std::make_shared<StateType>(std::move(state)), transport_); | ||
} | ||
|
||
void YacNetworkSender::sendStateViaTransport( | ||
PeerType to, | ||
StateInCollectionType state, | ||
std::shared_ptr<TransportType> transport) { | ||
transport->sendState(*to, *state) | ||
.subscribe([transport = transport, to, state](const auto &result) { | ||
iroha::visit_in_place( | ||
result, | ||
[transport, to, state]( | ||
const sending_statuses::UnavailableNetwork &) { | ||
// assume the message is undelivered if troubles occur with our | ||
// connection then it will resend the message | ||
|
||
sendStateViaTransport(to, state, transport); | ||
}, | ||
[&](const auto &) { | ||
// if message delivers or recipient peer goes down then it | ||
// will stop resending the message | ||
}); | ||
}); | ||
} |
68 changes: 68 additions & 0 deletions
68
irohad/consensus/yac/transport/impl/yac_network_sender.hpp
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,68 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_YAC_NETWORK_SENDER_HPP | ||
#define IROHA_YAC_NETWORK_SENDER_HPP | ||
|
||
#include "consensus/yac/transport/yac_network_interface.hpp" | ||
|
||
#include <memory> | ||
#include <rxcpp/rx.hpp> | ||
#include <unordered_map> | ||
|
||
namespace iroha { | ||
namespace consensus { | ||
namespace yac { | ||
/** | ||
* Transport layer wrapper which retries to send messages if the network | ||
* shut down | ||
*/ | ||
class YacNetworkSender : public YacNetwork { | ||
public: | ||
/// type of low transport level | ||
using TransportType = YacNetworkWithFeedBack; | ||
|
||
/// type of peer structure | ||
using PeerType = std::shared_ptr<shared_model::interface::Peer>; | ||
|
||
/// type of state | ||
using StateType = std::vector<VoteMessage>; | ||
|
||
YacNetworkSender(const YacNetworkSender &) = delete; | ||
YacNetworkSender(YacNetworkSender &&) = delete; | ||
YacNetworkSender &operator=(const YacNetworkSender &) = delete; | ||
YacNetworkSender &operator=(YacNetworkSender &&) = delete; | ||
|
||
/** | ||
* Creates transport with redelivery property | ||
* @param transport - instance of effective transport | ||
*/ | ||
YacNetworkSender(std::shared_ptr<TransportType> transport); | ||
|
||
void subscribe( | ||
std::shared_ptr<YacNetworkNotifications> handler) override; | ||
|
||
void sendState(PeerType to, StateType state) override; | ||
|
||
private: | ||
using StateInCollectionType = std::shared_ptr<StateType>; | ||
using StatesCollection = | ||
std::unordered_map<PeerType, StateInCollectionType>; | ||
|
||
static void sendStateViaTransport( | ||
PeerType to, | ||
StateInCollectionType state, | ||
std::shared_ptr<TransportType> transport); | ||
|
||
// ------------------------| Global state | ---------------------------- | ||
std::shared_ptr<TransportType> transport_; | ||
|
||
// ------------------------| Current state | --------------------------- | ||
StatesCollection undelivered_states_; | ||
}; | ||
} // namespace yac | ||
} // namespace consensus | ||
} // namespace iroha | ||
#endif // IROHA_YAC_NETWORK_SENDER_HPP |
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
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.
I would make
as returning a reference to a local object is scary, and then
as we do not want anyone to change the object (no matter it is an interface)