utils: add DirtyState

This commit is contained in:
Luna Yao
2026-02-21 23:24:17 +01:00
parent e9a0e4f042
commit efcdf4c456
5 changed files with 34 additions and 29 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ impl DnsClient {
..Default::default() ..Default::default()
}; };
loop { loop {
self.mgr.dirty.notified().await; self.mgr.dirty.notify.notified().await;
if let Err(e) = self.heartbeat(&mut rpc, &mut heartbeat).await { if let Err(e) = self.heartbeat(&mut rpc, &mut heartbeat).await {
tracing::error!("DnsClient heartbeat failed: {:?}", e); tracing::error!("DnsClient heartbeat failed: {:?}", e);
} }
+5 -11
View File
@@ -1,4 +1,4 @@
use crate::dns::utils::{DirtyFlag, NameServerAddr}; use crate::dns::utils::{DirtyFlag, DirtyState, NameServerAddr};
use crate::dns::zone::{Zone, ZoneGroup}; use crate::dns::zone::{Zone, ZoneGroup};
use crate::proto::dns::DnsClientMgrRpc; use crate::proto::dns::DnsClientMgrRpc;
use crate::proto::dns::{DnsSnapshot, HeartbeatRequest, HeartbeatResponse}; use crate::proto::dns::{DnsSnapshot, HeartbeatRequest, HeartbeatResponse};
@@ -6,13 +6,11 @@ use crate::proto::rpc_types;
use crate::proto::rpc_types::controller::BaseController; use crate::proto::rpc_types::controller::BaseController;
use crate::utils::{DeterministicDigest, MapTryInto}; use crate::utils::{DeterministicDigest, MapTryInto};
use anyhow::Error; use anyhow::Error;
use derive_more::{Deref, DerefMut};
use hickory_server::authority::Catalog; use hickory_server::authority::Catalog;
use itertools::Itertools; use itertools::Itertools;
use moka::future::Cache; use moka::future::Cache;
use std::collections::HashSet; use std::collections::HashSet;
use std::time::Duration; use std::time::Duration;
use tokio::sync::Notify;
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@@ -38,21 +36,17 @@ impl TryFrom<&DnsSnapshot> for DnsClientInfo {
const DNS_CLIENT_TTL: Duration = Duration::from_secs(5); const DNS_CLIENT_TTL: Duration = Duration::from_secs(5);
// TODO: same as DnsPeerMgrDirtyState #[derive(Debug, Default)]
#[derive(Debug, Default, Deref, DerefMut)] pub struct DnsClientMgrDirtyFlags {
pub struct DnsClientMgrDirtyState {
pub(super) catalog: DirtyFlag, pub(super) catalog: DirtyFlag,
pub(super) addresses: DirtyFlag, pub(super) addresses: DirtyFlag,
pub(super) listeners: DirtyFlag, pub(super) listeners: DirtyFlag,
#[deref]
#[deref_mut]
notify: Notify,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct DnsClientMgr { pub struct DnsClientMgr {
clients: Cache<Uuid, DnsClientInfo>, clients: Cache<Uuid, DnsClientInfo>,
pub(super) dirty: DnsClientMgrDirtyState, pub(super) dirty: DirtyState<DnsClientMgrDirtyFlags>,
} }
impl DnsClientMgr { impl DnsClientMgr {
@@ -150,7 +144,7 @@ impl DnsClientMgrRpc for DnsClientMgr {
} }
self.clients.insert(id, new).await; self.clients.insert(id, new).await;
self.dirty.notify_one(); self.dirty.notify.notify_one();
} }
false false
} else { } else {
+6 -10
View File
@@ -1,7 +1,7 @@
use crate::common::config::ConfigLoader; use crate::common::config::ConfigLoader;
use crate::common::PeerId; use crate::common::PeerId;
use crate::dns::config::{DnsExportConfig, DnsGlobalCtxExt}; use crate::dns::config::{DnsExportConfig, DnsGlobalCtxExt};
use crate::dns::utils::DirtyFlag; use crate::dns::utils::{DirtyFlag, DirtyState};
use crate::dns::zone::ZoneGroup; use crate::dns::zone::ZoneGroup;
use crate::peer_center::instance::PeerCenterPeerManagerTrait; use crate::peer_center::instance::PeerCenterPeerManagerTrait;
use crate::peers::peer_manager::PeerManager; use crate::peers::peer_manager::PeerManager;
@@ -13,12 +13,11 @@ use crate::proto::rpc_types;
use crate::proto::rpc_types::controller::BaseController; use crate::proto::rpc_types::controller::BaseController;
use crate::utils::DeterministicDigest; use crate::utils::DeterministicDigest;
use anyhow::Context; use anyhow::Context;
use derive_more::{Deref, DerefMut}; use derive_more::Deref;
use itertools::Itertools; use itertools::Itertools;
use moka::future::Cache; use moka::future::Cache;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::sync::Notify;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DnsPeerInfo { pub struct DnsPeerInfo {
@@ -40,18 +39,15 @@ impl TryFrom<DnsExportConfig> for DnsPeerInfo {
const DNS_PEER_TTL: Duration = Duration::from_secs(3); const DNS_PEER_TTL: Duration = Duration::from_secs(3);
#[derive(Debug, Default, Deref, DerefMut)] #[derive(Debug, Default)]
pub struct DnsPeerMgrDirtyState { pub struct DnsPeerMgrDirtyFlags {
pub(crate) peers: DirtyFlag, pub(crate) peers: DirtyFlag,
#[deref]
#[deref_mut]
notify: Notify,
} }
#[derive(Debug, Deref)] #[derive(Debug, Deref)]
pub struct DnsPeerMgr { pub struct DnsPeerMgr {
peers: Cache<PeerId, DnsPeerInfo>, peers: Cache<PeerId, DnsPeerInfo>,
pub(super) dirty: DnsPeerMgrDirtyState, pub(super) dirty: DirtyState<DnsPeerMgrDirtyFlags>,
#[deref] #[deref]
mgr: Arc<PeerManager>, mgr: Arc<PeerManager>,
@@ -112,7 +108,7 @@ impl DnsPeerMgr {
} }
} }
self.dirty.notify_one(); self.dirty.notify.notify_one();
} }
async fn fetch(&self, peer_id: PeerId) -> anyhow::Result<DnsPeerInfo> { async fn fetch(&self, peer_id: PeerId) -> anyhow::Result<DnsPeerInfo> {
+4 -7
View File
@@ -1,7 +1,5 @@
use super::{utils::NameServerAddr, zone::Zone}; use super::utils::NameServerAddr;
use crate::common::PeerId;
use crate::dns::client_mgr::DnsClientMgr; use crate::dns::client_mgr::DnsClientMgr;
use cidr::Ipv4Inet;
use derivative::Derivative; use derivative::Derivative;
use derive_more::{Deref, DerefMut, From, Into}; use derive_more::{Deref, DerefMut, From, Into};
use hickory_proto::rr::Record; use hickory_proto::rr::Record;
@@ -20,9 +18,8 @@ use std::{sync::Arc, time::Duration};
use tokio::net::{TcpListener, UdpSocket}; use tokio::net::{TcpListener, UdpSocket};
use tokio::{sync::RwLock, task::JoinHandle}; use tokio::{sync::RwLock, task::JoinHandle};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use crate::dns::peer_mgr::DnsPeerMgr;
use crate::peers::peer_manager::PeerManager; use crate::peers::peer_manager::PeerManager;
use crate::proto::dns::{DnsClientMgrRpcServer, DnsPeerMgrRpcServer}; use crate::proto::dns::DnsClientMgrRpcServer;
#[derive(Clone)] #[derive(Clone)]
pub struct DynamicCatalog { pub struct DynamicCatalog {
@@ -214,7 +211,7 @@ impl DnsServer {
let dirty = &self.mgr.dirty; let dirty = &self.mgr.dirty;
let mut runtime = None; let mut runtime = None;
loop { loop {
dirty.notified().await; dirty.notify.notified().await;
if dirty.catalog.reset() { if dirty.catalog.reset() {
self.catalog.replace(self.mgr.catalog()).await; self.catalog.replace(self.mgr.catalog()).await;
@@ -231,7 +228,7 @@ impl DnsServer {
{ {
tracing::error!("failed to reload listeners: {:?}", e); tracing::error!("failed to reload listeners: {:?}", e);
dirty.listeners.mark(); dirty.listeners.mark();
dirty.notify_one(); dirty.notify.notify_one();
} }
} }
+18
View File
@@ -18,6 +18,7 @@ use std::fmt::{Display, Formatter};
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, SocketAddr};
use std::str::FromStr; use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Notify;
use url::Url; use url::Url;
pub fn sanitize(name: &str) -> String { pub fn sanitize(name: &str) -> String {
@@ -267,6 +268,23 @@ where
} }
} }
#[derive(Debug, Deref, DerefMut)]
pub(super) struct DirtyState<T> {
#[deref]
#[deref_mut]
flags: T,
pub notify: Notify,
}
impl<T: Default> Default for DirtyState<T> {
fn default() -> Self {
Self {
flags: T::default(),
notify: Notify::new(),
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub(super) struct DirtyFlag(AtomicBool); pub(super) struct DirtyFlag(AtomicBool);