Skip to content
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

fix(core/sync): handle deadline timeouts by changing peer #4649

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions base_layer/core/src/base_node/sync/block_sync/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ use std::{
use futures::StreamExt;
use log::*;
use num_format::{Locale, ToFormattedString};
use tari_comms::{connectivity::ConnectivityRequester, peer_manager::NodeId, PeerConnection};
use tari_comms::{
connectivity::ConnectivityRequester,
peer_manager::NodeId,
protocol::rpc::{RpcClient, RpcError},
PeerConnection,
};
use tari_utilities::hex::Hex;
use tracing;

Expand Down Expand Up @@ -119,8 +124,11 @@ impl<B: BlockchainBackend + 'static> BlockSynchronizer<B> {
let sync_peer_node_ids = self.sync_peers.iter().map(|p| p.node_id()).cloned().collect::<Vec<_>>();
for (i, node_id) in sync_peer_node_ids.iter().enumerate() {
let mut conn = self.connect_to_sync_peer(node_id.clone()).await?;
let config = RpcClient::builder()
.with_deadline(self.config.rpc_deadline)
.with_deadline_grace_period(Duration::from_secs(5));
let mut client = conn
.connect_rpc_using_builder(rpc::BaseNodeSyncRpcClient::builder().with_deadline(Duration::from_secs(60)))
.connect_rpc_using_builder::<rpc::BaseNodeSyncRpcClient>(config)
.await?;
let latency = client
.get_last_request_latency()
Expand Down Expand Up @@ -158,6 +166,7 @@ impl<B: BlockchainBackend + 'static> BlockSynchronizer<B> {
self.ban_peer(node_id, &err).await?;
return Err(err.into());
},
Err(err @ BlockSyncError::RpcError(RpcError::ReplyTimeout)) |
Err(err @ BlockSyncError::MaxLatencyExceeded { .. }) => {
warn!(target: LOG_TARGET, "{}", err);
if i == self.sync_peers.len() - 1 {
Expand Down
6 changes: 5 additions & 1 deletion base_layer/core/src/base_node/sync/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ pub struct BlockchainSyncConfig {
pub forced_sync_peers: Vec<NodeId>,
/// Number of threads to use for validation
pub validation_concurrency: usize,
/// The RPC deadline to set on sync clients. If this deadline is reached, a new sync peer will be selected for
/// sync.
pub rpc_deadline: Duration,
}

impl Default for BlockchainSyncConfig {
fn default() -> Self {
Self {
initial_max_sync_latency: Duration::from_secs(10),
initial_max_sync_latency: Duration::from_secs(20),
max_latency_increase: Duration::from_secs(2),
ban_period: Duration::from_secs(30 * 60),
short_ban_period: Duration::from_secs(60),
forced_sync_peers: Default::default(),
validation_concurrency: 6,
rpc_deadline: Duration::from_secs(10),
}
}
}
10 changes: 8 additions & 2 deletions base_layer/core/src/base_node/sync/header_sync/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use tari_common_types::{chain_metadata::ChainMetadata, types::HashOutput};
use tari_comms::{
connectivity::ConnectivityRequester,
peer_manager::NodeId,
protocol::rpc::{RpcError, RpcHandshakeError},
protocol::rpc::{RpcClient, RpcError, RpcHandshakeError},
PeerConnection,
};
use tari_utilities::hex::Hex;
Expand Down Expand Up @@ -136,7 +136,12 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
"Attempting to synchronize headers with `{}`", node_id
);

let mut client = conn.connect_rpc::<rpc::BaseNodeSyncRpcClient>().await?;
let config = RpcClient::builder()
.with_deadline(self.config.rpc_deadline)
.with_deadline_grace_period(Duration::from_secs(5));
let mut client = conn
.connect_rpc_using_builder::<rpc::BaseNodeSyncRpcClient>(config)
.await?;

let latency = client
.get_last_request_latency()
Expand Down Expand Up @@ -208,6 +213,7 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
self.ban_peer_long(node_id, BanReason::GeneralHeaderSyncFailure(err))
.await?;
},
Err(err @ BlockHeaderSyncError::RpcError(RpcError::ReplyTimeout)) |
Err(err @ BlockHeaderSyncError::MaxLatencyExceeded { .. }) => {
warn!(target: LOG_TARGET, "{}", err);
if i == self.sync_peers.len() - 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ use croaring::Bitmap;
use futures::{stream::FuturesUnordered, StreamExt};
use log::*;
use tari_common_types::types::{Commitment, RangeProofService};
use tari_comms::{connectivity::ConnectivityRequester, peer_manager::NodeId};
use tari_comms::{
connectivity::ConnectivityRequester,
peer_manager::NodeId,
protocol::rpc::{RpcClient, RpcError},
};
use tari_crypto::{commitment::HomomorphicCommitment, tari_utilities::hex::Hex};
use tokio::task;

Expand Down Expand Up @@ -178,7 +182,10 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
async fn sync(&mut self, header: &BlockHeader) -> Result<(), HorizonSyncError> {
for (i, sync_peer) in self.sync_peers.iter().enumerate() {
let mut connection = self.connectivity.dial_peer(sync_peer.node_id().clone()).await?;
let mut client = connection.connect_rpc::<rpc::BaseNodeSyncRpcClient>().await?;
let config = RpcClient::builder()
.with_deadline(self.config.rpc_deadline)
.with_deadline_grace_period(Duration::from_secs(3));
let mut client = connection.connect_rpc_using_builder(config).await?;

match self.begin_sync(sync_peer.clone(), &mut client, header).await {
Ok(_) => match self.finalize_horizon_sync(sync_peer).await {
Expand All @@ -188,6 +195,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
return Err(err);
},
},
Err(err @ HorizonSyncError::RpcError(RpcError::ReplyTimeout)) |
Err(err @ HorizonSyncError::MaxLatencyExceeded { .. }) => {
warn!(target: LOG_TARGET, "{}", err);
if i == self.sync_peers.len() - 1 {
Expand Down