Skip to content

Commit 4884223

Browse files
committed
validator_worker - logging and bugfixing
1 parent b893bbd commit 4884223

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

validator_worker/src/error.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use primitives::ChannelId;
12
use serde::{Deserialize, Serialize};
23
use std::error::Error;
34
use std::fmt::{Display, Formatter, Result};
@@ -6,15 +7,19 @@ use std::fmt::{Display, Formatter, Result};
67
pub enum ValidatorWorker {
78
Configuration(String),
89
Failed(String),
10+
Channel(ChannelId, String),
911
}
1012

1113
impl Error for ValidatorWorker {}
1214

1315
impl Display for ValidatorWorker {
1416
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
1517
match self {
16-
ValidatorWorker::Configuration(error) => write!(f, "Configuration error: {}", error),
17-
ValidatorWorker::Failed(error) => write!(f, "error: {}", error),
18+
ValidatorWorker::Configuration(err) => write!(f, "Configuration error: {}", err),
19+
ValidatorWorker::Failed(err) => write!(f, "error: {}", err),
20+
ValidatorWorker::Channel(channel_id, err) => {
21+
write!(f, "Channel {}: {}", channel_id, err)
22+
}
1823
}
1924
}
2025
}

validator_worker/src/main.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::error::Error;
66
use std::time::Duration;
77

88
use clap::{App, Arg};
9-
use futures::future::{join, try_join_all};
9+
use futures::future::{join, join_all};
10+
use slog::{error, info, Logger};
1011
use tokio::runtime::Runtime;
1112
use tokio::time::{delay_for, timeout};
1213

@@ -15,7 +16,6 @@ use primitives::adapter::{Adapter, DummyAdapterOptions, KeystoreOptions};
1516
use primitives::config::{configuration, Config};
1617
use primitives::util::tests::prep_db::{AUTH, IDS};
1718
use primitives::{Channel, SpecValidator, ValidatorId};
18-
use slog::{error, info, Logger};
1919
use validator_worker::error::ValidatorWorker as ValidatorWorkerError;
2020
use validator_worker::{all_channels, follower, leader, SentryApi};
2121

@@ -165,26 +165,26 @@ async fn iterate_channels<A: Adapter + 'static>(args: Args<A>, logger: &Logger)
165165
let channels = match result {
166166
Ok(channels) => channels,
167167
Err(e) => {
168-
error!(logger, "Failed to get channels {}", &e; "main" => "iterate_channels");
168+
error!(logger, "Failed to get channels - {}", &e; "main" => "iterate_channels");
169169
return;
170170
}
171171
};
172172

173173
let channels_size = channels.len();
174174

175-
let tick = try_join_all(
175+
let tick_results = join_all(
176176
channels
177177
.into_iter()
178178
.map(|channel| validator_tick(args.adapter.clone(), channel, &args.config, logger)),
179179
)
180180
.await;
181181

182-
info!(logger, "processed {} channels", channels_size);
183-
184-
if let Err(e) = tick {
185-
error!(logger, "An occurred while processing channels {}", &e; "main" => "iterate_channels");
182+
for channel_err in tick_results.into_iter().filter_map(Result::err) {
183+
error!(logger, "Error processing channels - {}", channel_err; "main" => "iterate_channels");
186184
}
187185

186+
info!(logger, "processed {} channels", channels_size);
187+
188188
if channels_size >= args.config.max_channels as usize {
189189
error!(logger, "WARNING: channel limit cfg.MAX_CHANNELS={} reached", &args.config.max_channels; "main" => "iterate_channels");
190190
}
@@ -202,23 +202,25 @@ async fn validator_tick<A: Adapter + 'static>(
202202
let duration = Duration::from_millis(config.validator_tick_timeout as u64);
203203

204204
match channel.spec.validators.find(&whoami) {
205-
SpecValidator::Leader(_) => {
206-
if let Err(e) = timeout(duration, leader::tick(&sentry)).await {
207-
return Err(ValidatorWorkerError::Failed(e.to_string()));
208-
}
209-
}
210-
SpecValidator::Follower(_) => {
211-
if let Err(e) = timeout(duration, follower::tick(&sentry)).await {
212-
return Err(ValidatorWorkerError::Failed(e.to_string()));
213-
}
214-
}
215-
SpecValidator::None => {
216-
return Err(ValidatorWorkerError::Failed(
217-
"validatorTick: processing a channel where we are not validating".to_string(),
218-
))
219-
}
220-
};
221-
Ok(())
205+
SpecValidator::Leader(_) => timeout(duration, leader::tick(&sentry))
206+
.await
207+
.map_err(|timeout_err| {
208+
ValidatorWorkerError::Channel(channel.id, timeout_err.to_string())
209+
})?
210+
.map_err(|tick_err| ValidatorWorkerError::Channel(channel.id, tick_err.to_string()))
211+
.map(|_| ()),
212+
SpecValidator::Follower(_) => timeout(duration, follower::tick(&sentry))
213+
.await
214+
.map_err(|timeout_err| {
215+
ValidatorWorkerError::Channel(channel.id, timeout_err.to_string())
216+
})?
217+
.map_err(|tick_err| ValidatorWorkerError::Channel(channel.id, tick_err.to_string()))
218+
.map(|_| ()),
219+
SpecValidator::None => Err(ValidatorWorkerError::Channel(
220+
channel.id,
221+
"validatorTick: processing a channel which we are not validating".to_string(),
222+
)),
223+
}
222224
}
223225

224226
fn logger() -> Logger {

0 commit comments

Comments
 (0)