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

feat: Implement automatic source reconnect #60

Merged
merged 2 commits into from
Aug 6, 2022
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
minicbor = "0.14.1"
prometheus_exporter = { version = "0.8.4", default-features = false }
# gasket = { path = "../../gasketlibs/gasket-rs" }
# gasket = { path = "../../construkts/gasket-rs" }
gasket = { git = "https://github.com/construkts/gasket-rs.git" }
thiserror = "1.0.30"
redis = "0.21.5"
Expand Down
16 changes: 8 additions & 8 deletions src/bin/scrolls/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl TuiConsole {
}

fn refresh(&self, pipeline: &Pipeline) {
for (stage, tether) in pipeline.tethers.iter() {
for tether in pipeline.tethers.iter() {
let state = match tether.check_state() {
gasket::runtime::TetherState::Dropped => "dropped!",
gasket::runtime::TetherState::Blocked(_) => "blocked!",
Expand All @@ -90,7 +90,7 @@ impl TuiConsole {
match tether.read_metrics() {
Ok(readings) => {
for (key, value) in readings {
match (*stage, key, value) {
match (tether.name(), key, value) {
(_, "chain_tip", Reading::Gauge(x)) => {
self.chainsync_progress.set_length(x as u64);
}
Expand Down Expand Up @@ -173,24 +173,24 @@ impl PlainConsole {
return;
}

for (stage, tether) in pipeline.tethers.iter() {
for tether in pipeline.tethers.iter() {
match tether.check_state() {
gasket::runtime::TetherState::Dropped => {
log::error!("[{}] stage tether has been dropped", stage);
log::error!("[{}] stage tether has been dropped", tether.name());
}
gasket::runtime::TetherState::Blocked(_) => {
log::warn!("[{}] stage tehter is blocked or not reporting state", stage);
log::warn!("[{}] stage tehter is blocked or not reporting state", tether.name());
}
gasket::runtime::TetherState::Alive(state) => {
log::debug!("[{}] stage is alive with state: {:?}", stage, state);
log::debug!("[{}] stage is alive with state: {:?}", tether.name(), state);
match tether.read_metrics() {
Ok(readings) => {
for (key, value) in readings {
log::debug!("[{}] metric `{}` = {:?}", stage, key, value);
log::debug!("[{}] metric `{}` = {:?}", tether.name(), key, value);
}
}
Err(err) => {
log::error!("[{}] error reading metrics: {}", stage, err)
log::error!("[{}] error reading metrics: {}", tether.name(), err)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bin/scrolls/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn should_stop(pipeline: &bootstrap::Pipeline) -> bool {
pipeline
.tethers
.iter()
.any(|(_, tether)| match tether.check_state() {
.any(|tether| match tether.check_state() {
gasket::runtime::TetherState::Alive(x) => match x {
gasket::runtime::StageState::StandBy => true,
_ => false,
Expand All @@ -76,9 +76,9 @@ fn should_stop(pipeline: &bootstrap::Pipeline) -> bool {
}

fn shutdown(pipeline: bootstrap::Pipeline) {
for (name, tether) in pipeline.tethers {
for tether in pipeline.tethers {
let state = tether.check_state();
log::warn!("dismissing stage: {} with state {:?}", name, state);
log::warn!("dismissing stage: {} with state {:?}", tether.name(), state);
tether.dismiss_stage().expect("stage stops");

// Can't join the stage because there's a risk of deadlock, usually
Expand Down
8 changes: 3 additions & 5 deletions src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use crate::{enrich, reducers, sources, storage};

use gasket::{messaging::connect_ports, runtime::Tether};

type NamedTether = (&'static str, Tether);

pub struct Pipeline {
pub tethers: Vec<NamedTether>,
pub tethers: Vec<Tether>,
}

impl Pipeline {
Expand All @@ -15,8 +13,8 @@ impl Pipeline {
}
}

pub fn register_stage(&mut self, name: &'static str, tether: Tether) {
self.tethers.push((name, tether));
pub fn register_stage(&mut self, tether: Tether) {
self.tethers.push(tether);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/enrich/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ impl Bootstrapper {
};

pipeline.register_stage(
"enrich-skip",
spawn_stage(
worker,
gasket::runtime::Policy {
tick_timeout: Some(Duration::from_secs(600)),
..Default::default()
},
Some("enrich-skip"),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/enrich/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl Bootstrapper {
};

pipeline.register_stage(
"enrich-sled",
spawn_stage(
worker,
gasket::runtime::Policy {
tick_timeout: Some(Duration::from_secs(600)),
..Default::default()
},
Some("enrich-sled"),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ impl Bootstrapper {
pub fn spawn_stages(self, pipeline: &mut bootstrap::Pipeline) {
let worker = worker::Worker::new(self.reducers, self.input, self.output, self.policy);
pipeline.register_stage(
"reducers",
spawn_stage(
worker,
gasket::runtime::Policy {
tick_timeout: Some(Duration::from_secs(600)),
..Default::default()
},
Some("reducers"),
),
);
}
Expand Down
20 changes: 14 additions & 6 deletions src/sources/n2c/chainsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{collections::HashMap, ops::Deref};

use pallas::ledger::traverse::MultiEraBlock;
use pallas::network::miniprotocols::{self, chainsync, Agent, Point};
use pallas::network::multiplexer;

use gasket::{
error::AsWorkError,
Expand All @@ -11,6 +10,8 @@ use gasket::{

use crate::{crosscut, model::RawBlockPayload, sources::utils};

use super::transport::Transport;

struct ChainObserver {
min_depth: usize,
output: OutputPort,
Expand Down Expand Up @@ -97,35 +98,37 @@ type OutputPort = gasket::messaging::OutputPort<RawBlockPayload>;
type MyAgent = chainsync::BlockConsumer<ChainObserver>;

pub struct Worker {
channel: multiplexer::StdChannelBuffer,
socket: String,
min_depth: usize,
chain: crosscut::ChainWellKnownInfo,
intersect: crosscut::IntersectConfig,
cursor: crosscut::Cursor,
//finalize_config: Option<FinalizeConfig>,
agent: Option<MyAgent>,
transport: Option<Transport>,
output: OutputPort,
block_count: gasket::metrics::Counter,
chain_tip: gasket::metrics::Gauge,
}

impl Worker {
pub fn new(
channel: multiplexer::StdChannelBuffer,
socket: String,
min_depth: usize,
chain: crosscut::ChainWellKnownInfo,
intersect: crosscut::IntersectConfig,
cursor: crosscut::Cursor,
output: OutputPort,
) -> Self {
Self {
channel,
socket,
min_depth,
chain,
intersect,
cursor,
output,
agent: None,
transport: None,
block_count: Default::default(),
chain_tip: Default::default(),
}
Expand All @@ -141,11 +144,13 @@ impl gasket::runtime::Worker for Worker {
}

fn bootstrap(&mut self) -> Result<(), gasket::error::Error> {
let mut transport = Transport::setup(&self.socket, self.chain.magic).or_retry()?;

let known_points = utils::define_known_points(
&self.chain,
&self.intersect,
&self.cursor,
&mut self.channel,
&mut transport.channel5,
)
.or_retry()?;

Expand All @@ -162,18 +167,21 @@ impl gasket::runtime::Worker for Worker {
.or_retry()?;

self.agent = Some(agent);
self.transport = Some(transport);

Ok(())
}

fn work(&mut self) -> gasket::runtime::WorkResult {
let agent = self.agent.take().unwrap();
let mut transport = self.transport.take().unwrap();

let agent = miniprotocols::run_agent_step(agent, &mut self.channel).or_restart()?;
let agent = miniprotocols::run_agent_step(agent, &mut transport.channel5).or_restart()?;

let is_done = agent.is_done();

self.agent = Some(agent);
self.transport = Some(transport);

match is_done {
true => Ok(gasket::runtime::WorkOutcome::Done),
Expand Down
57 changes: 21 additions & 36 deletions src/sources/n2c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ mod transport;
use serde::Deserialize;
use std::time::Duration;

use self::transport::Transport;
use crate::{bootstrap::Pipeline, crosscut, model::RawBlockPayload};
use gasket::{error::AsWorkError, messaging::OutputPort, retries};
use gasket::messaging::OutputPort;

#[derive(Deserialize)]
pub struct Config {
Expand Down Expand Up @@ -36,45 +35,31 @@ pub struct Bootstrapper {
}

impl Bootstrapper {
fn bootstrap_transport(&self) -> Result<Transport, crate::Error> {
gasket::retries::retry_operation(
|| Transport::setup(&self.config.path, self.chain.magic).or_retry(),
&retries::Policy {
max_retries: 5,
backoff_factor: 2,
backoff_unit: Duration::from_secs(1),
max_backoff: Duration::from_secs(60),
},
None,
)
.map_err(crate::Error::source)
}

pub fn borrow_output_port(&mut self) -> &'_ mut OutputPort<RawBlockPayload> {
&mut self.output
}

pub fn spawn_stages(self, pipeline: &mut Pipeline, cursor: &Option<crosscut::PointArg>) {
let transport = self
.bootstrap_transport()
.expect("transport should be connected after several retries");

pipeline.register_stage(
"n2c",
gasket::runtime::spawn_stage(
self::chainsync::Worker::new(
transport.channel5,
0,
self.chain,
self.intersect,
cursor.clone(),
self.output,
),
gasket::runtime::Policy {
tick_timeout: Some(Duration::from_secs(600)),
..Default::default()
},
pipeline.register_stage(gasket::runtime::spawn_stage(
self::chainsync::Worker::new(
self.config.path.clone(),
0,
self.chain,
self.intersect,
cursor.clone(),
self.output,
),
);
gasket::runtime::Policy {
tick_timeout: Some(Duration::from_secs(600)),
bootstrap_retry: gasket::retries::Policy {
max_retries: 20,
backoff_factor: 2,
backoff_unit: Duration::from_secs(1),
max_backoff: Duration::from_secs(60),
},
..Default::default()
},
Some("n2c"),
));
}
}
Loading