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

draft: experiment with jiff crate for time #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
84 changes: 28 additions & 56 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bytes = "1.1"
clap = { version = "4.5.4", features = ["derive", "env"] }
dhcproto = "0.11.0"
futures = { version = "0.3", default-features = false, features = ["std"] }
jiff = { version = "0.1.1" }
ipnet = { features = ["serde"], version = "2.4.0" }
pnet = { features = ["serde", "std"], version = "0.34.0" }
prometheus = "0.13.0"
Expand Down
3 changes: 1 addition & 2 deletions dora-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ topo_sort = { path = "../libs/topo_sort" }
async-trait = { workspace = true }
anyhow = { workspace = true }
bytes = { workspace = true }
chrono = "0.4"
chrono-tz = "0.6"
jiff = { workspace = true, features = ["logging"] }
dhcproto = { workspace = true }
futures = { workspace = true }
lazy_static = "1.4"
Expand Down
3 changes: 1 addition & 2 deletions dora-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
))]
pub use anyhow;
pub use async_trait::async_trait;
pub use chrono;
pub use chrono_tz;
pub use dhcproto;
pub use jiff;
pub use pnet;
pub use tokio;
pub use tokio_stream;
Expand Down
18 changes: 9 additions & 9 deletions dora-core/src/server/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! context of current server message
use chrono::{DateTime, Utc};
use dhcproto::{v4, v6, Decodable, Decoder, Encodable};
use jiff::Zoned;
use pnet::ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
use tracing::{error, trace};
use unix_udp_sock::RecvMeta;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct MsgContext<T> {
/// address response sent to
dst_addr: Option<SocketAddr>,
/// time this context was created
time: DateTime<Utc>,
time: Zoned,
/// decoded from msg
msg: T,
/// decoded response msg -- **CAREFUL** do not call `take()` on this before
Expand All @@ -41,7 +41,7 @@ pub struct MsgContext<T> {
/// a type map for use by plugins to store values
type_map: TypeMap,
/// unique id we assign to each `MsgContext`
id: usize,
id: u64,
/// reference to `State`
state: Arc<State>,
/// whether the `MsgContext` counts towards `state.live_msgs`
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<T> Drop for MsgContext<T> {

impl<T> MsgContext<T> {
/// Get the id
pub fn id(&self) -> usize {
pub fn id(&self) -> u64 {
self.id
}

Expand Down Expand Up @@ -108,11 +108,11 @@ impl<T> MsgContext<T> {
self.msg_buf = msg;
}

/// Get the `DateTime` that we first created this `MsgContext`
/// Get the date & time that we first created this `MsgContext`
///
/// [`DateTime`]: chrono::DateTime
pub fn time(&self) -> DateTime<Utc> {
self.time
/// [`Zoned`]: jiff::zoned
pub fn time(&self) -> &Zoned {
&self.time
}

/// Store a value in the current `MsgContext` based on a type.
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<T: Encodable + Decodable> MsgContext<T> {
src_addr: meta.addr,
meta,
dst_addr: None,
time: Utc::now(),
time: Zoned::now(),
msg,
type_map: TypeMap::new(),
resp_msg: None,
Expand Down
10 changes: 8 additions & 2 deletions dora-core/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ struct RunInner<T> {

impl RunInner<v4::Message> {
/// Process handlers
#[instrument(name = "v4", level = "debug", skip_all)]
#[instrument(name = "v4", level = "debug", fields(
id = self.ctx.id(),
ifindex = self.ctx.meta().ifindex
), skip_all)]
async fn run(mut self) -> Result<()> {
let start = Instant::now();
if let Err(err) = self.ctx.recv_metrics() {
Expand Down Expand Up @@ -328,7 +331,10 @@ impl RunTask<v6::Message> {

impl RunInner<v6::Message> {
/// Process handlers
#[instrument(name = "v6", level = "debug", skip_all)]
#[instrument(name = "v6", level = "debug", fields(
id = self.ctx.id(),
ifindex = self.ctx.meta().ifindex
), skip_all)]
async fn run(mut self) -> Result<()> {
let start = Instant::now();
if let Err(err) = self.ctx.recv_metrics() {
Expand Down
8 changes: 4 additions & 4 deletions dora-core/src/server/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use tokio::sync::Semaphore;

use std::sync::{
atomic::{AtomicUsize, Ordering},
atomic::{AtomicU64, Ordering},
Arc,
};

Expand All @@ -17,7 +17,7 @@ pub struct State {
/// max live message count
live_limit: usize,
/// id to assign incoming messages
next_id: AtomicUsize,
next_id: AtomicU64,
}

impl State {
Expand All @@ -26,7 +26,7 @@ impl State {
State {
live_msgs: Arc::new(Semaphore::new(max_live)),
live_limit: max_live,
next_id: AtomicUsize::new(0),
next_id: AtomicU64::new(0),
}
}

Expand Down Expand Up @@ -58,7 +58,7 @@ impl State {

/// Increment the context id
#[inline]
pub fn inc_id(&self) -> usize {
pub fn inc_id(&self) -> u64 {
self.next_id.fetch_add(1, Ordering::Acquire)
}

Expand Down
2 changes: 1 addition & 1 deletion external-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ mod tests {
let r = reqwest::get("http://0.0.0.0:8889/health")
.await?
.error_for_status();
// initial health state will be BAD i.e. 500
// initial health; state will be BAD i.e. 500
match r {
Ok(_) => {}
Err(err) => {
Expand Down
2 changes: 1 addition & 1 deletion libs/ip-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ thiserror = { workspace = true }
tracing = { workspace = true, features = [
"log",
] } # TODO: do we need the log feature?
chrono = "0.4.19"
jiff = { workspace = true }
moka = { version = "0.10.0", features = ["future"] }
# TODO: hopefully the rustls feature can go away, the lib requires it
sqlx = { version = "0.5.13", features = [
Expand Down
18 changes: 11 additions & 7 deletions libs/ip-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use config::v4::{NetRange, Network};
use icmp_ping::{Icmpv4, Listener, PingReply};

use async_trait::async_trait;
use chrono::DateTime;
use chrono::{offset::Utc, SecondsFormat};
use thiserror::Error;
use tracing::{debug, error, info, trace, warn};

Expand Down Expand Up @@ -328,11 +326,17 @@ where
// ping success so insert probated IP
Err(err) => {
let probation_time = SystemTime::now() + network.probation_period();
info!(
?err,
probation_time = %DateTime::<Utc>::from(probation_time).to_rfc3339_opts(SecondsFormat::Secs, true),
"ping succeeded. address is in use. marking IP on probation"
);

if let Ok(probation_time) =
jiff::Timestamp::try_from(probation_time)
{
info!(
?err,
probation_time = %probation_time.to_string(),
"ping succeeded. address is in use. marking IP on probation"
);
}

// update regardless of expiry/id because something is using the IP
if let Err(err) = self
.store
Expand Down
Loading
Loading