Skip to content

Commit

Permalink
feat: use shutdown height as sync stop height (#11373)
Browse files Browse the repository at this point in the history
If one wants to take old backup to run some blocks on top of its head,
node will first run header sync, and its default behaviour is to
download **all** headers from chain. If backup is couple days old, this
takes a while.

The solution is to use `expected_shutdown` config, which will stop
header sync after shutdown height is reached and let the node download
and process blocks. I tested this on my node, it worked nicely, allowing
to change shutdown height on the fly.
  • Loading branch information
Longarithm committed May 22, 2024
1 parent 204b798 commit 26a32a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ impl Client {
config.header_sync_progress_timeout,
config.header_sync_stall_ban_timeout,
config.header_sync_expected_height_per_second,
config.expected_shutdown.clone(),
);
let block_sync = BlockSync::new(
clock.clone(),
Expand Down
3 changes: 2 additions & 1 deletion chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,8 @@ impl ClientActorInner {
};

let peer_id = peer_info.peer_info.id.clone();
let highest_height = peer_info.highest_block_height;
let shutdown_height = self.client.config.expected_shutdown.get().unwrap_or(u64::MAX);
let highest_height = peer_info.highest_block_height.min(shutdown_height);

if is_syncing {
if highest_height <= head.height {
Expand Down
23 changes: 18 additions & 5 deletions chain/client/src/sync/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ pub struct HeaderSync {

/// Expected increase of header head height per second during header sync
expected_height_per_second: u64,

/// Not for production use.
/// Expected height when node will be automatically shut down, so header
/// sync can be stopped.
shutdown_height: near_chain_configs::MutableConfigValue<Option<BlockHeight>>,
}

impl HeaderSync {
Expand All @@ -69,6 +74,7 @@ impl HeaderSync {
progress_timeout: Duration,
stall_ban_timeout: Duration,
expected_height_per_second: u64,
shutdown_height: near_chain_configs::MutableConfigValue<Option<BlockHeight>>,
) -> Self {
HeaderSync {
clock: clock.clone(),
Expand All @@ -81,10 +87,11 @@ impl HeaderSync {
},
syncing_peer: None,
stalling_ts: None,
initial_timeout: initial_timeout,
progress_timeout: progress_timeout,
stall_ban_timeout: stall_ban_timeout,
initial_timeout,
progress_timeout,
stall_ban_timeout,
expected_height_per_second,
shutdown_height,
}
}

Expand Down Expand Up @@ -147,8 +154,9 @@ impl HeaderSync {
self.syncing_peer = None;
// Pick a new random peer to request the next batch of headers.
if let Some(peer) = highest_height_peers.choose(&mut thread_rng()).cloned() {
// TODO: This condition should always be true, otherwise we can already complete header sync.
if peer.highest_block_height > header_head.height {
let shutdown_height = self.shutdown_height.get().unwrap_or(u64::MAX);
let highest_height = peer.highest_block_height.min(shutdown_height);
if highest_height > header_head.height {
self.syncing_peer = self.request_headers(chain, peer);
}
}
Expand Down Expand Up @@ -379,6 +387,7 @@ mod test {
use near_chain::test_utils::{process_block_sync, setup, setup_with_tx_validity_period};
use near_chain::types::Tip;
use near_chain::{BlockProcessingArtifact, Provenance};
use near_chain_configs::MutableConfigValue;
use near_client_primitives::types::SyncStatus;
use near_crypto::{KeyType, PublicKey};
use near_network::test_utils::MockPeerManagerAdapter;
Expand Down Expand Up @@ -447,6 +456,7 @@ mod test {
Duration::seconds(2),
Duration::seconds(120),
1_000_000_000,
MutableConfigValue::new(None, "expected_shutdown"),
);
let (mut chain, _, _, signer) = setup(Clock::real());
for _ in 0..3 {
Expand Down Expand Up @@ -534,6 +544,7 @@ mod test {
Duration::seconds(2),
Duration::seconds(120),
1_000_000_000,
MutableConfigValue::new(None, "expected_shutdown"),
);
let (mut chain, _, _, signer) = setup(Clock::real());
let (mut chain2, _, _, signer2) = setup(Clock::real());
Expand Down Expand Up @@ -645,6 +656,7 @@ mod test {
Duration::seconds(1),
Duration::seconds(3),
25,
MutableConfigValue::new(None, "expected_shutdown"),
);

let set_syncing_peer = |header_sync: &mut HeaderSync| {
Expand Down Expand Up @@ -741,6 +753,7 @@ mod test {
Duration::seconds(2),
Duration::seconds(120),
1_000_000_000,
MutableConfigValue::new(None, "expected_shutdown"),
);

let clock = FakeClock::new(Utc::UNIX_EPOCH);
Expand Down

0 comments on commit 26a32a9

Please sign in to comment.